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"
32 # define S_IRWXUGO (S_IRWXU | S_IRWXG | S_IRWXO)
35 /* The official name of this program (e.g., no `g' prefix). */
36 #define PROGRAM_NAME "mkdir"
38 #define AUTHORS "David MacKenzie"
40 /* The name this program was run with. */
43 /* If nonzero, ensure that all parents of the specified directory exist. */
44 static int create_parents;
46 static struct option const longopts[] =
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},
60 fprintf (stderr, _("Try `%s --help' for more information.\n"),
64 printf (_("Usage: %s [OPTION] DIRECTORY...\n"), program_name);
66 Create the DIRECTORY(ies), if they do not already exist.\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\
74 puts (_("\nReport bugs to <bug-fileutils@gnu.org>."));
80 main (int argc, char **argv)
84 const char *symbolic_mode = NULL;
85 const char *verbose_fmt_string = NULL;
89 program_name = argv[0];
90 setlocale (LC_ALL, "");
91 bindtextdomain (PACKAGE, LOCALEDIR);
94 atexit (close_stdout);
98 while ((optc = getopt_long (argc, argv, "pm:v", longopts, NULL)) != -1)
102 case 0: /* Long option. */
108 symbolic_mode = optarg;
110 case 'v': /* --verbose */
111 verbose_fmt_string = _("created directory %s");
113 case_GETOPT_HELP_CHAR;
114 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
122 error (0, 0, _("too few arguments"));
126 newmode = S_IRWXUGO & ~ umask (0);
127 parent_mode = S_IWUSR | S_IXUSR | newmode;
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);
138 for (; optind < argc; ++optind)
143 fail = make_path (argv[optind], newmode, parent_mode,
144 -1, -1, 1, verbose_fmt_string);
148 fail = mkdir (argv[optind], newmode);
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]));
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))
160 fail = chmod (argv[optind], newmode);
162 error (0, errno, _("cannot set permissions of directory %s"),
163 quote (argv[optind]));