Mercurial > hg > toybox
comparison toys/posix/mkdir.c @ 653:2986aa63a021
Move commands into "posix", "lsb", and "other" menus/directories.
author | Rob Landley <rob@landley.net> |
---|---|
date | Sat, 25 Aug 2012 14:25:22 -0500 |
parents | toys/mkdir.c@a864aa8c6331 |
children | 6df4ccc0acbe |
comparison
equal
deleted
inserted
replaced
652:2d7c56913fda | 653:2986aa63a021 |
---|---|
1 /* vi: set sw=4 ts=4: | |
2 * | |
3 * mkdir.c - Make directories | |
4 * | |
5 * Copyright 2012 Georgi Chorbadzhiyski <georgi@unixsol.org> | |
6 * | |
7 * See http://pubs.opengroup.org/onlinepubs/009695399/utilities/mkdir.html | |
8 * | |
9 * TODO: Add -m | |
10 | |
11 USE_MKDIR(NEWTOY(mkdir, "<1p", TOYFLAG_BIN)) | |
12 | |
13 config MKDIR | |
14 bool "mkdir" | |
15 default y | |
16 help | |
17 usage: mkdir [-p] [dirname...] | |
18 Create one or more directories. | |
19 | |
20 -p make parent directories as needed. | |
21 */ | |
22 | |
23 #include "toys.h" | |
24 | |
25 DEFINE_GLOBALS( | |
26 long mode; | |
27 ) | |
28 | |
29 #define TT this.mkdir | |
30 | |
31 static int do_mkdir(char *dir) | |
32 { | |
33 struct stat buf; | |
34 char *s; | |
35 | |
36 // mkdir -p one/two/three is not an error if the path already exists, | |
37 // but is if "three" is a file. The others we dereference and catch | |
38 // not-a-directory along the way, but the last one we must explicitly | |
39 // test for. Might as well do it up front. | |
40 | |
41 if (!stat(dir, &buf) && !S_ISDIR(buf.st_mode)) { | |
42 errno = EEXIST; | |
43 return 1; | |
44 } | |
45 | |
46 for (s=dir; ; s++) { | |
47 char save=0; | |
48 | |
49 // Skip leading / of absolute paths. | |
50 if (s!=dir && *s == '/' && toys.optflags) { | |
51 save = *s; | |
52 *s = 0; | |
53 } else if (*s) continue; | |
54 | |
55 if (mkdir(dir, TT.mode)<0 && (!toys.optflags || errno != EEXIST)) | |
56 return 1; | |
57 | |
58 if (!(*s = save)) break; | |
59 } | |
60 | |
61 return 0; | |
62 } | |
63 | |
64 void mkdir_main(void) | |
65 { | |
66 char **s; | |
67 | |
68 TT.mode = 0777; | |
69 | |
70 for (s=toys.optargs; *s; s++) { | |
71 if (do_mkdir(*s)) { | |
72 perror_msg("cannot create directory '%s'", *s); | |
73 toys.exitval = 1; | |
74 } | |
75 } | |
76 } |