From 66b48eea3326f9c652c394128993a275c064932d Mon Sep 17 00:00:00 2001 From: Ben Gras Date: Mon, 5 Sep 2005 17:17:58 +0000 Subject: [PATCH] Joren l'Ami 's updates to stdio, minor modification by me too (skip doing anything in fflush() if stream is a pipe). --- lib/stdio/fflush.c | 4 ++++ lib/stdio/fopen.c | 9 +++++++++ lib/stdio/freopen.c | 31 +++++++++++++++++++++---------- 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/lib/stdio/fflush.c b/lib/stdio/fflush.c index e99b18416..ce058570d 100755 --- a/lib/stdio/fflush.c +++ b/lib/stdio/fflush.c @@ -29,6 +29,10 @@ fflush(FILE *stream) if (io_testflag(stream, _IOREADING)) { /* (void) fseek(stream, 0L, SEEK_CUR); */ int adjust = 0; + if (io_testflag(stream, _IOFIFO)) { + /* Can't seek in a pipe. */ + return 0; + } if (stream->_buf && !io_testflag(stream,_IONBF)) adjust = -stream->_count; stream->_count = 0; diff --git a/lib/stdio/fopen.c b/lib/stdio/fopen.c index 915dcb2c6..82663f599 100755 --- a/lib/stdio/fopen.c +++ b/lib/stdio/fopen.c @@ -9,6 +9,7 @@ #include #include #include "loc_incl.h" +#include #define PMODE 0666 @@ -48,6 +49,7 @@ fopen(const char *name, const char *mode) register int i; int rwmode = 0, rwflags = 0; FILE *stream; + struct stat st; int fd, flags = 0; for (i = 0; __iotab[i] != 0 ; i++) @@ -103,6 +105,13 @@ fopen(const char *name, const char *mode) if (fd < 0) return (FILE *)NULL; + if ( fstat( fd, &st ) < 0 ) { + _close(fd); + return (FILE *)NULL; + } + + if ( st.st_mode & S_IFIFO ) flags |= _IOFIFO; + if (( stream = (FILE *) malloc(sizeof(FILE))) == NULL ) { _close(fd); return (FILE *)NULL; diff --git a/lib/stdio/freopen.c b/lib/stdio/freopen.c index c237bfaf6..a81137794 100755 --- a/lib/stdio/freopen.c +++ b/lib/stdio/freopen.c @@ -9,6 +9,7 @@ #include #include #include "loc_incl.h" +#include #define PMODE 0666 @@ -31,6 +32,7 @@ FILE * freopen(const char *name, const char *mode, FILE *stream) { register int i; + struct stat st; int rwmode = 0, rwflags = 0; int fd, flags = stream->_flags & (_IONBF | _IOFBF | _IOLBF | _IOMYBUF); @@ -53,7 +55,7 @@ freopen(const char *name, const char *mode, FILE *stream) rwflags |= O_APPEND | O_CREAT; break; default: - return (FILE *)NULL; + goto loser; } while (*mode) { @@ -81,19 +83,28 @@ freopen(const char *name, const char *mode, FILE *stream) } if (fd < 0) { - for( i = 0; i < FOPEN_MAX; i++) { - if (stream == __iotab[i]) { - __iotab[i] = 0; - break; - } - } - if (stream != stdin && stream != stdout && stream != stderr) - free((void *)stream); - return (FILE *)NULL; + goto loser; } + if ( fstat( fd, &st ) == 0 ) { + if ( st.st_mode & S_IFIFO ) flags |= _IOFIFO; + } else { + goto loser; + } + stream->_count = 0; stream->_fd = fd; stream->_flags = flags; return stream; + +loser: + for( i = 0; i < FOPEN_MAX; i++) { + if (stream == __iotab[i]) { + __iotab[i] = 0; + break; + } + } + if (stream != stdin && stream != stdout && stream != stderr) + free((void *)stream); + return (FILE *)NULL; }