1 /* stat.c : display file or file system status
2 * Copyright 2012 <warior.linux@gmail.com>
3 * Copyright 2013 <anand.sinha85@gmail.com>
5 USE_STAT(NEWTOY(stat, "<1c:fLt", TOYFLAG_BIN))
11 usage: stat [-tfL] [-c FORMAT] FILE...
13 Display status of files or filesystems.
15 -c Output specified FORMAT string instead of default
16 -f display filesystem status instead of file status
18 -t terse (-c "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o")
19 (with -f = -c "%n %i %l %t %s %S %b %f %a %c %d")
21 The valid format escape sequences for files:
22 %a Access bits (octal) |%A Access bits (flags)|%b Size/512
23 %B Bytes per %b (512) |%d Device ID (dec) |%D Device ID (hex)
24 %f All mode bits (hex) |%F File type |%g Group ID
25 %G Group name |%h Hard links |%i Inode
26 %m Mount point |%n Filename |%N Long filename
27 %o I/O block size |%s Size (bytes) |%t Devtype major (hex)
28 %T Devtype minor (hex) |%u User ID |%U User name
29 %x Access time |%X Access unix time |%y File write time
30 %Y File write unix time|%z Dir change time |%Z Dir change unix time
32 The valid format escape sequences for filesystems:
33 %a Available blocks |%b Total blocks |%c Total inodes
34 %d Free inodes |%f Free blocks |%i File system ID
35 %l Max filename length |%n File name |%s Fragment size
36 %S Best transfer size |%t FS type (hex) |%T FS type (driver name)
53 // Force numeric output to long long instead of manually typecasting everything
54 // and safely parse length prefix
55 static void out(char c, long long val)
57 sprintf(toybuf, "%.*sll%c", TT.patlen, TT.pattern, c);
61 // Output string with parsed length prefix
62 static void strout(char *val)
64 sprintf(toybuf, "%.*ss", TT.patlen, TT.pattern);
68 // Note: the atime, mtime, and ctime fields in struct stat are the start
69 // of embedded struct timespec, but posix won't let them use that
70 // struct definition for legacy/namespace reasons.
72 static void date_stat_format(struct timespec *ts)
75 strftime(s, sizeof(toybuf), "%Y-%m-%d %H:%M:%S",
76 localtime(&(ts->tv_sec)));
77 sprintf(s+strlen(s), ".%09ld", ts->tv_nsec);
81 static void print_stat(char type)
83 struct stat *stat = (struct stat *)&TT.stat;
85 if (type == 'a') out('o', stat->st_mode&~S_IFMT);
86 else if (type == 'A') {
89 mode_to_string(stat->st_mode, str);
91 } else if (type == 'b') out('u', stat->st_blocks);
92 else if (type == 'B') out('d', 512);
93 else if (type == 'd') out('d', stat->st_dev);
94 else if (type == 'D') out('x', stat->st_dev);
95 else if (type == 'f') out('x', stat->st_mode);
96 else if (type == 'F') {
97 char *t = "character device\0directory\0block device\0" \
98 "regular file\0symbolic link\0socket\0FIFO (named pipe)";
99 int i, filetype = stat->st_mode & S_IFMT;
101 for (i = 1; filetype != (i*8192) && i < 7; i++) t += strlen(t)+1;
102 if (!stat->st_size && filetype == S_IFREG) t = "regular empty file";
104 } else if (type == 'g') out('u', stat->st_gid);
105 else if (type == 'G') strout(getgroupname(stat->st_gid));
106 else if (type == 'h') out('u', stat->st_nlink);
107 else if (type == 'i') out('u', stat->st_ino);
108 else if (type == 'm') {
109 struct mtab_list *mt = xgetmountlist(0);
110 dev_t dev = stat->st_rdev ? stat->st_rdev : stat->st_dev;
112 // This mount point could exist multiple times, so show oldest.
113 for (dlist_terminate(mt); mt; mt = mt->next) if (mt->stat.st_dev == dev) {
117 llist_traverse(mt, free);
118 } else if (type == 'N') {
119 xprintf("`%s'", TT.file);
120 if (S_ISLNK(stat->st_mode))
121 if (readlink0(TT.file, toybuf, sizeof(toybuf)))
122 xprintf(" -> `%s'", toybuf);
123 } else if (type == 'o') out('u', stat->st_blksize);
124 else if (type == 's') out('u', stat->st_size);
125 else if (type == 't') out('x', dev_major(stat->st_rdev));
126 else if (type == 'T') out('x', dev_minor(stat->st_rdev));
127 else if (type == 'u') out('u', stat->st_uid);
128 else if (type == 'U') strout(getusername(stat->st_uid));
129 else if (type == 'x') date_stat_format((void *)&stat->st_atime);
130 else if (type == 'X') out('u', stat->st_atime);
131 else if (type == 'y') date_stat_format((void *)&stat->st_mtime);
132 else if (type == 'Y') out('u', stat->st_mtime);
133 else if (type == 'z') date_stat_format((void *)&stat->st_ctime);
134 else if (type == 'Z') out('u', stat->st_ctime);
138 static void print_statfs(char type) {
139 struct statfs *statfs = (struct statfs *)&TT.stat;
141 if (type == 'a') out('u', statfs->f_bavail);
142 else if (type == 'b') out('u', statfs->f_blocks);
143 else if (type == 'c') out('u', statfs->f_files);
144 else if (type == 'd') out('u', statfs->f_ffree);
145 else if (type == 'f') out('u', statfs->f_bfree);
146 else if (type == 'l') out('d', statfs->f_namelen);
147 else if (type == 't') out('x', statfs->f_type);
148 else if (type == 'T') {
150 struct {unsigned num; char *name;} nn[] = {
151 {0xADFF, "affs"}, {0x5346544e, "ntfs"}, {0x1Cd1, "devpts"},
152 {0x137D, "ext"}, {0xEF51, "ext2"}, {0xEF53, "ext3"},
153 {0x1BADFACE, "bfs"}, {0x9123683E, "btrfs"}, {0x28cd3d45, "cramfs"},
154 {0x3153464a, "jfs"}, {0x7275, "romfs"}, {0x01021994, "tmpfs"},
155 {0x3434, "nilfs"}, {0x6969, "nfs"}, {0x9fa0, "proc"},
156 {0x534F434B, "sockfs"}, {0x62656572, "sysfs"}, {0x517B, "smb"},
157 {0x4d44, "msdos"}, {0x4006, "fat"}, {0x43415d53, "smackfs"},
158 {0x73717368, "squashfs"}
162 for (i=0; i<ARRAY_LEN(nn); i++)
163 if (nn[i].num == statfs->f_type) s = nn[i].name;
165 } else if (type == 'i') {
168 sprintf(buf, "%08x%08x", statfs->f_fsid.__val[0], statfs->f_fsid.__val[1]);
170 } else if (type == 's') out('d', statfs->f_frsize);
171 else if (type == 'S') out('d', statfs->f_bsize);
177 int flagf = toys.optflags & FLAG_f, i;
180 if (toys.optflags&FLAG_t) {
181 format = flagf ? "%n %i %l %t %s %S %b %f %a %c %d" :
182 "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o";
183 } else format = flagf
184 ? " File: \"%n\"\n ID: %i Namelen: %l Type: %t\n"
185 "Block Size: %s Fundamental block size: %S\n"
186 "Blocks: Total: %b\tFree: %f\tAvailable: %a\n"
187 "Inodes: Total: %c\tFree: %d"
188 : " File: %N\n Size: %s\t Blocks: %b\t IO Blocks: %B\t%F\n"
189 "Device: %Dh/%dd\t Inode: %i\t Links: %h\n"
190 "Access: (%a/%A)\tUid: (%5u/%8U)\tGid: (%5g/%8G)\n"
191 "Access: %x\nModify: %y\nChange: %z";
192 char *terse_format = "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o";
194 if (toys.optflags & FLAG_c) format = TT.fmt;
195 if (toys.optflags & FLAG_t) format = terse_format;
197 for (i = 0; toys.optargs[i]; i++) {
198 int L = toys.optflags & FLAG_L;
200 TT.file = toys.optargs[i];
201 if (flagf && !statfs(TT.file, (void *)&TT.stat));
202 else if (flagf || (L ? stat : lstat)(TT.file, (void *)&TT.stat)) {
203 perror_msg("'%s'", TT.file);
207 for (f = format; *f; f++) {
208 if (*f != '%') putchar(*f);
210 f = next_printf(f, &TT.pattern);
211 TT.patlen = f-TT.pattern;
212 if (TT.patlen>99) error_exit("bad %s", TT.pattern);
213 if (*f == 'n') strout(TT.file);
214 else if (flagf) print_statfs(*f);