aboutsummaryrefslogtreecommitdiff
path: root/toys/mkdir.c
blob: d875f95d2b7774c30281846219a765dc8f9be3ca (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
59
60
61
62
/* vi: set sw=4 ts=4:
 *
 * mkdir.c - Make directories
 *
 * Copyright 2012 Georgi Chorbadzhiyski <georgi@unixsol.org>
 *
 * See http://pubs.opengroup.org/onlinepubs/009695399/utilities/mkdir.html
 *
 * TODO: Add -m

USE_MKDIR(NEWTOY(mkdir, "<1p", TOYFLAG_BIN))

config MKDIR
	bool "mkdir"
	default y
	help
	  usage: mkdir [-p] [dirname...]
	  Create one or more directories.

	  -p	make parent directories as needed.
*/

#include "toys.h"

DEFINE_GLOBALS(
	long mode;
)

#define TT this.mkdir

static int do_mkdir(char *dir)
{
	unsigned int i;

	if (toys.optflags && *dir) {
		// Skip first char (it can be /)
		for (i = 1; dir[i]; i++) {
			int ret;

			if (dir[i] != '/') continue;
			dir[i] = 0;
			ret = mkdir(dir, TT.mode);
			if (ret < 0 && errno != EEXIST) return ret;
			dir[i] = '/';
		}
	}
	return mkdir(dir, TT.mode);
}

void mkdir_main(void)
{
	char **s;

	TT.mode = 0777;

	for (s=toys.optargs; *s; s++) {
		if (do_mkdir(*s)) {
			perror_msg("cannot create directory '%s'", *s);
			toys.exitval = 1;
		}
	}
}