-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchown.c
More file actions
52 lines (44 loc) · 1 KB
/
chown.c
File metadata and controls
52 lines (44 loc) · 1 KB
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
44
45
46
47
48
49
50
51
52
#include "types.h"
#include "stat.h"
#include "fcntl.h"
#include "user.h"
int
main(int argc, char *argv[])
{
if (argc < 3) {
printf("chown: invalid arg count\n");
exit();
}
int i;
char* user = NULL;
if (strlen(argv[1]) <= 1) {
printf("chown: invalid args\n");
exit();
}
// No colon so only user was provided:
if (strstr(argv[1], ":") == NULL) {
user = argv[1];
}
else {
int arglen = strlen(argv[1]);
char* token = strtok(argv[1], ":");
// User and colon after him:
if (strlen(token) + 1 == arglen) {
user = token;
}
// We have both the user
else {
user = token;
token = strtok(NULL, ":");
}
}
uint uid = -1, gid = -1;
if (user != NULL) {
uid = atoi(user);
if (uid == -1)
uid = user_to_uid(user);
}
for (i = 2; i < argc; i ++)
chown(argv[i], uid );
exit();
}