VirtualBox

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

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

kmk: Make sure we use our fts.h, never the system one (except OS/2).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.8 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(FILE *);
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)
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 g_progname = argv[0];
101 opterr = 1;
102 optarg = NULL;
103 optopt = 0;
104 optind = 0; /* init */
105
106 set = NULL;
107 Hflag = Lflag = Rflag = fflag = hflag = vflag = 0;
108 while ((ch = getopt_long(argc, argv, "HLPRXfghorstuvwx", long_options, NULL)) != -1)
109 switch (ch) {
110 case 'H':
111 Hflag = 1;
112 Lflag = 0;
113 break;
114 case 'L':
115 Lflag = 1;
116 Hflag = 0;
117 break;
118 case 'P':
119 Hflag = Lflag = 0;
120 break;
121 case 'R':
122 Rflag = 1;
123 break;
124 case 'f':
125 fflag = 1;
126 break;
127 case 'h':
128 /*
129 * In System V (and probably POSIX.2) the -h option
130 * causes chmod to change the mode of the symbolic
131 * link. 4.4BSD's symbolic links didn't have modes,
132 * so it was an undocumented noop. In FreeBSD 3.0,
133 * lchmod(2) is introduced and this option does real
134 * work.
135 */
136 hflag = 1;
137 break;
138 /*
139 * XXX
140 * "-[rwx]" are valid mode commands. If they are the entire
141 * argument, getopt has moved past them, so decrement optind.
142 * Regardless, we're done argument processing.
143 */
144 case 'g': case 'o': case 'r': case 's':
145 case 't': case 'u': case 'w': case 'X': case 'x':
146 if (argv[optind - 1][0] == '-' &&
147 argv[optind - 1][1] == ch &&
148 argv[optind - 1][2] == '\0')
149 --optind;
150 goto done;
151 case 'v':
152 vflag++;
153 break;
154 case 261:
155 usage(stdout);
156 return 0;
157 case 262:
158 return kbuild_version(argv[0]);
159 case '?':
160 default:
161 return usage(stderr);
162 }
163done: argv += optind;
164 argc -= optind;
165
166 if (argc < 2)
167 return usage(stderr);
168
169 if (Rflag) {
170 fts_options = FTS_PHYSICAL;
171 if (hflag)
172 return errx(1,
173 "the -R and -h options may not be specified together.");
174 if (Hflag)
175 fts_options |= FTS_COMFOLLOW;
176 if (Lflag) {
177 fts_options &= ~FTS_PHYSICAL;
178 fts_options |= FTS_LOGICAL;
179 }
180 } else
181 fts_options = hflag ? FTS_PHYSICAL : FTS_LOGICAL;
182
183 if (hflag)
184 change_mode = lchmod;
185 else
186 change_mode = chmod;
187
188 mode = *argv;
189 if ((set = bsd_setmode(mode)) == NULL)
190 return errx(1, "invalid file mode: %s", mode);
191
192 if ((ftsp = fts_open(++argv, fts_options, 0)) == NULL)
193 return err(1, "fts_open");
194 for (rval = 0; (p = fts_read(ftsp)) != NULL;) {
195 switch (p->fts_info) {
196 case FTS_D: /* Change it at FTS_DP. */
197 if (!Rflag)
198 fts_set(ftsp, p, FTS_SKIP);
199 continue;
200 case FTS_DNR: /* Warn, chmod, continue. */
201 warnx("fts: %s: %s", p->fts_path, strerror(p->fts_errno));
202 rval = 1;
203 break;
204 case FTS_ERR: /* Warn, continue. */
205 case FTS_NS:
206 warnx("fts: %s: %s", p->fts_path, strerror(p->fts_errno));
207 rval = 1;
208 continue;
209 case FTS_SL: /* Ignore. */
210 case FTS_SLNONE:
211 /*
212 * The only symlinks that end up here are ones that
213 * don't point to anything and ones that we found
214 * doing a physical walk.
215 */
216 if (!hflag)
217 continue;
218 /* else */
219 /* FALLTHROUGH */
220 default:
221 break;
222 }
223 newmode = bsd_getmode(set, p->fts_statp->st_mode);
224 if ((newmode & ALLPERMS) == (p->fts_statp->st_mode & ALLPERMS))
225 continue;
226 if ((*change_mode)(p->fts_accpath, newmode) && !fflag) {
227 warn("%schmod: %s", hflag ? "l" : "", p->fts_path);
228 rval = 1;
229 } else {
230 if (vflag) {
231 (void)printf("%s", p->fts_path);
232
233 if (vflag > 1) {
234 char m1[12], m2[12];
235
236 bsd_strmode(p->fts_statp->st_mode, m1);
237 bsd_strmode((p->fts_statp->st_mode &
238 S_IFMT) | newmode, m2);
239
240 (void)printf(": 0%o [%s] -> 0%o [%s]",
241 (unsigned int)p->fts_statp->st_mode, m1,
242 (unsigned int)((p->fts_statp->st_mode & S_IFMT) | newmode), m2);
243 }
244 (void)printf("\n");
245 }
246
247 }
248 }
249 if (errno)
250 rval = err(1, "fts_read");
251 free(set);
252 fts_close(ftsp);
253 return rval;
254}
255
256int
257usage(FILE *out)
258{
259 (void)fprintf(out,
260 "usage: %s [-fhv] [-R [-H | -L | -P]] mode file ...\n"
261 " or: %s --version\n"
262 " or: %s --help\n",
263 g_progname, g_progname, g_progname);
264
265 return 1;
266}
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use