VirtualBox

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

Last change on this file since 3387 was 3220, checked in by bird, 6 years ago

kmk_chmod: converted to getopt_r.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.5 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/*********************************************************************************************************************************
45* Header Files *
46*********************************************************************************************************************************/
47#define FAKES_NO_GETOPT_H /* bird */
48#include "config.h"
49#include <sys/types.h>
50#include <sys/stat.h>
51
52#include "err.h"
53#include <errno.h>
54#include "fts.h"
55#include <limits.h>
56#include <stdio.h>
57#include <stdlib.h>
58#include <string.h>
59#ifndef _MSC_VER
60# include <unistd.h>
61#else
62# include "mscfakes.h"
63#endif
64#ifdef __sun__
65# include "solfakes.h"
66#endif
67#ifdef __HAIKU__
68# include "haikufakes.h"
69#endif
70#include "getopt_r.h"
71#include "kmkbuiltin.h"
72
73
74/*********************************************************************************************************************************
75* Global Variables *
76*********************************************************************************************************************************/
77static 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*********************************************************************************************************************************/
88extern void * bsd_setmode(const char *p);
89extern mode_t bsd_getmode(const void *bbox, mode_t omode);
90extern void bsd_strmode(mode_t mode, char *p);
91
92#if (defined(__APPLE__) && !defined(_DARWIN_FEATURE_UNIX_CONFORMANCE)) || defined(__OpenBSD__)
93extern int lchmod(const char *, mode_t);
94#endif
95
96static int usage(PKMKBUILTINCTX pCtx, int is_err);
97
98
99int
100kmk_builtin_chmod(int argc, char *argv[], char **envp, PKMKBUILTINCTX pCtx)
101{
102 struct getopt_state_r gos;
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;
114
115 getopt_initialize_r(&gos, argc, argv, "HLPRXfghorstuvwx", long_options, envp, pCtx);
116 while ((ch = getopt_long_r(&gos, NULL)) != -1)
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':
154 if (argv[gos.optind - 1][0] == '-' &&
155 argv[gos.optind - 1][1] == ch &&
156 argv[gos.optind - 1][2] == '\0')
157 --gos.optind;
158 goto done;
159 case 'v':
160 vflag++;
161 break;
162 case 261:
163 usage(pCtx, 0);
164 return 0;
165 case 262:
166 return kbuild_version(argv[0]);
167 case '?':
168 default:
169 return usage(pCtx, 1);
170 }
171done: argv += gos.optind;
172 argc -= gos.optind;
173
174 if (argc < 2)
175 return usage(pCtx, 1);
176
177 if (Rflag) {
178 fts_options = FTS_PHYSICAL;
179 if (hflag)
180 return errx(pCtx, 1,
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;
190#ifndef KMK_BUILTIN_STANDALONE
191 fts_options |= FTS_NOCHDIR; /* Don't change the CWD while inside kmk. */
192#endif
193
194 if (hflag)
195 change_mode = lchmod;
196 else
197 change_mode = chmod;
198
199 mode = *argv;
200 if ((set = bsd_setmode(mode)) == NULL)
201 return errx(pCtx, 1, "invalid file mode: %s", mode);
202
203 if ((ftsp = fts_open(++argv, fts_options, 0)) == NULL)
204 return err(pCtx, 1, "fts_open");
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. */
212 warnx(pCtx, "fts: %s: %s", p->fts_path, strerror(p->fts_errno));
213 rval = 1;
214 break;
215 case FTS_ERR: /* Warn, continue. */
216 case FTS_NS:
217 warnx(pCtx, "fts: %s: %s", p->fts_path, strerror(p->fts_errno));
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 }
234 newmode = bsd_getmode(set, p->fts_statp->st_mode);
235 if ((newmode & ALLPERMS) == (p->fts_statp->st_mode & ALLPERMS))
236 continue;
237 if ((*change_mode)(p->fts_accpath, newmode) && !fflag) {
238 warn(pCtx, "%schmod: %s", hflag ? "l" : "", p->fts_path);
239 rval = 1;
240 } else {
241 if (vflag) {
242 kmk_builtin_ctx_printf(pCtx, 0, "%s", p->fts_path);
243
244 if (vflag > 1) {
245 char m1[12], m2[12];
246
247 bsd_strmode(p->fts_statp->st_mode, m1);
248 bsd_strmode((p->fts_statp->st_mode &
249 S_IFMT) | newmode, m2);
250
251 kmk_builtin_ctx_printf(pCtx, 0, ": 0%o [%s] -> 0%o [%s]",
252 (unsigned int)p->fts_statp->st_mode, m1,
253 (unsigned int)((p->fts_statp->st_mode & S_IFMT) | newmode), m2);
254 }
255 kmk_builtin_ctx_printf(pCtx, 0, "\n");
256 }
257
258 }
259 }
260 if (errno)
261 rval = err(pCtx, 1, "fts_read");
262 free(set);
263 fts_close(ftsp);
264 return rval;
265}
266
267int
268usage(PKMKBUILTINCTX pCtx, int is_err)
269{
270 kmk_builtin_ctx_printf(pCtx, is_err,
271 "usage: %s [-fhv] [-R [-H | -L | -P]] mode file ...\n"
272 " or: %s --version\n"
273 " or: %s --help\n",
274 pCtx->pszProgName, pCtx->pszProgName, pCtx->pszProgName);
275
276 return 1;
277}
278
279#ifdef KMK_BUILTIN_STANDALONE
280int main(int argc, char **argv, char **envp)
281{
282 KMKBUILTINCTX Ctx = { "kmk_chmod", NULL };
283 return kmk_builtin_chmod(argc, argv, envp, &Ctx);
284}
285#endif
286
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use