md5/sha1sum: better fix for small resource leak
[platform/upstream/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 source tree.
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 //usage:#define mkdir_trivial_usage
23 //usage:       "[OPTIONS] DIRECTORY..."
24 //usage:#define mkdir_full_usage "\n\n"
25 //usage:       "Create DIRECTORY\n"
26 //usage:     "\nOptions:"
27 //usage:     "\n        -m MODE Mode"
28 //usage:     "\n        -p      No error if exists; make parent directories as needed"
29 //usage:        IF_SELINUX(
30 //usage:     "\n        -Z      Set security context"
31 //usage:        )
32 //usage:
33 //usage:#define mkdir_example_usage
34 //usage:       "$ mkdir /tmp/foo\n"
35 //usage:       "$ mkdir /tmp/foo\n"
36 //usage:       "/tmp/foo: File exists\n"
37 //usage:       "$ mkdir /tmp/foo/bar/baz\n"
38 //usage:       "/tmp/foo/bar/baz: No such file or directory\n"
39 //usage:       "$ mkdir -p /tmp/foo/bar/baz\n"
40
41 #include "libbb.h"
42
43 /* This is a NOFORK applet. Be very careful! */
44
45 #if ENABLE_FEATURE_MKDIR_LONG_OPTIONS
46 static const char mkdir_longopts[] ALIGN1 =
47         "mode\0"    Required_argument "m"
48         "parents\0" No_argument       "p"
49 #if ENABLE_SELINUX
50         "context\0" Required_argument "Z"
51 #endif
52         ;
53 #endif
54
55 int mkdir_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
56 int mkdir_main(int argc UNUSED_PARAM, char **argv)
57 {
58         mode_t mode = (mode_t)(-1);
59         int status = EXIT_SUCCESS;
60         int flags = 0;
61         unsigned opt;
62         char *smode;
63 #if ENABLE_SELINUX
64         security_context_t scontext;
65 #endif
66
67 #if ENABLE_FEATURE_MKDIR_LONG_OPTIONS
68         applet_long_options = mkdir_longopts;
69 #endif
70         opt = getopt32(argv, "m:p" IF_SELINUX("Z:"), &smode IF_SELINUX(,&scontext));
71         if (opt & 1) {
72                 mode = 0777;
73                 if (!bb_parse_mode(smode, &mode)) {
74                         bb_error_msg_and_die("invalid mode '%s'", smode);
75                 }
76         }
77         if (opt & 2)
78                 flags |= FILEUTILS_RECUR;
79 #if ENABLE_SELINUX
80         if (opt & 4) {
81                 selinux_or_die();
82                 setfscreatecon_or_die(scontext);
83         }
84 #endif
85
86         argv += optind;
87         if (!argv[0])
88                 bb_show_usage();
89
90         do {
91                 if (bb_make_directory(*argv, mode, flags)) {
92                         status = EXIT_FAILURE;
93                 }
94         } while (*++argv);
95
96         return status;
97 }