master xplshn/aruu / cmd / posix / chmod.c
 1/* See LICENSE file for copyright and license details. */
 2
 3
 4#include <fcntl.h>
 5#include <sys/stat.h>
 6
 7#include "fs.h"
 8#include "util.h"
 9
10static char  *modestr = "";
11static mode_t mask    = 0;
12static int    ret     = 0;
13
14static void
15chmodr(int dirfd, const char *name, struct stat *st, void *data, struct recursor *r)
16{
17	mode_t m;
18
19	(void)data;
20
21	m = parsemode(modestr, st->st_mode, mask);
22	if (!S_ISLNK(st->st_mode) && fchmodat(dirfd, name, m, 0) < 0) {
23		weprintf("chmod %s:", r->path);
24		ret = 1;
25	} else if (S_ISDIR(st->st_mode)) {
26		recurse(dirfd, name, NULL, r);
27	}
28}
29
30static void
31usage(void)
32{
33	eprintf("usage: %s [-R] mode file ...\n", argv0);
34}
35
36// ?man chmod: change file modes
37// ?man arguments: mode file ...
38// ?man change the file mode bits of files and directories
39int
40main(int argc, char *argv[])
41{
42	struct recursor r = { .fn = chmodr, .maxdepth = 1, .follow = 'H', .flags = DIRFIRST };
43	size_t i;
44
45	argv0 = *argv, argv0 ? (argc--, argv++) : (void *)0;
46
47	for (; *argv && (*argv)[0] == '-'; argc--, argv++) {
48		if (!(*argv)[1])
49			usage();
50		for (i = 1; (*argv)[i]; i++) {
51			switch ((*argv)[i]) {
52			case 'R':
53				r.maxdepth = 0;
54				break;
55			case 'r': case 'w': case 'x': case 'X': case 's': case 't':
56				/* -[rwxXst] are valid modes, so we're done */
57				if (i == 1)
58					goto done;
59				/* fallthrough */
60			case '-':
61				/* -- terminator */
62				if (i == 1 && !(*argv)[i + 1]) {
63					argv++;
64					argc--;
65					goto done;
66				}
67				/* fallthrough */
68			default:
69				usage();
70			}
71		}
72	}
73done:
74	mask = getumask();
75	modestr = *argv;
76
77	if (argc < 2)
78		usage();
79
80	for (--argc, ++argv; *argv; argc--, argv++)
81		recurse(AT_FDCWD, *argv, NULL, &r);
82
83	return ret || recurse_status;
84}