blob: 67819e7841155b2b9a863a86ee1d7afb88892336 [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 *
Matt Kraaiceeff732001-06-21 19:41:37 +00005 * Copyright (C) 2001 Matt Kraai <kraai@alumni.carnegiemellon.edu>
Eric Andersenf6be9441999-10-13 21:12:06 +00006 *
"Robert P. J. Day"801ab142006-07-12 07:56:04 +00007 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersenf6be9441999-10-13 21:12:06 +00008 */
9
Manuel Novoa III cad53642003-03-19 09:13:01 +000010/* BB_AUDIT SUSv3 compliant */
11/* http://www.opengroup.org/onlinepubs/007904975/utilities/mkdir.html */
Matt Kraaiceeff732001-06-21 19:41:37 +000012
Manuel Novoa III cad53642003-03-19 09:13:01 +000013/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
14 *
15 * Fixed broken permission setting when -p was used; especially in
16 * conjunction with -m.
17 */
18
19#include <stdlib.h>
20#include <unistd.h>
Rob Landleyb7128c62005-09-11 01:05:30 +000021#include <getopt.h> /* struct option */
Eric Andersen3570a342000-09-25 21:45:58 +000022#include "busybox.h"
Erik Andersenfac10d72000-02-07 05:29:42 +000023
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +000024#if ENABLE_FEATURE_MKDIR_LONG_OPTIONS
Eric Andersen8876fb22003-06-20 09:01:58 +000025static const struct option mkdir_long_options[] = {
26 { "mode", 1, NULL, 'm' },
27 { "parents", 0, NULL, 'p' },
28 { 0, 0, 0, 0 }
29};
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +000030#endif
Eric Andersen8876fb22003-06-20 09:01:58 +000031
Denis Vlasenko06af2162007-02-03 17:28:39 +000032int mkdir_main(int argc, char **argv);
Denis Vlasenko22dcd042006-11-19 13:53:50 +000033int mkdir_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000034{
Manuel Novoa III cad53642003-03-19 09:13:01 +000035 mode_t mode = (mode_t)(-1);
36 int status = EXIT_SUCCESS;
Matt Kraaiceeff732001-06-21 19:41:37 +000037 int flags = 0;
Denis Vlasenko67b23e62006-10-03 21:00:06 +000038 unsigned opt;
Eric Andersen8876fb22003-06-20 09:01:58 +000039 char *smode;
Eric Andersencc8ed391999-10-05 16:24:54 +000040
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +000041#if ENABLE_FEATURE_MKDIR_LONG_OPTIONS
Denis Vlasenko67b23e62006-10-03 21:00:06 +000042 applet_long_options = mkdir_long_options;
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +000043#endif
Denis Vlasenko67b23e62006-10-03 21:00:06 +000044 opt = getopt32(argc, argv, "m:p", &smode);
Denis Vlasenko22dcd042006-11-19 13:53:50 +000045 if (opt & 1) {
Denis Vlasenkodca0b702006-10-27 09:05:02 +000046 mode = 0777;
Denis Vlasenko22dcd042006-11-19 13:53:50 +000047 if (!bb_parse_mode(smode, &mode)) {
48 bb_error_msg_and_die("invalid mode '%s'", smode);
Eric Andersenfa0540f1999-10-22 18:18:31 +000049 }
Eric Andersenfa0540f1999-10-22 18:18:31 +000050 }
Denis Vlasenko22dcd042006-11-19 13:53:50 +000051 if (opt & 2)
Eric Andersen8876fb22003-06-20 09:01:58 +000052 flags |= FILEUTILS_RECUR;
Eric Andersenf6be9441999-10-13 21:12:06 +000053
Manuel Novoa III cad53642003-03-19 09:13:01 +000054 if (optind == argc) {
55 bb_show_usage();
Glenn L McGrath924f93e2002-09-28 08:32:14 +000056 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000057
Manuel Novoa III cad53642003-03-19 09:13:01 +000058 argv += optind;
59
60 do {
61 if (bb_make_directory(*argv, mode, flags)) {
62 status = EXIT_FAILURE;
63 }
64 } while (*++argv);
65
66 return status;
Eric Andersencc8ed391999-10-05 16:24:54 +000067}