-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathaccess.c
39 lines (34 loc) · 878 Bytes
/
access.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
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char **argv)
{
if (argc != 3) {
fprintf(stderr, "%s [filename] [mode]\n", argv[0]);
return -1;
}
int ret;
int mode;
if (!strcmp(argv[2], "F_OK")) {
mode = F_OK;
} else if (!strcmp(argv[2], "R_OK")) {
mode = R_OK;
} else if (!strcmp(argv[2], "W_OK")) {
mode = W_OK;
} else if (!strcmp(argv[2], "X_OK")) {
mode = X_OK;
} else {
fprintf(stderr, "invalid option %s entered\n", argv[2]);
return -1;
}
ret = access(argv[1], mode);
if (ret < 0) {
fprintf(stderr, "failed to access %s error : %s\n",
argv[1], strerror(errno));
return -1;
}
fprintf(stderr, "%s is ok\n", argv[2]);
return 0;
}