1 /* mkdir -- make directories
2 Copyright (C) 90, 1995-2002, 2004-2007 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 /* David MacKenzie <djm@ai.mit.edu> */
23 #include <sys/types.h>
24 #include <selinux/selinux.h>
30 #include "modechange.h"
34 /* The official name of this program (e.g., no `g' prefix). */
35 #define PROGRAM_NAME "mkdir"
37 #define AUTHORS "David MacKenzie"
39 /* The name this program was run with. */
42 static struct option
const longopts
[] =
44 {GETOPT_SELINUX_CONTEXT_OPTION_DECL
},
45 {"mode", required_argument
, NULL
, 'm'},
46 {"parents", no_argument
, NULL
, 'p'},
47 {"verbose", no_argument
, NULL
, 'v'},
48 {GETOPT_HELP_OPTION_DECL
},
49 {GETOPT_VERSION_OPTION_DECL
},
56 if (status
!= EXIT_SUCCESS
)
57 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
61 printf (_("Usage: %s [OPTION] DIRECTORY...\n"), program_name
);
63 Create the DIRECTORY(ies), if they do not already exist.\n\
67 Mandatory arguments to long options are mandatory for short options too.\n\
70 -m, --mode=MODE set file mode (as in chmod), not a=rwx - umask\n\
71 -p, --parents no error if existing, make parent directories as needed\n\
72 -v, --verbose print a message for each created directory\n\
73 -Z, --context=CTX set the SELinux security context of each created\n\
76 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
77 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
78 emit_bug_reporting_address ();
83 /* Options passed to subsidiary functions. */
86 /* Function to make an ancestor, or NULL if ancestors should not be
88 int (*make_ancestor_function
) (char const *, char const *, void *);
90 /* Mode for ancestor directory. */
93 /* Mode for directory itself. */
96 /* File mode bits affected by MODE. */
99 /* If not null, format to use when reporting newly made directories. */
100 char const *created_directory_format
;
103 /* Report that directory DIR was made, if OPTIONS requests this. */
105 announce_mkdir (char const *dir
, void *options
)
107 struct mkdir_options
const *o
= options
;
108 if (o
->created_directory_format
)
109 error (0, 0, o
->created_directory_format
, quote (dir
));
112 /* Make ancestor directory DIR, whose last component is COMPONENT,
113 with options OPTIONS. Assume the working directory is COMPONENT's
114 parent. Return 0 if successful and the resulting directory is
115 readable, 1 if successful but the resulting directory is not
116 readable, -1 (setting errno) otherwise. */
118 make_ancestor (char const *dir
, char const *component
, void *options
)
120 struct mkdir_options
const *o
= options
;
121 int r
= mkdir (component
, o
->ancestor_mode
);
124 r
= ! (o
->ancestor_mode
& S_IRUSR
);
125 announce_mkdir (dir
, options
);
130 /* Process a command-line file name. */
132 process_dir (char *dir
, struct savewd
*wd
, void *options
)
134 struct mkdir_options
const *o
= options
;
135 return (make_dir_parents (dir
, wd
, o
->make_ancestor_function
, options
,
136 o
->mode
, announce_mkdir
,
137 o
->mode_bits
, (uid_t
) -1, (gid_t
) -1, true)
143 main (int argc
, char **argv
)
145 const char *specified_mode
= NULL
;
147 security_context_t scontext
= NULL
;
148 struct mkdir_options options
;
150 options
.make_ancestor_function
= NULL
;
151 options
.mode
= S_IRWXUGO
;
152 options
.mode_bits
= 0;
153 options
.created_directory_format
= NULL
;
155 initialize_main (&argc
, &argv
);
156 program_name
= argv
[0];
157 setlocale (LC_ALL
, "");
158 bindtextdomain (PACKAGE
, LOCALEDIR
);
159 textdomain (PACKAGE
);
161 atexit (close_stdout
);
163 while ((optc
= getopt_long (argc
, argv
, "pm:vZ:", longopts
, NULL
)) != -1)
168 options
.make_ancestor_function
= make_ancestor
;
171 specified_mode
= optarg
;
173 case 'v': /* --verbose */
174 options
.created_directory_format
= _("created directory %s");
179 case_GETOPT_HELP_CHAR
;
180 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
182 usage (EXIT_FAILURE
);
188 error (0, 0, _("missing operand"));
189 usage (EXIT_FAILURE
);
192 if (scontext
&& setfscreatecon (scontext
) < 0)
193 error (EXIT_FAILURE
, errno
,
194 _("failed to set default file creation context to %s"),
197 if (options
.make_ancestor_function
|| specified_mode
)
199 mode_t umask_value
= umask (0);
201 options
.ancestor_mode
= (S_IRWXUGO
& ~umask_value
) | (S_IWUSR
| S_IXUSR
);
205 struct mode_change
*change
= mode_compile (specified_mode
);
207 error (EXIT_FAILURE
, 0, _("invalid mode %s"),
208 quote (specified_mode
));
209 options
.mode
= mode_adjust (S_IRWXUGO
, true, umask_value
, change
,
214 options
.mode
= S_IRWXUGO
& ~umask_value
;
217 exit (savewd_process_files (argc
- optind
, argv
+ optind
,
218 process_dir
, &options
));