projects
/
oweals
/
musl.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
| inline |
side by side
(parent:
82dc1e2
)
fix dirname to handle input of form "foo/" correctly
author
Rich Felker
<dalias@aerifal.cx>
Wed, 26 Sep 2012 04:56:07 +0000
(
00:56
-0400)
committer
Rich Felker
<dalias@aerifal.cx>
Wed, 26 Sep 2012 04:56:07 +0000
(
00:56
-0400)
also optimized a bit.
src/misc/dirname.c
patch
|
blob
|
history
diff --git
a/src/misc/dirname.c
b/src/misc/dirname.c
index 8f70dbb1feb1773fd5ed6325c374b749d2ab5e58..dd570883d8ac672716adaab40dce4bf34be1ddf7 100644
(file)
--- a/
src/misc/dirname.c
+++ b/
src/misc/dirname.c
@@
-4,12
+4,11
@@
char *dirname(char *s)
{
size_t i;
- if (!s || !*s
|| !strchr(s, '/')
) return ".";
+ if (!s || !*s) return ".";
i = strlen(s)-1;
- for (; i&&s[i]=='/'; i--);
- for (; i&&s[i-1]!='/'; i--);
- for (; i&&s[i-1]=='/'; i--);
- if (!i && *s=='/') i++;
- s[i] = 0;
+ for (; s[i]=='/'; i--) if (!i) return "/";
+ for (; s[i]!='/'; i--) if (!i) return ".";
+ for (; s[i]=='/'; i--) if (!i) return "/";
+ s[i+1] = 0;
return s;
}