home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 7 / FreshFishVol7.bin / bbs / gnu / libg++-2.6-fsf.lha / libg++-2.6 / libio / fileops.c < prev    next >
C/C++ Source or Header  |  1994-07-11  |  19KB  |  736 lines

  1. /* 
  2. Copyright (C) 1993 Free Software Foundation
  3.  
  4. This file is part of the GNU IO Library.  This library is free
  5. software; you can redistribute it and/or modify it under the
  6. terms of the GNU General Public License as published by the
  7. Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. This library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with GNU CC; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19. As a special exception, if you link this library with files
  20. compiled with a GNU compiler to produce an executable, this does not cause
  21. the resulting executable to be covered by the GNU General Public License.
  22. This exception does not however invalidate any other reasons why
  23. the executable file might be covered by the GNU General Public License. */
  24.  
  25. /*  written by Per Bothner (bothner@cygnus.com) */
  26.  
  27. #define _POSIX_SOURCE
  28. #include "libioP.h"
  29. #include <fcntl.h>
  30. #include <sys/types.h>
  31. #include <sys/stat.h>
  32. #include <string.h>
  33. #include <errno.h>
  34. #ifndef errno
  35. extern int errno;
  36. #endif
  37.  
  38. /* An fstream can be in at most one of put mode, get mode, or putback mode.
  39.    Putback mode is a variant of get mode.
  40.  
  41.    In a filebuf, there is only one current position, instead of two
  42.    separate get and put pointers.  In get mode, the current posistion
  43.    is that of gptr(); in put mode that of pptr().
  44.  
  45.    The position in the buffer that corresponds to the position
  46.    in external file system is file_ptr().
  47.    This is normally egptr(), except in putback mode, when it is _save_egptr.
  48.    If the field _fb._offset is >= 0, it gives the offset in
  49.    the file as a whole corresponding to eGptr(). (???)
  50.  
  51.    PUT MODE:
  52.    If a filebuf is in put mode, pbase() is non-NULL and equal to base().
  53.    Also, epptr() == ebuf().
  54.    Also, eback() == gptr() && gptr() == egptr().
  55.    The un-flushed character are those between pbase() and pptr().
  56.    GET MODE:
  57.    If a filebuf is in get or putback mode, eback() != egptr().
  58.    In get mode, the unread characters are between gptr() and egptr().
  59.    The OS file position corresponds to that of egptr().
  60.    PUTBACK MODE:
  61.    Putback mode is used to remember "excess" characters that have
  62.    been sputbackc'd in a separate putback buffer.
  63.    In putback mode, the get buffer points to the special putback buffer.
  64.    The unread characters are the characters between gptr() and egptr()
  65.    in the putback buffer, as well as the area between save_gptr()
  66.    and save_egptr(), which point into the original reserve buffer.
  67.    (The pointers save_gptr() and save_egptr() are the values
  68.    of gptr() and egptr() at the time putback mode was entered.)
  69.    The OS position corresponds to that of save_egptr().
  70.    
  71.    LINE BUFFERED OUTPUT:
  72.    During line buffered output, pbase()==base() && epptr()==base().
  73.    However, ptr() may be anywhere between base() and ebuf().
  74.    This forces a call to filebuf::overflow(int C) on every put.
  75.    If there is more space in the buffer, and C is not a '\n',
  76.    then C is inserted, and pptr() incremented.
  77.    
  78.    UNBUFFERED STREAMS:
  79.    If a filebuf is unbuffered(), the _shortbuf[1] is used as the buffer.
  80. */
  81.  
  82. #define CLOSED_FILEBUF_FLAGS \
  83.   (_IO_IS_FILEBUF+_IO_NO_READS+_IO_NO_WRITES+_IO_TIED_PUT_GET)
  84.  
  85.  
  86. void
  87. _IO_file_init(fp)
  88.      register _IO_FILE *fp;
  89. {
  90.   fp->_offset = _IO_pos_0;
  91.   fp->_IO_file_flags |= CLOSED_FILEBUF_FLAGS;
  92.  
  93.   _IO_link_in(fp);
  94.   fp->_fileno = -1;
  95. }
  96.  
  97. int
  98. _IO_file_close_it(fp)
  99.      register _IO_FILE* fp;
  100. {
  101.   int status;
  102.   if (!_IO_file_is_open(fp))
  103.     return EOF;
  104.  
  105.   _IO_do_flush (fp);
  106.  
  107.   _IO_unsave_markers(fp);
  108.  
  109.   status = fp->_jumps->__close(fp);
  110.   
  111.   /* Free buffer. */
  112.   _IO_setb(fp, NULL, NULL, 0);
  113.   _IO_setg(fp, NULL, NULL, NULL);
  114.   _IO_setp(fp, NULL, NULL);
  115.  
  116.   _IO_un_link(fp);
  117.   fp->_flags = _IO_MAGIC|CLOSED_FILEBUF_FLAGS;
  118.   fp->_fileno = EOF;
  119.   fp->_offset = _IO_pos_0;
  120.  
  121.   return status;
  122. }
  123.  
  124. void
  125. _IO_file_finish(fp)
  126.      register _IO_FILE* fp;
  127. {
  128.   if (_IO_file_is_open(fp))
  129.     {
  130.       _IO_do_flush (fp);
  131.       if (!(fp->_flags & _IO_DELETE_DONT_CLOSE))
  132.     fp->_jumps->__close(fp);
  133.     }
  134.   _IO_default_finish(fp);
  135. }
  136.  
  137. _IO_FILE *
  138. _IO_file_fopen(fp, filename, mode)
  139.      register _IO_FILE *fp;
  140.      const char *filename;
  141.      const char *mode;
  142. {
  143.   int oflags = 0, omode;
  144.   int read_write, fdesc;
  145.   int oprot = 0666;
  146.   if (_IO_file_is_open (fp))
  147.     return 0;
  148.   switch (*mode++) {
  149.   case 'r':
  150.     omode = O_RDONLY;
  151.     read_write = _IO_NO_WRITES;
  152.     break;
  153.   case 'w':
  154.     omode = O_WRONLY;
  155.     oflags = O_CREAT|O_TRUNC;
  156.     read_write = _IO_NO_READS;
  157.     break;
  158.   case 'a':
  159.     omode = O_WRONLY;
  160.     oflags = O_CREAT|O_APPEND;
  161.     read_write = _IO_NO_READS|_IO_IS_APPENDING;
  162.     break;
  163.   default:
  164.     errno = EINVAL;
  165.     return NULL;
  166.   }
  167.   if (mode[0] == '+' || (mode[0] == 'b' && mode[1] == '+')) {
  168.     omode = O_RDWR;
  169.     read_write &= _IO_IS_APPENDING;
  170.   }
  171.   fdesc = open(filename, omode|oflags, oprot);
  172.   if (fdesc < 0)
  173.     return NULL;
  174.   fp->_fileno = fdesc;
  175.   _IO_mask_flags(fp, read_write,_IO_NO_READS+_IO_NO_WRITES+_IO_IS_APPENDING);
  176.   if (read_write & _IO_IS_APPENDING)
  177.     if (fp->_jumps->__seekoff(fp, (_IO_off_t)0, _IO_seek_end) == _IO_pos_BAD)
  178.       return NULL;
  179.   _IO_link_in(fp);
  180.   return fp;
  181. }
  182.  
  183. _IO_FILE*
  184. _IO_file_attach(fp, fd)
  185.      _IO_FILE *fp;
  186.      int fd;
  187. {
  188.   if (_IO_file_is_open(fp))
  189.     return NULL;
  190.   fp->_fileno = fd;
  191.   fp->_flags &= ~(_IO_NO_READS+_IO_NO_WRITES);
  192.   fp->_flags |= _IO_DELETE_DONT_CLOSE;
  193.   fp->_offset = _IO_pos_BAD;
  194.   return fp;
  195. }
  196.  
  197. int
  198. _IO_file_setbuf(fp, p, len)
  199.      register _IO_FILE *fp;
  200.      char* p;
  201.      _IO_ssize_t len;
  202. {
  203.     if (_IO_default_setbuf(fp, p, len) == EOF)
  204.     return EOF;
  205.  
  206.     fp->_IO_write_base = fp->_IO_write_ptr = fp->_IO_write_end
  207.       = fp->_IO_buf_base;
  208.     _IO_setg(fp, fp->_IO_buf_base, fp->_IO_buf_base, fp->_IO_buf_base);
  209.  
  210.     return 0;
  211. }
  212.  
  213. int
  214. _IO_do_write(fp, data, to_do)
  215.      register _IO_FILE *fp;
  216.      const char* data;
  217.      _IO_size_t to_do;
  218. {
  219.   _IO_size_t count;
  220.   if (to_do == 0)
  221.     return 0;
  222.   if (fp->_flags & _IO_IS_APPENDING)
  223.     /* On a system without a proper O_APPEND implementation,
  224.        you would need to sys_seek(0, SEEK_END) here, but is
  225.        is not needed nor desirable for Unix- or Posix-like systems.
  226.        Instead, just indicate that offset (before and after) is
  227.        unpredictable. */
  228.     fp->_offset = _IO_pos_BAD;
  229.   else if (fp->_IO_read_end != fp->_IO_write_base)
  230.     { 
  231.       _IO_pos_t new_pos = fp->_jumps->__seek(fp, fp->_IO_write_base - fp->_IO_read_end, 1);
  232.       if (new_pos == _IO_pos_BAD)
  233.     return EOF;
  234.       fp->_offset = new_pos;
  235.     }
  236.   count = fp->_jumps->__write(fp, data, to_do);
  237.   if (fp->_cur_column)
  238.     fp->_cur_column = _IO_adjust_column(fp->_cur_column - 1, data, to_do) + 1;
  239.   _IO_setg(fp, fp->_IO_buf_base, fp->_IO_buf_base, fp->_IO_buf_base);
  240.   fp->_IO_write_base = fp->_IO_write_ptr = fp->_IO_buf_base;
  241.   fp->_IO_write_end = (fp->_flags & _IO_LINE_BUF+_IO_UNBUFFERED) ? fp->_IO_buf_base
  242.     : fp->_IO_buf_end;
  243.   return count != to_do ? EOF : 0;
  244. }
  245.  
  246. int
  247. _IO_file_underflow(fp)
  248.      register _IO_FILE *fp;
  249. {
  250.   _IO_ssize_t count;
  251. #if 0
  252.   /* SysV does not make this test; take it out for compatibility */
  253.   if (fp->_flags & _IO_EOF_SEEN)
  254.     return (EOF);
  255. #endif
  256.  
  257.   if (fp->_flags & _IO_NO_READS)
  258.     return EOF;
  259.   if (fp->_IO_read_ptr < fp->_IO_read_end)
  260.     return *(unsigned char*)fp->_IO_read_ptr;
  261.  
  262.   if (fp->_IO_buf_base == NULL)
  263.     _IO_doallocbuf(fp);
  264.  
  265.   /* Flush all line buffered files before reading. */
  266.   /* FIXME This can/should be moved to genops ?? */
  267.   if (fp->_flags & (_IO_LINE_BUF|_IO_UNBUFFERED))
  268.     _IO_flush_all_linebuffered();
  269.  
  270.   _IO_switch_to_get_mode(fp);
  271.  
  272.   count = fp->_jumps->__read(fp, fp->_IO_buf_base,
  273.                  fp->_IO_buf_end - fp->_IO_buf_base);
  274.   if (count <= 0)
  275.     {
  276.       if (count == 0)
  277.     fp->_flags |= _IO_EOF_SEEN;
  278.       else
  279.     fp->_flags |= _IO_ERR_SEEN, count = 0;
  280.   }
  281.   fp->_IO_read_base = fp->_IO_read_ptr = fp->_IO_buf_base;
  282.   fp->_IO_read_end = fp->_IO_buf_base + count;
  283.   fp->_IO_write_base = fp->_IO