(main): Rename local `symbolic_mode' to `specified_mode'.
[platform/upstream/coreutils.git] / src / mkdir.c
1 /* mkdir -- make directories
2    Copyright (C) 90, 1995-2000 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 "makepath.h"
28 #include "modechange.h"
29 #include "quote.h"
30
31 /* The official name of this program (e.g., no `g' prefix).  */
32 #define PROGRAM_NAME "mkdir"
33
34 #define AUTHORS "David MacKenzie"
35
36 /* The name this program was run with. */
37 char *program_name;
38
39 /* If nonzero, ensure that all parents of the specified directory exist.  */
40 static int create_parents;
41
42 static struct option const longopts[] =
43 {
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 != 0)
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       printf (_("\
62 Create the DIRECTORY(ies), if they do not already exist.\n\
63 \n\
64   -m, --mode=MODE   set permission mode (as in chmod), not rwxrwxrwx - umask\n\
65   -p, --parents     no error if existing, make parent directories as needed\n\
66   -v, --verbose     print a message for each created directory\n\
67       --help        display this help and exit\n\
68       --version     output version information and exit\n\
69 "));
70       puts (_("\nReport bugs to <bug-fileutils@gnu.org>."));
71     }
72   exit (status);
73 }
74
75 int
76 main (int argc, char **argv)
77 {
78   mode_t newmode;
79   mode_t parent_mode;
80   const char *specified_mode = NULL;
81   const char *verbose_fmt_string = NULL;
82   int errors = 0;
83   int optc;
84
85   program_name = argv[0];
86   setlocale (LC_ALL, "");
87   bindtextdomain (PACKAGE, LOCALEDIR);
88   textdomain (PACKAGE);
89
90   atexit (close_stdout);
91
92   create_parents = 0;
93
94   while ((optc = getopt_long (argc, argv, "pm:v", longopts, NULL)) != -1)
95     {
96       switch (optc)
97         {
98         case 0:                 /* Long option. */
99           break;
100         case 'p':
101           create_parents = 1;
102           break;
103         case 'm':
104           specified_mode = optarg;
105           break;
106         case 'v': /* --verbose  */
107           verbose_fmt_string = _("created directory %s");
108           break;
109         case_GETOPT_HELP_CHAR;
110         case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
111         default:
112           usage (1);
113         }
114     }
115
116   if (optind == argc)
117     {
118       error (0, 0, _("too few arguments"));
119       usage (1);
120     }
121
122   newmode = S_IRWXUGO;
123   if (specified_mode)
124     {
125       struct mode_change *change = mode_compile (specified_mode, 0);
126       newmode &= ~ umask (0);
127       if (change == MODE_INVALID)
128         error (1, 0, _("invalid mode %s"), quote (specified_mode));
129       else if (change == MODE_MEMORY_EXHAUSTED)
130         xalloc_die ();
131       newmode = mode_adjust (newmode, change);
132     }
133   parent_mode = S_IWUSR | S_IXUSR | newmode;
134
135   for (; optind < argc; ++optind)
136     {
137       int fail = 0;
138       if (create_parents)
139         {
140           fail = make_path (argv[optind], newmode, parent_mode,
141                             -1, -1, 1, verbose_fmt_string);
142         }
143       else
144         {
145           fail = mkdir (argv[optind], newmode);
146           if (fail)
147             error (0, errno, _("cannot create directory %s"),
148                    quote (argv[optind]));
149           else if (verbose_fmt_string)
150             error (0, 0, verbose_fmt_string, quote (argv[optind]));
151
152           /* mkdir(2) is required to honor only the file permission bits.
153              In particular, it needn't do anything about `special' bits,
154              so if any were set in newmode, apply them with chmod.
155              This extra step is necessary in some cases when the containing
156              directory has a default ACL.  */
157
158           if (fail == 0 && specified_mode)
159             {
160               fail = chmod (argv[optind], newmode);
161               if (fail)
162                 error (0, errno, _("cannot set permissions of directory %s"),
163                        quote (argv[optind]));
164             }
165         }
166
167       if (fail)
168         errors = 1;
169     }
170
171   exit (errors);
172 }