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 <fcntl.h>, at least on UN*X, to get open() declared
and the O_ flags defined.

svn path=/trunk/; revision=16409
This commit is contained in:
Guy Harris 2005-11-07 02:45:19 +00:00
parent b7407b5a2a
commit 7474bc0f13
3 changed files with 39 additions and 10 deletions

View File

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

View File

@ -99,23 +99,54 @@
#include <errno.h>
#include <stdio.h>
#ifdef HAVE_LIBZ
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif /* HAVE_FCNTL_H */
#include <string.h>
#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)
{

View File

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