Add/fix some HISTORY sections in bin/ manual pages.
[dragonfly.git] / bin / chmod / chmod.c
blob8c0dc71f0c09a751c7d634a9c0ac65d220d8cfd6
1 /*-
2 * Copyright (c) 1989, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
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 * 3. 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.
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.
29 * @(#) Copyright (c) 1989, 1993, 1994 The Regents of the University of California. All rights reserved.
30 * @(#)chmod.c 8.8 (Berkeley) 4/1/94
31 * $FreeBSD: head/bin/chmod/chmod.c 283997 2015-06-04 19:18:58Z pluknet $
34 #include <sys/param.h>
35 #include <sys/stat.h>
37 #include <err.h>
38 #include <errno.h>
39 #include <fcntl.h>
40 #include <fts.h>
41 #include <limits.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
47 static void usage(void);
49 int
50 main(int argc, char *argv[])
52 FTS *ftsp;
53 FTSENT *p;
54 void *set;
55 int Hflag, Lflag, Rflag, ch, fflag;
56 int fts_options, hflag, rval, vflag;
57 char *mode;
58 mode_t newmode;
60 Hflag = Lflag = Rflag = fflag = hflag = vflag = 0;
61 while ((ch = getopt(argc, argv, "HLPRXfghorstuvwx")) != -1)
62 switch (ch) {
63 case 'H':
64 Hflag = 1;
65 Lflag = 0;
66 break;
67 case 'L':
68 Lflag = 1;
69 Hflag = 0;
70 break;
71 case 'P':
72 Hflag = Lflag = 0;
73 break;
74 case 'R':
75 Rflag = 1;
76 break;
77 case 'f':
78 fflag = 1;
79 break;
80 case 'h':
82 * In System V (and probably POSIX.2) the -h option
83 * causes chmod to change the mode of the symbolic
84 * link. 4.4BSD's symbolic links didn't have modes,
85 * so it was an undocumented noop. In FreeBSD 3.0,
86 * lchmod(2) is introduced and this option does real
87 * work.
89 hflag = 1;
90 break;
92 * XXX
93 * "-[rwx]" are valid mode commands. If they are the entire
94 * argument, getopt has moved past them, so decrement optind.
95 * Regardless, we're done argument processing.
97 case 'g': case 'o': case 'r': case 's':
98 case 't': case 'u': case 'w': case 'X': case 'x':
99 if (argv[optind - 1][0] == '-' &&
100 argv[optind - 1][1] == ch &&
101 argv[optind - 1][2] == '\0')
102 --optind;
103 goto done;
104 case 'v':
105 vflag++;
106 break;
107 case '?':
108 default:
109 usage();
111 done: argv += optind;
112 argc -= optind;
114 if (argc < 2)
115 usage();
117 if (Rflag) {
118 if (hflag)
119 errx(1, "the -R and -h options may not be "
120 "specified together.");
121 if (Lflag) {
122 fts_options = FTS_LOGICAL;
123 } else {
124 fts_options = FTS_PHYSICAL;
126 if (Hflag) {
127 fts_options |= FTS_COMFOLLOW;
130 } else if (hflag) {
131 fts_options = FTS_PHYSICAL;
132 } else {
133 fts_options = FTS_LOGICAL;
136 mode = *argv;
137 errno = 0;
138 if ((set = setmode(mode)) == NULL) {
139 if (!errno)
140 errx(1, "invalid file mode: %s", mode);
141 else
142 /* malloc for setmode() failed */
143 err(1, "setmode failed");
146 if ((ftsp = fts_open(++argv, fts_options, 0)) == NULL)
147 err(1, "fts_open");
148 for (rval = 0; (p = fts_read(ftsp)) != NULL;) {
149 int atflag;
151 if ((fts_options & FTS_LOGICAL) ||
152 ((fts_options & FTS_COMFOLLOW) &&
153 p->fts_level == FTS_ROOTLEVEL))
154 atflag = 0;
155 else
156 atflag = AT_SYMLINK_NOFOLLOW;
158 switch (p->fts_info) {
159 case FTS_D:
160 if (!Rflag)
161 fts_set(ftsp, p, FTS_SKIP);
162 break;
163 case FTS_DNR: /* Warn, chmod. */
164 warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
165 rval = 1;
166 break;
167 case FTS_DP: /* Already changed at FTS_D. */
168 continue;
169 case FTS_ERR: /* Warn, continue. */
170 case FTS_NS:
171 warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
172 rval = 1;
173 continue;
174 default:
175 break;
177 newmode = getmode(set, p->fts_statp->st_mode);
178 if ((newmode & ALLPERMS) == (p->fts_statp->st_mode & ALLPERMS))
179 continue;
180 if (fchmodat(AT_FDCWD, p->fts_accpath, newmode, atflag) == -1
181 && !fflag) {
182 warn("%s", p->fts_path);
183 rval = 1;
184 } else if (vflag) {
185 printf("%s", p->fts_path);
187 if (vflag > 1) {
188 char m1[12], m2[12];
190 strmode(p->fts_statp->st_mode, m1);
191 strmode((p->fts_statp->st_mode &
192 S_IFMT) | newmode, m2);
193 printf(": 0%o [%s] -> 0%o [%s]",
194 p->fts_statp->st_mode, m1,
195 (p->fts_statp->st_mode & S_IFMT) |
196 newmode, m2);
198 printf("\n");
201 if (errno)
202 err(1, "fts_read");
203 exit(rval);
206 static void
207 usage(void)
209 fprintf(stderr,
210 "usage: chmod [-fhv] [-R [-H | -L | -P]] mode file ...\n");
211 exit(1);