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 / setvbuf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-12  |  783 b   |  32 lines

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <errno.h>
  4.  
  5. extern int __fflush(FILE *stream);
  6.  
  7. int setvbuf(FILE *stream,char *buf,int mode,size_t size)
  8. { stream->flags&=~3;
  9.   stream->flags|=mode==_IOLBF?1:(mode==_IONBF?2:0);
  10.   if(size!=stream->bufsize||buf!=(char *)stream->buffer)
  11.   { if(__fflush(stream))
  12.       return -1;
  13.     stream->incount=0;
  14.     stream->tmpp=0;
  15.     stream->flags&=~4;
  16.     mode=(buf==NULL);
  17.     if(mode)
  18.       if((buf=malloc(size))==NULL)
  19.       { errno=ENOMEM;
  20.         return -1; }
  21.     if(stream->flags&0x80)
  22.       free(stream->buffer);
  23.     if(mode)
  24.       stream->flags|=0x80;
  25.     else
  26.       stream->flags&=~0x80;
  27.     stream->buffer=buf;
  28.     stream->bufsize=size;
  29.   } /* Need not adjust outcount, since setvbuf affects only the NEXT full buffer */
  30.   return 0;
  31. }
  32.