Joren l'Ami 's updates to stdio, minor modification by me too (skip

doing anything in fflush() if stream is a pipe).
This commit is contained in:
Ben Gras 2005-09-05 17:17:58 +00:00
parent d9f669a326
commit 66b48eea33
3 changed files with 34 additions and 10 deletions

View file

@ -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;

View file

@ -9,6 +9,7 @@
#include <stdio.h>
#include <stdlib.h>
#include "loc_incl.h"
#include <sys/stat.h>
#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;

View file

@ -9,6 +9,7 @@
#include <stdio.h>
#include <stdlib.h>
#include "loc_incl.h"
#include <sys/stat.h>
#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;
}