Don't include dirname.h, since system.h does it now.
[platform/upstream/coreutils.git] / src / mkdir.c
1 /* mkdir -- make directories
2    Copyright (C) 90, 1995-2002, 2004, 2005, 2006 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 2, or (at your option)
7    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, write to the Free Software Foundation,
16    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17
18 /* David MacKenzie <djm@ai.mit.edu>  */
19
20 #include <config.h>
21 #include <stdio.h>
22 #include <getopt.h>
23 #include <sys/types.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
32 /* The official name of this program (e.g., no `g' prefix).  */
33 #define PROGRAM_NAME "mkdir"
34
35 #define AUTHORS "David MacKenzie"
36
37 /* The name this program was run with. */
38 char *program_name;
39
40 static struct option const longopts[] =
41 {
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 "), stdout);
71       fputs (HELP_OPTION_DESCRIPTION, stdout);
72       fputs (VERSION_OPTION_DESCRIPTION, stdout);
73       printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
74     }
75   exit (status);
76 }
77
78 /* Options for announce_mkdir and make_ancestor.  */
79 struct mkdir_options
80 {
81   /* Mode for ancestor directory.  */
82   mode_t ancestor_mode;
83
84   /* If not null, format to use when reporting newly made directories.  */
85   char const *created_directory_format;
86 };
87
88 /* Report that directory DIR was made, if OPTIONS requests this.  */
89 static void
90 announce_mkdir (char const *dir, void *options)
91 {
92   struct mkdir_options const *o = options;
93   if (o->created_directory_format)
94     error (0, 0, o->created_directory_format, quote (dir));
95 }
96
97 /* Make ancestor directory DIR, with options OPTIONS.  */
98 static int
99 make_ancestor (char const *dir, void *options)
100 {
101   struct mkdir_options const *o = options;
102   int r = mkdir (dir, o->ancestor_mode);
103   if (r == 0)
104     announce_mkdir (dir, options);
105   return r;
106 }
107
108 int
109 main (int argc, char **argv)
110 {
111   mode_t mode = S_IRWXUGO;
112   mode_t mode_bits = 0;
113   int (*make_ancestor_function) (char const *, void *) = NULL;
114   const char *specified_mode = NULL;
115   int exit_status = EXIT_SUCCESS;
116   int optc;
117   struct mkdir_options options;
118   options.created_directory_format = NULL;
119
120   initialize_main (&argc, &argv);
121   program_name = argv[0];
122   setlocale (LC_ALL, "");
123   bindtextdomain (PACKAGE, LOCALEDIR);
124   textdomain (PACKAGE);
125
126   atexit (close_stdout);
127
128   while ((optc = getopt_long (argc, argv, "pm:v", longopts, NULL)) != -1)
129     {
130       switch (optc)
131         {
132         case 'p':
133           make_ancestor_function = make_ancestor;
134           break;
135         case 'm':
136           specified_mode = optarg;
137           break;
138         case 'v': /* --verbose  */
139           options.created_directory_format = _("created directory %s");
140           break;
141         case_GETOPT_HELP_CHAR;
142         case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
143         default:
144           usage (EXIT_FAILURE);
145         }
146     }
147
148   if (optind == argc)
149     {
150       error (0, 0, _("missing operand"));
151       usage (EXIT_FAILURE);
152     }
153
154   if (make_ancestor_function || specified_mode)
155     {
156       mode_t umask_value = umask (0);
157
158       options.ancestor_mode = (S_IRWXUGO & ~umask_value) | (S_IWUSR | S_IXUSR);
159
160       if (specified_mode)
161         {
162           struct mode_change *change = mode_compile (specified_mode);
163           if (!change)
164             error (EXIT_FAILURE, 0, _("invalid mode %s"),
165                    quote (specified_mode));
166           mode = mode_adjust (S_IRWXUGO, true, umask_value, change,
167                               &mode_bits);
168           free (change);
169         }
170       else
171         mode &= ~umask_value;
172     }
173
174   for (; optind < argc; ++optind)
175     if (! make_dir_parents (argv[optind], make_ancestor_function, &options,
176                             mode, announce_mkdir,
177                             mode_bits, (uid_t) -1, (gid_t) -1, true))
178       exit_status = EXIT_FAILURE;
179
180   exit (exit_status);
181 }