2011-05-27 22:48:07 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
|
|
|
#include <sys/stat.h>
|
2014-11-13 18:54:28 +00:00
|
|
|
|
2011-05-27 22:48:07 +00:00
|
|
|
#include "util.h"
|
|
|
|
|
2015-02-16 16:47:07 +00:00
|
|
|
static int Rflag = 0;
|
|
|
|
static int fflag = 'P';
|
2015-01-16 23:01:51 +00:00
|
|
|
static char *modestr = "";
|
|
|
|
static mode_t mask = 0;
|
|
|
|
static int ret = 0;
|
2011-05-27 22:48:07 +00:00
|
|
|
|
|
|
|
void
|
2015-02-16 16:23:12 +00:00
|
|
|
chmodr(const char *path, int fflag)
|
2011-05-27 22:48:07 +00:00
|
|
|
{
|
2011-06-10 23:30:07 +00:00
|
|
|
struct stat st;
|
2014-04-23 19:28:59 +00:00
|
|
|
mode_t m;
|
2011-06-10 23:30:07 +00:00
|
|
|
|
2014-11-19 19:59:37 +00:00
|
|
|
if (stat(path, &st) < 0) {
|
2014-04-23 20:00:08 +00:00
|
|
|
weprintf("stat %s:", path);
|
2014-10-02 22:46:04 +00:00
|
|
|
ret = 1;
|
2014-04-23 20:00:08 +00:00
|
|
|
return;
|
|
|
|
}
|
2011-06-10 23:30:07 +00:00
|
|
|
|
2014-04-23 19:28:59 +00:00
|
|
|
m = parsemode(modestr, st.st_mode, mask);
|
2014-11-19 19:59:37 +00:00
|
|
|
if (chmod(path, m) < 0) {
|
2014-04-22 11:11:46 +00:00
|
|
|
weprintf("chmod %s:", path);
|
2014-10-02 22:46:04 +00:00
|
|
|
ret = 1;
|
2014-04-23 20:00:08 +00:00
|
|
|
}
|
2015-02-16 16:47:07 +00:00
|
|
|
if (Rflag)
|
2015-02-09 19:53:24 +00:00
|
|
|
recurse(path, chmodr, fflag);
|
2011-05-27 22:48:07 +00:00
|
|
|
}
|
2015-01-16 23:01:51 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2015-02-16 16:47:07 +00:00
|
|
|
eprintf("usage: %s [-R [-H | -L | -P]] mode file ...\n", argv0);
|
2015-01-16 23:01:51 +00: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':
|
2015-02-16 16:47:07 +00:00
|
|
|
Rflag = 1;
|
|
|
|
break;
|
|
|
|
case 'H':
|
|
|
|
case 'L':
|
|
|
|
case 'P':
|
|
|
|
fflag = argv[i][1];
|
2015-01-16 23:01:51 +00:00
|
|
|
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++)
|
2015-02-16 16:47:07 +00:00
|
|
|
chmodr(argv[i], fflag);
|
2015-01-16 23:01:51 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|