Encapsulate the code to take a pointer to a pathname and return a
authorguy <guy@f5534014-38df-0310-8fa8-9805f1628bb7>
Tue, 25 Jan 2000 04:31:17 +0000 (04:31 +0000)
committerguy <guy@f5534014-38df-0310-8fa8-9805f1628bb7>
Tue, 25 Jan 2000 04:31:17 +0000 (04:31 +0000)
pointer to the name of the file to which it refers (i.e., to the last
component of the pathname) in a "get_basename()" routine, and have the
code in "file.c" call it.

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

file.c
globals.h
util.c
util.h

diff --git a/file.c b/file.c
index e89fcbfa7fdca4d1af2ecd0ba98d6f26217630e0..079806df58d9b140896e5272ecb4c96cfada5efa 100644 (file)
--- a/file.c
+++ b/file.c
@@ -1,7 +1,7 @@
 /* file.c
  * File I/O routines
  *
- * $Id: file.c,v 1.158 2000/01/25 01:05:06 guy Exp $
+ * $Id: file.c,v 1.159 2000/01/25 04:31:14 guy Exp $
  *
  * Ethereal - Network traffic analyzer
  * By Gerald Combs <gerald@zing.org>
@@ -247,10 +247,7 @@ set_display_filename(capture_file *cf)
   if (!cf->is_tempfile) {
     /* Get the last component of the file name, and put that in the
        status bar. */
-    if ((name_ptr = (gchar *) strrchr(cf->filename, PATH_SEPARATOR)) == NULL)
-      name_ptr = cf->filename;
-    else
-      name_ptr++;
+    name_ptr = get_basename(cf->filename);
   } else {
     /* The file we read is a temporary file from a live capture;
        we don't mention its name in the status bar. */
@@ -281,10 +278,7 @@ read_cap_file(capture_file *cf)
   char    errmsg_errno[1024+1];
   gchar   err_str[2048+1];
 
-  if ((name_ptr = (gchar *) strrchr(cf->filename, PATH_SEPARATOR)) == NULL)
-    name_ptr = cf->filename;
-  else
-    name_ptr++;
+  name_ptr = get_basename(cf->filename);
 
   msg_len = strlen(name_ptr) + strlen(load_fmt) + 2;
   load_msg = g_malloc(msg_len);
@@ -1324,10 +1318,7 @@ save_cap_file(char *fname, capture_file *cf, gboolean save_filtered,
   struct wtap_pkthdr hdr;
   guint8        pd[65536];
 
-  if ((name_ptr = (gchar *) strrchr(fname, PATH_SEPARATOR)) == NULL)
-    name_ptr = fname;
-  else
-    name_ptr++;
+  name_ptr = get_basename(fname);
   msg_len = strlen(name_ptr) + strlen(save_fmt) + 2;
   save_msg = g_malloc(msg_len);
   snprintf(save_msg, msg_len, save_fmt, name_ptr);
index c8766d93884423dbe2d15ea16d027cd111f615e8..71e29bd52684cbdf22b4c1a3526f2c50f9ca57fb 100644 (file)
--- a/globals.h
+++ b/globals.h
@@ -1,7 +1,7 @@
 /* globals.h
  * Global defines, etc.
  *
- * $Id: globals.h,v 1.16 2000/01/25 00:17:01 guy Exp $
+ * $Id: globals.h,v 1.17 2000/01/25 04:31:16 guy Exp $
  *
  * Ethereal - Network traffic analyzer
  * By Gerald Combs <gerald@zing.org>
 # define MIN(x, y) ((x) < (y) ? (x) : (y))
 #endif
 
-/* Pathname separator. */
-#ifdef WIN32
-#define PATH_SEPARATOR  '\\'
-#else
-#define PATH_SEPARATOR  '/'
-#endif
-
 extern FILE        *data_out_file;
 extern packet_info  pi;
 extern capture_file cf;
diff --git a/util.c b/util.c
index 676b055fc78af7426f2670ece6cb74b1acd1ba98..40c8635f28e2de386456ec61165f62766ddbf97e 100644 (file)
--- a/util.c
+++ b/util.c
@@ -1,7 +1,7 @@
 /* util.c
  * Utility routines
  *
- * $Id: util.c,v 1.27 2000/01/16 02:47:47 guy Exp $
+ * $Id: util.c,v 1.28 2000/01/25 04:31:16 guy Exp $
  *
  * Ethereal - Network traffic analyzer
  * By Gerald Combs <gerald@zing.org>
@@ -88,6 +88,43 @@ typedef int mode_t;  /* for win32 */
 
 #endif
 
+/*
+ * Given a pathname, return the last component.
+ */
+char *
+get_basename(char *path)
+{
+       char *filename;
+
+#ifdef WIN32
+       /*
+        * XXX - do we need to search for '/' as well?
+        */
+       if ((filename = strrchr(path, '\\')) == NULL) {
+               /*
+                * OK, no directories - but there might be a drive
+                * letter....
+                */
+               filename = strchr(path, ':');
+       }
+#else
+       filename = strrchr(path, '/');
+#endif
+       if (filename == NULL) {
+               /*
+                * There're no directories, drive letters, etc. in the
+                * name; the pathname *is* the file name.
+                */
+               filename = path;
+       } else {
+               /*
+                * Skip past the pathname or drive letter separator.
+                */
+               filename++;
+       }
+       return filename;
+}
+
 static char *
 setup_tmpdir(char *dir)
 {
diff --git a/util.h b/util.h
index 9ea2a8bfa348b386c889cc3da49df9ade39741fd..f48be86a3db2bd645f6d65937a859906ee0a8af0 100644 (file)
--- a/util.h
+++ b/util.h
@@ -1,7 +1,7 @@
 /* util.h
  * Utility definitions
  *
- * $Id: util.h,v 1.15 2000/01/16 02:47:47 guy Exp $
+ * $Id: util.h,v 1.16 2000/01/25 04:31:17 guy Exp $
  *
  * Ethereal - Network traffic analyzer
  * By Gerald Combs <gerald@zing.org>
@@ -30,6 +30,8 @@
 extern "C" {
 #endif /* __cplusplus */
 
+char *get_basename(char *);
+
 int create_tempfile(char *, int, const char *);
 
 void ASCII_to_EBCDIC(guint8 *buf, guint bytes);