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