mkdir, split: write --verbose output to stdout, not stderr.
[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 "quote.h"
31 #include "savewd.h"
32
33 /* The official name of this program (e.g., no `g' prefix).  */
34 #define PROGRAM_NAME "mkdir"
35
36 #define AUTHORS "David MacKenzie"
37
38 /* The name this program was run with. */
39 char *program_name;
40
41 static struct option const longopts[] =
42 {
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},
49   {NULL, 0, NULL, 0}
50 };
51
52 void
53 usage (int status)
54 {
55   if (status != EXIT_SUCCESS)
56     fprintf (stderr, _("Try `%s --help' for more information.\n"),
57              program_name);
58   else
59     {
60       printf (_("Usage: %s [OPTION] DIRECTORY...\n"), program_name);
61       fputs (_("\
62 Create the DIRECTORY(ies), if they do not already exist.\n\
63 \n\
64 "), stdout);
65       fputs (_("\
66 Mandatory arguments to long options are mandatory for short options too.\n\
67 "), stdout);
68       fputs (_("\
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\
73                       directory to CTX\n\
74 "), stdout);
75       fputs (HELP_OPTION_DESCRIPTION, stdout);
76       fputs (VERSION_OPTION_DESCRIPTION, stdout);
77       emit_bug_reporting_address ();
78     }
79   exit (status);
80 }
81
82 /* Verbose formatted output of variable count of arguments.  */
83 static void
84 verbose_output (FILE *fp, char const *fmt, ...)
85 {
86   va_list ap;
87   fputs (program_name, fp);
88   fputs (": ", fp);
89   va_start (ap, fmt);
90   vfprintf (fp, fmt, ap);
91   va_end (ap);
92   fputc ('\n', fp);
93 }
94
95 /* Options passed to subsidiary functions.  */
96 struct mkdir_options
97 {
98   /* Function to make an ancestor, or NULL if ancestors should not be
99      made.  */
100   int (*make_ancestor_function) (char const *, char const *, void *);
101
102   /* Mode for ancestor directory.  */
103   mode_t ancestor_mode;
104
105   /* Mode for directory itself.  */
106   mode_t mode;
107
108   /* File mode bits affected by MODE.  */
109   mode_t mode_bits;
110
111   /* If not null, format to use when reporting newly made directories.  */
112   char const *created_directory_format;
113 };
114
115 /* Report that directory DIR was made, if OPTIONS requests this.  */
116 static void
117 announce_mkdir (char const *dir, void *options)
118 {
119   struct mkdir_options const *o = options;
120   if (o->created_directory_format)
121     verbose_output (stdout, o->created_directory_format, quote (dir));
122 }
123
124 /* Make ancestor directory DIR, whose last component is COMPONENT,
125    with options OPTIONS.  Assume the working directory is COMPONENT's
126    parent.  Return 0 if successful and the resulting directory is
127    readable, 1 if successful but the resulting directory is not
128    readable, -1 (setting errno) otherwise.  */
129 static int
130 make_ancestor (char const *dir, char const *component, void *options)
131 {
132   struct mkdir_options const *o = options;
133   int r = mkdir (component, o->ancestor_mode);
134   if (r == 0)
135     {
136       r = ! (o->ancestor_mode & S_IRUSR);
137       announce_mkdir (dir, options);
138     }
139   return r;
140 }
141
142 /* Process a command-line file name.  */
143 static int
144 process_dir (char *dir, struct savewd *wd, void *options)
145 {
146   struct mkdir_options const *o = options;
147   return (make_dir_parents (dir, wd, o->make_ancestor_function, options,
148                             o->mode, announce_mkdir,
149                             o->mode_bits, (uid_t) -1, (gid_t) -1, true)
150           ? EXIT_SUCCESS
151           : EXIT_FAILURE);
152 }
153
154 int
155 main (int argc, char **argv)
156 {
157   const char *specified_mode = NULL;
158   int optc;
159   security_context_t scontext = NULL;
160   struct mkdir_options options;
161
162   options.make_ancestor_function = NULL;
163   options.mode = S_IRWXUGO;
164   options.mode_bits = 0;
165   options.created_directory_format = NULL;
166
167   initialize_main (&argc, &argv);
168   program_name = argv[0];
169   setlocale (LC_ALL, "");
170   bindtextdomain (PACKAGE, LOCALEDIR);
171   textdomain (PACKAGE);
172
173   atexit (close_stdout);
174
175   while ((optc = getopt_long (argc, argv, "pm:vZ:", longopts, NULL)) != -1)
176     {
177       switch (optc)
178         {
179         case 'p':
180           options.make_ancestor_function = make_ancestor;
181           break;
182         case 'm':
183           specified_mode = optarg;
184           break;
185         case 'v': /* --verbose  */
186           options.created_directory_format = _("created directory %s");
187           break;
188         case 'Z':
189           scontext = optarg;
190           break;
191         case_GETOPT_HELP_CHAR;
192         case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
193         default:
194           usage (EXIT_FAILURE);
195         }
196     }
197
198   if (optind == argc)
199     {
200       error (0, 0, _("missing operand"));
201       usage (EXIT_FAILURE);
202     }
203
204   if (scontext && setfscreatecon (scontext) < 0)
205     error (EXIT_FAILURE, errno,
206            _("failed to set default file creation context to %s"),
207            quote (optarg));
208
209   if (options.make_ancestor_function || specified_mode)
210     {
211       mode_t umask_value = umask (0);
212
213       options.ancestor_mode = (S_IRWXUGO & ~umask_value) | (S_IWUSR | S_IXUSR);
214
215       if (specified_mode)
216         {
217           struct mode_change *change = mode_compile (specified_mode);
218           if (!change)
219             error (EXIT_FAILURE, 0, _("invalid mode %s"),
220                    quote (specified_mode));
221           options.mode = mode_adjust (S_IRWXUGO, true, umask_value, change,
222                                       &options.mode_bits);
223           free (change);
224         }
225       else
226         options.mode = S_IRWXUGO & ~umask_value;
227     }
228
229   exit (savewd_process_files (argc - optind, argv + optind,
230                               process_dir, &options));
231 }