2011-05-27 22:48:07 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
static void chmodr(const char *);
|
|
|
|
|
|
|
|
static bool rflag = false;
|
2014-04-09 13:17:20 +00:00
|
|
|
static int oper = '=';
|
2011-05-27 22:48:07 +00:00
|
|
|
static mode_t mode = 0;
|
|
|
|
|
2013-06-14 18:20:47 +00:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2013-10-10 13:50:52 +00:00
|
|
|
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[])
|
|
|
|
{
|
2013-10-10 13:50:52 +00:00
|
|
|
int c;
|
|
|
|
argv0 = argv[0];
|
2011-05-27 22:48:07 +00:00
|
|
|
|
2013-10-10 13:50:52 +00:00
|
|
|
while (--argc > 0 && (*++argv)[0] == '-') {
|
|
|
|
while ((c = *++argv[0])) {
|
|
|
|
switch (c) {
|
|
|
|
case 'R':
|
|
|
|
rflag = true;
|
|
|
|
break;
|
2013-10-20 08:53:43 +00:00
|
|
|
case 'r': case 'w': case 'x': case 's': case 't':
|
2013-10-10 13:50:52 +00:00
|
|
|
/*
|
2013-10-20 08:53:43 +00:00
|
|
|
* -[rwxst] are valid modes so do not interpret
|
2013-10-10 13:50:52 +00:00
|
|
|
* them as options - in any case we are done if
|
|
|
|
* we hit this case
|
|
|
|
*/
|
|
|
|
--argv[0];
|
|
|
|
goto done;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
2014-04-09 13:17:20 +00:00
|
|
|
parsemode(argv[0], &mode, &oper);
|
2013-10-10 13:50:52 +00:00
|
|
|
argv++;
|
|
|
|
argc--;
|
2013-06-14 18:20:47 +00:00
|
|
|
|
|
|
|
if(argc < 1)
|
|
|
|
usage();
|
2011-05-27 22:48:07 +00:00
|
|
|
|
2013-10-10 13:50:52 +00:00
|
|
|
for (; argc > 0; argc--, argv++)
|
2013-06-14 18:20:47 +00:00
|
|
|
chmodr(argv[0]);
|
2013-10-07 15:41:55 +00:00
|
|
|
return EXIT_SUCCESS;
|
2011-05-27 22:48:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
chmodr(const char *path)
|
|
|
|
{
|
2011-06-10 23:30:07 +00:00
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
if(stat(path, &st) == -1)
|
|
|
|
eprintf("stat %s:", path);
|
|
|
|
|
|
|
|
switch(oper) {
|
|
|
|
case '+':
|
|
|
|
st.st_mode |= mode;
|
|
|
|
break;
|
|
|
|
case '-':
|
|
|
|
st.st_mode &= ~mode;
|
|
|
|
break;
|
2011-06-10 23:31:44 +00:00
|
|
|
case '=':
|
|
|
|
st.st_mode = mode;
|
|
|
|
break;
|
2011-06-10 23:30:07 +00:00
|
|
|
}
|
|
|
|
if(chmod(path, st.st_mode) == -1)
|
2011-05-27 22:48:07 +00:00
|
|
|
eprintf("chmod %s:", path);
|
|
|
|
if(rflag)
|
|
|
|
recurse(path, chmodr);
|
|
|
|
}
|