[1704] | 1 | /*-
|
---|
| 2 | * Copyright (c) 1989, 1993, 1994
|
---|
| 3 | * The Regents of the University of California. All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | * 1. Redistributions of source code must retain the above copyright
|
---|
| 9 | * notice, this list of conditions and the following disclaimer.
|
---|
| 10 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 12 | * documentation and/or other materials provided with the distribution.
|
---|
| 13 | * 4. Neither the name of the University nor the names of its contributors
|
---|
| 14 | * may be used to endorse or promote products derived from this software
|
---|
| 15 | * without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
---|
| 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
| 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
---|
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
---|
| 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
---|
| 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
---|
| 23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
---|
| 24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
---|
| 25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
---|
| 26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
---|
| 27 | * SUCH DAMAGE.
|
---|
| 28 | */
|
---|
| 29 |
|
---|
| 30 | #if 0
|
---|
| 31 | #ifndef lint
|
---|
| 32 | static char const copyright[] =
|
---|
| 33 | "@(#) Copyright (c) 1989, 1993, 1994\n\
|
---|
| 34 | The Regents of the University of California. All rights reserved.\n";
|
---|
| 35 | #endif /* not lint */
|
---|
| 36 |
|
---|
| 37 | #ifndef lint
|
---|
| 38 | static char sccsid[] = "@(#)chmod.c 8.8 (Berkeley) 4/1/94";
|
---|
| 39 | #endif /* not lint */
|
---|
| 40 | #endif
|
---|
[1705] | 41 | /*#include <sys/cdefs.h> */
|
---|
| 42 | /*__FBSDID("$FreeBSD: src/bin/chmod/chmod.c,v 1.33 2005/01/10 08:39:20 imp Exp $");*/
|
---|
[1704] | 43 |
|
---|
[2113] | 44 | #include "config.h"
|
---|
[1704] | 45 | #include <sys/types.h>
|
---|
| 46 | #include <sys/stat.h>
|
---|
| 47 |
|
---|
[1705] | 48 | #include "err.h"
|
---|
[1704] | 49 | #include <errno.h>
|
---|
[3148] | 50 | #include "fts.h"
|
---|
[1704] | 51 | #include <limits.h>
|
---|
| 52 | #include <stdio.h>
|
---|
| 53 | #include <stdlib.h>
|
---|
| 54 | #include <string.h>
|
---|
[2546] | 55 | #ifndef _MSC_VER
|
---|
[1705] | 56 | # include <unistd.h>
|
---|
| 57 | #else
|
---|
| 58 | # include "mscfakes.h"
|
---|
[1710] | 59 | #endif
|
---|
[1711] | 60 | #ifdef __sun__
|
---|
| 61 | # include "solfakes.h"
|
---|
| 62 | #endif
|
---|
[2546] | 63 | #ifdef __HAIKU__
|
---|
| 64 | # include "haikufakes.h"
|
---|
| 65 | #endif
|
---|
[1705] | 66 | #include "getopt.h"
|
---|
| 67 | #include "kmkbuiltin.h"
|
---|
[1704] | 68 |
|
---|
[1710] | 69 | extern void * bsd_setmode(const char *p);
|
---|
| 70 | extern mode_t bsd_getmode(const void *bbox, mode_t omode);
|
---|
| 71 | extern void bsd_strmode(mode_t mode, char *p);
|
---|
| 72 |
|
---|
[2421] | 73 | #if (defined(__APPLE__) && !defined(_DARWIN_FEATURE_UNIX_CONFORMANCE)) || defined(__OpenBSD__)
|
---|
[1705] | 74 | extern int lchmod(const char *, mode_t);
|
---|
| 75 | #endif
|
---|
[1704] | 76 |
|
---|
[3192] | 77 | static int usage(PKMKBUILTINCTX pCtx, int is_err);
|
---|
[1705] | 78 |
|
---|
| 79 | static struct option long_options[] =
|
---|
| 80 | {
|
---|
| 81 | { "help", no_argument, 0, 261 },
|
---|
| 82 | { "version", no_argument, 0, 262 },
|
---|
| 83 | { 0, 0, 0, 0 },
|
---|
| 84 | };
|
---|
| 85 |
|
---|
| 86 |
|
---|
[1704] | 87 | int
|
---|
[3192] | 88 | kmk_builtin_chmod(int argc, char *argv[], char **envp, PKMKBUILTINCTX pCtx)
|
---|
[1704] | 89 | {
|
---|
| 90 | FTS *ftsp;
|
---|
| 91 | FTSENT *p;
|
---|
| 92 | mode_t *set;
|
---|
| 93 | int Hflag, Lflag, Rflag, ch, fflag, fts_options, hflag, rval;
|
---|
| 94 | int vflag;
|
---|
| 95 | char *mode;
|
---|
| 96 | mode_t newmode;
|
---|
| 97 | int (*change_mode)(const char *, mode_t);
|
---|
| 98 |
|
---|
[1705] | 99 | /* kmk: reset getopt and set progname */
|
---|
| 100 | opterr = 1;
|
---|
| 101 | optarg = NULL;
|
---|
| 102 | optopt = 0;
|
---|
| 103 | optind = 0; /* init */
|
---|
| 104 |
|
---|
[1704] | 105 | set = NULL;
|
---|
| 106 | Hflag = Lflag = Rflag = fflag = hflag = vflag = 0;
|
---|
[1705] | 107 | while ((ch = getopt_long(argc, argv, "HLPRXfghorstuvwx", long_options, NULL)) != -1)
|
---|
[1704] | 108 | switch (ch) {
|
---|
| 109 | case 'H':
|
---|
| 110 | Hflag = 1;
|
---|
| 111 | Lflag = 0;
|
---|
| 112 | break;
|
---|
| 113 | case 'L':
|
---|
| 114 | Lflag = 1;
|
---|
| 115 | Hflag = 0;
|
---|
| 116 | break;
|
---|
| 117 | case 'P':
|
---|
| 118 | Hflag = Lflag = 0;
|
---|
| 119 | break;
|
---|
| 120 | case 'R':
|
---|
| 121 | Rflag = 1;
|
---|
| 122 | break;
|
---|
| 123 | case 'f':
|
---|
| 124 | fflag = 1;
|
---|
| 125 | break;
|
---|
| 126 | case 'h':
|
---|
| 127 | /*
|
---|
| 128 | * In System V (and probably POSIX.2) the -h option
|
---|
| 129 | * causes chmod to change the mode of the symbolic
|
---|
| 130 | * link. 4.4BSD's symbolic links didn't have modes,
|
---|
| 131 | * so it was an undocumented noop. In FreeBSD 3.0,
|
---|
| 132 | * lchmod(2) is introduced and this option does real
|
---|
| 133 | * work.
|
---|
| 134 | */
|
---|
| 135 | hflag = 1;
|
---|
| 136 | break;
|
---|
| 137 | /*
|
---|
| 138 | * XXX
|
---|
| 139 | * "-[rwx]" are valid mode commands. If they are the entire
|
---|
| 140 | * argument, getopt has moved past them, so decrement optind.
|
---|
| 141 | * Regardless, we're done argument processing.
|
---|
| 142 | */
|
---|
| 143 | case 'g': case 'o': case 'r': case 's':
|
---|
| 144 | case 't': case 'u': case 'w': case 'X': case 'x':
|
---|
| 145 | if (argv[optind - 1][0] == '-' &&
|
---|
| 146 | argv[optind - 1][1] == ch &&
|
---|
| 147 | argv[optind - 1][2] == '\0')
|
---|
| 148 | --optind;
|
---|
| 149 | goto done;
|
---|
| 150 | case 'v':
|
---|
| 151 | vflag++;
|
---|
| 152 | break;
|
---|
[1705] | 153 | case 261:
|
---|
[3192] | 154 | usage(pCtx, 0);
|
---|
[1705] | 155 | return 0;
|
---|
| 156 | case 262:
|
---|
| 157 | return kbuild_version(argv[0]);
|
---|
[1704] | 158 | case '?':
|
---|
| 159 | default:
|
---|
[3192] | 160 | return usage(pCtx, 1);
|
---|
[1704] | 161 | }
|
---|
| 162 | done: argv += optind;
|
---|
| 163 | argc -= optind;
|
---|
| 164 |
|
---|
| 165 | if (argc < 2)
|
---|
[3192] | 166 | return usage(pCtx, 1);
|
---|
[1704] | 167 |
|
---|
| 168 | if (Rflag) {
|
---|
| 169 | fts_options = FTS_PHYSICAL;
|
---|
| 170 | if (hflag)
|
---|
[3192] | 171 | return errx(pCtx, 1,
|
---|
[1704] | 172 | "the -R and -h options may not be specified together.");
|
---|
| 173 | if (Hflag)
|
---|
| 174 | fts_options |= FTS_COMFOLLOW;
|
---|
| 175 | if (Lflag) {
|
---|
| 176 | fts_options &= ~FTS_PHYSICAL;
|
---|
| 177 | fts_options |= FTS_LOGICAL;
|
---|
| 178 | }
|
---|
| 179 | } else
|
---|
| 180 | fts_options = hflag ? FTS_PHYSICAL : FTS_LOGICAL;
|
---|
| 181 |
|
---|
| 182 | if (hflag)
|
---|
| 183 | change_mode = lchmod;
|
---|
| 184 | else
|
---|
| 185 | change_mode = chmod;
|
---|
| 186 |
|
---|
| 187 | mode = *argv;
|
---|
[1710] | 188 | if ((set = bsd_setmode(mode)) == NULL)
|
---|
[3192] | 189 | return errx(pCtx, 1, "invalid file mode: %s", mode);
|
---|
[1704] | 190 |
|
---|
| 191 | if ((ftsp = fts_open(++argv, fts_options, 0)) == NULL)
|
---|
[3192] | 192 | return err(pCtx, 1, "fts_open");
|
---|
[1704] | 193 | for (rval = 0; (p = fts_read(ftsp)) != NULL;) {
|
---|
| 194 | switch (p->fts_info) {
|
---|
| 195 | case FTS_D: /* Change it at FTS_DP. */
|
---|
| 196 | if (!Rflag)
|
---|
| 197 | fts_set(ftsp, p, FTS_SKIP);
|
---|
| 198 | continue;
|
---|
| 199 | case FTS_DNR: /* Warn, chmod, continue. */
|
---|
[3192] | 200 | warnx(pCtx, "fts: %s: %s", p->fts_path, strerror(p->fts_errno));
|
---|
[1704] | 201 | rval = 1;
|
---|
| 202 | break;
|
---|
| 203 | case FTS_ERR: /* Warn, continue. */
|
---|
| 204 | case FTS_NS:
|
---|
[3192] | 205 | warnx(pCtx, "fts: %s: %s", p->fts_path, strerror(p->fts_errno));
|
---|
[1704] | 206 | rval = 1;
|
---|
| 207 | continue;
|
---|
| 208 | case FTS_SL: /* Ignore. */
|
---|
| 209 | case FTS_SLNONE:
|
---|
| 210 | /*
|
---|
| 211 | * The only symlinks that end up here are ones that
|
---|
| 212 | * don't point to anything and ones that we found
|
---|
| 213 | * doing a physical walk.
|
---|
| 214 | */
|
---|
| 215 | if (!hflag)
|
---|
| 216 | continue;
|
---|
| 217 | /* else */
|
---|
| 218 | /* FALLTHROUGH */
|
---|
| 219 | default:
|
---|
| 220 | break;
|
---|
| 221 | }
|
---|
[1710] | 222 | newmode = bsd_getmode(set, p->fts_statp->st_mode);
|
---|
[1704] | 223 | if ((newmode & ALLPERMS) == (p->fts_statp->st_mode & ALLPERMS))
|
---|
| 224 | continue;
|
---|
| 225 | if ((*change_mode)(p->fts_accpath, newmode) && !fflag) {
|
---|
[3192] | 226 | warn(pCtx, "%schmod: %s", hflag ? "l" : "", p->fts_path);
|
---|
[1704] | 227 | rval = 1;
|
---|
| 228 | } else {
|
---|
| 229 | if (vflag) {
|
---|
[3192] | 230 | kmk_builtin_ctx_printf(pCtx, 0, "%s", p->fts_path);
|
---|
[1704] | 231 |
|
---|
| 232 | if (vflag > 1) {
|
---|
| 233 | char m1[12], m2[12];
|
---|
| 234 |
|
---|
[1710] | 235 | bsd_strmode(p->fts_statp->st_mode, m1);
|
---|
| 236 | bsd_strmode((p->fts_statp->st_mode &
|
---|
[1704] | 237 | S_IFMT) | newmode, m2);
|
---|
| 238 |
|
---|
[3192] | 239 | kmk_builtin_ctx_printf(pCtx, 0, ": 0%o [%s] -> 0%o [%s]",
|
---|
[2126] | 240 | (unsigned int)p->fts_statp->st_mode, m1,
|
---|
| 241 | (unsigned int)((p->fts_statp->st_mode & S_IFMT) | newmode), m2);
|
---|
[1704] | 242 | }
|
---|
[3192] | 243 | kmk_builtin_ctx_printf(pCtx, 0, "\n");
|
---|
[1704] | 244 | }
|
---|
| 245 |
|
---|
| 246 | }
|
---|
| 247 | }
|
---|
| 248 | if (errno)
|
---|
[3192] | 249 | rval = err(pCtx, 1, "fts_read");
|
---|
[1704] | 250 | free(set);
|
---|
[1705] | 251 | fts_close(ftsp);
|
---|
| 252 | return rval;
|
---|
[1704] | 253 | }
|
---|
| 254 |
|
---|
[1705] | 255 | int
|
---|
[3192] | 256 | usage(PKMKBUILTINCTX pCtx, int is_err)
|
---|
[1704] | 257 | {
|
---|
[3192] | 258 | kmk_builtin_ctx_printf(pCtx, is_err,
|
---|
[1705] | 259 | "usage: %s [-fhv] [-R [-H | -L | -P]] mode file ...\n"
|
---|
| 260 | " or: %s --version\n"
|
---|
| 261 | " or: %s --help\n",
|
---|
[3192] | 262 | pCtx->pszProgName, pCtx->pszProgName, pCtx->pszProgName);
|
---|
[1705] | 263 |
|
---|
| 264 | return 1;
|
---|
[1704] | 265 | }
|
---|
[3192] | 266 |
|
---|
| 267 | #ifdef KMK_BUILTIN_STANDALONE
|
---|
| 268 | int main(int argc, char **argv, char **envp)
|
---|
| 269 | {
|
---|
| 270 | KMKBUILTINCTX Ctx = { "kmk_chmod", NULL };
|
---|
| 271 | return kmk_builtin_chmod(argc, argv, envp, &Ctx);
|
---|
| 272 | }
|
---|
| 273 | #endif
|
---|
| 274 |
|
---|