[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 |
|
---|
[3220] | 44 | /*********************************************************************************************************************************
|
---|
| 45 | * Header Files *
|
---|
| 46 | *********************************************************************************************************************************/
|
---|
| 47 | #define FAKES_NO_GETOPT_H /* bird */
|
---|
[2113] | 48 | #include "config.h"
|
---|
[1704] | 49 | #include <sys/types.h>
|
---|
| 50 | #include <sys/stat.h>
|
---|
| 51 |
|
---|
[1705] | 52 | #include "err.h"
|
---|
[1704] | 53 | #include <errno.h>
|
---|
[3148] | 54 | #include "fts.h"
|
---|
[1704] | 55 | #include <limits.h>
|
---|
| 56 | #include <stdio.h>
|
---|
| 57 | #include <stdlib.h>
|
---|
| 58 | #include <string.h>
|
---|
[2546] | 59 | #ifndef _MSC_VER
|
---|
[1705] | 60 | # include <unistd.h>
|
---|
| 61 | #else
|
---|
| 62 | # include "mscfakes.h"
|
---|
[1710] | 63 | #endif
|
---|
[1711] | 64 | #ifdef __sun__
|
---|
| 65 | # include "solfakes.h"
|
---|
| 66 | #endif
|
---|
[2546] | 67 | #ifdef __HAIKU__
|
---|
| 68 | # include "haikufakes.h"
|
---|
| 69 | #endif
|
---|
[3220] | 70 | #include "getopt_r.h"
|
---|
[1705] | 71 | #include "kmkbuiltin.h"
|
---|
[1704] | 72 |
|
---|
[3220] | 73 |
|
---|
| 74 | /*********************************************************************************************************************************
|
---|
| 75 | * Global Variables *
|
---|
| 76 | *********************************************************************************************************************************/
|
---|
| 77 | static struct option long_options[] =
|
---|
| 78 | {
|
---|
| 79 | { "help", no_argument, 0, 261 },
|
---|
| 80 | { "version", no_argument, 0, 262 },
|
---|
| 81 | { 0, 0, 0, 0 },
|
---|
| 82 | };
|
---|
| 83 |
|
---|
| 84 |
|
---|
| 85 | /*********************************************************************************************************************************
|
---|
| 86 | * Internal Functions *
|
---|
| 87 | *********************************************************************************************************************************/
|
---|
[1710] | 88 | extern void * bsd_setmode(const char *p);
|
---|
| 89 | extern mode_t bsd_getmode(const void *bbox, mode_t omode);
|
---|
| 90 | extern void bsd_strmode(mode_t mode, char *p);
|
---|
| 91 |
|
---|
[2421] | 92 | #if (defined(__APPLE__) && !defined(_DARWIN_FEATURE_UNIX_CONFORMANCE)) || defined(__OpenBSD__)
|
---|
[1705] | 93 | extern int lchmod(const char *, mode_t);
|
---|
| 94 | #endif
|
---|
[1704] | 95 |
|
---|
[3192] | 96 | static int usage(PKMKBUILTINCTX pCtx, int is_err);
|
---|
[1705] | 97 |
|
---|
| 98 |
|
---|
[1704] | 99 | int
|
---|
[3192] | 100 | kmk_builtin_chmod(int argc, char *argv[], char **envp, PKMKBUILTINCTX pCtx)
|
---|
[1704] | 101 | {
|
---|
[3220] | 102 | struct getopt_state_r gos;
|
---|
[1704] | 103 | FTS *ftsp;
|
---|
| 104 | FTSENT *p;
|
---|
| 105 | mode_t *set;
|
---|
| 106 | int Hflag, Lflag, Rflag, ch, fflag, fts_options, hflag, rval;
|
---|
| 107 | int vflag;
|
---|
| 108 | char *mode;
|
---|
| 109 | mode_t newmode;
|
---|
| 110 | int (*change_mode)(const char *, mode_t);
|
---|
| 111 |
|
---|
| 112 | set = NULL;
|
---|
| 113 | Hflag = Lflag = Rflag = fflag = hflag = vflag = 0;
|
---|
[3220] | 114 |
|
---|
| 115 | getopt_initialize_r(&gos, argc, argv, "HLPRXfghorstuvwx", long_options, envp, pCtx);
|
---|
| 116 | while ((ch = getopt_long_r(&gos, NULL)) != -1)
|
---|
[1704] | 117 | switch (ch) {
|
---|
| 118 | case 'H':
|
---|
| 119 | Hflag = 1;
|
---|
| 120 | Lflag = 0;
|
---|
| 121 | break;
|
---|
| 122 | case 'L':
|
---|
| 123 | Lflag = 1;
|
---|
| 124 | Hflag = 0;
|
---|
| 125 | break;
|
---|
| 126 | case 'P':
|
---|
| 127 | Hflag = Lflag = 0;
|
---|
| 128 | break;
|
---|
| 129 | case 'R':
|
---|
| 130 | Rflag = 1;
|
---|
| 131 | break;
|
---|
| 132 | case 'f':
|
---|
| 133 | fflag = 1;
|
---|
| 134 | break;
|
---|
| 135 | case 'h':
|
---|
| 136 | /*
|
---|
| 137 | * In System V (and probably POSIX.2) the -h option
|
---|
| 138 | * causes chmod to change the mode of the symbolic
|
---|
| 139 | * link. 4.4BSD's symbolic links didn't have modes,
|
---|
| 140 | * so it was an undocumented noop. In FreeBSD 3.0,
|
---|
| 141 | * lchmod(2) is introduced and this option does real
|
---|
| 142 | * work.
|
---|
| 143 | */
|
---|
| 144 | hflag = 1;
|
---|
| 145 | break;
|
---|
| 146 | /*
|
---|
| 147 | * XXX
|
---|
| 148 | * "-[rwx]" are valid mode commands. If they are the entire
|
---|
| 149 | * argument, getopt has moved past them, so decrement optind.
|
---|
| 150 | * Regardless, we're done argument processing.
|
---|
| 151 | */
|
---|
| 152 | case 'g': case 'o': case 'r': case 's':
|
---|
| 153 | case 't': case 'u': case 'w': case 'X': case 'x':
|
---|
[3220] | 154 | if (argv[gos.optind - 1][0] == '-' &&
|
---|
| 155 | argv[gos.optind - 1][1] == ch &&
|
---|
| 156 | argv[gos.optind - 1][2] == '\0')
|
---|
| 157 | --gos.optind;
|
---|
[1704] | 158 | goto done;
|
---|
| 159 | case 'v':
|
---|
| 160 | vflag++;
|
---|
| 161 | break;
|
---|
[1705] | 162 | case 261:
|
---|
[3192] | 163 | usage(pCtx, 0);
|
---|
[1705] | 164 | return 0;
|
---|
| 165 | case 262:
|
---|
| 166 | return kbuild_version(argv[0]);
|
---|
[1704] | 167 | case '?':
|
---|
| 168 | default:
|
---|
[3192] | 169 | return usage(pCtx, 1);
|
---|
[1704] | 170 | }
|
---|
[3220] | 171 | done: argv += gos.optind;
|
---|
| 172 | argc -= gos.optind;
|
---|
[1704] | 173 |
|
---|
| 174 | if (argc < 2)
|
---|
[3192] | 175 | return usage(pCtx, 1);
|
---|
[1704] | 176 |
|
---|
| 177 | if (Rflag) {
|
---|
| 178 | fts_options = FTS_PHYSICAL;
|
---|
| 179 | if (hflag)
|
---|
[3192] | 180 | return errx(pCtx, 1,
|
---|
[1704] | 181 | "the -R and -h options may not be specified together.");
|
---|
| 182 | if (Hflag)
|
---|
| 183 | fts_options |= FTS_COMFOLLOW;
|
---|
| 184 | if (Lflag) {
|
---|
| 185 | fts_options &= ~FTS_PHYSICAL;
|
---|
| 186 | fts_options |= FTS_LOGICAL;
|
---|
| 187 | }
|
---|
| 188 | } else
|
---|
| 189 | fts_options = hflag ? FTS_PHYSICAL : FTS_LOGICAL;
|
---|
[3220] | 190 | #ifndef KMK_BUILTIN_STANDALONE
|
---|
| 191 | fts_options |= FTS_NOCHDIR; /* Don't change the CWD while inside kmk. */
|
---|
| 192 | #endif
|
---|
[1704] | 193 |
|
---|
| 194 | if (hflag)
|
---|
| 195 | change_mode = lchmod;
|
---|
| 196 | else
|
---|
| 197 | change_mode = chmod;
|
---|
| 198 |
|
---|
| 199 | mode = *argv;
|
---|
[1710] | 200 | if ((set = bsd_setmode(mode)) == NULL)
|
---|
[3192] | 201 | return errx(pCtx, 1, "invalid file mode: %s", mode);
|
---|
[1704] | 202 |
|
---|
| 203 | if ((ftsp = fts_open(++argv, fts_options, 0)) == NULL)
|
---|
[3192] | 204 | return err(pCtx, 1, "fts_open");
|
---|
[1704] | 205 | for (rval = 0; (p = fts_read(ftsp)) != NULL;) {
|
---|
| 206 | switch (p->fts_info) {
|
---|
| 207 | case FTS_D: /* Change it at FTS_DP. */
|
---|
| 208 | if (!Rflag)
|
---|
| 209 | fts_set(ftsp, p, FTS_SKIP);
|
---|
| 210 | continue;
|
---|
| 211 | case FTS_DNR: /* Warn, chmod, continue. */
|
---|
[3192] | 212 | warnx(pCtx, "fts: %s: %s", p->fts_path, strerror(p->fts_errno));
|
---|
[1704] | 213 | rval = 1;
|
---|
| 214 | break;
|
---|
| 215 | case FTS_ERR: /* Warn, continue. */
|
---|
| 216 | case FTS_NS:
|
---|
[3192] | 217 | warnx(pCtx, "fts: %s: %s", p->fts_path, strerror(p->fts_errno));
|
---|
[1704] | 218 | rval = 1;
|
---|
| 219 | continue;
|
---|
| 220 | case FTS_SL: /* Ignore. */
|
---|
| 221 | case FTS_SLNONE:
|
---|
| 222 | /*
|
---|
| 223 | * The only symlinks that end up here are ones that
|
---|
| 224 | * don't point to anything and ones that we found
|
---|
| 225 | * doing a physical walk.
|
---|
| 226 | */
|
---|
| 227 | if (!hflag)
|
---|
| 228 | continue;
|
---|
| 229 | /* else */
|
---|
| 230 | /* FALLTHROUGH */
|
---|
| 231 | default:
|
---|
| 232 | break;
|
---|
| 233 | }
|
---|
[1710] | 234 | newmode = bsd_getmode(set, p->fts_statp->st_mode);
|
---|
[1704] | 235 | if ((newmode & ALLPERMS) == (p->fts_statp->st_mode & ALLPERMS))
|
---|
| 236 | continue;
|
---|
| 237 | if ((*change_mode)(p->fts_accpath, newmode) && !fflag) {
|
---|
[3192] | 238 | warn(pCtx, "%schmod: %s", hflag ? "l" : "", p->fts_path);
|
---|
[1704] | 239 | rval = 1;
|
---|
| 240 | } else {
|
---|
| 241 | if (vflag) {
|
---|
[3192] | 242 | kmk_builtin_ctx_printf(pCtx, 0, "%s", p->fts_path);
|
---|
[1704] | 243 |
|
---|
| 244 | if (vflag > 1) {
|
---|
| 245 | char m1[12], m2[12];
|
---|
| 246 |
|
---|
[1710] | 247 | bsd_strmode(p->fts_statp->st_mode, m1);
|
---|
| 248 | bsd_strmode((p->fts_statp->st_mode &
|
---|
[1704] | 249 | S_IFMT) | newmode, m2);
|
---|
| 250 |
|
---|
[3192] | 251 | kmk_builtin_ctx_printf(pCtx, 0, ": 0%o [%s] -> 0%o [%s]",
|
---|
[2126] | 252 | (unsigned int)p->fts_statp->st_mode, m1,
|
---|
| 253 | (unsigned int)((p->fts_statp->st_mode & S_IFMT) | newmode), m2);
|
---|
[1704] | 254 | }
|
---|
[3192] | 255 | kmk_builtin_ctx_printf(pCtx, 0, "\n");
|
---|
[1704] | 256 | }
|
---|
| 257 |
|
---|
| 258 | }
|
---|
| 259 | }
|
---|
| 260 | if (errno)
|
---|
[3192] | 261 | rval = err(pCtx, 1, "fts_read");
|
---|
[1704] | 262 | free(set);
|
---|
[1705] | 263 | fts_close(ftsp);
|
---|
| 264 | return rval;
|
---|
[1704] | 265 | }
|
---|
| 266 |
|
---|
[1705] | 267 | int
|
---|
[3192] | 268 | usage(PKMKBUILTINCTX pCtx, int is_err)
|
---|
[1704] | 269 | {
|
---|
[3192] | 270 | kmk_builtin_ctx_printf(pCtx, is_err,
|
---|
[1705] | 271 | "usage: %s [-fhv] [-R [-H | -L | -P]] mode file ...\n"
|
---|
| 272 | " or: %s --version\n"
|
---|
| 273 | " or: %s --help\n",
|
---|
[3192] | 274 | pCtx->pszProgName, pCtx->pszProgName, pCtx->pszProgName);
|
---|
[1705] | 275 |
|
---|
| 276 | return 1;
|
---|
[1704] | 277 | }
|
---|
[3192] | 278 |
|
---|
| 279 | #ifdef KMK_BUILTIN_STANDALONE
|
---|
[3389] | 280 | mode_t g_fUMask;
|
---|
[3192] | 281 | int main(int argc, char **argv, char **envp)
|
---|
| 282 | {
|
---|
| 283 | KMKBUILTINCTX Ctx = { "kmk_chmod", NULL };
|
---|
[3389] | 284 | umask(g_fUMask = umask(0077));
|
---|
[3192] | 285 | return kmk_builtin_chmod(argc, argv, envp, &Ctx);
|
---|
| 286 | }
|
---|
| 287 | #endif
|
---|
| 288 |
|
---|