-
Notifications
You must be signed in to change notification settings - Fork 231
/
ex10-8.c
49 lines (43 loc) · 1.15 KB
/
ex10-8.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
/*
* p612, 10.8
*/
#include "csapp.h"
int main(int argc, char **argv)
{
if (argc != 2) {
fprintf(stderr, "usage: %s fd\n", argv[0]);
return -1;
}
// 有效性检查
char *c = argv[1];
for (;;) {
if (isalpha(*c)) {
fprintf(stderr, "%s: not a number\n", argv[1]);
return -1;
}
if (*c == '\0')
break;
c += 1;
}
int fd = atoi(argv[1]);
struct stat stat;
int ret = fstat(fd, &stat);
if (ret) {
perror("fstat");
return -1;
}
char *type;
if (S_ISREG(stat.st_mode)) /* Determine file type */
type = "regular";
else if (S_ISDIR(stat.st_mode))
type = "directory";
else
type = "other";
char *readok;
if ((stat.st_mode & S_IRUSR)) /* Check read access */
readok = "yes";
else
readok = "no";
printf("type: %s, read: %s\n", type, readok);
exit(0);
}