diff options
author | Rob Landley <rob@landley.net> | 2012-03-05 23:15:50 -0600 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2012-03-05 23:15:50 -0600 |
commit | 688c74534227e3241a83e41b1f14a3f6b700f475 (patch) | |
tree | f2fc23a24f83d1226076d5188b8b824f1cd08a91 /toys/mkdir.c | |
parent | 057aa04da8b39a72e38eedddb9ed27e61b25ed68 (diff) | |
download | toybox-688c74534227e3241a83e41b1f14a3f6b700f475.tar.gz |
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().
Diffstat (limited to 'toys/mkdir.c')
-rw-r--r-- | toys/mkdir.c | 50 |
1 files changed, 22 insertions, 28 deletions
diff --git a/toys/mkdir.c b/toys/mkdir.c index 4c9f5451..d875f95d 100644 --- a/toys/mkdir.c +++ b/toys/mkdir.c @@ -22,46 +22,40 @@ config MKDIR #include "toys.h" +DEFINE_GLOBALS( + long mode; +) -static int create_dir(const char *dir, mode_t mode) { - int ret = 0; - unsigned int i; +#define TT this.mkdir - // Shortcut - if (strchr(dir, '/') == NULL || !toys.optflags) - return mkdir(dir, mode); +static int do_mkdir(char *dir) +{ + unsigned int i; - char *d = strdup(dir); - if (!d) - return -1; - unsigned int dlen = strlen(dir); + if (toys.optflags && *dir) { + // Skip first char (it can be /) + for (i = 1; dir[i]; i++) { + int ret; - // Skip first char (it can be /) - for (i = 1; i < dlen; i++) { - if (d[i] != '/') - continue; - d[i] = '\0'; - ret = mkdir(d, mode); - d[i] = '/'; - if (ret < 0 && errno != EEXIST) - goto OUT; + if (dir[i] != '/') continue; + dir[i] = 0; + ret = mkdir(dir, TT.mode); + if (ret < 0 && errno != EEXIST) return ret; + dir[i] = '/'; + } } - ret = mkdir(d, mode); -OUT: - free(d); - return ret; + return mkdir(dir, TT.mode); } void mkdir_main(void) { char **s; - mode_t umask_val = umask(0); - mode_t dir_mode = (0777 & ~umask_val) | (S_IWUSR | S_IXUSR); - umask(umask_val); + + TT.mode = 0777; for (s=toys.optargs; *s; s++) { - if (create_dir(*s, dir_mode) != 0) { - fprintf(stderr, "mkdir: cannot create directory '%s': %s\n", *s, strerror(errno)); + if (do_mkdir(*s)) { + perror_msg("cannot create directory '%s'", *s); toys.exitval = 1; } } |