-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathstat.c
50 lines (46 loc) · 1.41 KB
/
stat.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
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
static char *filetype(mode_t mode);
int
main(int argc, char *argv[])
{
struct stat st;
if (argc != 2) {
fprintf(stderr, "wrong argument\n");
exit(1);
}
if (lstat(argv[1], &st) < 0) {
perror(argv[1]);
exit(1);
}
printf("type\t%o (%s)\n", (st.st_mode & S_IFMT), filetype(st.st_mode));
printf("mode\t%o\n", st.st_mode & ~S_IFMT);
printf("dev\t%llu\n", (unsigned long long)st.st_dev);
printf("ino\t%lu\n", (unsigned long)st.st_ino);
printf("rdev\t%llu\n", (unsigned long long)st.st_rdev);
printf("nlink\t%lu\n", (unsigned long)st.st_nlink);
printf("uid\t%d\n", st.st_uid);
printf("gid\t%d\n", st.st_gid);
printf("size\t%ld\n", st.st_size);
printf("blksize\t%lu\n", (unsigned long)st.st_blksize);
printf("blocks\t%lu\n", (unsigned long)st.st_blocks);
printf("atime\t%s", ctime(&st.st_atime));
printf("mtime\t%s", ctime(&st.st_mtime));
printf("ctime\t%s", ctime(&st.st_ctime));
exit(0);
}
static char*
filetype(mode_t mode)
{
if (S_ISREG(mode)) return "file";
if (S_ISDIR(mode)) return "directory";
if (S_ISCHR(mode)) return "chardev";
if (S_ISBLK(mode)) return "blockdev";
if (S_ISFIFO(mode)) return "fifo";
if (S_ISLNK(mode)) return "symlink";
if (S_ISSOCK(mode)) return "socket";
return "unknown";
}