aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2019-10-18 20:52:49 -0500
committerRob Landley <rob@landley.net>2019-10-18 20:52:49 -0500
commitf4a98356220bb1084ef72d26c9211ee63dd3c852 (patch)
tree70cdaf176c2b9aa2547b2e860c50e68b50e6e775
parentf5425b1c4875383d7a4963d79bca8dbf241ec05c (diff)
downloadtoybox-f4a98356220bb1084ef72d26c9211ee63dd3c852.tar.gz
Teach getdirname() to return "." as appropriate.
-rw-r--r--lib/lib.c14
1 files changed, 9 insertions, 5 deletions
diff --git a/lib/lib.c b/lib/lib.c
index fe402af8..f98f00a5 100644
--- a/lib/lib.c
+++ b/lib/lib.c
@@ -1005,14 +1005,18 @@ void mode_to_string(mode_t mode, char *buf)
}
// dirname() can modify its argument or return a pointer to a constant string
-// This always returns a malloc() copy of everyting before last (run of ) '/'.
+// This returns a malloc() copy of everyting before last (run of ) '/',
+// or "." if no path.
char *getdirname(char *name)
{
- char *s = xstrdup(name), *ss = strrchr(s, '/');
+ char *s, *ss, *keep;
- while (ss && *ss && *ss == '/' && s != ss) *ss-- = 0;
-
- return s;
+ for (s = name, ss = keep = 0; ; s++) {
+ if (!*s) return keep ? xstrndup(name, keep-name) : xstrdup(".");
+ if (*s != '/') keep = ss;
+ else if (s == name) keep = ss = s+1;
+ else if (s[-1] != '/') ss = s;
+ }
}
// basename() can modify its argument or return a pointer to a constant string