Fix mkpath() to handle relative dirs

After input from both StackOverflow and Valery Frolov.

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
Joachim Nilsson committed 2016-05-23 09:15:50 +02:00
1 parent 7df1b75fa0
commit 613d397161
1 file changed
+4 -1
+4 -1
View File
@@ -25,12 +25,14 @@
int mkpath(char *dir, mode_t mode) int mkpath(char *dir, mode_t mode)
{ {
struct stat sb;
if (!dir) { if (!dir) {
errno = EINVAL; errno = EINVAL;
return 1; return 1;
} }
if (strlen(dir) == 1 && dir[0] == '/') if (!stat(dir, &sb))
return 0; return 0;
mkpath(dirname(strdupa(dir)), mode); mkpath(dirname(strdupa(dir)), mode);
@@ -96,6 +98,7 @@ int main(void)
"/tmp/a/", "/tmp/a/",
"/tmp/a/b", "/tmp/a/b",
"/tmp/a/c/", "/tmp/a/c/",
"../../sometestdir/arpa/inet",
NULL NULL
}; };