1 /* 2 * Copyright (c) 1989 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #ifndef lint 9 char copyright[] = 10 "@(#) Copyright (c) 1989 The Regents of the University of California.\n\ 11 All rights reserved.\n"; 12 #endif /* not lint */ 13 14 #ifndef lint 15 static char sccsid[] = "@(#)chmod.c 5.19 (Berkeley) 03/12/91"; 16 #endif /* not lint */ 17 18 #include <sys/types.h> 19 #include <sys/stat.h> 20 #include <fts.h> 21 #include <stdio.h> 22 #include <string.h> 23 24 extern int errno; 25 int retval; 26 27 main(argc, argv) 28 int argc; 29 char **argv; 30 { 31 extern int optind; 32 register FTS *fts; 33 register FTSENT *p; 34 register int oct, omode; 35 register char *mode; 36 mode_t *set, *setmode(); 37 struct stat sb; 38 int ch, fflag, rflag; 39 40 fflag = rflag = 0; 41 while ((ch = getopt(argc, argv, "Rfrwx")) != EOF) 42 switch((char)ch) { 43 case 'R': 44 rflag = 1; 45 break; 46 case 'f': /* no longer documented */ 47 fflag = 1; 48 break; 49 case 'r': /* "-[rwx]" are valid file modes */ 50 case 'w': 51 case 'x': 52 --optind; 53 goto done; 54 case '?': 55 default: 56 usage(); 57 } 58 done: argv += optind; 59 argc -= optind; 60 61 if (argc < 2) 62 usage(); 63 64 mode = *argv; 65 if (*mode >= '0' && *mode <= '7') { 66 omode = (int)strtol(mode, (char **)NULL, 8); 67 oct = 1; 68 } else { 69 if (!(set = setmode(mode))) { 70 (void)fprintf(stderr, "chmod: invalid file mode.\n"); 71 exit(1); 72 } 73 oct = 0; 74 } 75 76 retval = 0; 77 if (rflag) { 78 if (!(fts = fts_open(++argv, 79 oct ? FTS_NOSTAT|FTS_PHYSICAL : FTS_PHYSICAL, 0))) { 80 (void)fprintf(stderr, "chmod: %s.\n", strerror(errno)); 81 exit(1); 82 } 83 while (p = fts_read(fts)) 84 switch(p->fts_info) { 85 case FTS_D: 86 break; 87 case FTS_DNR: 88 case FTS_ERR: 89 case FTS_NS: 90 (void)fprintf(stderr, "chmod: %s: %s.\n", 91 p->fts_path, strerror(errno)); 92 exit(1); 93 default: 94 if (chmod(p->fts_accpath, oct ? omode : 95 getmode(set, p->fts_statb.st_mode)) && 96 !fflag) 97 error(p->fts_path); 98 break; 99 } 100 exit(retval); 101 } 102 if (oct) { 103 while (*++argv) 104 if (chmod(*argv, omode) && !fflag) 105 error(*argv); 106 } else 107 while (*++argv) 108 if ((lstat(*argv, &sb) || 109 chmod(*argv, getmode(set, sb.st_mode))) && !fflag) 110 error(*argv); 111 exit(retval); 112 } 113 114 error(name) 115 char *name; 116 { 117 (void)fprintf(stderr, "chmod: %s: %s.\n", name, strerror(errno)); 118 retval = 1; 119 } 120 121 usage() 122 { 123 (void)fprintf(stderr, "chmod: chmod [-R] mode file ...\n"); 124 exit(1); 125 } 126