char *mydirname(const char *path)
{
- char *mypath, *dname, *ret;
-
- mypath = xstrdup(path);
- dname = dirname(mypath);
- ret = xstrdup(dname);
-
- /* We don't free mypath until we have strdup()'ed dname, because
- * dirname() returns a pointer into mypath -- mortenp 20040527 */
- free(mypath);
-
- return ret;
+ char *allocated;
+ char *walk;
+
+ if (path == NULL)
+ return (xstrdup("."));
+ allocated = xstrdup(path);
+ walk = strrchr(allocated, '/');
+ if (walk == NULL) {
+ allocated[0] = '.';
+ allocated[1] = '\0';
+ } else {
+ *walk = '\0';
+ }
+ return (allocated);
}
const char *mybasename(const char *path)
{
char plop[] = "/path/to/a/file";
- ATF_REQUIRE_STREQ(mydirname(plop), "/path/to/a");
+ char *t = mydirname(plop);
+ ATF_REQUIRE_STREQ(t, "/path/to/a");
+ free(t);
+ t = mydirname(NULL);
+ ATF_REQUIRE_STREQ(t, ".");
+ free(t);
+ t = mydirname("a");
+ ATF_REQUIRE_STREQ(t, ".");
+ free(t);
}
ATF_TC_BODY(mybasename, tc)