sbase/chmod.c

87 lines
1.4 KiB
C
Raw Normal View History

2011-05-27 18:48:07 -04:00
/* See LICENSE file for copyright and license details. */
#include <sys/stat.h>
2011-05-27 18:48:07 -04:00
#include "util.h"
static int Rflag = 0;
2015-01-16 18:01:51 -05:00
static char *modestr = "";
static mode_t mask = 0;
static int ret = 0;
2011-05-27 18:48:07 -04:00
void
chmodr(const char *path, int depth)
2011-05-27 18:48:07 -04:00
{
2011-06-10 19:30:07 -04:00
struct stat st;
mode_t m;
2011-06-10 19:30:07 -04:00
2014-11-19 14:59:37 -05:00
if (stat(path, &st) < 0) {
weprintf("stat %s:", path);
2014-10-02 18:46:04 -04:00
ret = 1;
return;
}
2011-06-10 19:30:07 -04:00
m = parsemode(modestr, st.st_mode, mask);
2014-11-19 14:59:37 -05:00
if (chmod(path, m) < 0) {
weprintf("chmod %s:", path);
2014-10-02 18:46:04 -04:00
ret = 1;
} else if (Rflag)
recurse(path, chmodr, depth);
2011-05-27 18:48:07 -04:00
}
2015-01-16 18:01:51 -05:00
static void
usage(void)
{
eprintf("usage: %s [-R [-H | -L | -P]] mode file ...\n", argv0);
2015-01-16 18:01:51 -05:00
}
int
main(int argc, char *argv[])
{
size_t i;
argv0 = *(argv++);
argc--;
for (; *argv && (*argv)[0] == '-'; argc--, argv++) {
if (!(*argv)[1])
2015-01-16 18:01:51 -05:00
usage();
for (i = 1; (*argv)[i]; i++) {
switch ((*argv)[i]) {
case 'R':
Rflag = 1;
break;
case 'H':
case 'L':
case 'P':
recurse_follow = (*argv)[i];
break;
case 'r': case 'w': case 'x': case 's': case 't':
/* -[rwxst] are valid modes, so we're done */
if (i == 1)
goto done;
/* fallthrough */
case '-':
/* -- terminator */
if (i == 1 && !(*argv)[i + 1]) {
argv++;
argc--;
goto done;
}
/* fallthrough */
default:
usage();
}
2015-01-16 18:01:51 -05:00
}
}
done:
mask = getumask();
modestr = *argv;
2015-01-16 18:01:51 -05:00
if (argc < 2)
2015-01-16 18:01:51 -05:00
usage();
for (--argc, ++argv; *argv; argc--, argv++)
chmodr(*argv, 0);
2015-01-16 18:01:51 -05:00
return ret;
}