From 7474bc0f13e306bfe66b5d86daf949c855c7cb57 Mon Sep 17 00:00:00 2001 From: Guy Harris Date: Mon, 7 Nov 2005 02:45:19 +0000 Subject: If we're using libz, make file_open() construct the open() flag argument, rather than requiring the caller to get the open() flag and the fopen() flag in sync. That also means that if we're *not* using libz, it can just be a wrapper around eth_fopen(). We need to include , at least on UN*X, to get open() declared and the O_ flags defined. svn path=/trunk/; revision=16409 --- wiretap/file_access.c | 2 +- wiretap/file_wrappers.c | 45 +++++++++++++++++++++++++++++++++++++-------- wiretap/file_wrappers.h | 4 ++-- 3 files changed, 40 insertions(+), 11 deletions(-) (limited to 'wiretap') diff --git a/wiretap/file_access.c b/wiretap/file_access.c index abc147afaf..19a0a7c269 100644 --- a/wiretap/file_access.c +++ b/wiretap/file_access.c @@ -254,7 +254,7 @@ wtap* wtap_open_offline(const char *filename, int *err, char **err_info, } if (do_random) { - if (!(wth->random_fh = file_open(filename, O_RDONLY|O_BINARY, "rb"))) { + if (!(wth->random_fh = file_open(filename, "rb"))) { *err = errno; file_close(wth->fh); g_free(wth); diff --git a/wiretap/file_wrappers.c b/wiretap/file_wrappers.c index 1b02038577..62e0cd86c1 100644 --- a/wiretap/file_wrappers.c +++ b/wiretap/file_wrappers.c @@ -99,23 +99,54 @@ #include #include +#ifdef HAVE_LIBZ +#ifdef HAVE_FCNTL_H +#include +#endif /* HAVE_FCNTL_H */ +#include +#endif /* HAVE_LIBZ */ #include "wtap-int.h" #include "file_wrappers.h" #include "file_util.h" +#ifdef HAVE_LIBZ FILE_T -file_open(const char *path, int oflag, const char *mode) +file_open(const char *path, const char *mode) { int fd; FILE_T ft; - + int oflag; + + if (*mode == 'r') { + if (strchr(mode + 1, '+') != NULL) + oflag = O_RDWR; + else + oflag = O_RDONLY; + } else if (*mode == 'w') { + if (strchr(mode + 1, '+') != NULL) + oflag = O_RDWR|O_CREAT|O_TRUNC; + else + oflag = O_RDONLY|O_CREAT|O_TRUNC; + } else if (*mode == 'a') { + if (strchr(mode + 1, '+') != NULL) + oflag = O_RDWR|O_APPEND; + else + oflag = O_RDONLY|O_APPEND; + } else { + errno = EINVAL; + return NULL; + } +#ifdef _WIN32 + if (strchr(mode + 1, 'b') != NULL) + oflag |= O_BINARY; +#endif /* open file and do correct filename conversions */ - if( (fd = eth_open( path, oflag, 0000 /* no creation so don't matter */)) == NULL ) - return NULL; + if ((fd = eth_open(path, oflag, 0666)) == -1) + return NULL; /* open zlib file handle */ - ft = gzdopen(fd, mode ); - if(ft == NULL) { + ft = gzdopen(fd, mode); + if (ft == NULL) { eth_close(fd); return NULL; } @@ -123,8 +154,6 @@ file_open(const char *path, int oflag, const char *mode) return ft; } - -#ifdef HAVE_LIBZ long file_seek(void *stream, long offset, int whence, int *err) { diff --git a/wiretap/file_wrappers.h b/wiretap/file_wrappers.h index 3d9af8bdcf..c5ace29953 100644 --- a/wiretap/file_wrappers.h +++ b/wiretap/file_wrappers.h @@ -26,7 +26,7 @@ #ifdef HAVE_LIBZ -extern FILE_T file_open(const char *path, int oflag, const char *mode); +extern FILE_T file_open(const char *path, const char *mode); #define filed_open gzdopen extern long file_seek(void *stream, long offset, int whence, int *err); #define file_read(buf, bsize, count, file) gzread((file),(buf),((count)*(bsize))) @@ -39,7 +39,7 @@ extern int file_error(void *fh); #define file_eof gzeof #else /* No zLib */ -#define file_open(path, oflag, mode) fopen(path, mode) +#define file_open(path, mode) eth_fopen(path, mode) #define filed_open fdopen extern long file_seek(void *stream, long offset, int whence, int *err); #define file_read fread -- cgit v1.2.3