home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / CLIPPER / MISC / EMXLIB8F.ZIP / EMX / LIB / IO / FLUSHSTR.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-02  |  1.7 KB  |  67 lines

  1. /* flushstr.c (emx+gcc) -- Copyright (c) 1990-1993 by Eberhard Mattes */
  2.  
  3. #include <sys/emx.h>
  4. #include <stdio.h>
  5. #include <io.h>
  6. #include <fcntl.h>
  7. #include <errno.h>
  8.  
  9. int _flushstream (FILE *stream, int c)
  10. {
  11.   int n, w, fh;
  12.   char ch;
  13.  
  14.   fh = fileno (stream);
  15.   if (c < 0)
  16.     {
  17.       /* fill --- not used yet*/
  18.     }
  19.   else
  20.     {
  21.       stream->flags |= _IOWRT;        /* Switch to write mode */
  22.       stream->rcount = 0;
  23.       stream->flags &= ~_IOEOF;       /* Clear EOF flag, writing! */
  24.       stream->wcount = 0;             /* Maybe negative at this point */
  25.       if (nbuf (stream))
  26.         _fbuf (stream);
  27.       if (bbuf (stream))
  28.         {
  29.           n = stream->ptr - stream->buffer;
  30.           if (n > 0)                  /* n should never be < 0 */
  31.             w = write (fh, stream->buffer, n);
  32.           else                        /* New or flushed buffer */
  33.             {
  34.               w = 0;
  35.               if (fh >= 0 && fh < _nfiles && (_files[fh] & O_APPEND))
  36.                 lseek (fh, 0L, SEEK_END);
  37.             }
  38.           stream->ptr = stream->buffer;
  39.           stream->wcount = stream->buf_size;
  40.           if (c == '\n' && (stream->flags & _IOLBF))
  41.             {
  42.               ch = (char)c;
  43.               if (write (fh, &ch, 1) != 1)
  44.                 ++n;            /* n != w */
  45.             }
  46.           else
  47.             {
  48.               *stream->ptr++ = (char)c;
  49.               --stream->wcount;
  50.             }
  51.         }
  52.       else
  53.         {
  54.           n = 1;
  55.           ch = (char)c;
  56.           w = write (fh, &ch, 1);
  57.           stream->wcount = 0;
  58.         }
  59.       if (n != w)
  60.         {
  61.           stream->flags |= _IOERR;
  62.           return (EOF);
  63.         }
  64.     }
  65.   return (0);
  66. }
  67.