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

52 lines
969 B
C
Raw Normal View History

1989-05-30 13:34:25 +00:00
/*
* setbuf.c - control buffering of a stream
*/
1994-06-24 14:02:31 +00:00
/* $Id$ */
1989-05-30 13:34:25 +00:00
#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);
1995-12-04 17:11:54 +00:00
if (buf && size <= 0) retval = EOF;
if (!buf && (mode != _IONBF)) {
1995-12-04 17:11:54 +00:00
if (size <= 0 || (buf = (char *) malloc(size)) == NULL) {
retval = EOF;
} else {
stream->_flags |= _IOMYBUF;
}
}
1989-06-26 10:37:05 +00:00
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;
}