VirtualBox

source: kBuild/trunk/src/kmk/kmkbuiltin/chmod.c@ 3219

Last change on this file since 3219 was 3192, checked in by bird, 6 years ago

kmkbuiltin: funnel output thru output.c (usually via err.c).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.2 KB
Line 
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
32static 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
38static char sccsid[] = "@(#)chmod.c 8.8 (Berkeley) 4/1/94";
39#endif /* not lint */
40#endif
41/*#include <sys/cdefs.h> */
42/*__FBSDID("$FreeBSD: src/bin/chmod/chmod.c,v 1.33 2005/01/10 08:39:20 imp Exp $");*/
43
44#include "config.h"
45#include <sys/types.h>
46#include <sys/stat.h>
47
48#include "err.h"
49#include <errno.h>
50#include "fts.h"
51#include <limits.h>
52#include <stdio.h>
53#include <stdlib.h>
54#include <string.h>
55#ifndef _MSC_VER
56# include <unistd.h>
57#else
58# include "mscfakes.h"
59#endif
60#ifdef __sun__
61# include "solfakes.h"
62#endif
63#ifdef __HAIKU__
64# include "haikufakes.h"
65#endif
66#include "getopt.h"
67#include "kmkbuiltin.h"
68
69extern void * bsd_setmode(const char *p);
70extern mode_t bsd_getmode(const void *bbox, mode_t omode);
71extern void bsd_strmode(mode_t mode, char *p);
72
73#if (defined(__APPLE__) && !defined(_DARWIN_FEATURE_UNIX_CONFORMANCE)) || defined(__OpenBSD__)
74extern int lchmod(const char *, mode_t);
75#endif
76
77static int usage(PKMKBUILTINCTX pCtx, int is_err);
78
79static 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
87int
88kmk_builtin_chmod(int argc, char *argv[], char **envp, PKMKBUILTINCTX pCtx)
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
99 /* kmk: reset getopt and set progname */
100 opterr = 1;
101 optarg = NULL;
102 optopt = 0;
103 optind = 0; /* init */
104
105 set = NULL;
106 Hflag = Lflag = Rflag = fflag = hflag = vflag = 0;
107 while ((ch = getopt_long(argc, argv, "HLPRXfghorstuvwx", long_options, NULL)) != -1)
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;
153 case 261:
154 usage(pCtx, 0);
155 return 0;
156 case 262:
157 return kbuild_version(argv[0]);
158 case '?':
159 default:
160 return usage(pCtx, 1);
161 }
162done: argv += optind;
163 argc -= optind;
164
165 if (argc < 2)
166 return usage(pCtx, 1);
167
168 if (Rflag) {
169 fts_options = FTS_PHYSICAL;
170 if (hflag)
171 return errx(pCtx, 1,
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;
188 if ((set = bsd_setmode(mode)) == NULL)
189 return errx(pCtx, 1, "invalid file mode: %s", mode);
190
191 if ((ftsp = fts_open(++argv, fts_options, 0)) == NULL)
192 return err(pCtx, 1, "fts_open");
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. */
200 warnx(pCtx, "fts: %s: %s", p->fts_path, strerror(p->fts_errno));
201 rval = 1;
202 break;
203 case FTS_ERR: /* Warn, continue. */
204 case FTS_NS:
205 warnx(pCtx, "fts: %s: %s", p->fts_path, strerror(p->fts_errno));
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 }
222 newmode = bsd_getmode(set, p->fts_statp->st_mode);
223 if ((newmode & ALLPERMS) == (p->fts_statp->st_mode & ALLPERMS))
224 continue;
225 if ((*change_mode)(p->fts_accpath, newmode) && !fflag) {
226 warn(pCtx, "%schmod: %s", hflag ? "l" : "", p->fts_path);
227 rval = 1;
228 } else {
229 if (vflag) {
230 kmk_builtin_ctx_printf(pCtx, 0, "%s", p->fts_path);
231
232 if (vflag > 1) {
233 char m1[12], m2[12];
234
235 bsd_strmode(p->fts_statp->st_mode, m1);
236 bsd_strmode((p->fts_statp->st_mode &
237 S_IFMT) | newmode, m2);
238
239 kmk_builtin_ctx_printf(pCtx, 0, ": 0%o [%s] -> 0%o [%s]",
240 (unsigned int)p->fts_statp->st_mode, m1,
241 (unsigned int)((p->fts_statp->st_mode & S_IFMT) | newmode), m2);
242 }
243 kmk_builtin_ctx_printf(pCtx, 0, "\n");
244 }
245
246 }
247 }
248 if (errno)
249 rval = err(pCtx, 1, "fts_read");
250 free(set);
251 fts_close(ftsp);
252 return rval;
253}
254
255int
256usage(PKMKBUILTINCTX pCtx, int is_err)
257{
258 kmk_builtin_ctx_printf(pCtx, is_err,
259 "usage: %s [-fhv] [-R [-H | -L | -P]] mode file ...\n"
260 " or: %s --version\n"
261 " or: %s --help\n",
262 pCtx->pszProgName, pCtx->pszProgName, pCtx->pszProgName);
263
264 return 1;
265}
266
267#ifdef KMK_BUILTIN_STANDALONE
268int 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
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use