1 /* `chmod.c` - change the file modes
2 Copyright (c) 2022, Alan Potteiger
3 See `LICENSE` for copyright and license details */
5 #define _POSIX_C_SOURCE 200809L
16 static const char *usage
= {
17 "usage: chmod [-R] mode file...\n"
20 static char *modestr
; /* mode string */
22 /* callback for ftw(3) to traverse directory tree */
24 recurse(const char *path
, const struct stat
*sb
, int flag
)
31 if (chmod(path
, m
) != 0)
32 fprintf(stderr
, "chmod: %s: %s\n", path
,
35 case FTW_F
: case FTW_SL
: case FTW_SLN
:
38 if (chmod(path
, m
) != 0)
39 fprintf(stderr
, "chmod: %s: %s\n", path
,
48 main(int argc
, char *argv
[])
66 if (argv
[0][0] == '-' && argv
[0][1] == 'R') {
80 for (; argc
> 0; argc
-- && argv
++) {
81 if (stat(argv
[0], &st
) != 0) {
82 fprintf(stderr
, "chmod: %s: %s\n", argv
[0],
89 if (modeset(modestr
, &mode
) != 0) {
90 fprintf(stderr
, "chmod: invalid mode");
95 ftw(argv
[0], recurse
, 1);
97 if (chmod(argv
[0], mode
) != 0) {
98 fprintf(stderr
, "chmod: %s: %s\n", argv
[0],