chmod.c revision 77333
11556Srgrimes/*
21556Srgrimes * Copyright (c) 1989, 1993, 1994
31556Srgrimes *	The Regents of the University of California.  All rights reserved.
41556Srgrimes *
51556Srgrimes * Redistribution and use in source and binary forms, with or without
61556Srgrimes * modification, are permitted provided that the following conditions
71556Srgrimes * are met:
81556Srgrimes * 1. Redistributions of source code must retain the above copyright
91556Srgrimes *    notice, this list of conditions and the following disclaimer.
101556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111556Srgrimes *    notice, this list of conditions and the following disclaimer in the
121556Srgrimes *    documentation and/or other materials provided with the distribution.
131556Srgrimes * 3. All advertising materials mentioning features or use of this software
141556Srgrimes *    must display the following acknowledgement:
151556Srgrimes *	This product includes software developed by the University of
161556Srgrimes *	California, Berkeley and its contributors.
171556Srgrimes * 4. Neither the name of the University nor the names of its contributors
181556Srgrimes *    may be used to endorse or promote products derived from this software
191556Srgrimes *    without specific prior written permission.
201556Srgrimes *
211556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241556Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311556Srgrimes * SUCH DAMAGE.
321556Srgrimes */
331556Srgrimes
341556Srgrimes#ifndef lint
3520411Sstevestatic char const copyright[] =
361556Srgrimes"@(#) Copyright (c) 1989, 1993, 1994\n\
371556Srgrimes	The Regents of the University of California.  All rights reserved.\n";
381556Srgrimes#endif /* not lint */
391556Srgrimes
401556Srgrimes#ifndef lint
4135773Scharnier#if 0
4236002Scharnierstatic char sccsid[] = "@(#)chmod.c	8.8 (Berkeley) 4/1/94";
4335773Scharnier#endif
4435773Scharnierstatic const char rcsid[] =
4550471Speter  "$FreeBSD: head/bin/chmod/chmod.c 77333 2001-05-28 12:58:10Z ru $";
461556Srgrimes#endif /* not lint */
471556Srgrimes
4836002Scharnier#include <sys/types.h>
491556Srgrimes#include <sys/stat.h>
501556Srgrimes
511556Srgrimes#include <err.h>
521556Srgrimes#include <errno.h>
531556Srgrimes#include <fts.h>
546170Sbde#include <limits.h>
551556Srgrimes#include <stdio.h>
561556Srgrimes#include <stdlib.h>
571556Srgrimes#include <string.h>
581556Srgrimes#include <unistd.h>
591556Srgrimes
6076871Skrisint main __P((int, char *[]));
611556Srgrimesvoid usage __P((void));
621556Srgrimes
631556Srgrimesint
641556Srgrimesmain(argc, argv)
651556Srgrimes	int argc;
661556Srgrimes	char *argv[];
671556Srgrimes{
681556Srgrimes	FTS *ftsp;
691556Srgrimes	FTSENT *p;
701556Srgrimes	mode_t *set;
711556Srgrimes	long val;
721556Srgrimes	int oct, omode;
731556Srgrimes	int Hflag, Lflag, Pflag, Rflag, ch, fflag, fts_options, hflag, rval;
7453780Sobrien	int vflag;
751556Srgrimes	char *ep, *mode;
7664013Speter	int newmode;
771556Srgrimes
787165Sjoerg	set = NULL;
797165Sjoerg	omode = 0;
8053824Sobrien	Hflag = Lflag = Pflag = Rflag = fflag = hflag = vflag = 0;
8153780Sobrien	while ((ch = getopt(argc, argv, "HLPRXfgorstuvwx")) != -1)
821556Srgrimes		switch (ch) {
831556Srgrimes		case 'H':
841556Srgrimes			Hflag = 1;
851556Srgrimes			Lflag = Pflag = 0;
861556Srgrimes			break;
871556Srgrimes		case 'L':
881556Srgrimes			Lflag = 1;
891556Srgrimes			Hflag = Pflag = 0;
901556Srgrimes			break;
911556Srgrimes		case 'P':
921556Srgrimes			Pflag = 1;
931556Srgrimes			Hflag = Lflag = 0;
941556Srgrimes			break;
951556Srgrimes		case 'R':
961556Srgrimes			Rflag = 1;
971556Srgrimes			break;
9849544Schris		case 'f':
991556Srgrimes			fflag = 1;
1001556Srgrimes			break;
1011556Srgrimes		case 'h':
1021556Srgrimes			/*
1031556Srgrimes			 * In System V (and probably POSIX.2) the -h option
1041556Srgrimes			 * causes chmod to change the mode of the symbolic
1051556Srgrimes			 * link.  4.4BSD's symbolic links don't have modes,
1061556Srgrimes			 * so it's an undocumented noop.  Do syntax checking,
1071556Srgrimes			 * though.
1081556Srgrimes			 */
1091556Srgrimes			hflag = 1;
1101556Srgrimes			break;
1111556Srgrimes		/*
1121556Srgrimes		 * XXX
1131556Srgrimes		 * "-[rwx]" are valid mode commands.  If they are the entire
1141556Srgrimes		 * argument, getopt has moved past them, so decrement optind.
1151556Srgrimes		 * Regardless, we're done argument processing.
1161556Srgrimes		 */
1171556Srgrimes		case 'g': case 'o': case 'r': case 's':
1181556Srgrimes		case 't': case 'u': case 'w': case 'X': case 'x':
1191556Srgrimes			if (argv[optind - 1][0] == '-' &&
1201556Srgrimes			    argv[optind - 1][1] == ch &&
1211556Srgrimes			    argv[optind - 1][2] == '\0')
1221556Srgrimes				--optind;
1231556Srgrimes			goto done;
12453780Sobrien		case 'v':
12553780Sobrien			vflag = 1;
12653780Sobrien			break;
1271556Srgrimes		case '?':
1281556Srgrimes		default:
1291556Srgrimes			usage();
1301556Srgrimes		}
1311556Srgrimesdone:	argv += optind;
1321556Srgrimes	argc -= optind;
1331556Srgrimes
1341556Srgrimes	if (argc < 2)
1351556Srgrimes		usage();
1361556Srgrimes
1371556Srgrimes	if (Rflag) {
13877333Sru		fts_options = FTS_PHYSICAL;
1391556Srgrimes		if (hflag)
1401556Srgrimes			errx(1,
1411556Srgrimes		"the -R and -h options may not be specified together.");
1421556Srgrimes		if (Hflag)
1431556Srgrimes			fts_options |= FTS_COMFOLLOW;
1441556Srgrimes		if (Lflag) {
1451556Srgrimes			fts_options &= ~FTS_PHYSICAL;
1461556Srgrimes			fts_options |= FTS_LOGICAL;
1471556Srgrimes		}
14877333Sru	} else
14977333Sru		fts_options = FTS_LOGICAL;
1501556Srgrimes
1511556Srgrimes	mode = *argv;
1521556Srgrimes	if (*mode >= '0' && *mode <= '7') {
1531556Srgrimes		errno = 0;
1541556Srgrimes		val = strtol(mode, &ep, 8);
1551556Srgrimes		if (val > INT_MAX || val < 0)
1561556Srgrimes			errno = ERANGE;
1571556Srgrimes		if (errno)
1581556Srgrimes			err(1, "invalid file mode: %s", mode);
1591556Srgrimes		if (*ep)
1601556Srgrimes			errx(1, "invalid file mode: %s", mode);
1611556Srgrimes		omode = val;
1621556Srgrimes		oct = 1;
1631556Srgrimes	} else {
1641556Srgrimes		if ((set = setmode(mode)) == NULL)
1651556Srgrimes			errx(1, "invalid file mode: %s", mode);
1661556Srgrimes		oct = 0;
1671556Srgrimes	}
1681556Srgrimes
1691556Srgrimes	if ((ftsp = fts_open(++argv, fts_options, 0)) == NULL)
1701556Srgrimes		err(1, NULL);
1711556Srgrimes	for (rval = 0; (p = fts_read(ftsp)) != NULL;) {
1721556Srgrimes		switch (p->fts_info) {
17317496Sadam		case FTS_D:			/* Change it at FTS_DP. */
17417496Sadam			if (!Rflag)
17517496Sadam				fts_set(ftsp, p, FTS_SKIP);
17617496Sadam			continue;
1771556Srgrimes		case FTS_DNR:			/* Warn, chmod, continue. */
1781556Srgrimes			warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
1791556Srgrimes			rval = 1;
1801556Srgrimes			break;
1811556Srgrimes		case FTS_ERR:			/* Warn, continue. */
1821556Srgrimes		case FTS_NS:
1831556Srgrimes			warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
1841556Srgrimes			rval = 1;
1851556Srgrimes			continue;
1861556Srgrimes		case FTS_SL:			/* Ignore. */
1871556Srgrimes		case FTS_SLNONE:
1881556Srgrimes			/*
1891556Srgrimes			 * The only symlinks that end up here are ones that
1901556Srgrimes			 * don't point to anything and ones that we found
1911556Srgrimes			 * doing a physical walk.
1921556Srgrimes			 */
1931556Srgrimes			continue;
1941556Srgrimes		default:
1951556Srgrimes			break;
1961556Srgrimes		}
19764013Speter		newmode = oct ? omode : getmode(set, p->fts_statp->st_mode);
19864013Speter		if ((newmode & ALLPERMS) == (p->fts_statp->st_mode & ALLPERMS))
19964013Speter			continue;
20064013Speter		if (chmod(p->fts_accpath, newmode) && !fflag) {
20162887Skris			warn("%s", p->fts_path);
2021556Srgrimes			rval = 1;
20353780Sobrien		} else {
20453780Sobrien		    	if (vflag)
20553780Sobrien				(void)printf("%s\n", p->fts_accpath);
2061556Srgrimes		}
2071556Srgrimes	}
2081556Srgrimes	if (errno)
2091556Srgrimes		err(1, "fts_read");
21041842Simp	free(set);
2111556Srgrimes	exit(rval);
2121556Srgrimes}
2131556Srgrimes
2141556Srgrimesvoid
2151556Srgrimesusage()
2161556Srgrimes{
2171556Srgrimes	(void)fprintf(stderr,
21853824Sobrien	    "usage: chmod [-fv] [-R [-H | -L | -P]] mode file ...\n");
2191556Srgrimes	exit(1);
2201556Srgrimes}
221