1 /* 2 * setbuf.c - control buffering of a stream 3 */ 4 /* $Id$ */ 5 6 #include <stdio.h> 7 #include <stdlib.h> 8 #include "loc_incl.h" 9 10 extern void (*_clean)(void); 11 12 int 13 setvbuf(register FILE *stream, char *buf, int mode, size_t size) 14 { 15 int retval = 0; 16 17 _clean = __cleanup; 18 if (mode != _IOFBF && mode != _IOLBF && mode != _IONBF) 19 return EOF; 20 21 if (stream->_buf && io_testflag(stream,_IOMYBUF) ) 22 free((void *)stream->_buf); 23 24 stream->_flags &= ~(_IOMYBUF | _IONBF | _IOLBF); 25 26 if (buf && size <= 0) retval = EOF; 27 if (!buf && (mode != _IONBF)) { 28 if (size <= 0 || (buf = (char *) malloc(size)) == NULL) { 29 retval = EOF; 30 } else { 31 stream->_flags |= _IOMYBUF; 32 } 33 } 34 35 stream->_buf = (unsigned char *) buf; 36 37 stream->_count = 0; 38 stream->_flags |= mode; 39 stream->_ptr = stream->_buf; 40 41 if (!buf) { 42 stream->_bufsiz = 1; 43 } else { 44 stream->_bufsiz = size; 45 } 46 47 return retval; 48 } 49