VirtualBox

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

Last change on this file since 2134 was 2126, checked in by bird, 16 years ago

chmod.c: warnings.

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