blob: eb6e7db7dbabc53342cfaf479757e2675b6dddc2 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersenf6be9441999-10-13 21:12:06 +00002/*
3 * Mini mkdir implementation for busybox
4 *
Erik Andersen61677fe2000-04-13 01:18:56 +00005 * Copyright (C) 1999,2000 by Lineo, inc.
Eric Andersenc4996011999-10-20 22:08:37 +00006 * 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 Andersen3570a342000-09-25 21:45:58 +000024#include "busybox.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>
Eric Andersened3ef502001-01-27 08:24:39 +000031#include <string.h>
32#include <stdlib.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000033
Eric Andersenf6be9441999-10-13 21:12:06 +000034static int parentFlag = FALSE;
Eric Andersended62591999-11-18 00:19:26 +000035static mode_t mode = 0777;
Eric Andersenf6be9441999-10-13 21:12:06 +000036
37
38extern int mkdir_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000039{
Erik Andersene49d5ec2000-02-08 19:58:47 +000040 int i = FALSE;
Eric Andersencc8ed391999-10-05 16:24:54 +000041
Erik Andersene49d5ec2000-02-08 19:58:47 +000042 argc--;
43 argv++;
44
45 /* Parse any options */
46 while (argc > 0 && **argv == '-') {
47 while (i == FALSE && *++(*argv)) {
48 switch (**argv) {
49 case 'm':
50 if (--argc == 0)
51 usage(mkdir_usage);
52 /* Find the specified modes */
53 mode = 0;
54 if (parse_mode(*(++argv), &mode) == FALSE) {
Mark Whitleyf57c9442000-12-07 19:56:48 +000055 error_msg("Unknown mode: %s\n", *argv);
Matt Kraai3e856ce2000-12-01 02:55:13 +000056 return EXIT_FAILURE;
Erik Andersene49d5ec2000-02-08 19:58:47 +000057 }
58 /* Set the umask for this process so it doesn't
59 * screw up whatever the user just entered. */
60 umask(0);
61 i = TRUE;
62 break;
63 case 'p':
64 parentFlag = TRUE;
65 break;
66 default:
67 usage(mkdir_usage);
68 }
Eric Andersenfa0540f1999-10-22 18:18:31 +000069 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000070 argc--;
71 argv++;
Eric Andersenfa0540f1999-10-22 18:18:31 +000072 }
Eric Andersenf6be9441999-10-13 21:12:06 +000073
Erik Andersene49d5ec2000-02-08 19:58:47 +000074 if (argc < 1) {
75 usage(mkdir_usage);
76 }
Eric Andersenf6be9441999-10-13 21:12:06 +000077
Erik Andersene49d5ec2000-02-08 19:58:47 +000078 while (argc > 0) {
79 int status;
80 struct stat statBuf;
Erik Andersen4f3f7572000-04-28 00:18:56 +000081 char buf[BUFSIZ + 1];
Erik Andersene49d5ec2000-02-08 19:58:47 +000082
Erik Andersen4f3f7572000-04-28 00:18:56 +000083 if (strlen(*argv) > BUFSIZ - 1) {
Mark Whitleyf57c9442000-12-07 19:56:48 +000084 error_msg(name_too_long);
Matt Kraai3e856ce2000-12-01 02:55:13 +000085 return EXIT_FAILURE;
Erik Andersene49d5ec2000-02-08 19:58:47 +000086 }
87 strcpy(buf, *argv);
88 status = stat(buf, &statBuf);
89 if (parentFlag == FALSE && status != -1 && errno != ENOENT) {
Mark Whitleyf57c9442000-12-07 19:56:48 +000090 error_msg("%s: File exists\n", buf);
Matt Kraai3e856ce2000-12-01 02:55:13 +000091 return EXIT_FAILURE;
Erik Andersene49d5ec2000-02-08 19:58:47 +000092 }
93 if (parentFlag == TRUE) {
94 strcat(buf, "/");
Mark Whitleyf57c9442000-12-07 19:56:48 +000095 create_path(buf, mode);
Erik Andersene49d5ec2000-02-08 19:58:47 +000096 } else {
97 if (mkdir(buf, mode) != 0 && parentFlag == FALSE) {
98 perror(buf);
Matt Kraai3e856ce2000-12-01 02:55:13 +000099 return EXIT_FAILURE;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000100 }
101 }
102 argc--;
103 argv++;
Erik Andersenfac10d72000-02-07 05:29:42 +0000104 }
Matt Kraai3e856ce2000-12-01 02:55:13 +0000105 return EXIT_SUCCESS;
Eric Andersencc8ed391999-10-05 16:24:54 +0000106}