summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGuy Harris <guy@alum.mit.edu>2000-01-25 05:48:47 +0000
committerGuy Harris <guy@alum.mit.edu>2000-01-25 05:48:47 +0000
commitd1aac3e35f7e2d1430a198f8c41a5bed7ac3b3a7 (patch)
treeb81626683b8737b43718d8b234fcbc1cf300ba5b
parentf71823a907ee22663dd666a0733c068f5475e69e (diff)
downloadwireshark-d1aac3e35f7e2d1430a198f8c41a5bed7ac3b3a7.tar.gz
Provide a "get_dirname()" routine, that takes a pathname and returns
either a pointer to the directory part of the pathname (after stomping on the pathname separator with a '\0', so don't use this on pathnames you plan to use afterwards), or NULL if the pathname contains no directory part, and make it handle Win32 pathnames on Win32 systems. Use it to get the containing directory of the currently open file, so that the "chdir()" stuff we do to cause the "File:Open" dialog box to show you files in the directory in which you last looked works on Win32 systems. svn path=/trunk/; revision=1555
-rw-r--r--gtk/file_dlg.c54
-rw-r--r--gtk/main.c14
-rw-r--r--util.c62
-rw-r--r--util.h12
4 files changed, 111 insertions, 31 deletions
diff --git a/gtk/file_dlg.c b/gtk/file_dlg.c
index 62daf702e0..062386e4d1 100644
--- a/gtk/file_dlg.c
+++ b/gtk/file_dlg.c
@@ -1,7 +1,7 @@
/* file_dlg.c
* Dialog boxes for handling files
*
- * $Id: file_dlg.c,v 1.17 2000/01/03 06:59:20 guy Exp $
+ * $Id: file_dlg.c,v 1.18 2000/01/25 05:48:46 guy Exp $
*
* Ethereal - Network traffic analyzer
* By Gerald Combs <gerald@zing.org>
@@ -64,6 +64,10 @@
#include "main.h"
#endif
+#ifndef __UTIL_H__
+#include "util.h"
+#endif
+
static void file_open_ok_cb(GtkWidget *w, GtkFileSelection *fs);
static void select_file_type_cb(GtkWidget *w, gpointer data);
static void file_save_as_ok_cb(GtkWidget *w, GtkFileSelection *fs);
@@ -73,6 +77,18 @@ void
file_open_cmd_cb(GtkWidget *w, gpointer data) {
GtkWidget *filter_hbox, *filter_bt, *filter_te;
+ /* XXX - GTK+'s file selection dialog box doesn't let you set the
+ initial directory it should show; it always uses the current
+ directory. We want to start out by showing the user the files
+ in the last directory in which they looked, so we have to "chdir()"
+ there.
+
+ This means means that if Ethereal dumps core, the core file will be
+ dumped in whatever directory it last "chdir()"red to, rather
+ than in the directory in which you started it.
+
+ It also means, for better or worse, that *all* file selection
+ dialog boxes will start in that directory. */
if (last_open_dir)
chdir(last_open_dir);
@@ -168,21 +184,27 @@ file_open_ok_cb(GtkWidget *w, GtkFileSelection *fs) {
gtk_widget_destroy(GTK_WIDGET (fs));
err = read_cap_file(&cf);
- /* Save the directory name; we can write over cf_name. */
- s = strrchr(cf_name, '/');
- if (s && last_open_dir) {
- *s = '\0';
- if (strcmp(last_open_dir, cf_name) != 0) {
- g_free(last_open_dir);
- last_open_dir = g_strdup(cf_name);
- }
- }
- else if (s) { /* ! last_open_dir */
- *s = '\0';
- last_open_dir = g_strdup(cf_name);
- }
- else {
- last_open_dir = NULL;
+ /* Save the name of the containing directory specified in the path name,
+ if any; we can write over cf_name, which is a good thing, given that
+ "get_dirname()" does write over its argument. */
+ s = get_dirname(cf_name);
+ if (s != NULL) {
+ /* Well, there is a directory in there... */
+ if (last_open_dir != NULL) {
+ /* ...and we already have one saved... */
+ if (strcmp(last_open_dir, s) != 0) {
+ /* ...and it's not the same as this one, so free the old one
+ and assign a copy of the new one to it. */
+ g_free(last_open_dir);
+ last_open_dir = g_strdup(s);
+ }
+ } else {
+ /* ...and we don't already have one saved, so just save this one. */
+ last_open_dir = g_strdup(s);
+ }
+ } else {
+ /* There was no directory in there. */
+ last_open_dir = NULL;
}
g_free(cf_name);
}
diff --git a/gtk/main.c b/gtk/main.c
index f7f5c7c67a..8041f9798b 100644
--- a/gtk/main.c
+++ b/gtk/main.c
@@ -1,6 +1,6 @@
/* main.c
*
- * $Id: main.c,v 1.96 2000/01/25 04:44:33 guy Exp $
+ * $Id: main.c,v 1.97 2000/01/25 05:48:47 guy Exp $
*
* Ethereal - Network traffic analyzer
* By Gerald Combs <gerald@zing.org>
@@ -1408,11 +1408,13 @@ main(int argc, char *argv[])
attached to "cf". */
cf.rfcode = rfcode;
err = read_cap_file(&cf);
- s = strrchr(cf_name, '/');
- if (s) {
- last_open_dir = cf_name;
- *s = '\0';
- }
+ /* Save the name of the containing directory specified in the
+ path name, if any; we can write over cf_name, which is a
+ good thing, given that "get_dirname()" does write over its
+ argument. */
+ s = get_dirname(cf_name);
+ if (s != NULL)
+ last_open_dir = s;
} else {
dfilter_destroy(rfcode);
cf.rfcode = NULL;
diff --git a/util.c b/util.c
index 40c8635f28..30a6b8cf82 100644
--- a/util.c
+++ b/util.c
@@ -1,7 +1,7 @@
/* util.c
* Utility routines
*
- * $Id: util.c,v 1.28 2000/01/25 04:31:16 guy Exp $
+ * $Id: util.c,v 1.29 2000/01/25 05:48:38 guy Exp $
*
* Ethereal - Network traffic analyzer
* By Gerald Combs <gerald@zing.org>
@@ -89,27 +89,41 @@ typedef int mode_t; /* for win32 */
#endif
/*
- * Given a pathname, return the last component.
+ * Given a pathname, return a pointer to the last pathname separator
+ * character in the pathname, or NULL if the pathname contains no
+ * separators.
*/
-char *
-get_basename(char *path)
+static char *
+find_last_pathname_separator(char *path)
{
- char *filename;
+ char *separator;
#ifdef WIN32
/*
* XXX - do we need to search for '/' as well?
*/
- if ((filename = strrchr(path, '\\')) == NULL) {
+ if ((separator = strrchr(path, '\\')) == NULL) {
/*
* OK, no directories - but there might be a drive
* letter....
*/
- filename = strchr(path, ':');
+ separator = strchr(path, ':');
}
#else
- filename = strrchr(path, '/');
+ separator = strrchr(path, '/');
#endif
+ return separator;
+}
+
+/*
+ * Given a pathname, return the last component.
+ */
+char *
+get_basename(char *path)
+{
+ char *filename;
+
+ filename = find_last_pathname_separator(path);
if (filename == NULL) {
/*
* There're no directories, drive letters, etc. in the
@@ -125,6 +139,38 @@ get_basename(char *path)
return filename;
}
+/*
+ * Given a pathname, return a string containing everything but the
+ * last component. NOTE: this overwrites the pathname handed into
+ * it....
+ */
+char *
+get_dirname(char *path)
+{
+ char *separator;
+
+ separator = find_last_pathname_separator(path);
+ if (separator == NULL) {
+ /*
+ * There're no directories, drive letters, etc. in the
+ * name; there is no directory path to return.
+ */
+ return NULL;
+ }
+
+ /*
+ * Get rid of the last pathname separator and the final file
+ * name following it.
+ */
+ *separator = '\0';
+
+ /*
+ * "path" now contains the pathname of the directory containing
+ * the file/directory to which it referred.
+ */
+ return path;
+}
+
static char *
setup_tmpdir(char *dir)
{
diff --git a/util.h b/util.h
index f48be86a3d..6bea55f116 100644
--- a/util.h
+++ b/util.h
@@ -1,7 +1,7 @@
/* util.h
* Utility definitions
*
- * $Id: util.h,v 1.16 2000/01/25 04:31:17 guy Exp $
+ * $Id: util.h,v 1.17 2000/01/25 05:48:39 guy Exp $
*
* Ethereal - Network traffic analyzer
* By Gerald Combs <gerald@zing.org>
@@ -30,8 +30,18 @@
extern "C" {
#endif /* __cplusplus */
+/*
+ * Given a pathname, return the last component.
+ */
char *get_basename(char *);
+/*
+ * Given a pathname, return a string containing everything but the
+ * last component. NOTE: this overwrites the pathname handed into
+ * it....
+ */
+char *get_dirname(char *);
+
int create_tempfile(char *, int, const char *);
void ASCII_to_EBCDIC(guint8 *buf, guint bytes);