1 /* mkdir -- make directories
2 Copyright (C) 90, 1995-2002, 2004-2007 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 /* David MacKenzie <djm@ai.mit.edu> */
22 #include <sys/types.h>
23 #include <selinux/selinux.h>
29 #include "modechange.h"
33 /* The official name of this program (e.g., no `g' prefix). */
34 #define PROGRAM_NAME "mkdir"
36 #define AUTHORS "David MacKenzie"
38 /* The name this program was run with. */
41 static struct option const longopts[] =
43 {GETOPT_SELINUX_CONTEXT_OPTION_DECL},
44 {"mode", required_argument, NULL, 'm'},
45 {"parents", no_argument, NULL, 'p'},
46 {"verbose", no_argument, NULL, 'v'},
47 {GETOPT_HELP_OPTION_DECL},
48 {GETOPT_VERSION_OPTION_DECL},
55 if (status != EXIT_SUCCESS)
56 fprintf (stderr, _("Try `%s --help' for more information.\n"),
60 printf (_("Usage: %s [OPTION] DIRECTORY...\n"), program_name);
62 Create the DIRECTORY(ies), if they do not already exist.\n\
66 Mandatory arguments to long options are mandatory for short options too.\n\
69 -m, --mode=MODE set file mode (as in chmod), not a=rwx - umask\n\
70 -p, --parents no error if existing, make parent directories as needed\n\
71 -v, --verbose print a message for each created directory\n\
72 -Z, --context=CTX set the SELinux security context of each created\n\
75 fputs (HELP_OPTION_DESCRIPTION, stdout);
76 fputs (VERSION_OPTION_DESCRIPTION, stdout);
77 emit_bug_reporting_address ();
82 /* Options passed to subsidiary functions. */
85 /* Function to make an ancestor, or NULL if ancestors should not be
87 int (*make_ancestor_function) (char const *, char const *, void *);
89 /* Mode for ancestor directory. */
92 /* Mode for directory itself. */
95 /* File mode bits affected by MODE. */
98 /* If not null, format to use when reporting newly made directories. */
99 char const *created_directory_format;
102 /* Report that directory DIR was made, if OPTIONS requests this. */
104 announce_mkdir (char const *dir, void *options)
106 struct mkdir_options const *o = options;
107 if (o->created_directory_format)
108 error (0, 0, o->created_directory_format, quote (dir));
111 /* Make ancestor directory DIR, whose last component is COMPONENT,
112 with options OPTIONS. Assume the working directory is COMPONENT's
113 parent. Return 0 if successful and the resulting directory is
114 readable, 1 if successful but the resulting directory is not
115 readable, -1 (setting errno) otherwise. */
117 make_ancestor (char const *dir, char const *component, void *options)
119 struct mkdir_options const *o = options;
120 int r = mkdir (component, o->ancestor_mode);
123 r = ! (o->ancestor_mode & S_IRUSR);
124 announce_mkdir (dir, options);
129 /* Process a command-line file name. */
131 process_dir (char *dir, struct savewd *wd, void *options)
133 struct mkdir_options const *o = options;
134 return (make_dir_parents (dir, wd, o->make_ancestor_function, options,
135 o->mode, announce_mkdir,
136 o->mode_bits, (uid_t) -1, (gid_t) -1, true)
142 main (int argc, char **argv)
144 const char *specified_mode = NULL;
146 security_context_t scontext = NULL;
147 struct mkdir_options options;
149 options.make_ancestor_function = NULL;
150 options.mode = S_IRWXUGO;
151 options.mode_bits = 0;
152 options.created_directory_format = NULL;
154 initialize_main (&argc, &argv);
155 program_name = argv[0];
156 setlocale (LC_ALL, "");
157 bindtextdomain (PACKAGE, LOCALEDIR);
158 textdomain (PACKAGE);
160 atexit (close_stdout);
162 while ((optc = getopt_long (argc, argv, "pm:vZ:", longopts, NULL)) != -1)
167 options.make_ancestor_function = make_ancestor;
170 specified_mode = optarg;
172 case 'v': /* --verbose */
173 options.created_directory_format = _("created directory %s");
178 case_GETOPT_HELP_CHAR;
179 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
181 usage (EXIT_FAILURE);
187 error (0, 0, _("missing operand"));
188 usage (EXIT_FAILURE);
191 if (scontext && setfscreatecon (scontext) < 0)
192 error (EXIT_FAILURE, errno,
193 _("failed to set default file creation context to %s"),
196 if (options.make_ancestor_function || specified_mode)
198 mode_t umask_value = umask (0);
200 options.ancestor_mode = (S_IRWXUGO & ~umask_value) | (S_IWUSR | S_IXUSR);
204 struct mode_change *change = mode_compile (specified_mode);
206 error (EXIT_FAILURE, 0, _("invalid mode %s"),
207 quote (specified_mode));
208 options.mode = mode_adjust (S_IRWXUGO, true, umask_value, change,
213 options.mode = S_IRWXUGO & ~umask_value;
216 exit (savewd_process_files (argc - optind, argv + optind,
217 process_dir, &options));