| File: | src/bin/mkdir/mkdir.c |
| Warning: | line 79, column 2 Value stored to 'argc' is never read |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
| 1 | /* $OpenBSD: mkdir.c,v 1.31 2019/06/28 13:34:59 deraadt Exp $ */ |
| 2 | /* $NetBSD: mkdir.c,v 1.14 1995/06/25 21:59:21 mycroft Exp $ */ |
| 3 | |
| 4 | /* |
| 5 | * Copyright (c) 1983, 1992, 1993 |
| 6 | * The Regents of the University of California. All rights reserved. |
| 7 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions |
| 10 | * are met: |
| 11 | * 1. Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * 2. Redistributions in binary form must reproduce the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer in the |
| 15 | * documentation and/or other materials provided with the distribution. |
| 16 | * 3. Neither the name of the University nor the names of its contributors |
| 17 | * may be used to endorse or promote products derived from this software |
| 18 | * without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 30 | * SUCH DAMAGE. |
| 31 | */ |
| 32 | |
| 33 | #include <sys/types.h> |
| 34 | #include <sys/stat.h> |
| 35 | |
| 36 | #include <err.h> |
| 37 | #include <errno(*__errno()).h> |
| 38 | #include <stdio.h> |
| 39 | #include <stdlib.h> |
| 40 | #include <string.h> |
| 41 | #include <unistd.h> |
| 42 | |
| 43 | extern char *__progname; |
| 44 | |
| 45 | int mkpath(char *, mode_t, mode_t); |
| 46 | static void __dead__attribute__((__noreturn__)) usage(void); |
| 47 | |
| 48 | int |
| 49 | main(int argc, char *argv[]) |
| 50 | { |
| 51 | int ch, rv, exitval, pflag; |
| 52 | void *set; |
| 53 | mode_t mode, dir_mode; |
| 54 | |
| 55 | /* |
| 56 | * The default file mode is a=rwx (0777) with selected permissions |
| 57 | * removed in accordance with the file mode creation mask. For |
| 58 | * intermediate path name components, the mode is the default modified |
| 59 | * by u+wx so that the subdirectories can always be created. |
| 60 | */ |
| 61 | mode = 0777 & ~umask(0); |
| 62 | dir_mode = mode | S_IWUSR0000200 | S_IXUSR0000100; |
| 63 | |
| 64 | pflag = 0; |
| 65 | while ((ch = getopt(argc, argv, "m:p")) != -1) |
| 66 | switch(ch) { |
| 67 | case 'p': |
| 68 | pflag = 1; |
| 69 | break; |
| 70 | case 'm': |
| 71 | if ((set = setmode(optarg)) == NULL((void *)0)) |
| 72 | errx(1, "invalid file mode: %s", optarg); |
| 73 | mode = getmode(set, S_IRWXU0000700 | S_IRWXG0000070 | S_IRWXO0000007); |
| 74 | free(set); |
| 75 | break; |
| 76 | default: |
| 77 | usage(); |
| 78 | } |
| 79 | argc -= optind; |
Value stored to 'argc' is never read | |
| 80 | argv += optind; |
| 81 | |
| 82 | if ((mode & (S_ISUID0004000 | S_ISGID0002000 | S_ISTXT0001000)) == 0) { |
| 83 | if (pledge("stdio rpath cpath fattr", NULL((void *)0)) == -1) |
| 84 | err(1, "pledge"); |
| 85 | } |
| 86 | |
| 87 | if (*argv == NULL((void *)0)) |
| 88 | usage(); |
| 89 | |
| 90 | for (exitval = 0; *argv != NULL((void *)0); ++argv) { |
| 91 | char *slash; |
| 92 | |
| 93 | /* Remove trailing slashes, per POSIX. */ |
| 94 | slash = strrchr(*argv, '\0'); |
| 95 | while (--slash > *argv && *slash == '/') |
| 96 | *slash = '\0'; |
| 97 | |
| 98 | if (pflag) { |
| 99 | rv = mkpath(*argv, mode, dir_mode); |
| 100 | } else { |
| 101 | rv = mkdir(*argv, mode); |
| 102 | /* |
| 103 | * The mkdir() and umask() calls both honor only the |
| 104 | * low nine bits, so if you try to set a mode including |
| 105 | * the sticky, setuid, setgid bits you lose them. Don't |
| 106 | * do this unless the user has specifically requested |
| 107 | * a mode as chmod will (obviously) ignore the umask. |
| 108 | */ |
| 109 | if (rv == 0 && mode > 0777) |
| 110 | rv = chmod(*argv, mode); |
| 111 | } |
| 112 | if (rv == -1) { |
| 113 | warn("%s", *argv); |
| 114 | exitval = 1; |
| 115 | } |
| 116 | } |
| 117 | return exitval; |
| 118 | } |
| 119 | |
| 120 | /* |
| 121 | * mkpath -- create directories. |
| 122 | * path - path |
| 123 | * mode - file mode of terminal directory |
| 124 | * dir_mode - file mode of intermediate directories |
| 125 | */ |
| 126 | int |
| 127 | mkpath(char *path, mode_t mode, mode_t dir_mode) |
| 128 | { |
| 129 | struct stat sb; |
| 130 | char *slash; |
| 131 | int done; |
| 132 | |
| 133 | slash = path; |
| 134 | |
| 135 | for (;;) { |
| 136 | slash += strspn(slash, "/"); |
| 137 | slash += strcspn(slash, "/"); |
| 138 | |
| 139 | done = (*slash == '\0'); |
| 140 | *slash = '\0'; |
| 141 | |
| 142 | if (mkdir(path, done ? mode : dir_mode) == 0) { |
| 143 | if (mode > 0777 && chmod(path, mode) == -1) |
| 144 | return (-1); |
| 145 | } else { |
| 146 | int mkdir_errno = errno(*__errno()); |
| 147 | |
| 148 | if (stat(path, &sb) == -1) { |
| 149 | /* Not there; use mkdir()s errno */ |
| 150 | errno(*__errno()) = mkdir_errno; |
| 151 | return (-1); |
| 152 | } |
| 153 | if (!S_ISDIR(sb.st_mode)((sb.st_mode & 0170000) == 0040000)) { |
| 154 | /* Is there, but isn't a directory */ |
| 155 | errno(*__errno()) = ENOTDIR20; |
| 156 | return (-1); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | if (done) |
| 161 | break; |
| 162 | |
| 163 | *slash = '/'; |
| 164 | } |
| 165 | |
| 166 | return (0); |
| 167 | } |
| 168 | |
| 169 | static void __dead__attribute__((__noreturn__)) |
| 170 | usage(void) |
| 171 | { |
| 172 | (void)fprintf(stderr(&__sF[2]), "usage: %s [-p] [-m mode] directory ...\n", |
| 173 | __progname); |
| 174 | exit(1); |
| 175 | } |