If we're using libz, make file_open() construct the open() flag
authorguy <guy@f5534014-38df-0310-8fa8-9805f1628bb7>
Mon, 7 Nov 2005 02:45:19 +0000 (02:45 +0000)
committerguy <guy@f5534014-38df-0310-8fa8-9805f1628bb7>
Mon, 7 Nov 2005 02:45:19 +0000 (02:45 +0000)
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.

git-svn-id: http://anonsvn.wireshark.org/wireshark/trunk@16409 f5534014-38df-0310-8fa8-9805f1628bb7

wiretap/file_access.c
wiretap/file_wrappers.c
wiretap/file_wrappers.h

index abc147afaffb9d4e5e325287e8a05bce3f6dced4..19a0a7c26968222f11c7e83382e4a437dc1f0106 100644 (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);
index 1b02038577b7e3170fee22becf48371560757936..62e0cd86c18068a44c5cf847ebe099c1518a022f 100644 (file)
 
 #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)
 {
index 3d9af8bdcf03f8f570fb50c78782d37fbda822ca..c5ace299530a7733f1f52eb238e9d5c24842fb9a 100644 (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