aboutsummaryrefslogtreecommitdiffstats
path: root/util.c
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 /util.c
parentf71823a907ee22663dd666a0733c068f5475e69e (diff)
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
Diffstat (limited to 'util.c')
-rw-r--r--util.c62
1 files changed, 54 insertions, 8 deletions
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)
{