119840Sdist /* 239829Sbostic * Copyright (c) 1989 The Regents of the University of California. 339829Sbostic * All rights reserved. 439829Sbostic * 542527Sbostic * %sccs.include.redist.c% 619840Sdist */ 716242Slayer 819840Sdist #ifndef lint 934043Sbostic char copyright[] = 1039829Sbostic "@(#) Copyright (c) 1989 The Regents of the University of California.\n\ 1134043Sbostic All rights reserved.\n"; 1234043Sbostic #endif /* not lint */ 1319840Sdist 1434043Sbostic #ifndef lint 15*47235Sbostic static char sccsid[] = "@(#)chmod.c 5.19 (Berkeley) 03/12/91"; 1634043Sbostic #endif /* not lint */ 1734043Sbostic 1816242Slayer #include <sys/types.h> 1916242Slayer #include <sys/stat.h> 2039829Sbostic #include <fts.h> 2139829Sbostic #include <stdio.h> 2242010Sbostic #include <string.h> 2316242Slayer 2440269Sbostic extern int errno; 2539829Sbostic int retval; 2616242Slayer 2724503Ssam main(argc, argv) 2834043Sbostic int argc; 2934043Sbostic char **argv; 3016242Slayer { 3140269Sbostic extern int optind; 3239829Sbostic register FTS *fts; 3339829Sbostic register FTSENT *p; 3439829Sbostic register int oct, omode; 3539829Sbostic register char *mode; 3640944Sbostic mode_t *set, *setmode(); 3739829Sbostic struct stat sb; 3839829Sbostic int ch, fflag, rflag; 3916242Slayer 4039829Sbostic fflag = rflag = 0; 4139829Sbostic while ((ch = getopt(argc, argv, "Rfrwx")) != EOF) 4234043Sbostic switch((char)ch) { 4324503Ssam case 'R': 4447072Sbostic rflag = 1; 4524503Ssam break; 4647072Sbostic case 'f': /* no longer documented */ 4747072Sbostic fflag = 1; 4824503Ssam break; 4947072Sbostic case 'r': /* "-[rwx]" are valid file modes */ 5039829Sbostic case 'w': 5139829Sbostic case 'x': 5239829Sbostic --optind; 5339829Sbostic goto done; 5434043Sbostic case '?': 5524503Ssam default: 5639829Sbostic usage(); 5723482Smckusick } 5834043Sbostic done: argv += optind; 5934043Sbostic argc -= optind; 6034043Sbostic 6139829Sbostic if (argc < 2) 6239829Sbostic usage(); 6339829Sbostic 6439829Sbostic mode = *argv; 6539829Sbostic if (*mode >= '0' && *mode <= '7') { 6639829Sbostic omode = (int)strtol(mode, (char **)NULL, 8); 6739829Sbostic oct = 1; 6839829Sbostic } else { 6940944Sbostic if (!(set = setmode(mode))) { 7039829Sbostic (void)fprintf(stderr, "chmod: invalid file mode.\n"); 7139829Sbostic exit(1); 7239829Sbostic } 7339829Sbostic oct = 0; 7416242Slayer } 7534043Sbostic 7639829Sbostic retval = 0; 7739829Sbostic if (rflag) { 7845601Sbostic if (!(fts = fts_open(++argv, 7943107Sbostic oct ? FTS_NOSTAT|FTS_PHYSICAL : FTS_PHYSICAL, 0))) { 8039829Sbostic (void)fprintf(stderr, "chmod: %s.\n", strerror(errno)); 8139829Sbostic exit(1); 8218473Smckusick } 8345639Sbostic while (p = fts_read(fts)) 8445639Sbostic switch(p->fts_info) { 8545639Sbostic case FTS_D: 8645639Sbostic break; 87*47235Sbostic case FTS_DNR: 8845639Sbostic case FTS_ERR: 89*47235Sbostic case FTS_NS: 9045639Sbostic (void)fprintf(stderr, "chmod: %s: %s.\n", 9145639Sbostic p->fts_path, strerror(errno)); 9245639Sbostic exit(1); 9345639Sbostic default: 9445639Sbostic if (chmod(p->fts_accpath, oct ? omode : 9545639Sbostic getmode(set, p->fts_statb.st_mode)) && 9645639Sbostic !fflag) 9742202Sbostic error(p->fts_path); 9845639Sbostic break; 9916242Slayer } 10039829Sbostic exit(retval); 10134043Sbostic } 10239829Sbostic if (oct) { 10339829Sbostic while (*++argv) 10439829Sbostic if (chmod(*argv, omode) && !fflag) 10539829Sbostic error(*argv); 10639829Sbostic } else 10739829Sbostic while (*++argv) 10840269Sbostic if ((lstat(*argv, &sb) || 10940944Sbostic chmod(*argv, getmode(set, sb.st_mode))) && !fflag) 11039829Sbostic error(*argv); 11139829Sbostic exit(retval); 11216242Slayer } 11316242Slayer 11439829Sbostic error(name) 11539829Sbostic char *name; 11616242Slayer { 11739829Sbostic (void)fprintf(stderr, "chmod: %s: %s.\n", name, strerror(errno)); 11839829Sbostic retval = 1; 11916242Slayer } 12016242Slayer 12139829Sbostic usage() 12216242Slayer { 12347072Sbostic (void)fprintf(stderr, "chmod: chmod [-R] mode file ...\n"); 12439829Sbostic exit(1); 12516242Slayer } 126