VirtualBox

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

Last change on this file since 1843 was 1711, checked in by bird, 16 years ago

kmk_chmod: made it build on solaris (no lchmod or ALLPERMS).

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette