selinux support by Yuichi Nakamura <ynakam@hitachisoft.jp> (HitachiSoft)
[oweals/busybox.git] / coreutils / mkdir.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini mkdir implementation for busybox
4  *
5  * Copyright (C) 2001 Matt Kraai <kraai@alumni.carnegiemellon.edu>
6  *
7  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8  */
9
10 /* BB_AUDIT SUSv3 compliant */
11 /* http://www.opengroup.org/onlinepubs/007904975/utilities/mkdir.html */
12
13 /* 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 /* Nov 28, 2006      Yoshinori Sato <ysato@users.sourceforge.jp>: Add SELinux Support.
20  */
21
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <getopt.h> /* struct option */
25 #include "busybox.h"
26
27 #if ENABLE_FEATURE_MKDIR_LONG_OPTIONS
28 static const struct option mkdir_long_options[] = {
29         { "mode", 1, NULL, 'm' },
30         { "parents", 0, NULL, 'p' },
31 #if ENABLE_SELINUX
32         { "context", 1, NULL, 'Z' },
33 #endif
34         { 0, 0, 0, 0 }
35 };
36 #endif
37
38 int mkdir_main(int argc, char **argv);
39 int mkdir_main(int argc, char **argv)
40 {
41         mode_t mode = (mode_t)(-1);
42         int status = EXIT_SUCCESS;
43         int flags = 0;
44         unsigned opt;
45         char *smode;
46 #if ENABLE_SELINUX
47         security_context_t scontext;
48 #endif
49
50 #if ENABLE_FEATURE_MKDIR_LONG_OPTIONS
51         applet_long_options = mkdir_long_options;
52 #endif
53         opt = getopt32(argc, argv, "m:p" USE_SELINUX("Z:"), &smode USE_SELINUX(,&scontext));
54         if (opt & 1) {
55                 mode = 0777;
56                 if (!bb_parse_mode(smode, &mode)) {
57                         bb_error_msg_and_die("invalid mode '%s'", smode);
58                 }
59         }
60         if (opt & 2)
61                 flags |= FILEUTILS_RECUR;
62 #if ENABLE_SELINUX
63         if (opt & 4) {
64                 selinux_or_die();
65                 if (setfscreatecon(scontext)) {
66                         bb_error_msg_and_die("cannot set default file creation context "
67                                               "to %s", scontext);
68                 }
69         }
70 #endif
71
72         if (optind == argc) {
73                 bb_show_usage();
74         }
75
76         argv += optind;
77
78         do {
79                 if (bb_make_directory(*argv, mode, flags)) {
80                         status = EXIT_FAILURE;
81                 }
82         } while (*++argv);
83
84         return status;
85 }