diff options
author | Guy Harris <guy@alum.mit.edu> | 1999-08-15 06:59:13 +0000 |
---|---|---|
committer | Guy Harris <guy@alum.mit.edu> | 1999-08-15 06:59:13 +0000 |
commit | 8f0acf35515b4e8940e9ccfdc1a7ecbebe3b755e (patch) | |
tree | 42c4b8bfa52e533bfd1b5e4d9fa0cdcc37f9aa64 | |
parent | f452748340735a029fa279860cd803040835cd3b (diff) | |
download | wireshark-8f0acf35515b4e8940e9ccfdc1a7ecbebe3b755e.tar.gz wireshark-8f0acf35515b4e8940e9ccfdc1a7ecbebe3b755e.tar.bz2 wireshark-8f0acf35515b4e8940e9ccfdc1a7ecbebe3b755e.zip |
Have "wtap_open_offline()" return, on failure, an indication of the
reason for the failure, and have it do the checks to make sure the file
being opened is a plain file or a pipe.
Have "open_cap_file()" make use of that.
Don't automatically set "last_open_dir" if a "-r" flag was specified on
the command line - do so only if the file in question could actually be
opened.
svn path=/trunk/; revision=495
-rw-r--r-- | ethereal.c | 17 | ||||
-rw-r--r-- | file.c | 51 | ||||
-rw-r--r-- | file.h | 15 | ||||
-rw-r--r-- | wiretap/file.c | 25 | ||||
-rw-r--r-- | wiretap/wtap.h | 17 |
5 files changed, 58 insertions, 67 deletions
diff --git a/ethereal.c b/ethereal.c index ced9adae6e..69816ec813 100644 --- a/ethereal.c +++ b/ethereal.c @@ -1,6 +1,6 @@ /* ethereal.c * - * $Id: ethereal.c,v 1.87 1999/08/15 01:02:24 guy Exp $ + * $Id: ethereal.c,v 1.88 1999/08/15 06:59:02 guy Exp $ * * Ethereal - Network traffic analyzer * By Gerald Combs <gerald@zing.org> @@ -1156,12 +1156,6 @@ main(int argc, char *argv[]) #endif case 'r': /* Read capture file xxx */ cf_name = g_strdup(optarg); - s = strrchr(cf_name, '/'); - if (s) { - last_open_dir = cf_name; - *s = '\0'; - cf_name = g_strdup(optarg); - } break; case 'R': /* Read file filter */ rfilter = g_strdup(optarg); @@ -1417,9 +1411,14 @@ main(int argc, char *argv[]) if (cf_name) { if ((err = open_cap_file(cf_name, &cf)) == 0) err = read_cap_file(rfilter, &cf); - cf_name[0] = '\0'; - if (err == 0) + if (err == 0) { + s = strrchr(cf_name, '/'); + if (s) { + last_open_dir = cf_name; + *s = '\0'; + } set_menu_sensitivity("/File/Save As...", TRUE); + } } /* If we failed to open the preferences file, pop up an alert box; @@ -1,7 +1,7 @@ /* file.c * File I/O routines * - * $Id: file.c,v 1.70 1999/08/15 01:02:25 guy Exp $ + * $Id: file.c,v 1.71 1999/08/15 06:59:03 guy Exp $ * * Ethereal - Network traffic analyzer * By Gerald Combs <gerald@zing.org> @@ -101,43 +101,20 @@ static gint dfilter_progress_cb(gpointer p); int open_cap_file(char *fname, capture_file *cf) { - struct stat cf_stat; - FILE *fh; wtap *wth; - int err; + int err; + FILE *fh; + struct stat cf_stat; - /* First, make sure the file is valid */ - if (stat(fname, &cf_stat)) { - err = errno; - goto fail; - } -#ifndef WIN32 - if (! S_ISREG(cf_stat.st_mode) && ! S_ISFIFO(cf_stat.st_mode)) { - err = OPEN_CAP_FILE_NOT_REGULAR; + wth = wtap_open_offline(fname, &err); + if (wth == NULL) goto fail; - } -#endif - /* Next, try to open the file. - XXX - we only need to do this because "wtap_open_offline()" - doesn't return an indication of whether the open failed because - we don't have access to the file, or because it's not a valid - capture file, so we first have to open it with "fopen()" to - make sure we have access to it as a boring ordinary file. */ - fh = fopen(fname, "r"); - if (fh == NULL) { + /* Find the size of the file. */ + fh = wtap_file(wth); + if (fstat(fileno(fh), &cf_stat) < 0) { err = errno; - goto fail; - } - fclose(fh); - - /* Next, try to open it as a wiretap capture file. */ - wth = wtap_open_offline(fname); - if (wth == NULL) { - /* XXX - we assume that, because we were able to open it above, - this must have failed because it's not a capture file in - a format we can read. */ - err = OPEN_CAP_FILE_UNKNOWN_FORMAT; + wtap_close(wth); goto fail; } @@ -149,7 +126,7 @@ open_cap_file(char *fname, capture_file *cf) { ncp_init_protocol(); cf->wth = wth; - cf->fh = wtap_file(cf->wth); + cf->fh = fh; cf->f_len = cf_stat.st_size; /* set the file name because we need it to set the follow stream filter */ @@ -1001,11 +978,11 @@ file_open_error_message(int err, int for_writing) switch (err) { - case OPEN_CAP_FILE_NOT_REGULAR: + case WTAP_ERR_NOT_REGULAR_FILE: errmsg = "The file \"%s\" is invalid."; break; - case OPEN_CAP_FILE_UNKNOWN_FORMAT: + case WTAP_ERR_FILE_UNKNOWN_FORMAT: errmsg = "The file \"%s\" is not a capture file in a format Ethereal understands."; break; @@ -1020,7 +997,7 @@ file_open_error_message(int err, int for_writing) if (for_writing) errmsg = "You do not have permission to create or write to the file \"%s\"."; else - errmsg = "You do not have permission to open the file \"%s\"."; + errmsg = "You do not have permission to read the file \"%s\"."; break; default: @@ -1,7 +1,7 @@ /* file.h * Definitions for file structures and routines * - * $Id: file.h,v 1.34 1999/08/15 01:02:26 guy Exp $ + * $Id: file.h,v 1.35 1999/08/15 06:59:04 guy Exp $ * * Ethereal - Network traffic analyzer * By Gerald Combs <gerald@zing.org> @@ -91,19 +91,6 @@ typedef struct _capture_file { FILE *print_fh; /* File we're printing to */ } capture_file; - -/* - * "open_cap_file()" can return: - * - * 0 on success; - * - * a positive "errno" value on an open failure; - * - * a negative number, indicating the type of error, on other failures. - */ -#define OPEN_CAP_FILE_NOT_REGULAR -1 /* not a plain file */ -#define OPEN_CAP_FILE_UNKNOWN_FORMAT -2 /* not a capture file in a known format */ - int open_cap_file(char *, capture_file *); void close_cap_file(capture_file *, void *, guint); int read_cap_file(char *, capture_file *); diff --git a/wiretap/file.c b/wiretap/file.c index 54734a0df5..1e08ffb821 100644 --- a/wiretap/file.c +++ b/wiretap/file.c @@ -1,6 +1,6 @@ /* file.c * - * $Id: file.c,v 1.12 1999/08/02 02:04:37 guy Exp $ + * $Id: file.c,v 1.13 1999/08/15 06:59:13 guy Exp $ * * Wiretap Library * Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu> @@ -26,6 +26,8 @@ #include <stdio.h> #include <string.h> #include <stdlib.h> +#include <sys/stat.h> +#include <errno.h> #include "wtap.h" #include "buffer.h" #include "lanalyzer.h" @@ -43,14 +45,29 @@ * WTAP_FILE_UNKNOWN */ /* Opens a file and prepares a wtap struct */ -wtap* wtap_open_offline(char *filename) +wtap* wtap_open_offline(const char *filename, int *err) { + struct stat statb; wtap *wth; + /* First, make sure the file is valid */ + if (stat(filename, &statb)) { + *err = errno; + return NULL; + } +#ifndef WIN32 + if (! S_ISREG(statb.st_mode) && ! S_ISFIFO(statb.st_mode)) { + *err = WTAP_ERR_NOT_REGULAR_FILE; + return NULL; + } +#endif + wth = (wtap*)malloc(sizeof(wtap)); /* Open the file */ if (!(wth->fh = fopen(filename, "rb"))) { + *err = errno; + free(wth); return NULL; } @@ -96,8 +113,8 @@ wtap* wtap_open_offline(char *filename) /* failure: */ fclose(wth->fh); free(wth); - wth = NULL; - return wth; + *err = WTAP_ERR_FILE_UNKNOWN_FORMAT; + return NULL; success: wth->frame_buffer = g_malloc(sizeof(struct Buffer)); diff --git a/wiretap/wtap.h b/wiretap/wtap.h index 32a6c7d9d5..9664d2a427 100644 --- a/wiretap/wtap.h +++ b/wiretap/wtap.h @@ -1,6 +1,6 @@ /* wtap.h * - * $Id: wtap.h,v 1.22 1999/08/02 02:04:38 guy Exp $ + * $Id: wtap.h,v 1.23 1999/08/15 06:59:13 guy Exp $ * * Wiretap Library * Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu> @@ -162,8 +162,20 @@ typedef struct wtap { types */ } wtap; +/* + * On failure, "wtap_open_offline()" returns NULL, and puts into the + * "int" pointed to by its second argument: + * + * 0 on success; + * + * a positive "errno" value if the capture file can't be opened; + * + * a negative number, indicating the type of error, on other failures. + */ +#define WTAP_ERR_NOT_REGULAR_FILE -1 /* not a plain file */ +#define WTAP_ERR_FILE_UNKNOWN_FORMAT -2 /* not a capture file in a known format */ -wtap* wtap_open_offline(char *filename); +wtap* wtap_open_offline(const char *filename, int *err); void wtap_loop(wtap *wth, int, wtap_handler, u_char*); FILE* wtap_file(wtap *wth); @@ -172,7 +184,6 @@ int wtap_file_type(wtap *wth); const char *wtap_file_type_string(wtap *wth); void wtap_close(wtap *wth); - /* Pointer versions of ntohs and ntohl. Given a pointer to a member of a * byte array, returns the value of the two or four bytes at the pointer. * The pletoh[sl] versions return the little-endian representation. |