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>
|
2014-11-13 13:54:28 -05:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2011-05-27 18:48:07 -04:00
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
static void chmodr(const char *);
|
|
|
|
|
2014-11-13 15:24:47 -05:00
|
|
|
static int rflag = 0;
|
2014-04-23 15:28:59 -04:00
|
|
|
static char *modestr = "";
|
|
|
|
static mode_t mask = 0;
|
2014-10-02 18:46:04 -04:00
|
|
|
static int ret = 0;
|
2011-05-27 18:48:07 -04:00
|
|
|
|
2013-06-14 14:20:47 -04:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2013-10-10 09:50:52 -04:00
|
|
|
eprintf("usage: %s [-R] mode [file...]\n", argv0);
|
2013-06-14 14:20:47 -04:00
|
|
|
}
|
|
|
|
|
2011-05-27 18:48:07 -04:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2013-10-10 09:50:52 -04:00
|
|
|
int c;
|
|
|
|
argv0 = argv[0];
|
2011-05-27 18:48:07 -04:00
|
|
|
|
2013-10-10 09:50:52 -04:00
|
|
|
while (--argc > 0 && (*++argv)[0] == '-') {
|
|
|
|
while ((c = *++argv[0])) {
|
|
|
|
switch (c) {
|
|
|
|
case 'R':
|
2014-11-13 15:24:47 -05:00
|
|
|
rflag = 1;
|
2013-10-10 09:50:52 -04:00
|
|
|
break;
|
2013-10-20 04:53:43 -04:00
|
|
|
case 'r': case 'w': case 'x': case 's': case 't':
|
2013-10-10 09:50:52 -04:00
|
|
|
/*
|
2013-10-20 04:53:43 -04:00
|
|
|
* -[rwxst] are valid modes so do not interpret
|
2013-10-10 09:50:52 -04:00
|
|
|
* them as options - in any case we are done if
|
|
|
|
* we hit this case
|
|
|
|
*/
|
|
|
|
--argv[0];
|
|
|
|
goto done;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
2014-04-23 15:28:59 -04:00
|
|
|
mask = getumask();
|
|
|
|
modestr = argv[0];
|
2013-10-10 09:50:52 -04:00
|
|
|
argv++;
|
|
|
|
argc--;
|
2013-06-14 14:20:47 -04:00
|
|
|
|
2014-11-13 12:29:30 -05:00
|
|
|
if (argc < 1)
|
2013-06-14 14:20:47 -04:00
|
|
|
usage();
|
2011-05-27 18:48:07 -04:00
|
|
|
|
2013-10-10 09:50:52 -04:00
|
|
|
for (; argc > 0; argc--, argv++)
|
2013-06-14 14:20:47 -04:00
|
|
|
chmodr(argv[0]);
|
2014-04-23 16:00:08 -04:00
|
|
|
return ret;
|
2011-05-27 18:48:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
chmodr(const char *path)
|
|
|
|
{
|
2011-06-10 19:30:07 -04:00
|
|
|
struct stat st;
|
2014-04-23 15:28:59 -04:00
|
|
|
mode_t m;
|
2011-06-10 19:30:07 -04:00
|
|
|
|
2014-11-19 14:59:37 -05:00
|
|
|
if (stat(path, &st) < 0) {
|
2014-04-23 16:00:08 -04:00
|
|
|
weprintf("stat %s:", path);
|
2014-10-02 18:46:04 -04:00
|
|
|
ret = 1;
|
2014-04-23 16:00:08 -04:00
|
|
|
return;
|
|
|
|
}
|
2011-06-10 19:30:07 -04:00
|
|
|
|
2014-04-23 15:28:59 -04:00
|
|
|
m = parsemode(modestr, st.st_mode, mask);
|
2014-11-19 14:59:37 -05:00
|
|
|
if (chmod(path, m) < 0) {
|
2014-04-22 07:11:46 -04:00
|
|
|
weprintf("chmod %s:", path);
|
2014-10-02 18:46:04 -04:00
|
|
|
ret = 1;
|
2014-04-23 16:00:08 -04:00
|
|
|
}
|
2014-11-13 12:29:30 -05:00
|
|
|
if (rflag)
|
2011-05-27 18:48:07 -04:00
|
|
|
recurse(path, chmodr);
|
|
|
|
}
|