aboutsummaryrefslogtreecommitdiff
path: root/mkdir.c
diff options
context:
space:
mode:
Diffstat (limited to 'mkdir.c')
-rw-r--r--mkdir.c133
1 files changed, 67 insertions, 66 deletions
diff --git a/mkdir.c b/mkdir.c
index 8e3f51bfb..70fdbdfb5 100644
--- a/mkdir.c
+++ b/mkdir.c
@@ -1,3 +1,4 @@
1/* vi: set sw=4 ts=4: */
1/* 2/*
2 * Mini mkdir implementation for busybox 3 * Mini mkdir implementation for busybox
3 * 4 *
@@ -28,14 +29,15 @@
28 29
29#include <stdio.h> 30#include <stdio.h>
30#include <errno.h> 31#include <errno.h>
31#include <sys/param.h> /* for PATH_MAX */ 32#include <sys/param.h> /* for PATH_MAX */
32 33
33static const char mkdir_usage[] = 34static const char mkdir_usage[] =
34"mkdir [OPTION] DIRECTORY...\n\n" 35 "mkdir [OPTION] DIRECTORY...\n\n"
35"Create the DIRECTORY(ies), if they do not already exist\n\n" 36 "Create the DIRECTORY(ies), if they do not already exist\n\n"
36"Options:\n" 37 "Options:\n"
37"\t-m\tset permission mode (as in chmod), not rwxrwxrwx - umask\n" 38
38"\t-p\tno error if existing, make parent directories as needed\n"; 39 "\t-m\tset permission mode (as in chmod), not rwxrwxrwx - umask\n"
40 "\t-p\tno error if existing, make parent directories as needed\n";
39 41
40 42
41static int parentFlag = FALSE; 43static int parentFlag = FALSE;
@@ -44,71 +46,70 @@ static mode_t mode = 0777;
44 46
45extern int mkdir_main(int argc, char **argv) 47extern int mkdir_main(int argc, char **argv)
46{ 48{
47 int i = FALSE; 49 int i = FALSE;
48 argc--;
49 argv++;
50 50
51 /* Parse any options */
52 while (argc > 0 && **argv == '-') {
53 while (i == FALSE && *++(*argv)) {
54 switch (**argv) {
55 case 'm':
56 if (--argc == 0)
57 usage( mkdir_usage);
58 /* Find the specified modes */
59 mode = 0;
60 if (parse_mode(*(++argv), &mode) == FALSE ) {
61 fprintf(stderr, "Unknown mode: %s\n", *argv);
62 exit FALSE;
63 }
64 /* Set the umask for this process so it doesn't
65 * screw up whatever the user just entered. */
66 umask(0);
67 i = TRUE;
68 break;
69 case 'p':
70 parentFlag = TRUE;
71 break;
72 default:
73 usage( mkdir_usage);
74 }
75 }
76 argc--; 51 argc--;
77 argv++; 52 argv++;
78 }
79
80 if (argc < 1) {
81 usage( mkdir_usage);
82 }
83 53
84 while (argc > 0) { 54 /* Parse any options */
85 int status; 55 while (argc > 0 && **argv == '-') {
86 struct stat statBuf; 56 while (i == FALSE && *++(*argv)) {
87 char buf[PATH_MAX + 1]; 57 switch (**argv) {
88 if (strlen(*argv) > PATH_MAX - 1) { 58 case 'm':
89 fprintf(stderr, name_too_long, "mkdir"); 59 if (--argc == 0)
90 exit FALSE; 60 usage(mkdir_usage);
91 } 61 /* Find the specified modes */
92 strcpy (buf, *argv); 62 mode = 0;
93 status = stat(buf, &statBuf); 63 if (parse_mode(*(++argv), &mode) == FALSE) {
94 if (parentFlag == FALSE && status != -1 && errno != ENOENT) { 64 fprintf(stderr, "Unknown mode: %s\n", *argv);
95 fprintf(stderr, "%s: File exists\n", buf); 65 exit FALSE;
96 exit FALSE; 66 }
97 } 67 /* Set the umask for this process so it doesn't
98 if (parentFlag == TRUE) { 68 * screw up whatever the user just entered. */
99 strcat( buf, "/"); 69 umask(0);
100 createPath(buf, mode); 70 i = TRUE;
71 break;
72 case 'p':
73 parentFlag = TRUE;
74 break;
75 default:
76 usage(mkdir_usage);
77 }
78 }
79 argc--;
80 argv++;
101 } 81 }
102 else { 82
103 if (mkdir (buf, mode) != 0 && parentFlag == FALSE) { 83 if (argc < 1) {
104 perror(buf); 84 usage(mkdir_usage);
105 exit FALSE;
106 }
107 } 85 }
108 argc--;
109 argv++;
110 }
111 exit TRUE;
112}
113 86
87 while (argc > 0) {
88 int status;
89 struct stat statBuf;
90 char buf[PATH_MAX + 1];
114 91
92 if (strlen(*argv) > PATH_MAX - 1) {
93 fprintf(stderr, name_too_long, "mkdir");
94 exit FALSE;
95 }
96 strcpy(buf, *argv);
97 status = stat(buf, &statBuf);
98 if (parentFlag == FALSE && status != -1 && errno != ENOENT) {
99 fprintf(stderr, "%s: File exists\n", buf);
100 exit FALSE;
101 }
102 if (parentFlag == TRUE) {
103 strcat(buf, "/");
104 createPath(buf, mode);
105 } else {
106 if (mkdir(buf, mode) != 0 && parentFlag == FALSE) {
107 perror(buf);
108 exit FALSE;
109 }
110 }
111 argc--;
112 argv++;
113 }
114 exit TRUE;
115}