aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--coreutils/mkdir.c4
-rw-r--r--libbb/make_directory.c34
2 files changed, 29 insertions, 9 deletions
diff --git a/coreutils/mkdir.c b/coreutils/mkdir.c
index 6a4ce6e1a..f003db99f 100644
--- a/coreutils/mkdir.c
+++ b/coreutils/mkdir.c
@@ -41,8 +41,10 @@ extern int mkdir_main (int argc, char **argv)
41 switch (opt) { 41 switch (opt) {
42 case 'm': 42 case 'm':
43 mode = 0777; 43 mode = 0777;
44 if (!parse_mode (optarg, &mode)) 44 if (!parse_mode (optarg, &mode)) {
45 error_msg_and_die ("invalid mode `%s'", optarg); 45 error_msg_and_die ("invalid mode `%s'", optarg);
46 }
47 umask(0);
46 break; 48 break;
47 case 'p': 49 case 'p':
48 flags |= FILEUTILS_RECUR; 50 flags |= FILEUTILS_RECUR;
diff --git a/libbb/make_directory.c b/libbb/make_directory.c
index ca3eb495c..e25ac21ee 100644
--- a/libbb/make_directory.c
+++ b/libbb/make_directory.c
@@ -38,24 +38,41 @@
38 * Also create parent directories as necessary if flags contains 38 * Also create parent directories as necessary if flags contains
39 * FILEUTILS_RECUR. */ 39 * FILEUTILS_RECUR. */
40 40
41static mode_t default_permission(char *path, mode_t old_permision)
42{
43 struct stat statbuf;
44 char *pp;
45
46 statbuf.st_mode = 0777;
47
48 /* stat the directory */
49 pp = strrchr(path, '/');
50 if ((pp) && (pp != path)) {
51 *pp = '\0';
52 stat(path, &statbuf);
53 *pp = '/';
54 }
55
56 return(statbuf.st_mode & old_permision);
57}
58
41int make_directory (char *path, long mode, int flags) 59int make_directory (char *path, long mode, int flags)
42{ 60{
43 int ret; 61 int ret;
44 62
45 /* Calling apps probably should use 0777 instead of -1
46 * then we dont need this condition
47 */
48 if (mode == -1) {
49 mode = 0777;
50 }
51 if (flags == FILEUTILS_RECUR) { 63 if (flags == FILEUTILS_RECUR) {
52 char *pp = strrchr(path, '/'); 64 char *pp = strrchr(path, '/');
53 if ((pp) && (pp != path)) { 65 if ((pp) && (pp != path)) {
54 *pp = '\0'; 66 *pp = '\0';
55 make_directory(path, mode, flags); 67 make_directory(path, -1, flags);
56 *pp = '/'; 68 *pp = '/';
57 } 69 }
58 } 70 }
71
72 if (mode == -1) {
73 mode = default_permission(path, 07777);
74 }
75
59 ret = mkdir(path, mode); 76 ret = mkdir(path, mode);
60 if (ret == -1) { 77 if (ret == -1) {
61 if ((flags == FILEUTILS_RECUR) && (errno == EEXIST)) { 78 if ((flags == FILEUTILS_RECUR) && (errno == EEXIST)) {
@@ -64,5 +81,6 @@ int make_directory (char *path, long mode, int flags)
64 perror_msg_and_die("Cannot create directory '%s'", path); 81 perror_msg_and_die("Cannot create directory '%s'", path);
65 } 82 }
66 } 83 }
84
67 return(ret); 85 return(ret);
68} 86}