* NEWS: Mention these SELinux changes.
[coreutils/ericb.git] / src / mkdir.c
blobb85186540d0526f378711943c4f0ba46634ca206
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 2, or (at your option)
7 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, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 /* David MacKenzie <djm@ai.mit.edu> */
20 #include <config.h>
21 #include <stdio.h>
22 #include <getopt.h>
23 #include <sys/types.h>
24 #include <selinux/selinux.h>
26 #include "system.h"
27 #include "error.h"
28 #include "lchmod.h"
29 #include "mkdir-p.h"
30 #include "modechange.h"
31 #include "quote.h"
32 #include "savewd.h"
34 /* The official name of this program (e.g., no `g' prefix). */
35 #define PROGRAM_NAME "mkdir"
37 #define AUTHORS "David MacKenzie"
39 /* The name this program was run with. */
40 char *program_name;
42 static struct option const longopts[] =
44 {GETOPT_SELINUX_CONTEXT_OPTION_DECL},
45 {"mode", required_argument, NULL, 'm'},
46 {"parents", no_argument, NULL, 'p'},
47 {"verbose", no_argument, NULL, 'v'},
48 {GETOPT_HELP_OPTION_DECL},
49 {GETOPT_VERSION_OPTION_DECL},
50 {NULL, 0, NULL, 0}
53 void
54 usage (int status)
56 if (status != EXIT_SUCCESS)
57 fprintf (stderr, _("Try `%s --help' for more information.\n"),
58 program_name);
59 else
61 printf (_("Usage: %s [OPTION] DIRECTORY...\n"), program_name);
62 fputs (_("\
63 Create the DIRECTORY(ies), if they do not already exist.\n\
64 \n\
65 "), stdout);
66 fputs (_("\
67 Mandatory arguments to long options are mandatory for short options too.\n\
68 "), stdout);
69 fputs (_("\
70 -m, --mode=MODE set file mode (as in chmod), not a=rwx - umask\n\
71 -p, --parents no error if existing, make parent directories as needed\n\
72 -v, --verbose print a message for each created directory\n\
73 -Z, --context=CTX set the SELinux security context of each created\n\
74 directory to CTX\n\
75 "), stdout);
76 fputs (HELP_OPTION_DESCRIPTION, stdout);
77 fputs (VERSION_OPTION_DESCRIPTION, stdout);
78 emit_bug_reporting_address ();
80 exit (status);
83 /* Options passed to subsidiary functions. */
84 struct mkdir_options
86 /* Function to make an ancestor, or NULL if ancestors should not be
87 made. */
88 int (*make_ancestor_function) (char const *, char const *, void *);
90 /* Mode for ancestor directory. */
91 mode_t ancestor_mode;
93 /* Mode for directory itself. */
94 mode_t mode;
96 /* File mode bits affected by MODE. */
97 mode_t mode_bits;
99 /* If not null, format to use when reporting newly made directories. */
100 char const *created_directory_format;
103 /* Report that directory DIR was made, if OPTIONS requests this. */
104 static void
105 announce_mkdir (char const *dir, void *options)
107 struct mkdir_options const *o = options;
108 if (o->created_directory_format)
109 error (0, 0, o->created_directory_format, quote (dir));
112 /* Make ancestor directory DIR, whose last component is COMPONENT,
113 with options OPTIONS. Assume the working directory is COMPONENT's
114 parent. Return 0 if successful and the resulting directory is
115 readable, 1 if successful but the resulting directory is not
116 readable, -1 (setting errno) otherwise. */
117 static int
118 make_ancestor (char const *dir, char const *component, void *options)
120 struct mkdir_options const *o = options;
121 int r = mkdir (component, o->ancestor_mode);
122 if (r == 0)
124 r = ! (o->ancestor_mode & S_IRUSR);
125 announce_mkdir (dir, options);
127 return r;
130 /* Process a command-line file name. */
131 static int
132 process_dir (char *dir, struct savewd *wd, void *options)
134 struct mkdir_options const *o = options;
135 return (make_dir_parents (dir, wd, o->make_ancestor_function, options,
136 o->mode, announce_mkdir,
137 o->mode_bits, (uid_t) -1, (gid_t) -1, true)
138 ? EXIT_SUCCESS
139 : EXIT_FAILURE);
143 main (int argc, char **argv)
145 const char *specified_mode = NULL;
146 int optc;
147 security_context_t scontext = NULL;
148 struct mkdir_options options;
150 options.make_ancestor_function = NULL;
151 options.mode = S_IRWXUGO;
152 options.mode_bits = 0;
153 options.created_directory_format = NULL;
155 initialize_main (&argc, &argv);
156 program_name = argv[0];
157 setlocale (LC_ALL, "");
158 bindtextdomain (PACKAGE, LOCALEDIR);
159 textdomain (PACKAGE);
161 atexit (close_stdout);
163 while ((optc = getopt_long (argc, argv, "pm:vZ:", longopts, NULL)) != -1)
165 switch (optc)
167 case 'p':
168 options.make_ancestor_function = make_ancestor;
169 break;
170 case 'm':
171 specified_mode = optarg;
172 break;
173 case 'v': /* --verbose */
174 options.created_directory_format = _("created directory %s");
175 break;
176 case 'Z':
177 scontext = optarg;
178 break;
179 case_GETOPT_HELP_CHAR;
180 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
181 default:
182 usage (EXIT_FAILURE);
186 if (optind == argc)
188 error (0, 0, _("missing operand"));
189 usage (EXIT_FAILURE);
192 if (scontext && setfscreatecon (scontext) < 0)
193 error (EXIT_FAILURE, errno,
194 _("failed to set default file creation context to %s"),
195 quote (optarg));
197 if (options.make_ancestor_function || specified_mode)
199 mode_t umask_value = umask (0);
201 options.ancestor_mode = (S_IRWXUGO & ~umask_value) | (S_IWUSR | S_IXUSR);
203 if (specified_mode)
205 struct mode_change *change = mode_compile (specified_mode);
206 if (!change)
207 error (EXIT_FAILURE, 0, _("invalid mode %s"),
208 quote (specified_mode));
209 options.mode = mode_adjust (S_IRWXUGO, true, umask_value, change,
210 &options.mode_bits);
211 free (change);
213 else
214 options.mode = S_IRWXUGO & ~umask_value;
217 exit (savewd_process_files (argc - optind, argv + optind,
218 process_dir, &options));