blob: 9ea3b4ea0905d96e8e8c3721e6ee8f26631c68b8 [file] [log] [blame]
Eric Andersenf6be9441999-10-13 21:12:06 +00001/*
2 * Mini mkdir implementation for busybox
3 *
Eric Andersenc4996011999-10-20 22:08:37 +00004 *
5 * Copyright (C) 1999 by Lineo, inc.
6 * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
Eric Andersenf6be9441999-10-13 21:12:06 +00007 *
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
Eric Andersencc8ed391999-10-05 16:24:54 +000024#include "internal.h"
Eric Andersenf6be9441999-10-13 21:12:06 +000025#include <stdio.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000026#include <errno.h>
27#include <sys/param.h>
28
Eric Andersend73dc5b1999-11-10 23:13:02 +000029static const char mkdir_usage[] = "Usage: mkdir [OPTION] DIRECTORY...\n\n"
Eric Andersenf6be9441999-10-13 21:12:06 +000030"Create the DIRECTORY(ies), if they do not already exist\n\n"
Eric Andersend73dc5b1999-11-10 23:13:02 +000031"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";
Eric Andersencc8ed391999-10-05 16:24:54 +000034
Eric Andersenf6be9441999-10-13 21:12:06 +000035
36static int parentFlag = FALSE;
Eric Andersended62591999-11-18 00:19:26 +000037static mode_t mode = 0777;
Eric Andersenf6be9441999-10-13 21:12:06 +000038
39
40extern int mkdir_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000041{
Eric Andersenfa0540f1999-10-22 18:18:31 +000042 int i=FALSE;
Eric Andersenf6be9441999-10-13 21:12:06 +000043 argc--;
44 argv++;
Eric Andersencc8ed391999-10-05 16:24:54 +000045
Eric Andersenf6be9441999-10-13 21:12:06 +000046 /* Parse any options */
Eric Andersenfa0540f1999-10-22 18:18:31 +000047 while (argc > 0 && **argv == '-') {
48 while (i==FALSE && *++(*argv)) {
Eric Andersenf6be9441999-10-13 21:12:06 +000049 switch (**argv) {
50 case 'm':
Eric Andersenfa0540f1999-10-22 18:18:31 +000051 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;
Eric Andersenf6be9441999-10-13 21:12:06 +000063 break;
64 case 'p':
65 parentFlag = TRUE;
66 break;
67 default:
Eric Andersenb0e9a701999-10-18 22:28:26 +000068 usage( mkdir_usage);
Eric Andersenf6be9441999-10-13 21:12:06 +000069 }
Eric Andersenfa0540f1999-10-22 18:18:31 +000070 }
Eric Andersenf6be9441999-10-13 21:12:06 +000071 argc--;
72 argv++;
73 }
74
75
76 if (argc < 1) {
Eric Andersenb0e9a701999-10-18 22:28:26 +000077 usage( mkdir_usage);
Eric Andersenf6be9441999-10-13 21:12:06 +000078 }
79
Eric Andersenfa0540f1999-10-22 18:18:31 +000080 while (argc > 0) {
81 int status;
Eric Andersenf6be9441999-10-13 21:12:06 +000082 struct stat statBuf;
Eric Andersencb41c2e1999-11-22 07:41:00 +000083 char buf[NAME_MAX];
84
85 strcpy (buf, *argv);
86 status=stat(buf, &statBuf);
Eric Andersenfa0540f1999-10-22 18:18:31 +000087 if (status != -1 && status != ENOENT ) {
Eric Andersencb41c2e1999-11-22 07:41:00 +000088 fprintf(stderr, "%s: File exists\n", buf);
Eric Andersenfa0540f1999-10-22 18:18:31 +000089 exit( FALSE);
Eric Andersencc8ed391999-10-05 16:24:54 +000090 }
Eric Andersen08b10341999-11-19 02:38:58 +000091 if (parentFlag == TRUE) {
Eric Andersencb41c2e1999-11-22 07:41:00 +000092 strcat( buf, "/");
93 createPath(buf, mode);
Eric Andersen08b10341999-11-19 02:38:58 +000094 }
Eric Andersenf6be9441999-10-13 21:12:06 +000095 else {
Eric Andersencb41c2e1999-11-22 07:41:00 +000096 if (mkdir (buf, mode) != 0) {
97 perror(buf);
Eric Andersenf6be9441999-10-13 21:12:06 +000098 exit( FALSE);
99 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000100 }
Eric Andersenfa0540f1999-10-22 18:18:31 +0000101 argc--;
102 argv++;
Eric Andersenf6be9441999-10-13 21:12:06 +0000103 }
104 exit( TRUE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000105}
106
Eric Andersencc8ed391999-10-05 16:24:54 +0000107