home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mitsch75.zip / scheme-7_5_17-src.zip / scheme-7.5.17 / src / microcode / uxfile.c < prev    next >
C/C++ Source or Header  |  2000-12-05  |  5KB  |  177 lines

  1. /* -*-C-*-
  2.  
  3. $Id: uxfile.c,v 1.10 2000/12/05 21:23:49 cph Exp $
  4.  
  5. Copyright (c) 1990-2000 Massachusetts Institute of Technology
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or (at
  10. your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful, but
  13. WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15. General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21.  
  22. #include "ux.h"
  23. #include "osfile.h"
  24. #include "uxio.h"
  25.  
  26. extern void EXFUN (terminal_open, (Tchannel channel));
  27.  
  28. static enum channel_type
  29. DEFUN (fd_channel_type, (fd), int fd)
  30. {
  31.   struct stat stat_buf;
  32.   if ((UX_fstat (fd, (&stat_buf))) < 0)
  33.     return (channel_type_unknown);
  34.   {
  35.     mode_t type = ((stat_buf . st_mode) & S_IFMT);
  36.     return
  37.       ((type == S_IFREG) ? channel_type_file
  38.        : (type == S_IFCHR)
  39.        ? ((isatty (fd))
  40.       ? channel_type_terminal
  41.       : channel_type_unix_character_device)
  42. #ifdef S_IFIFO
  43.        : (type == S_IFIFO) ? channel_type_unix_fifo
  44. #endif
  45. #ifdef S_IFBLK
  46.        : (type == S_IFBLK) ? channel_type_unix_block_device
  47. #endif
  48.        : (type == S_IFDIR) ? channel_type_directory
  49.        : channel_type_unknown);
  50.   }
  51. }
  52.  
  53. Tchannel
  54. DEFUN (OS_open_fd, (fd), int fd)
  55. {
  56.   enum channel_type type = (fd_channel_type (fd));
  57.   Tchannel channel;
  58.   MAKE_CHANNEL (fd, type, channel =);
  59.   if (type == channel_type_terminal)
  60.     terminal_open (channel);
  61.   return (channel);
  62. }
  63.  
  64. static Tchannel
  65. DEFUN (open_file, (filename, oflag), CONST char * filename AND int oflag)
  66. {
  67.   int fd;
  68.   STD_UINT_SYSTEM_CALL
  69.     (syscall_open, fd, (UX_open (filename, oflag, MODE_REG)));
  70. #ifdef SLAVE_PTY_P
  71.   if ((SLAVE_PTY_P (filename)) && (!UX_setup_slave_pty (fd)))
  72.     {
  73.       int xerrno = errno;
  74.       UX_close (fd);
  75.       error_system_call (xerrno, syscall_open);
  76.     }
  77. #endif
  78.   return (OS_open_fd (fd));
  79. }
  80.  
  81. #define DEFUN_OPEN_FILE(name, oflag)                    \
  82. Tchannel                                \
  83. DEFUN (name, (filename), CONST char * filename)                \
  84. {                                    \
  85.   return (open_file (filename, oflag));                    \
  86. }
  87.  
  88. DEFUN_OPEN_FILE (OS_open_input_file, O_RDONLY)
  89. DEFUN_OPEN_FILE (OS_open_output_file, (O_WRONLY | O_CREAT | O_TRUNC))
  90. DEFUN_OPEN_FILE (OS_open_io_file, (O_RDWR | O_CREAT))
  91.  
  92. #ifdef O_APPEND
  93.  
  94. DEFUN_OPEN_FILE (OS_open_append_file, (O_WRONLY | O_CREAT | O_APPEND))
  95.  
  96. #else
  97.  
  98. Tchannel
  99. DEFUN (OS_open_append_file, (filename), CONST char * filename)
  100. {
  101.   error_unimplemented_primitive ();
  102.   return (0);
  103. }
  104.  
  105. #endif
  106.  
  107. static Tchannel
  108. DEFUN (make_load_channel, (fd), int fd)
  109. {
  110.   enum channel_type type = (fd_channel_type (fd));
  111.   if ((type == channel_type_terminal)
  112.       || (type == channel_type_directory)
  113.       || (type == channel_type_unknown))
  114.     return (NO_CHANNEL);
  115.   MAKE_CHANNEL (fd, type, return);
  116. }
  117.  
  118. Tchannel
  119. DEFUN (OS_open_load_file, (filename), CONST char * filename)
  120. {
  121.   while (1)
  122.     {
  123.       int fd = (UX_open (filename, O_RDONLY, MODE_REG));
  124.       if (fd >= 0)
  125.     return (make_load_channel (fd));
  126.       if (errno != EINTR)
  127.     return (NO_CHANNEL);
  128.     }
  129. }
  130.  
  131. Tchannel
  132. DEFUN (OS_open_dump_file, (filename), CONST char * filename)
  133. {
  134.   while (1)
  135.     {
  136.       int fd = (UX_open (filename, (O_WRONLY | O_CREAT | O_TRUNC), MODE_REG));
  137.       if (fd >= 0)
  138.     return (make_load_channel (fd));
  139.       if (errno != EINTR)
  140.     return (NO_CHANNEL);
  141.     }
  142. }
  143.  
  144. off_t
  145. DEFUN (OS_file_length, (channel), Tchannel channel)
  146. {
  147.   struct stat stat_buf;
  148.   STD_VOID_SYSTEM_CALL
  149.     (syscall_fstat, (UX_fstat ((CHANNEL_DESCRIPTOR (channel)), (&stat_buf))));
  150.   return (stat_buf . st_size);
  151. }
  152.  
  153. off_t
  154. DEFUN (OS_file_position, (channel), Tchannel channel)
  155. {
  156.   off_t result;
  157.   STD_UINT_SYSTEM_CALL
  158.     (syscall_lseek,
  159.      result,
  160.      (UX_lseek ((CHANNEL_DESCRIPTOR (channel)), 0L, SEEK_CUR)));
  161.   return (result);
  162. }
  163.  
  164. void
  165. DEFUN (OS_file_set_position, (channel, position),
  166.        Tchannel channel AND
  167.        off_t position)
  168. {
  169.   off_t result;
  170.   STD_UINT_SYSTEM_CALL
  171.     (syscall_lseek,
  172.      result,
  173.      (UX_lseek ((CHANNEL_DESCRIPTOR (channel)), position, SEEK_SET)));
  174.   if (result != position)
  175.     error_external_return ();
  176. }
  177.