* src/dd.c (flags): noatime and nofollow now depend on
[coreutils/bo.git] / src / mkdir.c
blobb28a02ac0d83a5df5cbf24d7efceb55b5fd4ee6a
1 /* mkdir -- make directories
2 Copyright (C) 90, 1995-2002, 2004, 2005, 2006 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>
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"
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. */
39 char *program_name;
41 static struct option const longopts[] =
43 {"mode", required_argument, NULL, 'm'},
44 {"parents", no_argument, NULL, 'p'},
45 {"verbose", no_argument, NULL, 'v'},
46 {GETOPT_HELP_OPTION_DECL},
47 {GETOPT_VERSION_OPTION_DECL},
48 {NULL, 0, NULL, 0}
51 void
52 usage (int status)
54 if (status != EXIT_SUCCESS)
55 fprintf (stderr, _("Try `%s --help' for more information.\n"),
56 program_name);
57 else
59 printf (_("Usage: %s [OPTION] DIRECTORY...\n"), program_name);
60 fputs (_("\
61 Create the DIRECTORY(ies), if they do not already exist.\n\
62 \n\
63 "), stdout);
64 fputs (_("\
65 Mandatory arguments to long options are mandatory for short options too.\n\
66 "), stdout);
67 fputs (_("\
68 -m, --mode=MODE set file mode (as in chmod), not a=rwx - umask\n\
69 -p, --parents no error if existing, make parent directories as needed\n\
70 -v, --verbose print a message for each created directory\n\
71 "), stdout);
72 fputs (HELP_OPTION_DESCRIPTION, stdout);
73 fputs (VERSION_OPTION_DESCRIPTION, stdout);
74 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
76 exit (status);
79 /* Options passed to subsidiary functions. */
80 struct mkdir_options
82 /* Function to make an ancestor, or NULL if ancestors should not be
83 made. */
84 int (*make_ancestor_function) (char const *, void *);
86 /* Mode for ancestor directory. */
87 mode_t ancestor_mode;
89 /* Mode for directory itself. */
90 mode_t mode;
92 /* File mode bits affected by MODE. */
93 mode_t mode_bits;
95 /* If not null, format to use when reporting newly made directories. */
96 char const *created_directory_format;
99 /* Report that directory DIR was made, if OPTIONS requests this. */
100 static void
101 announce_mkdir (char const *dir, void *options)
103 struct mkdir_options const *o = options;
104 if (o->created_directory_format)
105 error (0, 0, o->created_directory_format, quote (dir));
108 /* Make ancestor directory DIR, with options OPTIONS. Return 0 if
109 successful and the resulting directory is readable, 1 if successful
110 but the resulting directory is not readable, -1 (setting errno)
111 otherwise. */
112 static int
113 make_ancestor (char const *dir, void *options)
115 struct mkdir_options const *o = options;
116 int r = mkdir (dir, o->ancestor_mode);
117 if (r == 0)
119 r = ! (o->ancestor_mode & S_IRUSR);
120 announce_mkdir (dir, options);
122 return r;
125 /* Process a command-line file name. */
126 static int
127 process_dir (char *dir, struct savewd *wd, void *options)
129 struct mkdir_options const *o = options;
130 return (make_dir_parents (dir, wd, o->make_ancestor_function, options,
131 o->mode, announce_mkdir,
132 o->mode_bits, (uid_t) -1, (gid_t) -1, true)
133 ? EXIT_SUCCESS
134 : EXIT_FAILURE);
138 main (int argc, char **argv)
140 const char *specified_mode = NULL;
141 int optc;
142 struct mkdir_options options;
143 options.make_ancestor_function = NULL;
144 options.mode = S_IRWXUGO;
145 options.mode_bits = 0;
146 options.created_directory_format = NULL;
148 initialize_main (&argc, &argv);
149 program_name = argv[0];
150 setlocale (LC_ALL, "");
151 bindtextdomain (PACKAGE, LOCALEDIR);
152 textdomain (PACKAGE);
154 atexit (close_stdout);
156 while ((optc = getopt_long (argc, argv, "pm:v", longopts, NULL)) != -1)
158 switch (optc)
160 case 'p':
161 options.make_ancestor_function = make_ancestor;
162 break;
163 case 'm':
164 specified_mode = optarg;
165 break;
166 case 'v': /* --verbose */
167 options.created_directory_format = _("created directory %s");
168 break;
169 case_GETOPT_HELP_CHAR;
170 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
171 default:
172 usage (EXIT_FAILURE);
176 if (optind == argc)
178 error (0, 0, _("missing operand"));
179 usage (EXIT_FAILURE);
182 if (options.make_ancestor_function || specified_mode)
184 mode_t umask_value = umask (0);
186 options.ancestor_mode = (S_IRWXUGO & ~umask_value) | (S_IWUSR | S_IXUSR);
188 if (specified_mode)
190 struct mode_change *change = mode_compile (specified_mode);
191 if (!change)
192 error (EXIT_FAILURE, 0, _("invalid mode %s"),
193 quote (specified_mode));
194 options.mode = mode_adjust (S_IRWXUGO, true, umask_value, change,
195 &options.mode_bits);
196 free (change);
198 else
199 options.mode = S_IRWXUGO & ~umask_value;
202 exit (savewd_process_files (argc - optind, argv + optind,
203 process_dir, &options));