chmod.c revision 53780
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 53780 1999-11-27 19:25:08Z obrien $"; 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 601556Srgrimesvoid usage __P((void)); 611556Srgrimes 621556Srgrimesint 631556Srgrimesmain(argc, argv) 641556Srgrimes int argc; 651556Srgrimes char *argv[]; 661556Srgrimes{ 671556Srgrimes FTS *ftsp; 681556Srgrimes FTSENT *p; 691556Srgrimes mode_t *set; 701556Srgrimes long val; 711556Srgrimes int oct, omode; 721556Srgrimes int Hflag, Lflag, Pflag, Rflag, ch, fflag, fts_options, hflag, rval; 7353780Sobrien int vflag; 741556Srgrimes char *ep, *mode; 751556Srgrimes 767165Sjoerg set = NULL; 777165Sjoerg omode = 0; 781556Srgrimes Hflag = Lflag = Pflag = Rflag = fflag = hflag = 0; 7953780Sobrien while ((ch = getopt(argc, argv, "HLPRXfgorstuvwx")) != -1) 801556Srgrimes switch (ch) { 811556Srgrimes case 'H': 821556Srgrimes Hflag = 1; 831556Srgrimes Lflag = Pflag = 0; 841556Srgrimes break; 851556Srgrimes case 'L': 861556Srgrimes Lflag = 1; 871556Srgrimes Hflag = Pflag = 0; 881556Srgrimes break; 891556Srgrimes case 'P': 901556Srgrimes Pflag = 1; 911556Srgrimes Hflag = Lflag = 0; 921556Srgrimes break; 931556Srgrimes case 'R': 941556Srgrimes Rflag = 1; 951556Srgrimes break; 9649544Schris case 'f': 971556Srgrimes fflag = 1; 981556Srgrimes break; 991556Srgrimes case 'h': 1001556Srgrimes /* 1011556Srgrimes * In System V (and probably POSIX.2) the -h option 1021556Srgrimes * causes chmod to change the mode of the symbolic 1031556Srgrimes * link. 4.4BSD's symbolic links don't have modes, 1041556Srgrimes * so it's an undocumented noop. Do syntax checking, 1051556Srgrimes * though. 1061556Srgrimes */ 1071556Srgrimes hflag = 1; 1081556Srgrimes break; 1091556Srgrimes /* 1101556Srgrimes * XXX 1111556Srgrimes * "-[rwx]" are valid mode commands. If they are the entire 1121556Srgrimes * argument, getopt has moved past them, so decrement optind. 1131556Srgrimes * Regardless, we're done argument processing. 1141556Srgrimes */ 1151556Srgrimes case 'g': case 'o': case 'r': case 's': 1161556Srgrimes case 't': case 'u': case 'w': case 'X': case 'x': 1171556Srgrimes if (argv[optind - 1][0] == '-' && 1181556Srgrimes argv[optind - 1][1] == ch && 1191556Srgrimes argv[optind - 1][2] == '\0') 1201556Srgrimes --optind; 1211556Srgrimes goto done; 12253780Sobrien case 'v': 12353780Sobrien vflag = 1; 12453780Sobrien break; 1251556Srgrimes case '?': 1261556Srgrimes default: 1271556Srgrimes usage(); 1281556Srgrimes } 1291556Srgrimesdone: argv += optind; 1301556Srgrimes argc -= optind; 1311556Srgrimes 1321556Srgrimes if (argc < 2) 1331556Srgrimes usage(); 1341556Srgrimes 1351556Srgrimes fts_options = FTS_PHYSICAL; 1361556Srgrimes if (Rflag) { 1371556Srgrimes if (hflag) 1381556Srgrimes errx(1, 1391556Srgrimes "the -R and -h options may not be specified together."); 1401556Srgrimes if (Hflag) 1411556Srgrimes fts_options |= FTS_COMFOLLOW; 1421556Srgrimes if (Lflag) { 1431556Srgrimes fts_options &= ~FTS_PHYSICAL; 1441556Srgrimes fts_options |= FTS_LOGICAL; 1451556Srgrimes } 1461556Srgrimes } 1471556Srgrimes 1481556Srgrimes mode = *argv; 1491556Srgrimes if (*mode >= '0' && *mode <= '7') { 1501556Srgrimes errno = 0; 1511556Srgrimes val = strtol(mode, &ep, 8); 1521556Srgrimes if (val > INT_MAX || val < 0) 1531556Srgrimes errno = ERANGE; 1541556Srgrimes if (errno) 1551556Srgrimes err(1, "invalid file mode: %s", mode); 1561556Srgrimes if (*ep) 1571556Srgrimes errx(1, "invalid file mode: %s", mode); 1581556Srgrimes omode = val; 1591556Srgrimes oct = 1; 1601556Srgrimes } else { 1611556Srgrimes if ((set = setmode(mode)) == NULL) 1621556Srgrimes errx(1, "invalid file mode: %s", mode); 1631556Srgrimes oct = 0; 1641556Srgrimes } 1651556Srgrimes 1661556Srgrimes if ((ftsp = fts_open(++argv, fts_options, 0)) == NULL) 1671556Srgrimes err(1, NULL); 1681556Srgrimes for (rval = 0; (p = fts_read(ftsp)) != NULL;) { 1691556Srgrimes switch (p->fts_info) { 17017496Sadam case FTS_D: /* Change it at FTS_DP. */ 17117496Sadam if (!Rflag) 17217496Sadam fts_set(ftsp, p, FTS_SKIP); 17317496Sadam continue; 1741556Srgrimes case FTS_DNR: /* Warn, chmod, continue. */ 1751556Srgrimes warnx("%s: %s", p->fts_path, strerror(p->fts_errno)); 1761556Srgrimes rval = 1; 1771556Srgrimes break; 1781556Srgrimes case FTS_ERR: /* Warn, continue. */ 1791556Srgrimes case FTS_NS: 1801556Srgrimes warnx("%s: %s", p->fts_path, strerror(p->fts_errno)); 1811556Srgrimes rval = 1; 1821556Srgrimes continue; 1831556Srgrimes case FTS_SL: /* Ignore. */ 1841556Srgrimes case FTS_SLNONE: 1851556Srgrimes /* 1861556Srgrimes * The only symlinks that end up here are ones that 1871556Srgrimes * don't point to anything and ones that we found 1881556Srgrimes * doing a physical walk. 1891556Srgrimes */ 1901556Srgrimes continue; 1911556Srgrimes default: 1921556Srgrimes break; 1931556Srgrimes } 1941556Srgrimes if (chmod(p->fts_accpath, oct ? omode : 1951556Srgrimes getmode(set, p->fts_statp->st_mode)) && !fflag) { 1961556Srgrimes warn(p->fts_path); 1971556Srgrimes rval = 1; 19853780Sobrien } else { 19953780Sobrien if (vflag) 20053780Sobrien (void)printf("%s\n", p->fts_accpath); 2011556Srgrimes } 2021556Srgrimes } 2031556Srgrimes if (errno) 2041556Srgrimes err(1, "fts_read"); 20541842Simp free(set); 2061556Srgrimes exit(rval); 2071556Srgrimes} 2081556Srgrimes 2091556Srgrimesvoid 2101556Srgrimesusage() 2111556Srgrimes{ 2121556Srgrimes (void)fprintf(stderr, 21353780Sobrien "usage: chmod [-f -R [-H | -L | -P] -v] mode file ...\n"); 2141556Srgrimes exit(1); 2151556Srgrimes} 216