sbase/chmod.c

75 lines
1.2 KiB
C
Raw Normal View History

2011-05-27 18:48:07 -04:00
/* See LICENSE file for copyright and license details. */
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
2011-05-27 18:48:07 -04:00
#include "util.h"
2015-01-16 18:01:51 -05:00
static int rflag = 0;
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)
{
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;
}
if (rflag)
2011-05-27 18:48:07 -04:00
recurse(path, chmodr);
}
2015-01-16 18:01:51 -05:00
static void
usage(void)
{
eprintf("usage: %s [-R] mode [file ...]\n", argv0);
2015-01-16 18:01:51 -05:00
}
int
main(int argc, char *argv[])
{
size_t i;
argv0 = argv[0];
for (i = 1; i < argc && argv[i][0] == '-'; i++) {
switch (argv[i][1]) {
case 'R':
rflag = 1;
break;
case 'r': case 'w': case 'x': case 's': case 't':
/*
* -[rwxst] are valid modes so do not interpret
* them as options - in any case we are done if
* we hit this case
*/
goto done;
default:
usage();
}
}
done:
mask = getumask();
modestr = argv[i];
if (argc - i - 1 < 1)
usage();
for (++i; i < argc; i++)
chmodr(argv[i]);
return ret;
}