From 314dc6881f0439478f483d26726c52c1c3f536ff Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Thu, 5 Sep 2013 04:28:40 -0500 Subject: Make chdir handle permissions according to posix, reported by Jacek Bukarewicz.. --- toys/posix/mkdir.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'toys') diff --git a/toys/posix/mkdir.c b/toys/posix/mkdir.c index 24930f4a..202eaa79 100644 --- a/toys/posix/mkdir.c +++ b/toys/posix/mkdir.c @@ -4,7 +4,7 @@ * * See http://opengroup.org/onlinepubs/9699919799/utilities/mkdir.html -USE_MKDIR(NEWTOY(mkdir, "<1pm:", TOYFLAG_BIN)) +USE_MKDIR(NEWTOY(mkdir, "<1pm:", TOYFLAG_BIN|TOYFLAG_UMASK)) config MKDIR bool "mkdir" @@ -30,7 +30,6 @@ 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,6 +43,7 @@ static int do_mkdir(char *dir) for (s=dir; ; s++) { char save=0; + mode_t mode = 0777&~toys.old_umask; // Skip leading / of absolute paths. if (s!=dir && *s == '/' && (toys.optflags&FLAG_p)) { @@ -52,9 +52,10 @@ static int do_mkdir(char *dir) } else if (*s) continue; // Use the mode from the -m option only for the last directory. - if ((toys.optflags&FLAG_m) && save != '/') mode = TT.mode; + if (save == '/') mode |= 0300; + else if (toys.optflags&FLAG_m) mode = TT.mode; - if (mkdir(dir, mode)<0 && ((toys.optflags&~FLAG_p) || errno != EEXIST)) + if (mkdir(dir, mode)<0 && (!(toys.optflags&FLAG_p) || errno != EEXIST)) return 1; if (!(*s = save)) break; -- cgit v1.2.3