summaryrefslogtreecommitdiff
path: root/src/misc/dirname.c
diff options
context:
space:
mode:
authorRich Felker <dalias@aerifal.cx>2012-09-26 00:56:07 -0400
committerRich Felker <dalias@aerifal.cx>2012-09-26 00:56:07 -0400
commit507faa63cb12571c14d66eb37dedbe6adbbf40a8 (patch)
treee01596e47979931728740b1b2f493d8533577aa1 /src/misc/dirname.c
parent82dc1e2e783815e00a90cd3f681436a80d54a314 (diff)
downloadmusl-507faa63cb12571c14d66eb37dedbe6adbbf40a8.tar.gz
fix dirname to handle input of form "foo/" correctly
also optimized a bit.
Diffstat (limited to 'src/misc/dirname.c')
-rw-r--r--src/misc/dirname.c11
1 files changed, 5 insertions, 6 deletions
diff --git a/src/misc/dirname.c b/src/misc/dirname.c
index 8f70dbb1..dd570883 100644
--- 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;
}