blob: 6bdf76d29362d38730027813d5c2342b59752eb4 [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
Denis Vlasenko49622d72007-03-10 16:58:49 +000019/* Nov 28, 2006 Yoshinori Sato <ysato@users.sourceforge.jp>: Add SELinux Support.
20 */
21
Rob Landleyb7128c62005-09-11 01:05:30 +000022#include <getopt.h> /* struct option */
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000023#include "libbb.h"
Erik Andersenfac10d72000-02-07 05:29:42 +000024
Denis Vlasenko99912ca2007-04-10 15:43:37 +000025/* This is a NOFORK applet. Be very careful! */
26
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +000027#if ENABLE_FEATURE_MKDIR_LONG_OPTIONS
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000028static const char mkdir_longopts[] ALIGN1 =
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +000029 "mode\0" Required_argument "m"
30 "parents\0" No_argument "p"
Denis Vlasenko49622d72007-03-10 16:58:49 +000031#if ENABLE_SELINUX
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +000032 "context\0" Required_argument "Z"
Denis Vlasenko49622d72007-03-10 16:58:49 +000033#endif
Denis Vlasenko990d0f62007-07-24 15:54:42 +000034 ;
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +000035#endif
Eric Andersen8876fb22003-06-20 09:01:58 +000036
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000037int mkdir_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko22dcd042006-11-19 13:53:50 +000038int mkdir_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000039{
Manuel Novoa III cad53642003-03-19 09:13:01 +000040 mode_t mode = (mode_t)(-1);
41 int status = EXIT_SUCCESS;
Matt Kraaiceeff732001-06-21 19:41:37 +000042 int flags = 0;
Denis Vlasenko67b23e62006-10-03 21:00:06 +000043 unsigned opt;
Eric Andersen8876fb22003-06-20 09:01:58 +000044 char *smode;
Denis Vlasenko49622d72007-03-10 16:58:49 +000045#if ENABLE_SELINUX
46 security_context_t scontext;
47#endif
Eric Andersencc8ed391999-10-05 16:24:54 +000048
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +000049#if ENABLE_FEATURE_MKDIR_LONG_OPTIONS
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +000050 applet_long_options = mkdir_longopts;
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +000051#endif
Denis Vlasenkofe7cd642007-08-18 15:32:12 +000052 opt = getopt32(argv, "m:p" USE_SELINUX("Z:"), &smode USE_SELINUX(,&scontext));
Denis Vlasenko22dcd042006-11-19 13:53:50 +000053 if (opt & 1) {
Denis Vlasenkodca0b702006-10-27 09:05:02 +000054 mode = 0777;
Denis Vlasenko22dcd042006-11-19 13:53:50 +000055 if (!bb_parse_mode(smode, &mode)) {
56 bb_error_msg_and_die("invalid mode '%s'", smode);
Eric Andersenfa0540f1999-10-22 18:18:31 +000057 }
Eric Andersenfa0540f1999-10-22 18:18:31 +000058 }
Denis Vlasenko22dcd042006-11-19 13:53:50 +000059 if (opt & 2)
Eric Andersen8876fb22003-06-20 09:01:58 +000060 flags |= FILEUTILS_RECUR;
Denis Vlasenko49622d72007-03-10 16:58:49 +000061#if ENABLE_SELINUX
62 if (opt & 4) {
63 selinux_or_die();
Denis Vlasenko39c651e2007-03-12 18:22:55 +000064 setfscreatecon_or_die(scontext);
Denis Vlasenko49622d72007-03-10 16:58:49 +000065 }
66#endif
Eric Andersenf6be9441999-10-13 21:12:06 +000067
Manuel Novoa III cad53642003-03-19 09:13:01 +000068 if (optind == argc) {
69 bb_show_usage();
Glenn L McGrath924f93e2002-09-28 08:32:14 +000070 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000071
Manuel Novoa III cad53642003-03-19 09:13:01 +000072 argv += optind;
73
74 do {
75 if (bb_make_directory(*argv, mode, flags)) {
76 status = EXIT_FAILURE;
77 }
78 } while (*++argv);
79
80 return status;
Eric Andersencc8ed391999-10-05 16:24:54 +000081}