1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
/* Function with behaviour like `mkdir -p' */
/* From: http://niallohiggins.com/2009/01/08/mkpath-mkdir-p-alike-in-c-for-unix/
* with some tweaks
* libglib'i'fied by Miek Gieben
*/
#include "rdup-up.h"
int
mkpath(const char *s, mode_t mode)
{
char *q, *r = NULL, *path = NULL, *up = NULL;
int rv = -1;
if (strcmp(s, ".") == 0 || strcmp(s, "/") == 0)
return 0;
if ((path = g_strdup(s)) == NULL)
return -1;
if ((q = g_strdup(s)) == NULL)
return -1;
if ((r = dirname(q)) == NULL)
goto out;
if ((up = g_strdup(r)) == NULL)
return -1;
if ((mkpath(up, mode) == -1) && (errno != EEXIST))
goto out;
if ((mkdir(path, mode) == -1) && (errno != EEXIST))
rv = -1;
else
rv = 0;
out:
if (up)
free(up);
free(q);
free(path);
return (rv);
}
|