summaryrefslogtreecommitdiff
path: root/mkdir.c
blob: 8f1fa04db50d705f93a888e6db2deb99a6314a80 (plain)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include "internal.h"
#include <errno.h>
#include <sys/param.h>

const char mkdir_usage[] = "mkdir [-m mode] directory [directory ...]\n"
"\tCreate directories.\n"
"\n"
"\t-m mode:\tSpecifiy the mode for the new directory\n"
"\t\tunder the argument directory.";

/*make directories skipping the last part of the path. Used here and by untar*/
int mkdir_until(const char *fpath, const struct FileInfo * fi)
{
	char    path[PATH_MAX];
	char *  s = path;

	strcpy(path, fpath);
	if ( s[0] == '\0' && s[1] == '\0' ) {
		usage(mkdir_usage);
		return 1;
	}
	s++;
	while ( *s != '\0' ) {
		if ( *s == '/' ) {
			int	status;

			*s = '\0';
			status = mkdir(path, (fi?fi->orWithMode:0700) );
			*s = '/';

			if ( status != 0 ) {
				if ( errno != EEXIST ) {
					name_and_error(fpath);
					return 1;
				}
			}

		}
		s++;
	}
	return 0;
}

int
mkdir_fn(const struct FileInfo * i)
{
	if ( i->makeParentDirectories ) {
		if(mkdir_until(i->source, i)) return 1;
	}

	if ( mkdir(i->source, i->orWithMode) != 0 && errno != EEXIST ) {
		name_and_error(i->source);
		return 1;
	}
	else
		return 0;
}