Mercurial > hg > toybox
annotate toys/posix/mkdir.c @ 1143:2115856395e2 draft
Ashwini Sharma added -v, I tweaked it a bit and added a couple test suite entries.
author | Rob Landley <rob@landley.net> |
---|---|
date | Wed, 18 Dec 2013 10:20:16 -0600 |
parents | d90840f337ea |
children | c6c5565cc859 |
rev | line source |
---|---|
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.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
1 /* mkdir.c - Make directories |
526 | 2 * |
3 * Copyright 2012 Georgi Chorbadzhiyski <georgi@unixsol.org> | |
4 * | |
656
6df4ccc0acbe
Regularize command headers, update links to standards documents.
Rob Landley <rob@landley.net>
parents:
653
diff
changeset
|
5 * See http://opengroup.org/onlinepubs/9699919799/utilities/mkdir.html |
526 | 6 |
1143
2115856395e2
Ashwini Sharma added -v, I tweaked it a bit and added a couple test suite entries.
Rob Landley <rob@landley.net>
parents:
1047
diff
changeset
|
7 USE_MKDIR(NEWTOY(mkdir, "<1vpm:", TOYFLAG_BIN|TOYFLAG_UMASK)) |
526 | 8 |
9 config MKDIR | |
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.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
10 bool "mkdir" |
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.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
11 default y |
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.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
12 help |
1143
2115856395e2
Ashwini Sharma added -v, I tweaked it a bit and added a couple test suite entries.
Rob Landley <rob@landley.net>
parents:
1047
diff
changeset
|
13 usage: mkdir [-vp] [-m mode] [dirname...] |
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.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
14 Create one or more directories. |
526 | 15 |
1143
2115856395e2
Ashwini Sharma added -v, I tweaked it a bit and added a couple test suite entries.
Rob Landley <rob@landley.net>
parents:
1047
diff
changeset
|
16 -m set permissions of directory to mode. |
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.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
17 -p make parent directories as needed. |
1143
2115856395e2
Ashwini Sharma added -v, I tweaked it a bit and added a couple test suite entries.
Rob Landley <rob@landley.net>
parents:
1047
diff
changeset
|
18 -v verbose |
526 | 19 */ |
20 | |
674
7e846e281e38
New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
21 #define FOR_mkdir |
526 | 22 #include "toys.h" |
23 | |
674
7e846e281e38
New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
24 GLOBALS( |
763 | 25 char *arg_mode; |
765 | 26 |
763 | 27 mode_t mode; |
527
a3116cb7ba1e
mkdir cleanups: Let umask do its thing at the syscall level, have mode be a global to prepare for -m, use do_blah name loopfiles() usually calls, one less redundant mkdir() call, go ahead and modify writeable args instead of strdup(), return before restoring / so error message is better, use perror_msg().
Rob Landley <rob@landley.net>
parents:
526
diff
changeset
|
28 ) |
526 | 29 |
527
a3116cb7ba1e
mkdir cleanups: Let umask do its thing at the syscall level, have mode be a global to prepare for -m, use do_blah name loopfiles() usually calls, one less redundant mkdir() call, go ahead and modify writeable args instead of strdup(), return before restoring / so error message is better, use perror_msg().
Rob Landley <rob@landley.net>
parents:
526
diff
changeset
|
30 static int do_mkdir(char *dir) |
a3116cb7ba1e
mkdir cleanups: Let umask do its thing at the syscall level, have mode be a global to prepare for -m, use do_blah name loopfiles() usually calls, one less redundant mkdir() call, go ahead and modify writeable args instead of strdup(), return before restoring / so error message is better, use perror_msg().
Rob Landley <rob@landley.net>
parents:
526
diff
changeset
|
31 { |
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.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
32 struct stat buf; |
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.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
33 char *s; |
526 | 34 |
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.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
35 // mkdir -p one/two/three is not an error if the path already exists, |
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.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
36 // but is if "three" is a file. The others we dereference and catch |
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.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
37 // not-a-directory along the way, but the last one we must explicitly |
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.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
38 // test for. Might as well do it up front. |
534
a864aa8c6331
Fix mkdir -p to accept paths that already exist, and detect path ending in a file.
Rob Landley <rob@landley.net>
parents:
527
diff
changeset
|
39 |
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.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
40 if (!stat(dir, &buf) && !S_ISDIR(buf.st_mode)) { |
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.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
41 errno = EEXIST; |
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.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
42 return 1; |
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.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
43 } |
526 | 44 |
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.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
45 for (s=dir; ; s++) { |
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.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
46 char save=0; |
1047
d90840f337ea
Make chdir handle permissions according to posix, reported by Jacek Bukarewicz..
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
47 mode_t mode = 0777&~toys.old_umask; |
534
a864aa8c6331
Fix mkdir -p to accept paths that already exist, and detect path ending in a file.
Rob Landley <rob@landley.net>
parents:
527
diff
changeset
|
48 |
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.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
49 // Skip leading / of absolute paths. |
763 | 50 if (s!=dir && *s == '/' && (toys.optflags&FLAG_p)) { |
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.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
51 save = *s; |
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.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
52 *s = 0; |
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.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
53 } else if (*s) continue; |
534
a864aa8c6331
Fix mkdir -p to accept paths that already exist, and detect path ending in a file.
Rob Landley <rob@landley.net>
parents:
527
diff
changeset
|
54 |
763 | 55 // Use the mode from the -m option only for the last directory. |
1047
d90840f337ea
Make chdir handle permissions according to posix, reported by Jacek Bukarewicz..
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
56 if (save == '/') mode |= 0300; |
d90840f337ea
Make chdir handle permissions according to posix, reported by Jacek Bukarewicz..
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
57 else if (toys.optflags&FLAG_m) mode = TT.mode; |
763 | 58 |
1143
2115856395e2
Ashwini Sharma added -v, I tweaked it a bit and added a couple test suite entries.
Rob Landley <rob@landley.net>
parents:
1047
diff
changeset
|
59 if (mkdir(dir, mode)) { |
2115856395e2
Ashwini Sharma added -v, I tweaked it a bit and added a couple test suite entries.
Rob Landley <rob@landley.net>
parents:
1047
diff
changeset
|
60 if (!(toys.optflags&FLAG_p) || errno != EEXIST) return 1; |
2115856395e2
Ashwini Sharma added -v, I tweaked it a bit and added a couple test suite entries.
Rob Landley <rob@landley.net>
parents:
1047
diff
changeset
|
61 } else if (toys.optflags&FLAG_v) |
2115856395e2
Ashwini Sharma added -v, I tweaked it a bit and added a couple test suite entries.
Rob Landley <rob@landley.net>
parents:
1047
diff
changeset
|
62 fprintf(stderr, "%s: created directory '%s'\n", toys.which->name, dir); |
2115856395e2
Ashwini Sharma added -v, I tweaked it a bit and added a couple test suite entries.
Rob Landley <rob@landley.net>
parents:
1047
diff
changeset
|
63 |
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.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
64 if (!(*s = save)) break; |
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.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
65 } |
534
a864aa8c6331
Fix mkdir -p to accept paths that already exist, and detect path ending in a file.
Rob Landley <rob@landley.net>
parents:
527
diff
changeset
|
66 |
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.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
67 return 0; |
526 | 68 } |
69 | |
70 void mkdir_main(void) | |
71 { | |
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.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
72 char **s; |
527
a3116cb7ba1e
mkdir cleanups: Let umask do its thing at the syscall level, have mode be a global to prepare for -m, use do_blah name loopfiles() usually calls, one less redundant mkdir() call, go ahead and modify writeable args instead of strdup(), return before restoring / so error message is better, use perror_msg().
Rob Landley <rob@landley.net>
parents:
526
diff
changeset
|
73 |
765 | 74 if(toys.optflags&FLAG_m) TT.mode = string_to_mode(TT.arg_mode, 0777); |
526 | 75 |
780
6cc69be43c42
Have error_msg() and friends set TT.exitval to 1 if it's still 0, clean out other places that were setting it that no longer need to.
Rob Landley <rob@landley.net>
parents:
765
diff
changeset
|
76 for (s=toys.optargs; *s; s++) if (do_mkdir(*s)) perror_msg("'%s'", *s); |
526 | 77 } |