summaryrefslogtreecommitdiffstats
path: root/toolbox/chmod.c
blob: 31a53bf7e7991138892ac13a80bdfe4a1641fda2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>

#include <unistd.h>
#include <time.h>

int chmod_main(int argc, char **argv)
{
    int i;

    if (argc < 3) {
        fprintf(stderr, "Usage: chmod <MODE> <FILE>\n");
        return 10;
    }

    int mode = 0;
    const char* s = argv[1];
    while (*s) {
        if (*s >= '0' && *s <= '7') {
            mode = (mode<<3) | (*s-'0');
        }
        else {
            fprintf(stderr, "Bad mode\n");
            return 10;
        }
        s++;
    }
    for (i = 2; i < argc; i++) {
        if (chmod(argv[i], mode) < 0) {
            fprintf(stderr, "Unable to chmod %s: %s\n", argv[i], strerror(errno));
            return 10;
        }
    }
    return 0;
}