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 / __swbuf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-12  |  1.0 KB  |  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 __swbuf(int c,FILE *stream) /* Get next output block */
  8. {
  9.   int out,lbs;
  10.   __chkabort();
  11.   if(stream->flags&0x240) /* Error on stream | sprintf buffer */
  12.   { stream->outcount=0;
  13.     errno=EPERM;
  14.     return EOF;
  15.   }else if(stream->flags&4)
  16.   {
  17.     stream->incount=0; /* throw away input buffer */
  18.     stream->tmpp=NULL;
  19.     stream->flags&=~4;
  20.   }
  21.   lbs=stream->flags&_IOLBF?-stream->bufsize:0;
  22.   out=(stream->flags&_IONBF?0:stream->bufsize-1)+lbs;
  23.   if(!(stream->flags&8)) /* File wasn't in write mode */
  24.   { stream->p=stream->buffer; /* set buffer */
  25.     stream->outcount=--out;   /* and buffercount */
  26.     stream->flags|=8; }       /* and write mode */
  27.   *stream->p++=c; /* put this character */
  28.   if(stream->outcount<0&&(stream->outcount<lbs||(char)c=='\n'))
  29.   { if(__fflush(stream)) /* Buffer full */
  30.       return EOF;
  31.     stream->p=stream->buffer; /* Set new buffer */
  32.   }
  33.   stream->linebufsize=lbs;
  34.   stream->outcount=out;
  35.   stream->flags|=8;
  36.   return c;
  37. }
  38.