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:
parent
d9f669a326
commit
66b48eea33
3 changed files with 34 additions and 10 deletions
|
@ -29,6 +29,10 @@ fflush(FILE *stream)
|
||||||
if (io_testflag(stream, _IOREADING)) {
|
if (io_testflag(stream, _IOREADING)) {
|
||||||
/* (void) fseek(stream, 0L, SEEK_CUR); */
|
/* (void) fseek(stream, 0L, SEEK_CUR); */
|
||||||
int adjust = 0;
|
int adjust = 0;
|
||||||
|
if (io_testflag(stream, _IOFIFO)) {
|
||||||
|
/* Can't seek in a pipe. */
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
if (stream->_buf && !io_testflag(stream,_IONBF))
|
if (stream->_buf && !io_testflag(stream,_IONBF))
|
||||||
adjust = -stream->_count;
|
adjust = -stream->_count;
|
||||||
stream->_count = 0;
|
stream->_count = 0;
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include "loc_incl.h"
|
#include "loc_incl.h"
|
||||||
|
#include <sys/stat.h>
|
||||||
|
|
||||||
#define PMODE 0666
|
#define PMODE 0666
|
||||||
|
|
||||||
|
@ -48,6 +49,7 @@ fopen(const char *name, const char *mode)
|
||||||
register int i;
|
register int i;
|
||||||
int rwmode = 0, rwflags = 0;
|
int rwmode = 0, rwflags = 0;
|
||||||
FILE *stream;
|
FILE *stream;
|
||||||
|
struct stat st;
|
||||||
int fd, flags = 0;
|
int fd, flags = 0;
|
||||||
|
|
||||||
for (i = 0; __iotab[i] != 0 ; i++)
|
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 (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 ) {
|
if (( stream = (FILE *) malloc(sizeof(FILE))) == NULL ) {
|
||||||
_close(fd);
|
_close(fd);
|
||||||
return (FILE *)NULL;
|
return (FILE *)NULL;
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include "loc_incl.h"
|
#include "loc_incl.h"
|
||||||
|
#include <sys/stat.h>
|
||||||
|
|
||||||
#define PMODE 0666
|
#define PMODE 0666
|
||||||
|
|
||||||
|
@ -31,6 +32,7 @@ FILE *
|
||||||
freopen(const char *name, const char *mode, FILE *stream)
|
freopen(const char *name, const char *mode, FILE *stream)
|
||||||
{
|
{
|
||||||
register int i;
|
register int i;
|
||||||
|
struct stat st;
|
||||||
int rwmode = 0, rwflags = 0;
|
int rwmode = 0, rwflags = 0;
|
||||||
int fd, flags = stream->_flags & (_IONBF | _IOFBF | _IOLBF | _IOMYBUF);
|
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;
|
rwflags |= O_APPEND | O_CREAT;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
return (FILE *)NULL;
|
goto loser;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (*mode) {
|
while (*mode) {
|
||||||
|
@ -81,19 +83,28 @@ freopen(const char *name, const char *mode, FILE *stream)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
for( i = 0; i < FOPEN_MAX; i++) {
|
goto loser;
|
||||||
if (stream == __iotab[i]) {
|
|
||||||
__iotab[i] = 0;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (stream != stdin && stream != stdout && stream != stderr)
|
|
||||||
free((void *)stream);
|
|
||||||
return (FILE *)NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ( fstat( fd, &st ) == 0 ) {
|
||||||
|
if ( st.st_mode & S_IFIFO ) flags |= _IOFIFO;
|
||||||
|
} else {
|
||||||
|
goto loser;
|
||||||
|
}
|
||||||
|
|
||||||
stream->_count = 0;
|
stream->_count = 0;
|
||||||
stream->_fd = fd;
|
stream->_fd = fd;
|
||||||
stream->_flags = flags;
|
stream->_flags = flags;
|
||||||
return stream;
|
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;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue