Have "do_capture()" take, as an argument, a pointer to the name of the

file to which to write the capture; if it's NULL, create a temporary
file and use that.

Have "-w" set a local variable, which starts out null, and, for "-k"
captures, call "do_capture()" and pass it that local variable as an
argument; this lets you do "-k" without "-w", which makes it use a
temporary file for the capture.

This means "run_capture()" no longer serves a useful purpose, as its
only caller is "do_capture()"; swallow it into "do_capture()".

svn path=/trunk/; revision=748
This commit is contained in:
Guy Harris 1999-10-02 06:26:53 +00:00
parent b34d0437c9
commit 3d2cc0cb06
4 changed files with 36 additions and 62 deletions

View File

@ -1,7 +1,7 @@
/* capture.c
* Routines for packet capture windows
*
* $Id: capture.c,v 1.76 1999/10/02 06:00:06 guy Exp $
* $Id: capture.c,v 1.77 1999/10/02 06:26:45 guy Exp $
*
* Ethereal - Network traffic analyzer
* By Gerald Combs <gerald@zing.org>
@ -43,6 +43,7 @@
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <fcntl.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
@ -103,14 +104,30 @@ typedef struct _loop_data {
wtap_dumper *pdh;
} loop_data;
/* Create a temporary file and start a capture to it. */
/* Open a specified file, or create a temporary file, and start a capture
to the file in question. */
void
do_capture(void)
do_capture(char *capfile_name)
{
char tmpname[128+1];
gboolean is_temp_file;
u_char c;
int i;
guint byte_count;
char *msg;
int err;
int capture_succeeded;
/* Choose a random name for the capture buffer */
cf.save_file_fd = create_tempfile(tmpname, sizeof tmpname, "ether");
if (capfile_name != NULL) {
/* Try to open/create the specified file for use as a capture buffer. */
cf.save_file_fd = open(capfile_name, O_RDWR|O_TRUNC|O_CREAT, 0600);
is_temp_file = FALSE;
} else {
/* Choose a random name for the capture buffer */
cf.save_file_fd = create_tempfile(tmpname, sizeof tmpname, "ether");
capfile_name = g_strdup(tmpname);
is_temp_file = TRUE;
}
if (cf.save_file_fd == -1) {
simple_dialog(ESD_TYPE_WARN, NULL,
"The file to which the capture would be saved (\"%s\")"
@ -124,24 +141,8 @@ do_capture(void)
unlink(cf.save_file); /* silently ignore error */
g_free(cf.save_file);
}
cf.save_file = g_strdup(tmpname);
cf.user_saved = 0;
run_capture();
}
/* Start a capture to a file we've opened; "cf.save_file" is the
pathname of the file, and "cf.save_file_fd" is the file descriptor
we got when we opened it. */
void
run_capture(void)
{
u_char c;
int i;
guint byte_count;
char *msg;
int err;
int capture_succeeded;
cf.save_file = capfile_name;
cf.user_saved = !is_temp_file;
if (sync_mode || fork_mode) { /* use fork() for capture */
int fork_child;

View File

@ -1,7 +1,7 @@
/* capture.h
* Definitions for packet capture windows
*
* $Id: capture.h,v 1.18 1999/10/02 06:00:07 guy Exp $
* $Id: capture.h,v 1.19 1999/10/02 06:26:45 guy Exp $
*
* Ethereal - Network traffic analyzer
* By Gerald Combs <gerald@zing.org>
@ -51,13 +51,9 @@
/* Name we give to the child process when doing a "-S" or "-F" capture. */
#define CHILD_NAME "ethereal-capture"
/* Create a temporary file and start a capture to it. */
void do_capture(void);
/* Start a capture to a file we've opened; "cf.save_file" is the
pathname of the file, and "cf.save_file_fd" is the file descriptor
we got when we opened it. */
void run_capture(void);
/* Open a specified file, or create a temporary file, and start a capture
to the file in question. */
void do_capture(char *capfile_name);
/* Do the low-level work of a capture. */
int capture(void);

View File

@ -1,7 +1,7 @@
/* capture_dlg.c
* Routines for packet capture windows
*
* $Id: capture_dlg.c,v 1.5 1999/09/26 14:40:01 deniel Exp $
* $Id: capture_dlg.c,v 1.6 1999/10/02 06:26:53 guy Exp $
*
* Ethereal - Network traffic analyzer
* By Gerald Combs <gerald@zing.org>
@ -293,7 +293,7 @@ capture_prep_ok_cb(GtkWidget *ok_bt, gpointer parent_w) {
gtk_widget_destroy(GTK_WIDGET(parent_w));
do_capture();
do_capture(NULL);
}
static void

View File

@ -1,6 +1,6 @@
/* main.c
*
* $Id: main.c,v 1.14 1999/10/02 05:59:58 guy Exp $
* $Id: main.c,v 1.15 1999/10/02 06:26:52 guy Exp $
*
* Ethereal - Network traffic analyzer
* By Gerald Combs <gerald@zing.org>
@ -54,7 +54,6 @@
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#ifdef HAVE_DIRECT_H
#include <direct.h>
@ -465,6 +464,7 @@ main(int argc, char *argv[])
int err;
#ifdef HAVE_LIBPCAP
gboolean start_capture = FALSE;
gchar *save_file = NULL;
#endif
GtkWidget *window, *main_vbox, *menubar, *u_pane, *l_pane,
*bv_table, *bv_hscroll, *bv_vscroll, *stat_hbox,
@ -638,8 +638,7 @@ main(int argc, char *argv[])
break;
#ifdef HAVE_LIBPCAP
case 'w': /* Write to capture file xxx */
cf.save_file = g_strdup(optarg);
cf.user_saved = TRUE; /* it's not a temporary file */
save_file = g_strdup(optarg);
break;
case 'W': /* Write to capture file FD xxx */
cf.save_file_fd = atoi(optarg);
@ -654,10 +653,6 @@ main(int argc, char *argv[])
fprintf(stderr, "ethereal: \"-k\" flag was specified without \"-i\" flag\n");
exit(1);
}
if (cf.save_file == NULL) {
fprintf(stderr, "ethereal: \"-k\" flag was specified without \"-w\" flag\n");
exit(1);
}
}
#ifdef HAVE_LIBPCAP
if (capture_child) {
@ -911,8 +906,8 @@ main(int argc, char *argv[])
if (capture_child) {
/* This is the child process for a sync mode or fork mode capture,
so just do the low-level work of a capture - don't create
a temporary file (so don't call "do_capture()"), and don't
fork off *another* child process (so don't call "run_capture()"). */
a temporary file and fork off *another* child process (so don't
call "do_capture()"). */
capture();
@ -921,25 +916,7 @@ main(int argc, char *argv[])
} else {
if (start_capture) {
/* "-k" was specified; start a capture. */
/* Try to open/create the file specified on the command line with
the "-w" flag. (We already checked in "main()" that "-w" was
specified. */
cf.save_file_fd = open(cf.save_file, O_RDWR|O_TRUNC|O_CREAT, 0600);
if (cf.save_file_fd == -1) {
/* XXX - display the error in a message box, or on the command line? */
simple_dialog(ESD_TYPE_WARN, NULL,
"The file to which the capture would be saved (\"%s\")"
"could not be opened: %s.", cf.save_file, strerror(errno));
} else {
/* XXX - "capture()" used to do this, but we now do it in
"do_capture()", before calling "capture()"; will we ever
have a capture file open here?
Yes, if "-r" was specified - but that's arguably silly, so
perhaps we should treate that as an error. */
close_cap_file(&cf, info_bar, file_ctx);
run_capture();
}
do_capture(save_file);
}
}
#endif