-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1Stat.c
84 lines (64 loc) · 1.44 KB
/
1Stat.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
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
int main(int argc, char *argv[])
{
struct stat info;
int fd;
if(argc != 2)
{
printf("Usage: %s <pathname>\n", argv[0]);
return 1;
}
fd = open(argv[1],O_RDONLY);
if(fd == -1)
{
perror("Failed to open");
close(fd);
return 2;
}
if (fstat(fd, &info) < 0)
{
perror("Failed to stat");
return 3;
}
printf("File type: ");
switch (info.st_mode & S_IFMT)
{
case S_IFBLK:
printf("block device\n");
break;
case S_IFCHR:
printf("character device\n");
break;
case S_IFDIR:
printf("directory\n");
break;
case S_IFIFO:
printf("FIFO/pipe\n");
break;
case S_IFLNK:
printf("symlink\n");
break;
case S_IFREG:
printf("regular file\n");
break;
case S_IFSOCK:
printf("socket\n");
break;
default:
printf("unknown?\n");
break;
}
printf("File size: %lld bytes\n",
(long long) info.st_size);
printf("Last status change: %s", ctime(&info.st_ctime));
printf("Last file access: %s", ctime(&info.st_atime));
printf("Last file modification: %s", ctime(&info.st_mtime));
return 0;
}