diff options
Diffstat (limited to 'mkdir.c')
-rw-r--r-- | mkdir.c | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/mkdir.c b/mkdir.c new file mode 100644 index 000000000..8f1fa04db --- /dev/null +++ b/mkdir.c | |||
@@ -0,0 +1,58 @@ | |||
1 | #include "internal.h" | ||
2 | #include <errno.h> | ||
3 | #include <sys/param.h> | ||
4 | |||
5 | const char mkdir_usage[] = "mkdir [-m mode] directory [directory ...]\n" | ||
6 | "\tCreate directories.\n" | ||
7 | "\n" | ||
8 | "\t-m mode:\tSpecifiy the mode for the new directory\n" | ||
9 | "\t\tunder the argument directory."; | ||
10 | |||
11 | /*make directories skipping the last part of the path. Used here and by untar*/ | ||
12 | int mkdir_until(const char *fpath, const struct FileInfo * fi) | ||
13 | { | ||
14 | char path[PATH_MAX]; | ||
15 | char * s = path; | ||
16 | |||
17 | strcpy(path, fpath); | ||
18 | if ( s[0] == '\0' && s[1] == '\0' ) { | ||
19 | usage(mkdir_usage); | ||
20 | return 1; | ||
21 | } | ||
22 | s++; | ||
23 | while ( *s != '\0' ) { | ||
24 | if ( *s == '/' ) { | ||
25 | int status; | ||
26 | |||
27 | *s = '\0'; | ||
28 | status = mkdir(path, (fi?fi->orWithMode:0700) ); | ||
29 | *s = '/'; | ||
30 | |||
31 | if ( status != 0 ) { | ||
32 | if ( errno != EEXIST ) { | ||
33 | name_and_error(fpath); | ||
34 | return 1; | ||
35 | } | ||
36 | } | ||
37 | |||
38 | } | ||
39 | s++; | ||
40 | } | ||
41 | return 0; | ||
42 | } | ||
43 | |||
44 | int | ||
45 | mkdir_fn(const struct FileInfo * i) | ||
46 | { | ||
47 | if ( i->makeParentDirectories ) { | ||
48 | if(mkdir_until(i->source, i)) return 1; | ||
49 | } | ||
50 | |||
51 | if ( mkdir(i->source, i->orWithMode) != 0 && errno != EEXIST ) { | ||
52 | name_and_error(i->source); | ||
53 | return 1; | ||
54 | } | ||
55 | else | ||
56 | return 0; | ||
57 | } | ||
58 | |||