Include lchmod.h.
[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 "dirname.h"
27 #include "error.h"
28 #include "lchmod.h"
29 #include "mkdir-p.h"
30 #include "modechange.h"
31 #include "quote.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   {"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}
49 };
50
51 void
52 usage (int status)
53 {
54   if (status != EXIT_SUCCESS)
55     fprintf (stderr, _("Try `%s --help' for more information.\n"),
56              program_name);
57   else
58     {
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);
75     }
76   exit (status);
77 }
78
79 int
80 main (int argc, char **argv)
81 {
82   mode_t newmode;
83   mode_t parent_mode IF_LINT (= 0);
84   const char *specified_mode = NULL;
85   const char *verbose_fmt_string = NULL;
86   bool create_parents = false;
87   int exit_status = EXIT_SUCCESS;
88   int optc;
89   int cwd_errno = 0;
90
91   initialize_main (&argc, &argv);
92   program_name = argv[0];
93   setlocale (LC_ALL, "");
94   bindtextdomain (PACKAGE, LOCALEDIR);
95   textdomain (PACKAGE);
96
97   atexit (close_stdout);
98
99   while ((optc = getopt_long (argc, argv, "pm:v", longopts, NULL)) != -1)
100     {
101       switch (optc)
102         {
103         case 'p':
104           create_parents = true;
105           break;
106         case 'm':
107           specified_mode = optarg;
108           break;
109         case 'v': /* --verbose  */
110           verbose_fmt_string = _("created directory %s");
111           break;
112         case_GETOPT_HELP_CHAR;
113         case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
114         default:
115           usage (EXIT_FAILURE);
116         }
117     }
118
119   if (optind == argc)
120     {
121       error (0, 0, _("missing operand"));
122       usage (EXIT_FAILURE);
123     }
124
125   newmode = S_IRWXUGO;
126
127   if (specified_mode || create_parents)
128     {
129       mode_t umask_value = umask (0);
130
131       parent_mode = (S_IRWXUGO & ~umask_value) | (S_IWUSR | S_IXUSR);
132
133       if (specified_mode)
134         {
135           struct mode_change *change = mode_compile (specified_mode);
136           if (!change)
137             error (EXIT_FAILURE, 0, _("invalid mode %s"),
138                    quote (specified_mode));
139           newmode = mode_adjust (S_IRWXUGO, change, umask_value);
140           free (change);
141         }
142       else
143         umask (umask_value);
144     }
145
146   for (; optind < argc; ++optind)
147     {
148       char *dir = argv[optind];
149       bool ok;
150
151       if (create_parents)
152         {
153           if (cwd_errno != 0 && IS_RELATIVE_FILE_NAME (dir))
154             {
155               error (0, cwd_errno, _("cannot return to working directory"));
156               ok = false;
157             }
158           else
159             ok = make_dir_parents (dir, newmode, parent_mode,
160                                    -1, -1, true, verbose_fmt_string,
161                                    &cwd_errno);
162         }
163       else
164         {
165           ok = (mkdir (dir, newmode) == 0);
166
167           if (! ok)
168             error (0, errno, _("cannot create directory %s"), quote (dir));
169           else if (verbose_fmt_string)
170             error (0, 0, verbose_fmt_string, quote (dir));
171
172           /* mkdir(2) is required to honor only the file permission bits.
173              In particular, it needn't do anything about `special' bits,
174              so if any were set in newmode, apply them with lchmod.  */
175
176           /* Set the permissions only if this directory has just
177              been created.  */
178
179           if (ok && specified_mode && (newmode & ~S_IRWXUGO)
180               && lchmod (dir, newmode) != 0)
181             {
182               error (0, errno, _("cannot set permissions of directory %s"),
183                      quote (dir));
184               ok = false;
185             }
186         }
187
188       if (! ok)
189         exit_status = EXIT_FAILURE;
190     }
191
192   exit (exit_status);
193 }