xref: /original-bsd/bin/chmod/chmod.c (revision c8876cb1)
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.17 (Berkeley) 11/21/90";
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++;
45 			break;
46 		case 'f':
47 			fflag++;
48 			break;
49 		/* "-[rwx]" are valid file modes */
50 		case 'r':
51 		case 'w':
52 		case 'x':
53 			--optind;
54 			goto done;
55 		case '?':
56 		default:
57 			usage();
58 		}
59 done:	argv += optind;
60 	argc -= optind;
61 
62 	if (argc < 2)
63 		usage();
64 
65 	mode = *argv;
66 	if (*mode >= '0' && *mode <= '7') {
67 		omode = (int)strtol(mode, (char **)NULL, 8);
68 		oct = 1;
69 	} else {
70 		if (!(set = setmode(mode))) {
71 			(void)fprintf(stderr, "chmod: invalid file mode.\n");
72 			exit(1);
73 		}
74 		oct = 0;
75 	}
76 
77 	retval = 0;
78 	if (rflag) {
79 		if (!(fts = fts_open(++argv,
80 		    oct ? FTS_NOSTAT|FTS_PHYSICAL : FTS_PHYSICAL, 0))) {
81 			(void)fprintf(stderr, "chmod: %s.\n", strerror(errno));
82 			exit(1);
83 		}
84 		while (p = fts_read(fts))
85 			switch(p->fts_info) {
86 			case FTS_DNR:
87 				(void)fprintf(stderr,
88 				    "chmod: %s: unable to read.\n",
89 				    p->fts_path);
90 				break;
91 			case FTS_DNX:
92 				(void)fprintf(stderr,
93 				    "chmod: %s: unable to search.\n",
94 				    p->fts_path);
95 				break;
96 			case FTS_D:
97 			case FTS_DC:
98 				break;
99 			case FTS_ERR:
100 				(void)fprintf(stderr, "chmod: %s: %s.\n",
101 				    p->fts_path, strerror(errno));
102 				exit(1);
103 			case FTS_NS:
104 				(void)fprintf(stderr,
105 				    "chmod: %s: unable to stat.\n",
106 				    p->fts_path);
107 				break;
108 			default:
109 				if (chmod(p->fts_accpath, oct ? omode :
110 				    getmode(set, p->fts_statb.st_mode)) &&
111 				    !fflag)
112 					error(p->fts_path);
113 				break;
114 			}
115 		exit(retval);
116 	}
117 	if (oct) {
118 		while (*++argv)
119 			if (chmod(*argv, omode) && !fflag)
120 				error(*argv);
121 	} else
122 		while (*++argv)
123 			if ((lstat(*argv, &sb) ||
124 			    chmod(*argv, getmode(set, sb.st_mode))) && !fflag)
125 				error(*argv);
126 	exit(retval);
127 }
128 
129 error(name)
130 	char *name;
131 {
132 	(void)fprintf(stderr, "chmod: %s: %s.\n", name, strerror(errno));
133 	retval = 1;
134 }
135 
136 usage()
137 {
138 	(void)fprintf(stderr, "chmod: chmod [-fR] mode file ...\n");
139 	exit(1);
140 }
141