struct stat

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

#include <sys/stat.h>
#include <unistd.h>
#include <sys/types.h>
#include <assert.h>
#include <errno.h>
#include <stdlib.h>
#include <time.h>

int (void)
{
struct stat st;
const char *pathName = "/home/jack/Code/Tinyhttpd/stat.c";
int stat_result = stat(pathName, &st);
if (stat_result == -1) {

printf(errno);
exit(1);
} else {
// print st_mode
printf("The type of %s is ",pathName);
switch (st.st_mode & S_IFMT) {
case S_IFBLK:
printf("block devicen");
break;
case S_IFCHR:
printf("character devicen");
break;
case S_IFDIR:
printf("direcotryn");
break;
case S_IFIFO:
printf("FIFO/PIPEn");
break;
case S_IFLNK:
printf("symlinkn");
break;
case S_IFREG:
printf("regular filen");
break;
case S_IFSOCK:
printf("socket");
break;
default:
printf("unknown?n");
break;
}
printf("Last status change: %s", ctime(&st.st_ctime));
printf("Last file access: %s", ctime(&st.st_atime));
printf("Last file modification: %s", ctime(&st.st_mtime));
printf("The owner has read permission: %dn", st.st_mode & S_IRUSR);
printf("The owner has write permission: %dn", st.st_mode & S_IWUSR);
printf("The owner has execute permission: %dn", st.st_mode & S_IXUSR);
printf("%sn", "END");
}
printf("exi(%d)n",1);
return 0;
}