install, rmdir: write --verbose output to stdout, not to 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 "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 "David MacKenzie"
38
39 /* The name this program was run with. */
40 char *program_name;
41
42 static struct option const longopts[] =
43 {
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}
51 };
52
53 void
54 usage (int status)
55 {
56   if (status != EXIT_SUCCESS)
57     fprintf (stderr, _("Try `%s --help' for more information.\n"),
58              program_name);
59   else
60     {
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 ();
79     }
80   exit (status);
81 }
82
83 /* Options passed to subsidiary functions.  */
84 struct mkdir_options
85 {
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 *);
89
90   /* Mode for ancestor directory.  */
91   mode_t ancestor_mode;
92
93   /* Mode for directory itself.  */
94   mode_t mode;
95
96   /* File mode bits affected by MODE.  */
97   mode_t mode_bits;
98
99   /* If not null, format to use when reporting newly made directories.  */
100   char const *created_directory_format;
101 };
102
103 /* Report that directory DIR was made, if OPTIONS requests this.  */
104 static void
105 announce_mkdir (char const *dir, void *options)
106 {
107   struct mkdir_options const *o = options;
108   if (o->created_directory_format)
109     prog_fprintf (stdout, o->created_directory_format, quote (dir));
110 }
111
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)
119 {
120   struct mkdir_options const *o = options;
121   int r = mkdir (component, o->ancestor_mode);
122   if (r == 0)
123     {
124       r = ! (o->ancestor_mode & S_IRUSR);
125       announce_mkdir (dir, options);
126     }
127   return r;
128 }
129
130 /* Process a command-line file name.  */
131 static int
132 process_dir (char *dir, struct savewd *wd, void *options)
133 {
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);
140 }
141
142 int
143 main (int argc, char **argv)
144 {
145   const char *specified_mode = NULL;
146   int optc;
147   security_context_t scontext = NULL;
148   struct mkdir_options options;
149
150   options.make_ancestor_function = NULL;
151   options.mode = S_IRWXUGO;
152   options.mode_bits = 0;
153   options.created_directory_format = NULL;
154
155   initialize_main (&argc, &argv);
156   program_name = argv[0];
157   setlocale (LC_ALL, "");
158   bindtextdomain (PACKAGE, LOCALEDIR);
159   textdomain (PACKAGE);
160
161   atexit (close_stdout);
162
163   while ((optc = getopt_long (argc, argv, "pm:vZ:", longopts, NULL)) != -1)
164     {
165       switch (optc)
166         {
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);
183         }
184     }
185
186   if (optind == argc)
187     {
188       error (0, 0, _("missing operand"));
189       usage (EXIT_FAILURE);
190     }
191
192   if (scontext && setfscreatecon (scontext) < 0)
193     error (EXIT_FAILURE, errno,
194            _("failed to set default file creation context to %s"),
195            quote (optarg));
196
197   if (options.make_ancestor_function || specified_mode)
198     {
199       mode_t umask_value = umask (0);
200
201       options.ancestor_mode = (S_IRWXUGO & ~umask_value) | (S_IWUSR | S_IXUSR);
202
203       if (specified_mode)
204         {
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);
212         }
213       else
214         options.mode = S_IRWXUGO & ~umask_value;
215     }
216
217   exit (savewd_process_files (argc - optind, argv + optind,
218                               process_dir, &options));
219 }