annotate toys/posix/mkdir.c @ 1776:7bf68329eb3b draft default tip

Repository switched to git at https://github.com/landley/toybox
author Rob Landley <rob@landley.net>
date Thu, 09 Apr 2015 02:28:32 -0500
parents fc1bb49e58a9
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
f435e8e3a0ba Add mkdir.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff changeset
2 *
f435e8e3a0ba Add mkdir.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff changeset
3 * Copyright 2012 Georgi Chorbadzhiyski <georgi@unixsol.org>
f435e8e3a0ba Add mkdir.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff changeset
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
f435e8e3a0ba Add mkdir.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff changeset
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
f435e8e3a0ba Add mkdir.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff changeset
8
f435e8e3a0ba Add mkdir.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff changeset
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...]
1333
fc1bb49e58a9 Help text should have a blank line after usage: lines, and a couple other whitespace tweaks.
Rob Landley <rob@landley.net>
parents: 1219
diff changeset
14
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
15 Create one or more directories.
526
f435e8e3a0ba Add mkdir.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff changeset
16
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
17 -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
18 -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
19 -v verbose
526
f435e8e3a0ba Add mkdir.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff changeset
20 */
f435e8e3a0ba Add mkdir.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff changeset
21
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
22 #define FOR_mkdir
526
f435e8e3a0ba Add mkdir.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff changeset
23 #include "toys.h"
f435e8e3a0ba Add mkdir.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff changeset
24
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
25 GLOBALS(
763
d8e2d723ab07 Add -m option to mkdir
Felix Janda <felix.janda@posteo.de>
parents: 694
diff changeset
26 char *arg_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
27 )
526
f435e8e3a0ba Add mkdir.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff changeset
28
f435e8e3a0ba Add mkdir.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff changeset
29 void mkdir_main(void)
f435e8e3a0ba Add mkdir.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff changeset
30 {
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
31 char **s;
1217
c6c5565cc859 Factor out mkpathat.
Rob Landley <rob@landley.net>
parents: 1143
diff changeset
32 mode_t mode = (0777&~toys.old_umask);
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
33
1217
c6c5565cc859 Factor out mkpathat.
Rob Landley <rob@landley.net>
parents: 1143
diff changeset
34
c6c5565cc859 Factor out mkpathat.
Rob Landley <rob@landley.net>
parents: 1143
diff changeset
35 if (TT.arg_mode) mode = string_to_mode(TT.arg_mode, 0777);
526
f435e8e3a0ba Add mkdir.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff changeset
36
1217
c6c5565cc859 Factor out mkpathat.
Rob Landley <rob@landley.net>
parents: 1143
diff changeset
37 // Note, -p and -v flags line up with mkpathat() flags
c6c5565cc859 Factor out mkpathat.
Rob Landley <rob@landley.net>
parents: 1143
diff changeset
38
c6c5565cc859 Factor out mkpathat.
Rob Landley <rob@landley.net>
parents: 1143
diff changeset
39 for (s=toys.optargs; *s; s++)
c6c5565cc859 Factor out mkpathat.
Rob Landley <rob@landley.net>
parents: 1143
diff changeset
40 if (mkpathat(AT_FDCWD, *s, mode, toys.optflags|1))
c6c5565cc859 Factor out mkpathat.
Rob Landley <rob@landley.net>
parents: 1143
diff changeset
41 perror_msg("'%s'", *s);
526
f435e8e3a0ba Add mkdir.
Georgi Chorbadzhiyski <georgi@unixsol.org>
parents:
diff changeset
42 }