From fd6d3e00a1a3658c594db6d1fe3a430406f9296d Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Sun, 7 Jan 2007 20:50:49 +0000 Subject: [PATCH] * Implemented freopen(). --- doc/zzuf.1 | 5 +++-- src/load-stream.c | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/doc/zzuf.1 b/doc/zzuf.1 index da3e955..e807156 100644 --- a/doc/zzuf.1 +++ b/doc/zzuf.1 @@ -266,8 +266,9 @@ Unix file descriptor handling: \fBmmap\fR(), \fBmunmap\fR(), \fBclose\fR() .TP Standard IO streams: -\fBfopen\fR(), \fBfseek\fR(), \fBfseeko\fR(), \fBrewind\fR(), \fBfread\fR(), -\fBgetc\fR(), \fBfgetc\fR(), \fBfgets\fR(), \fBungetc\fR(), \fBfclose\fR() +\fBfopen\fR(), \fBfreopen\fR(), \fBfseek\fR(), \fBfseeko\fR(), \fBrewind\fR(), +\fBfread\fR(), \fBgetc\fR(), \fBfgetc\fR(), \fBfgets\fR(), \fBungetc\fR(), +\fBfclose\fR() .TP Linux-specific: \fBopen64\fR(), \fBlseek64\fR(), \fBmmap64\fR(), \fB_IO_getc\fR(), diff --git a/src/load-stream.c b/src/load-stream.c index 9497876..2660c04 100644 --- a/src/load-stream.c +++ b/src/load-stream.c @@ -54,6 +54,8 @@ static FILE * (*fopen_orig) (const char *path, const char *mode); #ifdef HAVE_FOPEN64 static FILE * (*fopen64_orig) (const char *path, const char *mode); #endif +static FILE * (*freopen_orig) (const char *path, const char *mode, + FILE *stream); static int (*fseek_orig) (FILE *stream, long offset, int whence); #ifdef HAVE_FSEEKO static int (*fseeko_orig) (FILE *stream, off_t offset, int whence); @@ -98,6 +100,7 @@ void _zz_load_stream(void) #ifdef HAVE_FOPEN64 LOADSYM(fopen64); #endif + LOADSYM(freopen); LOADSYM(fseek); #ifdef HAVE_FSEEKO LOADSYM(fseeko); @@ -161,6 +164,36 @@ FILE *fopen64(const char *path, const char *mode) } #endif +FILE *freopen(const char *path, const char *mode, FILE *stream) +{ + FILE *ret; + int fd0 = -1, fd1 = -1, disp = 0; + + if(!_zz_ready) + LOADSYM(freopen); + if(_zz_ready && (fd0 = fileno(stream)) >= 0 && _zz_iswatched(fd0)) + { + _zz_unregister(fd0); + disp = 1; + } + + _zz_disabled = 1; + ret = freopen_orig(path, mode, stream); + _zz_disabled = 0; + + if(ret && _zz_mustwatch(path)) + { + fd1 = fileno(ret); + _zz_register(fd1); + disp = 1; + } + + if(disp) + debug("freopen(\"%s\", \"%s\", [%i]) = [%i]", path, mode, fd0, fd1); + + return ret; +} + #if defined HAVE___SREFILL /* Don't fuzz or seek if we have __srefill() */ # define FSEEK_FUZZ(fn2) #else