Bug fixes
[oweals/busybox.git] / coreutils / mkdir.c
1 /*
2  * Mini mkdir implementation for busybox
3  *
4  *
5  * Copyright (C) 1999 by Lineo, inc.
6  * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21  *
22  */
23
24 #include "internal.h"
25 #include <stdio.h>
26 #include <errno.h>
27 #include <sys/param.h>
28
29 static const char mkdir_usage[] = "Usage: mkdir [OPTION] DIRECTORY...\n\n"
30 "Create the DIRECTORY(ies), if they do not already exist\n\n"
31 "Options:\n"
32 "\t-m\tset permission mode (as in chmod), not rwxrwxrwx - umask\n"
33 "\t-p\tno error if existing, make parent directories as needed\n";
34
35
36 static int parentFlag = FALSE;
37 static mode_t mode = 0777;
38
39
40 extern int mkdir_main(int argc, char **argv)
41 {
42     int i=FALSE;
43     argc--;
44     argv++;
45
46     /* Parse any options */
47     while (argc > 0 && **argv == '-') {
48         while (i==FALSE && *++(*argv)) {
49             switch (**argv) {
50             case 'm':
51                 if (--argc == 0)
52                     usage( mkdir_usage);
53                 /* Find the specified modes */
54                 mode = 0;
55                 if ( parse_mode(*(++argv), &mode) == FALSE ) {
56                     fprintf(stderr, "Unknown mode: %s\n", *argv);
57                     exit( FALSE);
58                 }
59                 /* Set the umask for this process so it doesn't 
60                  * screw up whatever the user just entered. */
61                 umask(0);
62                 i=TRUE;
63                 break;
64             case 'p':
65                 parentFlag = TRUE;
66                 break;
67             default:
68                 usage( mkdir_usage);
69             }
70         }
71         argc--;
72         argv++;
73     }
74
75
76     if (argc < 1) {
77         usage( mkdir_usage);
78     }
79
80     while (argc > 0) {
81         int status;
82         struct stat statBuf;
83         char buf[NAME_MAX];
84
85         strcpy (buf, *argv);
86         status=stat(buf, &statBuf);
87         if (status != -1 && status != ENOENT ) {
88             fprintf(stderr, "%s: File exists\n", buf);
89             exit( FALSE);
90         }
91         if (parentFlag == TRUE) {
92             strcat( buf, "/");
93             createPath(buf, mode);
94         }
95         else { 
96             if (mkdir (buf, mode) != 0) {
97                 perror(buf);
98                 exit( FALSE);
99             }
100         }
101         argc--;
102         argv++;
103     }
104     exit( TRUE);
105 }
106
107