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

56 lines
894 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
2018-06-21 20:33:47 +00:00
#include <stdio.h>
#include <stdlib.h>
#include "loc_incl.h"
1989-05-30 13:34:25 +00:00
extern void (*_clean)(void);
1989-12-18 15:04:14 +00:00
2018-06-21 20:33:47 +00:00
int setvbuf(register FILE* stream, char* buf, int mode, size_t size)
1989-05-30 13:34:25 +00:00
{
int retval = 0;
__register_stdio_cleanup();
1989-05-30 13:34:25 +00:00
if (mode != _IOFBF && mode != _IOLBF && mode != _IONBF)
return EOF;
2018-06-21 20:33:47 +00:00
if (stream->_buf && io_testflag(stream, _IOMYBUF))
free((void*)stream->_buf);
1989-05-30 13:34:25 +00:00
stream->_flags &= ~(_IOMYBUF | _IONBF | _IOLBF);
2018-06-21 20:33:47 +00:00
if (buf && size <= 0)
retval = EOF;
if (!buf && (mode != _IONBF))
{
if (size <= 0 || (buf = (char*)malloc(size)) == NULL)
{
retval = EOF;
2018-06-21 20:33:47 +00:00
}
else
{
stream->_flags |= _IOMYBUF;
}
}
1989-06-26 10:37:05 +00:00
2018-06-21 20:33:47 +00:00
stream->_buf = (unsigned char*)buf;
1989-05-30 13:34:25 +00:00
stream->_count = 0;
stream->_flags |= mode;
stream->_ptr = stream->_buf;
2018-06-21 20:33:47 +00:00
if (!buf)
{
1989-05-30 13:34:25 +00:00
stream->_bufsiz = 1;
2018-06-21 20:33:47 +00:00
}
else
{
1989-05-30 13:34:25 +00:00
stream->_bufsiz = size;
}
return retval;
}