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