1 /* vi: set sw=4 ts=4: */
3 * parse_mode implementation for busybox
5 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
10 /* Mar 5, 2003 Manuel Novoa III
12 * This is the main work function for the 'mkdir' applet. As such, it
13 * strives to be SUSv3 compliant in it's behaviour when recursively
14 * making missing parent dirs, and in it's mode setting of the final
17 * To recursively build all missing intermediate directories, make
18 * sure that (flags & FILEUTILS_RECUR) is non-zero. Newly created
19 * intermediate directories will have at least u+wx perms.
21 * To set specific permissions on 'path', pass the appropriate 'mode'
22 * val. Otherwise, pass -1 to get default permissions.
30 int bb_make_directory (char *path, long mode, int flags)
41 mode = (S_IXUSR | S_IXGRP | S_IXOTH |
42 S_IWUSR | S_IWGRP | S_IWOTH |
43 S_IRUSR | S_IRGRP | S_IROTH) & ~mask;
51 if (flags & FILEUTILS_RECUR) { /* Get the parent. */
52 /* Bypass leading non-'/'s and then subsequent '/'s. */
58 c = *s; /* Save the current char */
59 *s = 0; /* and replace it with nul. */
66 if (mkdir(path, 0777) < 0) {
67 /* If we failed for any other reason than the directory
68 * already exists, output a diagnostic and return -1.*/
70 || !(flags & FILEUTILS_RECUR)
71 || (stat(path, &st) < 0 || !S_ISDIR(st.st_mode))) {
76 /* Since the directory exists, don't attempt to change
77 * permissions if it was the full target. Note that
78 * this is not an error conditon. */
86 /* Done. If necessary, updated perms on the newly
87 * created directory. Failure to update here _is_
90 if ((mode != -1) && (chmod(path, mode) < 0)){
91 fail_msg = "set permissions of";
97 /* Remove any inserted nul from the path (recursive mode). */
102 bb_perror_msg ("Cannot %s directory `%s'", fail_msg, path);