forked from sudheesh001/OS-CS302
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchown.c
94 lines (88 loc) · 2.39 KB
/
chown.c
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <sys/types.h>
#include <ctype.h>
#include <grp.h>
#include <pwd.h>
#include <string.h>
#include <sys/stat.h>
#include <stdio.h>
#ifndef TRUE
# define TRUE 1
# define FALSE 0
#endif
static char rcsid[] = "$Header: chown.c $";
main(argc, argv)
int argc;
char *argv[];
{
int i, uid, gid, Chown, status = 0;
char *pgmname, *uids, *gids;
struct passwd *pwd;
struct group *grp;
struct stat st;
if ((pgmname = strrchr(argv[0], '/')) != NULL) pgmname++;
else pgmname = argv[0];
if (strcmp(pgmname, "chown") == 0) Chown = TRUE;
else if (strcmp(pgmname, "chgrp") == 0) Chown = FALSE;
else {
(void) fprintf(stderr, "%s: should be named \"chown\" or \"chgrp\"\n", argv[0]);
(void) exit(-1);
}
if (argc < 3) {
(void) fprintf(stderr,"Usage: %s %s file ...\n", pgmname, (Chown) ? "owner" : "group");
(void) exit(1);
}
if ((gids = strchr(argv[1], '.')) != NULL) {
*gids++ = '\0';
uids = argv[1];
} else {
if (Chown) {
uids = argv[1];
gids = NULL;
} else {
uids = NULL;
gids = argv[1];
}
}
if (uids == NULL) pwd = NULL;
else {
if (isdigit(*uids)) pwd = getpwuid(atoi(uids));
else pwd = getpwnam(uids);
if (pwd == NULL) {
(void) fprintf(stderr, "%s: unknown user id %s\n", pgmname, uids);
(void) exit(-1);
}
}
if (gids == NULL) grp = NULL;
else {
if (isdigit(*gids)) grp = getgrgid(atoi(gids));
else grp = getgrnam(gids);
if (grp == NULL) {
(void) fprintf(stderr, "%s: unknown group: %s\n", pgmname, gids);
(void) exit(-1);
}
}
for (i = 2; i < argc; i++) {
if (stat(argv[i], &st) != -1) {
uid = (pwd == NULL) ? st.st_uid : pwd->pw_uid;
gid = (grp == NULL) ? st.st_gid : grp->gr_gid;
if (chown(argv[i], uid, gid) == -1) {
(void) fprintf(stderr,"%s: not changed\n", argv[i]);
status++;
}
#ifdef _MINIX
if (getuid() != 0) {
st.st_mode &= ~S_ISUID;
st.st_mode &= ~S_ISGID;
if (chmod(argv[i], st.st_mode) == -1) {
(void) fprintf(stderr, "%s: mode not changed\n", argv[i]);
status++;
}
}
#endif
} else {
(void) perror(argv[i]);
status++;
}
}
(void) exit(status);
}