/* ETHEREal.c
*
- * $Id: ethereal.c,v 1.85 1999/08/14 19:53:31 gram Exp $
+ * $Id: ethereal.c,v 1.86 1999/08/15 00:26:09 guy Exp $
*
* Ethereal - Network traffic analyzer
* By Gerald Combs <gerald@zing.org>
rfilter = g_strdup(s);
else
rfilter = NULL;
+
+ /* Try to open the capture file. */
+ if ((err = open_cap_file(cf_name, &cf)) != 0) {
+ /* We couldn't open it; don't dismiss the open dialog box,
+ just leave it around so that the user can, after they
+ dismiss the alert box popped up for the open error,
+ try again. */
+ return;
+ }
+
gtk_widget_hide(GTK_WIDGET (fs));
gtk_widget_destroy(GTK_WIDGET (fs));
/* save the directory name. We can write over cf_name */
- if ((err = load_cap_file(cf_name, rfilter, &cf)) == 0) {
+ if ((err = read_cap_file(cf_name, rfilter, &cf)) == 0) {
s = strrchr(cf_name, '/');
if (s && last_open_dir) {
*s = '\0';
g_free(cf.save_file);
cf.save_file = g_strdup(cf_name);
cf.user_saved = 1;
- err = load_cap_file(cf_name, g_strdup(cf.rfilter), &cf);
- if (err == 0) {
+ if ((err = open_cap_file(cf_name, &cf)) == 0 &&
+ (err = read_cap_file(cf_name, g_strdup(cf.rfilter), &cf)) == 0) {
set_menu_sensitivity("/File/Save", FALSE);
set_menu_sensitivity("/File/Save As...", TRUE);
}
g_free(cf.filename);
cf.filename = g_strdup(cf_name);
cf.user_saved = 1;
- err = load_cap_file(cf.filename, g_strdup(cf.rfilter), &cf);
- if (err == 0) {
+ if ((err = open_cap_file(cf.filename, &cf)) == 0 &&
+ (err = read_cap_file(cf.filename, g_strdup(cf.rfilter), &cf)) == 0) {
set_menu_sensitivity("/File/Save", FALSE);
set_menu_sensitivity("/File/Save As...", TRUE);
}
if (cf.dfilter) g_free(cf.dfilter);
cf.dfilter = g_strdup(gtk_entry_get_text(GTK_ENTRY(filter_te)));
- load_cap_file(cf.filename, g_strdup(cf.rfilter), &cf);
+ if (open_cap_file(cf.filename, &cf) == 0)
+ read_cap_file(cf.filename, g_strdup(cf.rfilter), &cf);
/* XXX - change the menu if it fails? */
}
alert box, so, if we get one of those, it's more likely to come
up on top of us. */
if (cf_name) {
- err = load_cap_file(cf_name, rfilter, &cf);
+ if ((err = open_cap_file(cf_name, &cf)) == 0)
+ err = read_cap_file(cf_name, rfilter, &cf);
cf_name[0] = '\0';
if (err == 0)
set_menu_sensitivity("/File/Save As...", TRUE);
/* file.c
* File I/O routines
*
- * $Id: file.c,v 1.67 1999/08/14 18:51:26 gram Exp $
+ * $Id: file.c,v 1.68 1999/08/15 00:26:09 guy Exp $
*
* Ethereal - Network traffic analyzer
* By Gerald Combs <gerald@zing.org>
int
open_cap_file(char *fname, capture_file *cf) {
struct stat cf_stat;
+ FILE *fh;
+ wtap *wth;
+ int err;
/* First, make sure the file is valid */
- if (stat(fname, &cf_stat))
- return (errno);
+ if (stat(fname, &cf_stat)) {
+ err = errno;
+ goto fail;
+ }
#ifndef WIN32
- if (! S_ISREG(cf_stat.st_mode) && ! S_ISFIFO(cf_stat.st_mode))
- return (OPEN_CAP_FILE_NOT_REGULAR);
+ if (! S_ISREG(cf_stat.st_mode) && ! S_ISFIFO(cf_stat.st_mode)) {
+ err = OPEN_CAP_FILE_NOT_REGULAR;
+ goto fail;
+ }
#endif
- /* Next, try to open the file */
- cf->fh = fopen(fname, "r");
- if (cf->fh == NULL)
- return (errno);
-
- fseek(cf->fh, 0L, SEEK_END);
- cf->f_len = ftell(cf->fh);
- fclose(cf->fh);
- cf->fh = NULL;
- /* set the file name beacuse we need it to set the follow stream filter */
- cf->filename = g_strdup( fname );
-
- /* Next, find out what type of file we're dealing with */
- cf->cd_t = WTAP_FILE_UNKNOWN;
- cf->cd_t_desc = "unknown";
- cf->count = 0;
- cf->drops = 0;
- cf->esec = 0;
- cf->eusec = 0;
- cf->snap = 0;
- firstsec = 0, firstusec = 0;
- prevsec = 0, prevusec = 0;
-
- cf->wth = wtap_open_offline(fname);
- if (cf->wth == NULL) {
+ /* 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) {
+ 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. */
- return (OPEN_CAP_FILE_UNKNOWN_FORMAT);
+ err = OPEN_CAP_FILE_UNKNOWN_FORMAT;
+ goto fail;
}
+ /* The open succeeded. Close whatever capture file we had open,
+ and fill in the information for this file. */
+ close_cap_file(cf, info_bar, file_ctx);
+
+ /* Initialize protocol-specific variables */
+ ncp_init_protocol();
+
+ cf->wth = wth;
cf->fh = wtap_file(cf->wth);
- cf->cd_t = wtap_file_type(cf->wth);
+ cf->f_len = cf_stat.st_size;
+
+ /* set the file name because we need it to set the follow stream filter */
+ cf->filename = g_strdup(fname);
+
+ cf->cd_t = wtap_file_type(cf->wth);
cf->cd_t_desc = wtap_file_type_string(cf->wth);
- cf->snap = wtap_snapshot_length(cf->wth);
+ cf->count = 0;
+ cf->drops = 0;
+ cf->esec = 0;
+ cf->eusec = 0;
+ cf->snap = wtap_snapshot_length(cf->wth);
+ firstsec = 0, firstusec = 0;
+ prevsec = 0, prevusec = 0;
+
return (0);
+
+fail:
+ simple_dialog(ESD_TYPE_WARN, NULL,
+ file_open_error_message(err, FALSE), fname);
+ return (err);
}
/* Reset everything to a pristine state */
}
int
-load_cap_file(char *fname, char *rfilter, capture_file *cf) {
+read_cap_file(char *fname, char *rfilter, capture_file *cf) {
gchar *name_ptr, *load_msg, *load_fmt = " Loading: %s...";
gchar *done_fmt = " File: %s Drops: %d";
gchar *err_fmt = " Error: Could not load '%s'";
size_t msg_len;
int err;
- close_cap_file(cf, info_bar, file_ctx);
-
- /* Initialize protocol-specific variables */
- ncp_init_protocol();
-
if ((name_ptr = (gchar *) strrchr(fname, '/')) == NULL)
name_ptr = fname;
else
}
err = open_cap_file(fname, cf);
- if (err != 0) {
- simple_dialog(ESD_TYPE_WARN, NULL,
- file_open_error_message(err, FALSE), fname);
+ if (err != 0)
goto fail;
- }
load_msg = g_malloc(strlen(name_ptr) + strlen(load_fmt) + 2);
sprintf(load_msg, load_fmt, name_ptr);
int err;
int i;
- close_cap_file(cf, info_bar, file_ctx);
-
- /* Initialize protocol-specific variables */
- ncp_init_protocol();
-
err = open_cap_file(fname, cf);
if ((err == 0) && (cf->cd_t != WTAP_FILE_UNKNOWN)) {