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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
#include <sys/sysmacros.h>
#if defined(_AIX)
#define _BSD
#endif
#if defined(__sgi) || defined(__sun) /* Some systems need this */
#include <sys/mkdev.h> /* To get major() and minor() */
#endif
#if defined(__hpux) /* Other systems need this */
#include <sys/mknod.h>
#endif
#include <sys/stat.h>
#include <time.h>
#include "file_perms.h"
#include "tlpi_hdr.h"
static void displayStatInfo(const struct stat *sb)
{
printf("File type: ");
switch (sb->st_mode & S_IFMT)
{
case S_IFREG:
printf("regular file\n");
break;
case S_IFDIR:
printf("directory\n");
break;
case S_IFCHR:
printf("character device\n");
break;
case S_IFBLK:
printf("block device\n");
break;
case S_IFLNK:
printf("symbolic (soft) link\n");
break;
case S_IFIFO:
printf("FIFO or pipe\n");
break;
case S_IFSOCK:
printf("socket\n");
break;
default:
printf("unknown file type?\n");
break;
}
printf("Device containing i-node: major=%ld minor=%ld\n",
(long)major(sb->st_dev), (long)minor(sb->st_dev));
printf("I-node number: %ld\n", (long)sb->st_ino);
printf("Mode: %lo (%s)\n",
(unsigned long)sb->st_mode, filePermStr(sb->st_mode, 0));
if (sb->st_mode & (S_ISUID | S_ISGID | S_ISVTX))
printf(" special bits set: %s%s%s\n",
(sb->st_mode & S_ISUID) ? "set-UID " : "",
(sb->st_mode & S_ISGID) ? "set-GID " : "",
(sb->st_mode & S_ISVTX) ? "sticky " : "");
printf("Number of (hard) links: %ld\n", (long)sb->st_nlink);
printf("Ownership: UID=%ld GID=%ld\n",
(long)sb->st_uid, (long)sb->st_gid);
if (S_ISCHR(sb->st_mode) || S_ISBLK(sb->st_mode))
printf("Device number (st_rdev): major=%ld; minor=%ld\n",
(long)major(sb->st_rdev), (long)minor(sb->st_rdev));
printf("File size: %lld bytes\n", (long long)sb->st_size);
printf("Optimal I/O block size: %ld bytes\n", (long)sb->st_blksize);
printf("512B blocks allocated: %lld\n", (long long)sb->st_blocks);
printf("Last file access: %s", ctime(&sb->st_atime));
printf("Last file modification: %s", ctime(&sb->st_mtime));
printf("Last status change: %s", ctime(&sb->st_ctime));
}
int main(int argc, char *argv[])
{
struct stat sb;
Boolean statLink; /* True if "-l" specified (i.e., use lstat) */
int fname; /* Location of filename argument in argv[] */
statLink = (argc > 1) && strcmp(argv[1], "-l") == 0;
/* Simple parsing for "-l" */
fname = statLink ? 2 : 1;
if (fname >= argc || (argc > 1 && strcmp(argv[1], "--help") == 0))
usageErr("%s [-l] file\n"
" -l = use lstat() instead of stat()\n",
argv[0]);
if (statLink)
{
if (lstat(argv[fname], &sb) == -1)
errExit("lstat");
}
else
{
if (stat(argv[fname], &sb) == -1)
errExit("stat");
}
displayStatInfo(&sb);
exit(EXIT_SUCCESS);
}
|