blob: 8f1fa04db50d705f93a888e6db2deb99a6314a80 [file] [log] [blame]
Eric Andersencc8ed391999-10-05 16:24:54 +00001#include "internal.h"
2#include <errno.h>
3#include <sys/param.h>
4
5const 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*/
12int 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
44int
45mkdir_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