Mercurial > hg > toybox
comparison toys/posix/mkdir.c @ 694:786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
The actual code should be the same afterward, this is just cosmetic refactoring.
author | Rob Landley <rob@landley.net> |
---|---|
date | Tue, 13 Nov 2012 17:14:08 -0600 |
parents | 7e846e281e38 |
children | d8e2d723ab07 |
comparison
equal
deleted
inserted
replaced
693:4a5a250e0633 | 694:786841fdb1e0 |
---|---|
1 /* vi: set sw=4 ts=4: | 1 /* mkdir.c - Make directories |
2 * | |
3 * mkdir.c - Make directories | |
4 * | 2 * |
5 * Copyright 2012 Georgi Chorbadzhiyski <georgi@unixsol.org> | 3 * Copyright 2012 Georgi Chorbadzhiyski <georgi@unixsol.org> |
6 * | 4 * |
7 * See http://opengroup.org/onlinepubs/9699919799/utilities/mkdir.html | 5 * See http://opengroup.org/onlinepubs/9699919799/utilities/mkdir.html |
8 * | 6 * |
9 * TODO: Add -m | 7 * TODO: Add -m |
10 | 8 |
11 USE_MKDIR(NEWTOY(mkdir, "<1p", TOYFLAG_BIN)) | 9 USE_MKDIR(NEWTOY(mkdir, "<1p", TOYFLAG_BIN)) |
12 | 10 |
13 config MKDIR | 11 config MKDIR |
14 bool "mkdir" | 12 bool "mkdir" |
15 default y | 13 default y |
16 help | 14 help |
17 usage: mkdir [-p] [dirname...] | 15 usage: mkdir [-p] [dirname...] |
18 Create one or more directories. | 16 Create one or more directories. |
19 | 17 |
20 -p make parent directories as needed. | 18 -p make parent directories as needed. |
21 */ | 19 */ |
22 | 20 |
23 #define FOR_mkdir | 21 #define FOR_mkdir |
24 #include "toys.h" | 22 #include "toys.h" |
25 | 23 |
26 GLOBALS( | 24 GLOBALS( |
27 long mode; | 25 long mode; |
28 ) | 26 ) |
29 | 27 |
30 static int do_mkdir(char *dir) | 28 static int do_mkdir(char *dir) |
31 { | 29 { |
32 struct stat buf; | 30 struct stat buf; |
33 char *s; | 31 char *s; |
34 | 32 |
35 // mkdir -p one/two/three is not an error if the path already exists, | 33 // mkdir -p one/two/three is not an error if the path already exists, |
36 // but is if "three" is a file. The others we dereference and catch | 34 // but is if "three" is a file. The others we dereference and catch |
37 // not-a-directory along the way, but the last one we must explicitly | 35 // not-a-directory along the way, but the last one we must explicitly |
38 // test for. Might as well do it up front. | 36 // test for. Might as well do it up front. |
39 | 37 |
40 if (!stat(dir, &buf) && !S_ISDIR(buf.st_mode)) { | 38 if (!stat(dir, &buf) && !S_ISDIR(buf.st_mode)) { |
41 errno = EEXIST; | 39 errno = EEXIST; |
42 return 1; | 40 return 1; |
43 } | 41 } |
44 | 42 |
45 for (s=dir; ; s++) { | 43 for (s=dir; ; s++) { |
46 char save=0; | 44 char save=0; |
47 | 45 |
48 // Skip leading / of absolute paths. | 46 // Skip leading / of absolute paths. |
49 if (s!=dir && *s == '/' && toys.optflags) { | 47 if (s!=dir && *s == '/' && toys.optflags) { |
50 save = *s; | 48 save = *s; |
51 *s = 0; | 49 *s = 0; |
52 } else if (*s) continue; | 50 } else if (*s) continue; |
53 | 51 |
54 if (mkdir(dir, TT.mode)<0 && (!toys.optflags || errno != EEXIST)) | 52 if (mkdir(dir, TT.mode)<0 && (!toys.optflags || errno != EEXIST)) |
55 return 1; | 53 return 1; |
56 | 54 |
57 if (!(*s = save)) break; | 55 if (!(*s = save)) break; |
58 } | 56 } |
59 | 57 |
60 return 0; | 58 return 0; |
61 } | 59 } |
62 | 60 |
63 void mkdir_main(void) | 61 void mkdir_main(void) |
64 { | 62 { |
65 char **s; | 63 char **s; |
66 | 64 |
67 TT.mode = 0777; | 65 TT.mode = 0777; |
68 | 66 |
69 for (s=toys.optargs; *s; s++) { | 67 for (s=toys.optargs; *s; s++) { |
70 if (do_mkdir(*s)) { | 68 if (do_mkdir(*s)) { |
71 perror_msg("cannot create directory '%s'", *s); | 69 perror_msg("cannot create directory '%s'", *s); |
72 toys.exitval = 1; | 70 toys.exitval = 1; |
73 } | 71 } |
74 } | 72 } |
75 } | 73 } |