home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 9 / FreshFishVol9-CD2.bin / bbs / gnu / libnix-0.8-src.lha / libnix-0.8 / sources / nix / stdio / ungetc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-12  |  993 b   |  38 lines

  1. #include <stdio.h>
  2. #include <errno.h>
  3.  
  4. extern void __chkabort(void);
  5. extern int __fflush(FILE *stream);
  6.  
  7. int ungetc(int c,FILE *stream)
  8. {
  9.   __chkabort();
  10.   if(c==EOF)
  11.     return EOF;
  12.   if(stream->flags&64) /* Error on stream */
  13.   { errno=EPERM;
  14.     return EOF; }
  15.   if(stream->flags&8) /* File is in write mode */
  16.     if(__fflush(stream)) /* write buffer */
  17.       return EOF;
  18.   if(stream->tmpp==NULL)
  19.   { if(stream->flags&4) /* File is in read mode */
  20.     { stream->tmpp=stream->p; /* store actual position */
  21.       stream->tmpinc=stream->incount;
  22.       stream->p=&stream->unget[4]; /* and use other buffer */
  23.       stream->incount=0;
  24.     }else /* File is in indeterminated state */
  25.     { stream->tmpp=(unsigned char *)2; /* Dummy */
  26.       stream->tmpinc=0;
  27.       stream->p=&stream->unget[4]; /* use other buffer */
  28.       stream->incount=0;
  29.       stream->flags|=4;
  30.     }
  31.   }
  32.   if(stream->incount==4) /* ungetc buffer overflow */
  33.     return EOF;
  34.   stream->incount++;
  35.   return *--stream->p=c;
  36. }
  37.  
  38.