home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / gnu / djgpp / src / libgplus.5 / libio / fileops.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-24  |  16.7 KB  |  657 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. #include "libioP.h"
  28. #include <fcntl.h>
  29. #include <errno.h>
  30. #include <sys/types.h>
  31. #include <sys/stat.h>
  32. #ifndef errno
  33. extern int errno;
  34. #endif
  35.  
  36. /* An fstream can be in at most one of put mode, get mode, or putback mode.
  37.    Putback mode is a variant of get mode.
  38.  
  39.    In a filebuf, there is only one current position, instead of two
  40.    separate get and put pointers.  In get mode, the current posistion
  41.    is that of gptr(); in put mode that of pptr().
  42.  
  43.    The position in the buffer that corresponds to the position
  44.    in external file system is file_ptr().
  45.    This is normally egptr(), except in putback mode, when it is _save_egptr.
  46.    If the field _fb._offset is >= 0, it gives the offset in
  47.    the file as a whole corresponding to eGptr(). (???)
  48.  
  49.    PUT MODE:
  50.    If a filebuf is in put mode, pbase() is non-NULL and equal to base().
  51.    Also, epptr() == ebuf().
  52.    Also, eback() == gptr() && gptr() == egptr().
  53.    The un-flushed character are those between pbase() and pptr().
  54.    GET MODE:
  55.    If a filebuf is in get or putback mode, eback() != egptr().
  56.    In get mode, the unread characters are between gptr() and egptr().
  57.    The OS file position corresponds to that of egptr().
  58.    PUTBACK MODE:
  59.    Putback mode is used to remember "excess" characters that have
  60.    been sputbackc'd in a separate putback buffer.
  61.    In putback mode, the get buffer points to the special putback buffer.
  62.    The unread characters are the characters between gptr() and egptr()
  63.    in the putback buffer, as well as the area between save_gptr()
  64.    and save_egptr(), which point into the original reserve buffer.
  65.    (The pointers save_gptr() and save_egptr() are the values
  66.    of gptr() and egptr() at the time putback mode was entered.)
  67.    The OS position corresponds to that of save_egptr().
  68.    
  69.    LINE BUFFERED OUTPUT:
  70.    During line buffered output, pbase()==base() && epptr()==base().
  71.    However, ptr() may be anywhere between base() and ebuf().
  72.    This forces a call to filebuf::overflow(int C) on every put.
  73.    If there is more space in the buffer, and C is not a '\n',
  74.    then C is inserted, and pptr() incremented.
  75.    
  76.    UNBUFFERED STREAMS:
  77.    If a filebuf is unbuffered(), the _shortbuf[1] is used as the buffer.
  78. */
  79.  
  80. #define CLOSED_FILEBUF_FLAGS \
  81.   (_IO_IS_FILEBUF+_IO_NO_READS+_IO_NO_WRITES+_IO_TIED_PUT_GET)
  82.  
  83.  
  84. void
  85. _IO_file_init(fp)
  86.      register _IO_FILE *fp;
  87. {
  88.   fp->_offset = _IO_pos_0;
  89.  
  90.   _IO_link_in(fp);
  91.   fp->_fileno = -1;
  92. }
  93.  
  94. int
  95. _IO_file_close_it(fp)
  96.      register _IO_FILE* fp;
  97. {
  98.   int status;
  99.   if (!_IO_file_is_open(fp))
  100.     return EOF;
  101.  
  102.   /* This flushes as well as switching mode. */
  103.   if (fp->_pptr > fp->_pbase || _IO_in_put_mode(fp))
  104.     if (_IO_switch_to_get_mode(fp))
  105.       return EOF;
  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 (!(fp->_flags & _IO_DELETE_DONT_CLOSE))
  129.     _IO_file_close_it(fp);
  130.   _IO_un_link(fp);
  131.   _IO_default_finish(fp);
  132. }
  133.  
  134. _IO_FILE *
  135. _IO_file_fopen(fp, filename, mode)
  136.      register _IO_FILE *fp;
  137.      const char *filename;
  138.      const char *mode;
  139. {
  140.   int oflags = 0, omode;
  141.   int read_write, fdesc;
  142.   int oprot = 0666;
  143.   if (_IO_file_is_open (fp))
  144.     return 0;
  145.   switch (*mode++) {
  146.   case 'r':
  147.     omode = O_RDONLY;
  148.     read_write = _IO_NO_WRITES;
  149.     break;
  150.   case 'w':
  151.     omode = O_WRONLY;
  152.     oflags = O_CREAT|O_TRUNC;
  153.     read_write = _IO_NO_READS;
  154.     break;
  155.   case 'a':
  156.     omode = O_WRONLY;
  157.     oflags = O_CREAT|O_APPEND;
  158.     read_write = _IO_NO_READS|_IO_IS_APPENDING;
  159.     break;
  160.   default:
  161.     errno = EINVAL;
  162.     return NULL;
  163.   }
  164.   if (mode[0] == '+' || (mode[0] == 'b' && mode[1] == '+')) {
  165.     omode = O_RDWR;
  166.     read_write &= _IO_IS_APPENDING;
  167.   }
  168.   fdesc = open(filename, omode|oflags, oprot);
  169.   if (fdesc < 0)
  170.     return NULL;
  171.   fp->_fileno = fdesc;
  172.   fp->_IO_file_flags = 
  173.     _IO_mask_flags(fp, read_write,_IO_NO_READS+_IO_NO_WRITES+_IO_IS_APPENDING);
  174.   if (read_write & _IO_IS_APPENDING)
  175.     if (fp->_jumps->__seekoff(fp, (_IO_off_t)0, _IO_seek_end) == _IO_pos_BAD)
  176.       return NULL;
  177.   _IO_link_in(fp);
  178.   return fp;
  179. }
  180.  
  181. _IO_FILE*
  182. _IO_file_attach(fp, fd)
  183.      _IO_FILE *fp;
  184.      int fd;
  185. {
  186.   if (_IO_file_is_open(fp))
  187.     return NULL;
  188.   fp->_fileno = fd;
  189.   fp->_flags &= ~(_IO_NO_READS+_IO_NO_WRITES);
  190.   fp->_offset = _IO_pos_BAD;
  191.   return fp;
  192. }
  193.  
  194. int
  195. _IO_file_setbuf(fp, p, len)
  196.      register _IO_FILE *fp;
  197.      char* p;
  198.      _IO_ssize_t len;
  199. {
  200.     if (_IO_default_setbuf(fp, p, len) == EOF)
  201.     return EOF;
  202.  
  203.     fp->_pbase = fp->_pptr = fp->_epptr = fp->_base;
  204.     _IO_setg(fp, fp->_base, fp->_base, fp->_base);
  205.  
  206.     return 0;
  207. }
  208.  
  209. int
  210. _IO_do_write(fp, data, to_do)
  211.      register _IO_FILE *fp;
  212.      const char* data;
  213.      _IO_size_t to_do;
  214. {
  215.   _IO_size_t count;
  216.   if (to_do == 0)
  217.     return 0;
  218.   if (fp->_flags & _IO_IS_APPENDING)
  219.     /* On a system without a proper O_APPEND implementation,
  220.        you would need to sys_seek(0, SEEK_END) here, but is
  221.        is not needed nor desirable for Unix- or Posix-like systems.
  222.        Instead, just indicate that offset (before and after) is
  223.        unpredictable. */
  224.     fp->_offset = _IO_pos_BAD;
  225.   else if (fp->_egptr != fp->_pbase)
  226.     { 
  227.       _IO_pos_t new_pos = fp->_jumps->__seek(fp, fp->_pbase - fp->_egptr, 1);
  228.       if (new_pos == _IO_pos_BAD)
  229.     return EOF;
  230.       fp->_offset = new_pos;
  231.     }
  232.   count = fp->_jumps->__write(fp, data, to_do);
  233.   if (fp->_cur_column)
  234.     fp->_cur_column = _IO_adjust_column(fp->_cur_column - 1, data, to_do) + 1;
  235.   _IO_setg(fp, fp->_base, fp->_base, fp->_base);
  236.   fp->_pbase = fp->_pptr = fp->_base;
  237.   fp->_epptr = (fp->_flags & _IO_LINE_BUF+_IO_UNBUFFERED) ? fp->_base : fp->_ebuf;
  238.   return count != to_do ? EOF : 0;
  239. }
  240.  
  241. int
  242. _IO_file_underflow(fp)
  243.      register _IO_FILE *fp;
  244. {
  245.   _IO_size_t count;
  246. #if 0
  247.   /* SysV does not make this test; take it out for compatibility */
  248.   if (fp->_flags & _IO_EOF_SEEN)
  249.     return (EOF);
  250. #endif
  251.  
  252.   if (fp->_flags & _IO_NO_READS)
  253.     return EOF;
  254.   if (fp->_gptr < fp->_egptr)
  255.     return *(unsigned char*)fp->_gptr;
  256.  
  257.   if (fp->_base == NULL)
  258.     _IO_doallocbuf(fp);
  259.  
  260.   /* Flush all line buffered files before reading. */
  261.   /* FIXME This can/should be moved to genops ?? */
  262.   if (fp->_flags & (_IO_LINE_BUF|_IO_UNBUFFERED))
  263.     _IO_flush_all_linebuffered();
  264.  
  265.   _IO_switch_to_get_mode(fp);
  266.  
  267.   count = fp->_jumps->__read(fp, fp->_base, fp->_ebuf - fp->_base);
  268.   if (count <= 0)
  269.     {
  270.       if (count == 0)
  271.     fp->_flags |= _IO_EOF_SEEN;
  272.       else
  273.     fp->_flags |= _IO_ERR_SEEN, count = 0;
  274.   }
  275.   fp->_eback = fp->_gptr = fp->_base;
  276.   fp->_egptr = fp->_base + count;
  277.   fp->_pbase = fp->_pptr = fp->_epptr = fp->_base;
  278.   if (count == 0)
  279.     return EOF;
  280.   if (fp->_offset != _IO_pos_BAD)
  281.     _IO_pos_adjust(fp->_offset, count);
  282.   return *(unsigned char*)fp->_gptr;
  283. }
  284.  
  285. int _IO_file_overflow (f, ch)
  286.      register _IO_FILE* f;
  287.      int ch;
  288. {
  289.   if (f->_flags & _IO_NO_WRITES) /* SET ERROR */
  290.     return EOF;
  291.   /* Allocate a buffer if needed. */
  292.   if (f->_base == 0)
  293.     {
  294.       _IO_doallocbuf(f);
  295.       if (f->_flags & _IO_LINE_BUF+_IO_UNBUFFERED)
  296.     f->_pbase = f->_pptr = f->_epptr = f->_base;
  297.       else
  298.     f->_pbase= f->_pptr = f->_base, f->_epptr = f->_ebuf;
  299.       f->_eback = f->_gptr = f->_egptr = f->_base;
  300.       f->_flags |= _IO_CURRENTLY_PUTTING;
  301.     }
  302.   /* If currently reading, switch to writing. */
  303.   else if ((f->_flags & _IO_CURRENTLY_PUTTING) == 0)
  304.     {
  305.       if (f->_flags & _IO_LINE_BUF+_IO_UNBUFFERED)
  306.     f->_pbase = f->_pptr = f->_epptr = f->_gptr;
  307.       else
  308.     f->_pbase = f->_pptr = f->_gptr, f->_epptr = f->_ebuf;
  309.       f->_eback = f->_gptr = f->_egptr;
  310.       f->_flags |= _IO_CURRENTLY_PUTTING;
  311.     }
  312.   if (ch == EOF)
  313.     return _IO_do_flush(f);
  314.   if (f->_pptr == f->_ebuf ) /* Buffer is really full */
  315.     if (_IO_do_flush(f) == EOF)
  316.       return EOF;
  317.   *f->_pptr++ = ch;
  318.   if ((f->_flags & _IO_UNBUFFERED)
  319.       || ((f->_flags & _IO_LINE_BUF) && ch == '\n'))
  320.     if (_IO_do_flush(f) == EOF)
  321.       return EOF;
  322.   return (unsigned char)ch;
  323. }
  324.  
  325. int
  326. _IO_file_sync(fp)
  327.      register _IO_FILE* fp;
  328. {
  329.   _IO_size_t delta;
  330.   /*    char* ptr = cur_ptr(); */
  331.   if (fp->_pptr > fp->_pbase)
  332.     if (_IO_do_flush(fp)) return EOF;
  333.   delta = fp->_gptr - fp->_egptr; 
  334.   if (delta != 0)
  335.     {
  336. #ifdef TODO
  337.       if (_IO_in_backup(fp))
  338.     delta -= eGptr() - Gbase();
  339. #endif
  340.       _IO_off_t new_pos = fp->_jumps->__seek(fp, delta, 1);
  341.       if (new_pos == (_IO_off_t)EOF)
  342.     return EOF;
  343.       fp->_offset = new_pos;
  344.       fp->_IO_read_end = fp->_IO_read_ptr;
  345.     }
  346.   /* FIXME: Cleanup - can this be shared? */
  347.   /*    setg(base(), ptr, ptr); */
  348.   return 0;
  349. }
  350.  
  351. _IO_pos_t
  352. _IO_file_seekoff(fp, offset, mode)
  353.      register _IO_FILE *fp;
  354.      _IO_off_t offset;
  355.      _IO_seekflags mode;
  356. {
  357.   _IO_pos_t result;
  358.   _IO_off_t delta, new_offset;
  359.   long count;
  360.   int dir = mode & 3;
  361.  
  362.   if ((mode & _IO_seek_not_in) && (mode & _IO_seek_not_out))
  363.     dir = _IO_seek_cur, offset = 0; /* Don't move any pointers. */
  364.  
  365.   /* Flush unwritten characters.
  366.      (This may do an unneeded write if we seek within the buffer.
  367.      But to be able to switch to reading, we would need to set
  368.      egptr to ptr.  That can't be done in the current design,
  369.      which assumes file_ptr() is eGptr.  Anyway, since we probably
  370.      end up flushing when we close(), it doesn't make much difference.)
  371.      FIXME: simulate mem-papped files. */
  372.  
  373.   if (fp->_pptr > fp->_pbase || _IO_in_put_mode(fp))
  374.     if (_IO_switch_to_get_mode(fp)) return EOF;
  375.  
  376.   if (fp->_base == NULL)
  377.     {
  378.       _IO_doallocbuf(fp);
  379.       _IO_setp(fp, fp->_base, fp->_base);
  380.       _IO_setg(fp, fp->_base, fp->_base, fp->_base);
  381.     }
  382.  
  383.   switch (dir)
  384.     {
  385.     case _IO_seek_cur:
  386.       if (fp->_offset == _IO_pos_BAD)
  387.     goto dumb;
  388.       /* Adjust for read-ahead (bytes is buffer). */
  389.       offset -= fp->_IO_read_end - fp->_IO_read_ptr;
  390.       /* Make offset absolute, assuming current pointer is file_ptr(). */
  391.       offset += _IO_pos_as_off(fp->_offset);
  392.  
  393.       dir = _IO_seek_set;
  394.       break;
  395.     case _IO_seek_set:
  396.       break;
  397.     case _IO_seek_end:
  398.       {
  399.     struct stat st;
  400.     if (fp->_jumps->__stat(fp, &st) == 0 && S_ISREG(st.st_mode))
  401.       {
  402.         offset += st.st_size;
  403.         dir = _IO_seek_set;
  404.       }
  405.     else
  406.       goto dumb;
  407.       }
  408.     }
  409.   /* At this point, dir==_IO_seek_set. */
  410.  
  411. #ifdef TODO
  412.   /* If destination is within current buffer, optimize: */
  413.   if (fp->_offset != IO_pos_BAD && fp->_IO_read_base != NULL)
  414.     {
  415.       /* Offset relative to start of main get area. */
  416.       _IO_pos_t rel_offset = offset - _fb._offset
  417.     + (eGptr()-Gbase());
  418.       if (rel_offset >= 0)
  419.     {
  420.       if (_IO_in_backup(fp))
  421.         _IO_switch_to_main_get_area(fp);
  422.       if (rel_offset <= _egptr - _eback)
  423.         {
  424.           _IO_setg(fp->_base, fp->_base + rel_offset, fp->_egptr);
  425.           _IO_setp(fp->_base, fp->_base);
  426.           return offset;
  427.         }
  428.         /* If we have streammarkers, seek forward by reading ahead. */
  429.         if (_IO_have_markers(fp))
  430.           {
  431.         int to_skip = rel_offset - (fp->_gptr - fp->_eback);
  432.         if (ignore(to_skip) != to_skip)
  433.           goto dumb;
  434.         return offset;
  435.           }
  436.     }
  437.       if (rel_offset < 0 && rel_offset >= Bbase() - Bptr())
  438.     {
  439.       if (!_IO_in_backup(fp))
  440.         _IO_switch_to_backup_area(fp);
  441.       gbump(_egptr + rel_offset - gptr());
  442.       return offset;
  443.     }
  444.     }
  445.  
  446.   _IO_unsave_markers(fp);
  447. #endif
  448.  
  449.   /* Try to seek to a block boundary, to improve kernel page management. */
  450.   new_offset = offset & ~(fp->_ebuf - fp->_base - 1);
  451.   delta = offset - new_offset;
  452.   if (delta > fp->_ebuf - fp->_base)
  453.     {
  454.       new_offset = offset;
  455.       delta = 0;
  456.     }
  457.   result = fp->_jumps->__seek(fp, new_offset, 0);
  458.   if (result < 0)
  459.     return EOF;
  460.   if (delta == 0)
  461.     count = 0;
  462.   else
  463.     {
  464.       count = fp->_jumps->__read(fp, fp->_base, fp->_ebuf - fp->_base);
  465.       if (count < delta)
  466.     {
  467.       /* We weren't allowed to read, but try to seek the remainder. */
  468.       offset = count == EOF ? delta : delta-count;
  469.       dir = _IO_seek_cur;
  470.       goto dumb;
  471.     }
  472.     }
  473.   _IO_setg(fp, fp->_base, fp->_base+delta, fp->_base+count);
  474.   _IO_setp(fp, fp->_base, fp->_base);
  475.   fp->_offset = result + count;
  476.   _IO_mask_flags(fp, 0, _IO_EOF_SEEN);
  477.   return offset;
  478.  dumb:
  479.  
  480.   _IO_unsave_markers(fp);
  481.   result = fp->_jumps->__seek(fp, offset, dir);
  482.   if (result != EOF)
  483.     _IO_mask_flags(fp, 0, _IO_EOF_SEEN);
  484.   fp->_offset = result;
  485.   _IO_setg(fp, fp->_base, fp->_base, fp->_base);
  486.   _IO_setp(fp, fp->_base, fp->_base);
  487.   return result;
  488. }
  489.  
  490. _IO_ssize_t
  491. _IO_file_read(fp, buf, size)
  492.      register _IO_FILE* fp;
  493.      void* buf;
  494.      _IO_ssize_t size;
  495. {
  496.   for (;;)
  497.     {
  498.       _IO_ssize_t count = _IO_read(fp->_fileno, buf, size);
  499. #ifdef EINTR
  500.       if (errno == EINTR && count == -1)
  501.     continue;
  502. #endif
  503.       return count;
  504.     }
  505. }
  506.  
  507. _IO_pos_t
  508. _IO_file_seek(fp, offset, dir)
  509.      _IO_FILE *fp;
  510.      _IO_off_t offset;
  511.      int dir;
  512. {
  513.   return _IO_lseek(fp->_fileno, offset, dir);
  514. }
  515.  
  516. int
  517. _IO_file_stat(fp, st)
  518.      _IO_FILE *fp;
  519.      void* st;
  520. {
  521.   return _IO_fstat(fp->_fileno, (struct stat*)st);
  522. }
  523.  
  524. int
  525. _IO_file_close(fp)
  526.      _IO_FILE* fp;
  527. {
  528.   return _IO_close(fp->_fileno);
  529. }
  530.  
  531. _IO_ssize_t
  532. _IO_file_write(f, data, n)
  533.      register _IO_FILE* f;
  534.      const void* data;
  535.      _IO_ssize_t n;
  536. {
  537.   _IO_ssize_t to_do = n;
  538.   while (to_do > 0)
  539.     {
  540.       _IO_ssize_t count = _IO_write(f->_fileno, data, to_do);
  541.       if (count == EOF)
  542.     {
  543. #ifdef EINTR
  544.       if (errno == EINTR)
  545.         continue;
  546.       else
  547. #endif
  548.         {
  549.           f->_flags |= _IO_ERR_SEEN;
  550.           break;
  551.             }
  552.         }
  553.       to_do -= count;
  554.       data = (void*)((char*)data + count);
  555.     }
  556.   n -= to_do;
  557.   if (f->_offset >= 0)
  558.     f->_offset += n;
  559.   return n;
  560. }
  561.  
  562. _IO_size_t
  563. _IO_file_xsputn(f, data, n)
  564.      _IO_FILE *f;
  565.      const void *data;
  566.      _IO_size_t n;
  567. {
  568.   register const char *s = data;
  569.   _IO_size_t to_do = n;
  570.   int must_flush = 0;
  571.   _IO_size_t count;
  572.  
  573.   if (n <= 0)
  574.     return 0;
  575.   /* This is an optimized implementation.
  576.      If the amount to be written straddles a block boundary
  577.      (or the filebuf is unbuffered), use sys_write directly. */
  578.  
  579.   /* First figure out how much space is available in the buffer. */
  580.   count = f->_epptr - f->_pptr; /* Space available. */
  581.   if ((f->_flags & _IO_LINE_BUF) && (f->_flags & _IO_CURRENTLY_PUTTING))
  582.     {
  583.       count = f->_ebuf - f->_pptr;
  584.       if (count >= n)
  585.     { register const char *p;
  586.       for (p = s + n; p > s; )
  587.         {
  588.           if (*--p == '\n') {
  589.         count = p - s + 1;
  590.         must_flush = 1;
  591.         break;
  592.           }
  593.         }
  594.     }
  595.     }
  596.   /* Then fill the buffer. */
  597.   if (count > 0)
  598.     {
  599.       if (count > to_do)
  600.     count = to_do;
  601.       if (count > 20) {
  602.     memcpy(f->_pptr, s, count);
  603.     s += count;
  604.       }
  605.       else
  606.     {
  607.       register char *p = f->_pptr;
  608.       register int i = (int)count;
  609.       while (--i >= 0) *p++ = *s++;
  610.     }
  611.       f->_pptr += count;
  612.       to_do -= count;
  613.     }
  614.   if (to_do + must_flush > 0)
  615.     { _IO_size_t block_size, dont_write;
  616.       /* Next flush the (full) buffer. */
  617.       if (__overflow(f, EOF) == EOF)
  618.     return n - to_do;
  619.  
  620.       /* Try to maintain alignment: write a whole number of blocks.
  621.      dont_write is what gets left over. */
  622.       block_size = f->_ebuf - f->_base;
  623.       dont_write = block_size >= 128 ? to_do % block_size : 0;
  624.  
  625.       count = to_do - dont_write;
  626.       if (_IO_do_write(f, s, count) == EOF)
  627.     return n - to_do;
  628.       to_do = dont_write;
  629.       
  630.       /* Now write out the remainder.  Normally, this will fit in the
  631.      buffer, but it's somewhat messier for line-buffered files,
  632.      so we let _IO_xsputn handle the general case. */
  633.       if (dont_write)
  634.     to_do -= _IO_default_xsputn(f, s+count, dont_write);
  635.     }
  636.   return n - to_do;
  637. }
  638.  
  639. struct _IO_jump_t _IO_file_jumps = {
  640.   _IO_file_overflow,
  641.   _IO_file_underflow,
  642.   _IO_file_xsputn,
  643.   _IO_default_xsgetn,
  644.   _IO_file_read,
  645.   _IO_file_write,
  646.   _IO_file_doallocate,
  647.   _IO_default_pbackfail,
  648.   _IO_file_setbuf,
  649.   _IO_file_sync,
  650.   _IO_file_finish,
  651.   _IO_file_close,
  652.   _IO_file_stat,
  653.   _IO_file_seek,
  654.   _IO_file_seekoff,
  655.   _IO_default_seekpos,
  656. };
  657.