VirtualBox

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

最後變更 在這個檔案從1710是 1710,由 bird 提交於 16 年 前

kmk: Made chmod build on windows. Some cleanup of the bsdisms.

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

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