blob: 72bd10581c2eaf78153a2920eb5b368874c40450 [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
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000022#include "libbb.h"
Erik Andersenfac10d72000-02-07 05:29:42 +000023
Denis Vlasenko99912ca2007-04-10 15:43:37 +000024/* This is a NOFORK applet. Be very careful! */
25
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +000026#if ENABLE_FEATURE_MKDIR_LONG_OPTIONS
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000027static const char mkdir_longopts[] ALIGN1 =
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +000028 "mode\0" Required_argument "m"
29 "parents\0" No_argument "p"
Denis Vlasenko49622d72007-03-10 16:58:49 +000030#if ENABLE_SELINUX
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +000031 "context\0" Required_argument "Z"
Denis Vlasenko49622d72007-03-10 16:58:49 +000032#endif
Denis Vlasenko990d0f62007-07-24 15:54:42 +000033 ;
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +000034#endif
Eric Andersen8876fb22003-06-20 09:01:58 +000035
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000036int mkdir_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko22dcd042006-11-19 13:53:50 +000037int mkdir_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000038{
Manuel Novoa III cad53642003-03-19 09:13:01 +000039 mode_t mode = (mode_t)(-1);
40 int status = EXIT_SUCCESS;
Matt Kraaiceeff732001-06-21 19:41:37 +000041 int flags = 0;
Denis Vlasenko67b23e62006-10-03 21:00:06 +000042 unsigned opt;
Eric Andersen8876fb22003-06-20 09:01:58 +000043 char *smode;
Denis Vlasenko49622d72007-03-10 16:58:49 +000044#if ENABLE_SELINUX
45 security_context_t scontext;
46#endif
Eric Andersencc8ed391999-10-05 16:24:54 +000047
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +000048#if ENABLE_FEATURE_MKDIR_LONG_OPTIONS
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +000049 applet_long_options = mkdir_longopts;
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +000050#endif
Denis Vlasenkofe7cd642007-08-18 15:32:12 +000051 opt = getopt32(argv, "m:p" USE_SELINUX("Z:"), &smode USE_SELINUX(,&scontext));
Denis Vlasenko22dcd042006-11-19 13:53:50 +000052 if (opt & 1) {
Denis Vlasenkodca0b702006-10-27 09:05:02 +000053 mode = 0777;
Denis Vlasenko22dcd042006-11-19 13:53:50 +000054 if (!bb_parse_mode(smode, &mode)) {
55 bb_error_msg_and_die("invalid mode '%s'", smode);
Eric Andersenfa0540f1999-10-22 18:18:31 +000056 }
Eric Andersenfa0540f1999-10-22 18:18:31 +000057 }
Denis Vlasenko22dcd042006-11-19 13:53:50 +000058 if (opt & 2)
Eric Andersen8876fb22003-06-20 09:01:58 +000059 flags |= FILEUTILS_RECUR;
Denis Vlasenko49622d72007-03-10 16:58:49 +000060#if ENABLE_SELINUX
61 if (opt & 4) {
62 selinux_or_die();
Denis Vlasenko39c651e2007-03-12 18:22:55 +000063 setfscreatecon_or_die(scontext);
Denis Vlasenko49622d72007-03-10 16:58:49 +000064 }
65#endif
Eric Andersenf6be9441999-10-13 21:12:06 +000066
Manuel Novoa III cad53642003-03-19 09:13:01 +000067 if (optind == argc) {
68 bb_show_usage();
Glenn L McGrath924f93e2002-09-28 08:32:14 +000069 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000070
Manuel Novoa III cad53642003-03-19 09:13:01 +000071 argv += optind;
72
73 do {
74 if (bb_make_directory(*argv, mode, flags)) {
75 status = EXIT_FAILURE;
76 }
77 } while (*++argv);
78
79 return status;
Eric Andersencc8ed391999-10-05 16:24:54 +000080}