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

  1. /* ungetc.c (emx+gcc) -- Copyright (c) 1990-1993 by Eberhard Mattes */
  2.  
  3. #include <sys/emx.h>
  4. #include <stdio.h>
  5. #include <errno.h>
  6.  
  7. int ungetc (int c, FILE *stream)
  8. {
  9.   if (!(stream->flags & _IOOPEN) || c == EOF)
  10.     return (EOF);
  11.   if (stream->flags & _IOSTRING)
  12.     {
  13.       /* ungetc on a string is used only by scanf, and this does an */
  14.       /* ungetc of the recently read character, so we don't have to */
  15.       /* write it to the (read-only!) string.                       */
  16.       --stream->ptr;
  17.       ++stream->rcount;
  18.       stream->flags &= ~_IOEOF;
  19.       return ((unsigned char)c);
  20.     }
  21.   if (!(stream->flags & _IOREAD))      /* File in read mode? */
  22.     {
  23.       stream->flags |= _IOERR;
  24.       errno = EACCES;
  25.       return (EOF);
  26.     }
  27.   if (nbuf (stream))
  28.     _fbuf (stream);
  29.   if (stream->ptr == stream->buffer)
  30.     {
  31.       if (stream->rcount != 0)
  32.         return (EOF);                  /* multiple ungetc: no space */
  33.       *stream->ptr = (char)c;
  34.     }
  35.   else
  36.     {
  37.       --stream->ptr;
  38.       *stream->ptr = (char)c;
  39.     }
  40.   ++stream->rcount;
  41.   stream->flags &= ~_IOEOF;
  42.   stream->flags |= _IOREREAD;          /* for fseek */
  43.   return ((unsigned char)c);
  44. }
  45.