1 /* mkdir -- make directories
2 Copyright (C) 90, 1995-2000 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)
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* David MacKenzie <djm@ai.mit.edu> */
23 #include <sys/types.h>
28 #include "modechange.h"
31 /* The official name of this program (e.g., no `g' prefix). */
32 #define PROGRAM_NAME "mkdir"
34 #define AUTHORS "David MacKenzie"
36 /* The name this program was run with. */
39 /* If nonzero, ensure that all parents of the specified directory exist. */
40 static int create_parents;
42 static struct option const longopts[] =
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},
56 fprintf (stderr, _("Try `%s --help' for more information.\n"),
60 printf (_("Usage: %s [OPTION] DIRECTORY...\n"), program_name);
62 Create the DIRECTORY(ies), if they do not already exist.\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\
70 puts (_("\nReport bugs to <bug-fileutils@gnu.org>."));
76 main (int argc, char **argv)
80 const char *symbolic_mode = NULL;
81 const char *verbose_fmt_string = NULL;
85 program_name = argv[0];
86 setlocale (LC_ALL, "");
87 bindtextdomain (PACKAGE, LOCALEDIR);
90 atexit (close_stdout);
94 while ((optc = getopt_long (argc, argv, "pm:v", longopts, NULL)) != -1)
98 case 0: /* Long option. */
104 symbolic_mode = optarg;
106 case 'v': /* --verbose */
107 verbose_fmt_string = _("created directory %s");
109 case_GETOPT_HELP_CHAR;
110 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
118 error (0, 0, _("too few arguments"));
125 struct mode_change *change = mode_compile (symbolic_mode, 0);
126 newmode &= ~ umask (0);
127 if (change == MODE_INVALID)
128 error (1, 0, _("invalid mode %s"), quote (symbolic_mode));
129 else if (change == MODE_MEMORY_EXHAUSTED)
131 newmode = mode_adjust (newmode, change);
133 parent_mode = S_IWUSR | S_IXUSR | newmode;
135 for (; optind < argc; ++optind)
140 fail = make_path (argv[optind], newmode, parent_mode,
141 -1, -1, 1, verbose_fmt_string);
145 fail = mkdir (argv[optind], newmode);
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]));
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. */
158 if (fail == 0 && symbolic_mode)
160 fail = chmod (argv[optind], newmode);
162 error (0, errno, _("cannot set permissions of directory %s"),
163 quote (argv[optind]));