Mercurial > hg > toybox
diff toys/posix/mkdir.c @ 763:d8e2d723ab07
Add -m option to mkdir
author | Felix Janda <felix.janda@posteo.de> |
---|---|
date | Sun, 23 Dec 2012 16:25:31 +0100 |
parents | 786841fdb1e0 |
children | e2e99fa861e9 |
line wrap: on
line diff
--- a/toys/posix/mkdir.c Sun Dec 16 13:43:36 2012 +0100 +++ b/toys/posix/mkdir.c Sun Dec 23 16:25:31 2012 +0100 @@ -4,31 +4,33 @@ * * See http://opengroup.org/onlinepubs/9699919799/utilities/mkdir.html * - * TODO: Add -m -USE_MKDIR(NEWTOY(mkdir, "<1p", TOYFLAG_BIN)) +USE_MKDIR(NEWTOY(mkdir, "<1pm:", TOYFLAG_BIN)) config MKDIR bool "mkdir" default y help - usage: mkdir [-p] [dirname...] + usage: mkdir [-p] [-m mode] [dirname...] Create one or more directories. -p make parent directories as needed. + -m set permissions of directory to mode. */ #define FOR_mkdir #include "toys.h" GLOBALS( - long mode; + char *arg_mode; + mode_t mode; ) static int do_mkdir(char *dir) { struct stat buf; char *s; + mode_t mode = 0777; // mkdir -p one/two/three is not an error if the path already exists, // but is if "three" is a file. The others we dereference and catch @@ -44,12 +46,16 @@ char save=0; // Skip leading / of absolute paths. - if (s!=dir && *s == '/' && toys.optflags) { + if (s!=dir && *s == '/' && (toys.optflags&FLAG_p)) { save = *s; *s = 0; } else if (*s) continue; - if (mkdir(dir, TT.mode)<0 && (!toys.optflags || errno != EEXIST)) + // Use the mode from the -m option only for the last directory. + if ((toys.optflags&FLAG_m) && save != '/') + mode = TT.mode; + + if (mkdir(dir, mode)<0 && ((toys.optflags&~FLAG_p) || errno != EEXIST)) return 1; if (!(*s = save)) break; @@ -63,6 +69,8 @@ char **s; TT.mode = 0777; + if(toys.optflags&FLAG_m) + TT.mode = string_to_mode(TT.arg_mode, TT.mode); for (s=toys.optargs; *s; s++) { if (do_mkdir(*s)) {