ack/lang/cem/libcc.ansi/stdio/setvbuf.c

46 lines
867 B
C
Raw Normal View History

1989-05-30 13:34:25 +00:00
/*
* setbuf.c - control buffering of a stream
*/
/* $Header$ */
#include <stdio.h>
#include <stdlib.h>
#include "loc_incl.h"
extern void (*_clean)(void);
1989-12-18 15:04:14 +00:00
1989-05-30 13:34:25 +00:00
int
setvbuf(register FILE *stream, char *buf, int mode, size_t size)
{
int retval = 0;
_clean = __cleanup;
1989-05-30 13:34:25 +00:00
if (mode != _IOFBF && mode != _IOLBF && mode != _IONBF)
return EOF;
1989-06-26 10:37:05 +00:00
if (stream->_buf && io_testflag(stream,_IOMYBUF) )
1989-05-30 13:34:25 +00:00
free((void *)stream->_buf);
stream->_flags &= ~(_IOMYBUF | _IONBF | _IOLBF);
if (!buf && (mode != _IONBF))
1989-06-26 10:37:05 +00:00
if ((buf = (char *) malloc(size)) == NULL) retval = EOF;
if (io_testflag(stream, _IOREADING) || io_testflag(stream, _IOWRITING))
retval = EOF;
1989-05-30 13:34:25 +00:00
stream->_buf = (unsigned char *) buf;
stream->_count = 0;
stream->_flags |= mode;
stream->_ptr = stream->_buf;
if (!buf) {
stream->_bufsiz = 1;
} else {
stream->_bufsiz = size;
}
return retval;
}