summaryrefslogtreecommitdiff
path: root/Userland/chown.cpp
blob: e5b2be2a3696b94667ee983388ee0536a5bccf3b (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
41
42
43
#include <unistd.h>
#include <sys/stat.h>
#include <stdio.h>
#include <string.h>
#include <AK/AKString.h>

int main(int argc, char **argv)
{
    if (argc < 2) {
        printf("usage: chown <uid[:gid]> <path>\n");
        return 0;
    }

    uid_t new_uid = -1;
    gid_t new_gid = -1;

    auto parts = String(argv[1]).split(':');
    if (parts.is_empty()) {
        fprintf(stderr, "Empty uid/gid spec\n");
        return 1;
    }
    bool ok;
    new_uid = parts[0].to_uint(ok);
    if (!ok) {
        fprintf(stderr, "Invalid uid: '%s'\n", parts[0].characters());
        return 1;
    }
    if (parts.size() == 2) {
        new_gid = parts[1].to_uint(ok);
        if (!ok) {
            fprintf(stderr, "Invalid gid: '%s'\n", parts[1].characters());
            return 1;
        }
    }

    int rc = chown(argv[2], new_uid, new_gid);
    if (rc < 0) {
        perror("chown");
        return 1;
    }

    return 0;
}