aboutsummaryrefslogtreecommitdiff
path: root/lib/util.c
diff options
context:
space:
mode:
authorBen Pfaff <blp@nicira.com>2010-11-09 14:38:28 -0800
committerBen Pfaff <blp@nicira.com>2010-11-10 10:56:01 -0800
commite1aff6f9f7103ee59e2501d3e6c705a685b20aeb (patch)
treee081b78ff112641da3581a5d433fe26b9920aac8 /lib/util.c
parentc214278b0d0e9b574a1f505f14a47d3d8d81aea1 (diff)
util: New function base_name().
Diffstat (limited to 'lib/util.c')
-rw-r--r--lib/util.c40
1 files changed, 32 insertions, 8 deletions
diff --git a/lib/util.c b/lib/util.c
index 328cfac4..12403cbb 100644
--- a/lib/util.c
+++ b/lib/util.c
@@ -395,6 +395,14 @@ get_cwd(void)
}
}
+static char *
+all_slashes_name(const char *s)
+{
+ return xstrdup(s[0] == '/' && s[1] == '/' && s[2] != '/' ? "//"
+ : s[0] == '/' ? "/"
+ : ".");
+}
+
/* Returns the directory name portion of 'file_name' as a malloc()'d string,
* similar to the POSIX dirname() function but thread-safe. */
char *
@@ -410,15 +418,31 @@ dir_name(const char *file_name)
while (len > 0 && file_name[len - 1] == '/') {
len--;
}
- if (!len) {
- return xstrdup((file_name[0] == '/'
- && file_name[1] == '/'
- && file_name[2] != '/') ? "//"
- : file_name[0] == '/' ? "/"
- : ".");
- } else {
- return xmemdup0(file_name, len);
+ return len ? xmemdup0(file_name, len) : all_slashes_name(file_name);
+}
+
+/* Returns the file name portion of 'file_name' as a malloc()'d string,
+ * similar to the POSIX basename() function but thread-safe. */
+char *
+base_name(const char *file_name)
+{
+ size_t end, start;
+
+ end = strlen(file_name);
+ while (end > 0 && file_name[end - 1] == '/') {
+ end--;
+ }
+
+ if (!end) {
+ return all_slashes_name(file_name);
}
+
+ start = end;
+ while (start > 0 && file_name[start - 1] != '/') {
+ start--;
+ }
+
+ return xmemdup0(file_name + start, end - start);
}
/* If 'file_name' starts with '/', returns a copy of 'file_name'. Otherwise,