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
2016-05-23 09:15:50 +02:00
parent 7df1b75fa0
commit 613d397161
+4 -1
View File
@@ -25,12 +25,14 @@
int mkpath(char *dir, mode_t mode)
{
struct stat sb;
if (!dir) {
errno = EINVAL;
return 1;
}
if (strlen(dir) == 1 && dir[0] == '/')
if (!stat(dir, &sb))
return 0;
mkpath(dirname(strdupa(dir)), mode);
@@ -96,6 +98,7 @@ int main(void)
"/tmp/a/",
"/tmp/a/b",
"/tmp/a/c/",
"../../sometestdir/arpa/inet",
NULL
};