blob: 8e3f51bfb9ea724c49fa1f545dbfdc8f03710084 [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"
Erik Andersenfac10d72000-02-07 05:29:42 +000025#define bb_need_name_too_long
26#define BB_DECLARE_EXTERN
27#include "messages.c"
28
Eric Andersenf6be9441999-10-13 21:12:06 +000029#include <stdio.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000030#include <errno.h>
Erik Andersenfac10d72000-02-07 05:29:42 +000031#include <sys/param.h> /* for PATH_MAX */
Eric Andersencc8ed391999-10-05 16:24:54 +000032
Erik Andersen5509af72000-01-23 18:19:02 +000033static const char mkdir_usage[] =
34"mkdir [OPTION] DIRECTORY...\n\n"
Eric Andersenf6be9441999-10-13 21:12:06 +000035"Create the DIRECTORY(ies), if they do not already exist\n\n"
Eric Andersend73dc5b1999-11-10 23:13:02 +000036"Options:\n"
37"\t-m\tset permission mode (as in chmod), not rwxrwxrwx - umask\n"
38"\t-p\tno error if existing, make parent directories as needed\n";
Eric Andersencc8ed391999-10-05 16:24:54 +000039
Eric Andersenf6be9441999-10-13 21:12:06 +000040
41static int parentFlag = FALSE;
Eric Andersended62591999-11-18 00:19:26 +000042static mode_t mode = 0777;
Eric Andersenf6be9441999-10-13 21:12:06 +000043
44
45extern int mkdir_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000046{
Erik Andersenfac10d72000-02-07 05:29:42 +000047 int i = FALSE;
Eric Andersenf6be9441999-10-13 21:12:06 +000048 argc--;
49 argv++;
Eric Andersencc8ed391999-10-05 16:24:54 +000050
Eric Andersenf6be9441999-10-13 21:12:06 +000051 /* Parse any options */
Eric Andersenfa0540f1999-10-22 18:18:31 +000052 while (argc > 0 && **argv == '-') {
Erik Andersenfac10d72000-02-07 05:29:42 +000053 while (i == FALSE && *++(*argv)) {
Eric Andersenf6be9441999-10-13 21:12:06 +000054 switch (**argv) {
55 case 'm':
Eric Andersenfa0540f1999-10-22 18:18:31 +000056 if (--argc == 0)
57 usage( mkdir_usage);
58 /* Find the specified modes */
59 mode = 0;
Erik Andersenfac10d72000-02-07 05:29:42 +000060 if (parse_mode(*(++argv), &mode) == FALSE ) {
Eric Andersenfa0540f1999-10-22 18:18:31 +000061 fprintf(stderr, "Unknown mode: %s\n", *argv);
Erik Andersenfac10d72000-02-07 05:29:42 +000062 exit FALSE;
Eric Andersenfa0540f1999-10-22 18:18:31 +000063 }
64 /* Set the umask for this process so it doesn't
65 * screw up whatever the user just entered. */
66 umask(0);
Erik Andersenfac10d72000-02-07 05:29:42 +000067 i = TRUE;
Eric Andersenf6be9441999-10-13 21:12:06 +000068 break;
69 case 'p':
70 parentFlag = TRUE;
71 break;
72 default:
Eric Andersenb0e9a701999-10-18 22:28:26 +000073 usage( mkdir_usage);
Eric Andersenf6be9441999-10-13 21:12:06 +000074 }
Eric Andersenfa0540f1999-10-22 18:18:31 +000075 }
Eric Andersenf6be9441999-10-13 21:12:06 +000076 argc--;
77 argv++;
78 }
79
Eric Andersenf6be9441999-10-13 21:12:06 +000080 if (argc < 1) {
Eric Andersenb0e9a701999-10-18 22:28:26 +000081 usage( mkdir_usage);
Eric Andersenf6be9441999-10-13 21:12:06 +000082 }
83
Eric Andersenfa0540f1999-10-22 18:18:31 +000084 while (argc > 0) {
85 int status;
Eric Andersenf6be9441999-10-13 21:12:06 +000086 struct stat statBuf;
Erik Andersenfac10d72000-02-07 05:29:42 +000087 char buf[PATH_MAX + 1];
88 if (strlen(*argv) > PATH_MAX - 1) {
89 fprintf(stderr, name_too_long, "mkdir");
90 exit FALSE;
91 }
Eric Andersencb41c2e1999-11-22 07:41:00 +000092 strcpy (buf, *argv);
Erik Andersenfac10d72000-02-07 05:29:42 +000093 status = stat(buf, &statBuf);
94 if (parentFlag == FALSE && status != -1 && errno != ENOENT) {
Eric Andersencb41c2e1999-11-22 07:41:00 +000095 fprintf(stderr, "%s: File exists\n", buf);
Erik Andersenfac10d72000-02-07 05:29:42 +000096 exit FALSE;
Eric Andersencc8ed391999-10-05 16:24:54 +000097 }
Eric Andersen08b10341999-11-19 02:38:58 +000098 if (parentFlag == TRUE) {
Eric Andersencb41c2e1999-11-22 07:41:00 +000099 strcat( buf, "/");
100 createPath(buf, mode);
Eric Andersen08b10341999-11-19 02:38:58 +0000101 }
Eric Andersenf6be9441999-10-13 21:12:06 +0000102 else {
Erik Andersen05df2392000-01-13 04:43:48 +0000103 if (mkdir (buf, mode) != 0 && parentFlag == FALSE) {
Eric Andersencb41c2e1999-11-22 07:41:00 +0000104 perror(buf);
Erik Andersenfac10d72000-02-07 05:29:42 +0000105 exit FALSE;
Eric Andersenf6be9441999-10-13 21:12:06 +0000106 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000107 }
Eric Andersenfa0540f1999-10-22 18:18:31 +0000108 argc--;
109 argv++;
Eric Andersenf6be9441999-10-13 21:12:06 +0000110 }
Erik Andersenfac10d72000-02-07 05:29:42 +0000111 exit TRUE;
Eric Andersencc8ed391999-10-05 16:24:54 +0000112}
113
Eric Andersencc8ed391999-10-05 16:24:54 +0000114