1989-05-30 13:34:25 +00:00
|
|
|
/*
|
|
|
|
* flushbuf.c - flush a buffer
|
|
|
|
*/
|
1994-06-24 14:02:31 +00:00
|
|
|
/* $Id$ */
|
1989-05-30 13:34:25 +00:00
|
|
|
|
2007-04-21 23:18:14 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include "loc_incl.h"
|
1989-05-30 13:34:25 +00:00
|
|
|
|
1990-01-22 11:13:26 +00:00
|
|
|
extern void (*_clean)(void);
|
1989-05-30 13:34:25 +00:00
|
|
|
|
1991-06-10 17:07:18 +00:00
|
|
|
static int
|
|
|
|
do_write(int d, char *buf, int nbytes)
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
|
|
|
|
/* POSIX actually allows write() to return a positive value less
|
|
|
|
than nbytes, so loop ...
|
|
|
|
*/
|
2007-04-21 23:18:14 +00:00
|
|
|
while ((c = write(d, buf, nbytes)) > 0 && c < nbytes) {
|
1991-06-10 17:07:18 +00:00
|
|
|
nbytes -= c;
|
|
|
|
buf += c;
|
|
|
|
}
|
|
|
|
return c > 0;
|
|
|
|
}
|
|
|
|
|
1989-05-30 13:34:25 +00:00
|
|
|
int
|
1989-12-18 15:04:14 +00:00
|
|
|
__flushbuf(int c, FILE * stream)
|
1989-05-30 13:34:25 +00:00
|
|
|
{
|
1990-01-22 11:13:26 +00:00
|
|
|
_clean = __cleanup;
|
1989-05-30 13:34:25 +00:00
|
|
|
if (fileno(stream) < 0) return EOF;
|
1989-06-26 10:37:05 +00:00
|
|
|
if (!io_testflag(stream, _IOWRITE)) return EOF;
|
|
|
|
if (io_testflag(stream, _IOREADING) && !feof(stream)) return EOF;
|
|
|
|
|
|
|
|
stream->_flags &= ~_IOREADING;
|
|
|
|
stream->_flags |= _IOWRITING;
|
1989-05-30 13:34:25 +00:00
|
|
|
if (!io_testflag(stream, _IONBF)) {
|
|
|
|
if (!stream->_buf) {
|
2007-04-21 23:18:14 +00:00
|
|
|
if (stream == stdout && isatty(fileno(stdout))) {
|
1989-05-30 13:34:25 +00:00
|
|
|
if (!(stream->_buf =
|
|
|
|
(unsigned char *) malloc(BUFSIZ))) {
|
|
|
|
stream->_flags |= _IONBF;
|
1989-12-18 15:04:14 +00:00
|
|
|
} else {
|
1991-02-22 16:45:47 +00:00
|
|
|
stream->_flags |= _IOLBF|_IOMYBUF;
|
1989-05-30 13:34:25 +00:00
|
|
|
stream->_bufsiz = BUFSIZ;
|
|
|
|
stream->_count = -1;
|
|
|
|
}
|
1989-12-18 15:04:14 +00:00
|
|
|
} else {
|
1989-05-30 13:34:25 +00:00
|
|
|
if (!(stream->_buf =
|
|
|
|
(unsigned char *) malloc(BUFSIZ))) {
|
|
|
|
stream->_flags |= _IONBF;
|
1989-12-18 15:04:14 +00:00
|
|
|
} else {
|
1989-05-30 13:34:25 +00:00
|
|
|
stream->_flags |= _IOMYBUF;
|
|
|
|
stream->_bufsiz = BUFSIZ;
|
1989-12-18 15:04:14 +00:00
|
|
|
if (!io_testflag(stream, _IOLBF))
|
|
|
|
stream->_count = BUFSIZ - 1;
|
1991-02-22 16:45:47 +00:00
|
|
|
else stream->_count = -1;
|
1989-05-30 13:34:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
stream->_ptr = stream->_buf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (io_testflag(stream, _IONBF)) {
|
|
|
|
char c1 = c;
|
|
|
|
|
|
|
|
stream->_count = 0;
|
1989-12-18 15:04:14 +00:00
|
|
|
if (io_testflag(stream, _IOAPPEND)) {
|
2007-04-21 23:18:14 +00:00
|
|
|
if (lseek(fileno(stream), 0L, SEEK_END) == -1) {
|
1989-12-18 15:04:14 +00:00
|
|
|
stream->_flags |= _IOERR;
|
|
|
|
return EOF;
|
|
|
|
}
|
|
|
|
}
|
2007-04-21 23:18:14 +00:00
|
|
|
if (write(fileno(stream), &c1, 1) != 1) {
|
1989-05-30 13:34:25 +00:00
|
|
|
stream->_flags |= _IOERR;
|
|
|
|
return EOF;
|
|
|
|
}
|
1996-04-24 13:06:00 +00:00
|
|
|
return (unsigned char) c;
|
1989-12-18 15:04:14 +00:00
|
|
|
} else if (io_testflag(stream, _IOLBF)) {
|
1989-05-30 13:34:25 +00:00
|
|
|
*stream->_ptr++ = c;
|
1993-01-14 10:02:28 +00:00
|
|
|
/* stream->_count has been updated in putc macro. */
|
1989-05-30 13:34:25 +00:00
|
|
|
if (c == '\n' || stream->_count == -stream->_bufsiz) {
|
1993-01-14 10:02:28 +00:00
|
|
|
int count = -stream->_count;
|
|
|
|
|
|
|
|
stream->_ptr = stream->_buf;
|
|
|
|
stream->_count = 0;
|
|
|
|
|
1989-12-18 15:04:14 +00:00
|
|
|
if (io_testflag(stream, _IOAPPEND)) {
|
2007-04-21 23:18:14 +00:00
|
|
|
if (lseek(fileno(stream), 0L, SEEK_END) == -1) {
|
1989-12-18 15:04:14 +00:00
|
|
|
stream->_flags |= _IOERR;
|
|
|
|
return EOF;
|
|
|
|
}
|
|
|
|
}
|
1991-06-10 17:07:18 +00:00
|
|
|
if (! do_write(fileno(stream), (char *)stream->_buf,
|
1993-01-14 10:02:28 +00:00
|
|
|
count)) {
|
1989-05-30 13:34:25 +00:00
|
|
|
stream->_flags |= _IOERR;
|
|
|
|
return EOF;
|
|
|
|
}
|
|
|
|
}
|
1989-12-18 15:04:14 +00:00
|
|
|
} else {
|
1989-05-30 13:34:25 +00:00
|
|
|
int count = stream->_ptr - stream->_buf;
|
|
|
|
|
|
|
|
stream->_count = stream->_bufsiz - 1;
|
|
|
|
stream->_ptr = stream->_buf + 1;
|
|
|
|
|
|
|
|
if (count > 0) {
|
1989-12-18 15:04:14 +00:00
|
|
|
if (io_testflag(stream, _IOAPPEND)) {
|
2007-04-21 23:18:14 +00:00
|
|
|
if (lseek(fileno(stream), 0L, SEEK_END) == -1) {
|
1989-12-18 15:04:14 +00:00
|
|
|
stream->_flags |= _IOERR;
|
|
|
|
return EOF;
|
|
|
|
}
|
|
|
|
}
|
1991-06-10 17:07:18 +00:00
|
|
|
if (! do_write(fileno(stream), (char *)stream->_buf, count)) {
|
1989-05-30 13:34:25 +00:00
|
|
|
*(stream->_buf) = c;
|
|
|
|
stream->_flags |= _IOERR;
|
|
|
|
return EOF;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*(stream->_buf) = c;
|
|
|
|
}
|
1996-04-24 13:06:00 +00:00
|
|
|
return (unsigned char) c;
|
1989-05-30 13:34:25 +00:00
|
|
|
}
|