Convert "`%s'" in format strings to "%s", and wrap each
[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 #ifndef S_IRWXUGO
32 # define S_IRWXUGO (S_IRWXU | S_IRWXG | S_IRWXO)
33 #endif
34
35 /* The official name of this program (e.g., no `g' prefix).  */
36 #define PROGRAM_NAME "mkdir"
37
38 #define AUTHORS "David MacKenzie"
39
40 /* The name this program was run with. */
41 char *program_name;
42
43 /* If nonzero, ensure that all parents of the specified directory exist.  */
44 static int create_parents;
45
46 static struct option const longopts[] =
47 {
48   {"mode", required_argument, NULL, 'm'},
49   {"parents", no_argument, NULL, 'p'},
50   {"verbose", no_argument, NULL, 'v'},
51   {GETOPT_HELP_OPTION_DECL},
52   {GETOPT_VERSION_OPTION_DECL},
53   {NULL, 0, NULL, 0}
54 };
55
56 void
57 usage (int status)
58 {
59   if (status != 0)
60     fprintf (stderr, _("Try `%s --help' for more information.\n"),
61              program_name);
62   else
63     {
64       printf (_("Usage: %s [OPTION] DIRECTORY...\n"), program_name);
65       printf (_("\
66 Create the DIRECTORY(ies), if they do not already exist.\n\
67 \n\
68   -m, --mode=MODE   set permission mode (as in chmod), not rwxrwxrwx - 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       --help        display this help and exit\n\
72       --version     output version information and exit\n\
73 "));
74       puts (_("\nReport bugs to <bug-fileutils@gnu.org>."));
75     }
76   exit (status);
77 }
78
79 int
80 main (int argc, char **argv)
81 {
82   mode_t newmode;
83   mode_t parent_mode;
84   const char *symbolic_mode = NULL;
85   const char *verbose_fmt_string = NULL;
86   int errors = 0;
87   int optc;
88
89   program_name = argv[0];
90   setlocale (LC_ALL, "");
91   bindtextdomain (PACKAGE, LOCALEDIR);
92   textdomain (PACKAGE);
93
94   atexit (close_stdout);
95
96   create_parents = 0;
97
98   while ((optc = getopt_long (argc, argv, "pm:v", longopts, NULL)) != -1)
99     {
100       switch (optc)
101         {
102         case 0:                 /* Long option. */
103           break;
104         case 'p':
105           create_parents = 1;
106           break;
107         case 'm':
108           symbolic_mode = optarg;
109           break;
110         case 'v': /* --verbose  */
111           verbose_fmt_string = _("created directory %s");
112           break;
113         case_GETOPT_HELP_CHAR;
114         case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
115         default:
116           usage (1);
117         }
118     }
119
120   if (optind == argc)
121     {
122       error (0, 0, _("too few arguments"));
123       usage (1);
124     }
125
126   newmode = S_IRWXUGO & ~ umask (0);
127   parent_mode = S_IWUSR | S_IXUSR | newmode;
128   if (symbolic_mode)
129     {
130       struct mode_change *change = mode_compile (symbolic_mode, 0);
131       if (change == MODE_INVALID)
132         error (1, 0, _("invalid mode %s"), quote (symbolic_mode));
133       else if (change == MODE_MEMORY_EXHAUSTED)
134         error (1, 0, _("virtual memory exhausted"));
135       newmode = mode_adjust (newmode, change);
136     }
137
138   for (; optind < argc; ++optind)
139     {
140       int fail = 0;
141       if (create_parents)
142         {
143           fail = make_path (argv[optind], newmode, parent_mode,
144                             -1, -1, 1, verbose_fmt_string);
145         }
146       else
147         {
148           fail = mkdir (argv[optind], newmode);
149           if (fail)
150             error (0, errno, _("cannot create directory %s"),
151                    quote (argv[optind]));
152           else if (verbose_fmt_string)
153             error (0, 0, verbose_fmt_string, quote (argv[optind]));
154
155           /* mkdir(2) is required to honor only the file permission bits.
156              In particular, it needn't do anything about `special' bits,
157              so if any were set in newmode, apply them with chmod.  */
158           if (fail == 0 && (newmode & ~S_IRWXUGO))
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       errors |= fail;
168     }
169
170   exit (errors);
171 }