[go: up one dir, main page]

File: mkpath.c

package info (click to toggle)
rdup 1.0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 1,996 kB
  • ctags: 185
  • sloc: ansic: 3,326; sh: 3,221; exp: 166; makefile: 64
file content (44 lines) | stat: -rw-r--r-- 1,003 bytes parent folder | download
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);
}