source: trunk/src/kmk/kmkbuiltin/chmod.c@ 1707

Last change on this file since 1707 was 1705, checked in by bird, 17 years ago

kmk_chmod: Ported chmod. Fixes #41.

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