setvbuf.c 873 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /*
  2. * setbuf.c - control buffering of a stream
  3. */
  4. /* $Header$ */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include "loc_incl.h"
  8. extern int (*_fflush)(FILE *stream);
  9. int
  10. setvbuf(register FILE *stream, char *buf, int mode, size_t size)
  11. {
  12. int retval = 0;
  13. _fflush = fflush;
  14. if (mode != _IOFBF && mode != _IOLBF && mode != _IONBF)
  15. return EOF;
  16. if (stream->_buf && io_testflag(stream,_IOMYBUF) )
  17. free((void *)stream->_buf);
  18. stream->_flags &= ~(_IOMYBUF | _IONBF | _IOLBF);
  19. if (!buf && (mode != _IONBF))
  20. if ((buf = (char *) malloc(size)) == NULL) retval = EOF;
  21. if (io_testflag(stream, _IOREADING) || io_testflag(stream, _IOWRITING))
  22. retval = EOF;
  23. stream->_buf = (unsigned char *) buf;
  24. stream->_count = 0;
  25. stream->_flags |= mode;
  26. stream->_ptr = stream->_buf;
  27. if (!buf) {
  28. stream->_bufsiz = 1;
  29. } else {
  30. stream->_bufsiz = size;
  31. }
  32. return retval;
  33. }