sbase/chmod.c

82 lines
1.3 KiB
C
Raw Normal View History

2011-05-27 22:48:07 +00: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 22:48:07 +00:00
#include "util.h"
static void chmodr(const char *);
static int rflag = 0;
static char *modestr = "";
static mode_t mask = 0;
2014-10-02 22:46:04 +00:00
static int ret = 0;
2011-05-27 22:48:07 +00:00
2013-06-14 18:20:47 +00:00
static void
usage(void)
{
eprintf("usage: %s [-R] mode [file...]\n", argv0);
2013-06-14 18:20:47 +00:00
}
2011-05-27 22:48:07 +00:00
int
main(int argc, char *argv[])
{
int c;
argv0 = argv[0];
2011-05-27 22:48:07 +00:00
while (--argc > 0 && (*++argv)[0] == '-') {
while ((c = *++argv[0])) {
switch (c) {
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
*/
--argv[0];
goto done;
default:
usage();
}
}
}
done:
mask = getumask();
modestr = argv[0];
argv++;
argc--;
2013-06-14 18:20:47 +00:00
if (argc < 1)
2013-06-14 18:20:47 +00:00
usage();
2011-05-27 22:48:07 +00:00
for (; argc > 0; argc--, argv++)
2013-06-14 18:20:47 +00:00
chmodr(argv[0]);
return ret;
2011-05-27 22:48:07 +00:00
}
void
chmodr(const char *path)
{
2011-06-10 23:30:07 +00:00
struct stat st;
mode_t m;
2011-06-10 23:30:07 +00:00
2014-11-19 19:59:37 +00:00
if (stat(path, &st) < 0) {
weprintf("stat %s:", path);
2014-10-02 22:46:04 +00:00
ret = 1;
return;
}
2011-06-10 23:30:07 +00:00
m = parsemode(modestr, st.st_mode, mask);
2014-11-19 19:59:37 +00:00
if (chmod(path, m) < 0) {
weprintf("chmod %s:", path);
2014-10-02 22:46:04 +00:00
ret = 1;
}
if (rflag)
2011-05-27 22:48:07 +00:00
recurse(path, chmodr);
}