|
|
1.1 root 1: /*
2: * Copyright (c) 1989 The Regents of the University of California.
3: * All rights reserved.
4: *
5: * Redistribution and use in source and binary forms are permitted provided
6: * that: (1) source distributions retain this entire copyright notice and
7: * comment, and (2) distributions including binaries display the following
8: * acknowledgement: ``This product includes software developed by the
9: * University of California, Berkeley and its contributors'' in the
10: * documentation or other materials provided with the distribution and in
11: * all advertising materials mentioning features or use of this software.
12: * Neither the name of the University nor the names of its contributors may
13: * be used to endorse or promote products derived from this software without
14: * specific prior written permission.
15: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
16: * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
17: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18: */
19:
20: #ifndef lint
21: char copyright[] =
22: "@(#) Copyright (c) 1989 The Regents of the University of California.\n\
23: All rights reserved.\n";
24: #endif /* not lint */
25:
26: #ifndef lint
27: static char sccsid[] = "@(#)chmod.c 5.15 (Berkeley) 6/16/90";
28: #endif /* not lint */
29:
30: #include <sys/types.h>
31: #include <sys/stat.h>
32: #include <fts.h>
33: #include <stdio.h>
34: #include <string.h>
35:
36: extern int errno;
37: int retval;
38:
39: main(argc, argv)
40: int argc;
41: char **argv;
42: {
43: extern int optind;
44: register FTS *fts;
45: register FTSENT *p;
46: register int oct, omode;
47: register char *mode;
48: mode_t *set, *setmode();
49: struct stat sb;
50: int ch, fflag, rflag;
51:
52: fflag = rflag = 0;
53: while ((ch = getopt(argc, argv, "Rfrwx")) != EOF)
54: switch((char)ch) {
55: case 'R':
56: rflag++;
57: break;
58: case 'f':
59: fflag++;
60: break;
61: /* "-[rwx]" are valid file modes */
62: case 'r':
63: case 'w':
64: case 'x':
65: --optind;
66: goto done;
67: case '?':
68: default:
69: usage();
70: }
71: done: argv += optind;
72: argc -= optind;
73:
74: if (argc < 2)
75: usage();
76:
77: mode = *argv;
78: if (*mode >= '0' && *mode <= '7') {
79: omode = (int)strtol(mode, (char **)NULL, 8);
80: oct = 1;
81: } else {
82: if (!(set = setmode(mode))) {
83: (void)fprintf(stderr, "chmod: invalid file mode.\n");
84: exit(1);
85: }
86: oct = 0;
87: }
88:
89: retval = 0;
90: if (rflag) {
91: if (!(fts = ftsopen(++argv,
92: oct ? FTS_NOSTAT|FTS_PHYSICAL : FTS_PHYSICAL, 0))) {
93: (void)fprintf(stderr, "chmod: %s.\n", strerror(errno));
94: exit(1);
95: }
96: while (p = ftsread(fts)) {
97: if (p->fts_info == FTS_D)
98: continue;
99: if (p->fts_info == FTS_ERR) {
100: if (!fflag)
101: error(p->fts_path);
102: continue;
103: }
104: if (chmod(p->fts_accpath, oct ?
105: omode : getmode(set, p->fts_statb.st_mode)) &&
106: !fflag)
107: error(p->fts_path);
108: }
109: exit(retval);
110: }
111: if (oct) {
112: while (*++argv)
113: if (chmod(*argv, omode) && !fflag)
114: error(*argv);
115: } else
116: while (*++argv)
117: if ((lstat(*argv, &sb) ||
118: chmod(*argv, getmode(set, sb.st_mode))) && !fflag)
119: error(*argv);
120: exit(retval);
121: }
122:
123: error(name)
124: char *name;
125: {
126: (void)fprintf(stderr, "chmod: %s: %s.\n", name, strerror(errno));
127: retval = 1;
128: }
129:
130: usage()
131: {
132: (void)fprintf(stderr, "chmod: chmod [-fR] mode file ...\n");
133: exit(1);
134: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.