This avoids a crash when running "ccache -s" with CCACHE_DIR="".
CHECK_STR_EQ_FREE2(".", dirname("foo.c"));
CHECK_STR_EQ_FREE2(".", dirname(""));
CHECK_STR_EQ_FREE2("/", dirname("/"));
+ CHECK_STR_EQ_FREE2("/", dirname("/foo.c"));
CHECK_STR_EQ_FREE2("dir1/dir2", dirname("dir1/dir2/foo.c"));
CHECK_STR_EQ_FREE2("/dir", dirname("/dir/foo.c"));
CHECK_STR_EQ_FREE2("dir1/dir2", dirname("dir1/dir2/"));
dirname(const char *path)
{
char *p;
- char *p2 = NULL;
+#ifdef _WIN32
+ char *p2;
+#endif
char *s;
s = x_strdup(path);
p = strrchr(s, '/');
#ifdef _WIN32
p2 = strrchr(s, '\\');
-#endif
- if (p < p2)
+ if (!p || (p2 && p < p2)) {
p = p2;
- if (p == s) {
- return s;
- } else if (p) {
- *p = 0;
- return s;
- } else {
+ }
+#endif
+ if (!p) {
free(s);
- return x_strdup(".");
+ s = x_strdup(".");
+ } else if (p == s) {
+ *(p + 1) = 0;
+ } else {
+ *p = 0;
}
+ return s;
}
/*