提交 507faa63 编写于 作者: R Rich Felker

fix dirname to handle input of form "foo/" correctly

also optimized a bit.
上级 82dc1e2e
...@@ -4,12 +4,11 @@ ...@@ -4,12 +4,11 @@
char *dirname(char *s) char *dirname(char *s)
{ {
size_t i; size_t i;
if (!s || !*s || !strchr(s, '/')) return "."; if (!s || !*s) return ".";
i = strlen(s)-1; i = strlen(s)-1;
for (; i&&s[i]=='/'; i--); for (; s[i]=='/'; i--) if (!i) return "/";
for (; i&&s[i-1]!='/'; i--); for (; s[i]!='/'; i--) if (!i) return ".";
for (; i&&s[i-1]=='/'; i--); for (; s[i]=='/'; i--) if (!i) return "/";
if (!i && *s=='/') i++; s[i+1] = 0;
s[i] = 0;
return s; return s;
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册