summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJim Meyering <jim@meyering.net>2000-10-21 11:44:43 +0000
committerJim Meyering <jim@meyering.net>2000-10-21 11:44:43 +0000
commitef43db1c4baa92428389eb3a3c667dcee47c6f13 (patch)
tree1cf079c368a1c9e7472a946544ba2fa061a9d72b
parentb9dbd947126cee9b31e9226392083e32c0eaae63 (diff)
downloadcoreutils-ef43db1c4baa92428389eb3a3c667dcee47c6f13.tar.xz
(dir_name_r): New function, factored out of dir_name.
(dir_name): Use dir_name_r.
-rw-r--r--lib/dirname.c30
1 files changed, 20 insertions, 10 deletions
diff --git a/lib/dirname.c b/lib/dirname.c
index e01fa42ac..eddbe97a2 100644
--- a/lib/dirname.c
+++ b/lib/dirname.c
@@ -49,15 +49,11 @@ void *memrchr ();
#define BACKSLASH_IS_PATH_SEPARATOR ISSLASH ('\\')
-/* Return the leading directories part of PATH,
- allocated with malloc. If out of memory, return 0.
- Works properly even if there are trailing slashes
- (by effectively ignoring them). */
-
-char *
-dir_name (const char *path)
+/* Return the length of the directory part of PATH.
+ Set *RESULT to point to PATH or to `"."', as appropriate. */
+size_t
+dir_name_r (const char *path, const char **result)
{
- char *newpath;
char *slash;
int length; /* Length of result, not including NUL. */
@@ -118,10 +114,24 @@ dir_name (const char *path)
length = slash - path + 1;
}
- newpath = (char *) malloc (length + 1);
+ *result = path;
+ return length;
+}
+
+/* Return the leading directories part of PATH,
+ allocated with malloc. If out of memory, return 0.
+ Works properly even if there are trailing slashes
+ (by effectively ignoring them). */
+
+char *
+dir_name (const char *path)
+{
+ const char *result;
+ size_t length = dir_name_r (path, &result);
+ char *newpath = (char *) malloc (length + 1);
if (newpath == 0)
return 0;
- strncpy (newpath, path, length);
+ strncpy (newpath, result, length);
newpath[length] = 0;
return newpath;
}