OSDN Git Service

toybox: Add link for ls
[android-x86/external-toybox.git] / toys / other / stat.c
1 /* stat.c : display file or file system status
2  * Copyright 2012 <warior.linux@gmail.com>
3  * Copyright 2013 <anand.sinha85@gmail.com>
4
5 USE_STAT(NEWTOY(stat, "<1c:tf", TOYFLAG_BIN)) 
6
7 config STAT
8   bool stat
9   default y
10   help
11     usage: stat [-f] [-t] [-c FORMAT] FILE...
12
13     Display status of files or filesystems.
14
15     -f display filesystem status instead of file status
16     -c Output specified FORMAT string instead of default
17     -t Display info in terse form
18
19     The valid format escape sequences for files:
20     %a  Access bits (octal) |%A  Access bits (flags)|%b  Blocks allocated
21     %B  Bytes per block     |%d  Device ID (dec)    |%D  Device ID (hex)
22     %f  All mode bits (hex) |%F  File type          |%g  Group ID
23     %G  Group name          |%h  Hard links         |%i  Inode
24     %n  Filename            |%N  Long filename      |%o  I/O block size
25     %s  Size (bytes)        |%u  User ID            |%U  User name
26     %x  Access time         |%X  Access unix time   |%y  File write time
27     %Y  File write unix time|%z  Dir change time    |%Z  Dir change unix time
28
29     The valid format escape sequences for filesystems:
30     %a  Available blocks    |%b  Total blocks       |%c  Total inodes
31     %d  Free inodes         |%f  Free blocks        |%i  File system ID
32     %l  Max filename length |%n  File name          |%s  Fragment size
33     %S  Best transfer size  |%t  Filesystem type    |%T  Filesystem type name
34 */
35
36 #define FOR_stat
37 #include "toys.h"
38
39 GLOBALS(
40   char *fmt;
41
42   union {
43     struct stat st;
44     struct statfs sf;
45   } stat;
46   struct passwd *user_name;
47   struct group *group_name;
48 )
49
50
51 // Note: the atime, mtime, and ctime fields in struct stat are the start
52 // of embedded struct timespec, but posix won't let them use that
53 // struct definition for legacy/namespace reasons.
54
55 static void date_stat_format(struct timespec *ts)
56 {
57   strftime(toybuf, sizeof(toybuf), "%Y-%m-%d %H:%M:%S",
58     localtime(&(ts->tv_sec)));
59   xprintf("%s.%09ld", toybuf, ts->tv_nsec);
60 }
61
62 // Force numeric output to long long instead of manually typecasting everything
63 static void out(char c, long long val)
64 {
65   sprintf(toybuf, "%%ll%c", c);
66   printf(toybuf, val);
67 }
68
69 static void print_stat(char type)
70 {
71   struct stat *stat = (struct stat *)&TT.stat;
72
73   if (type == 'a') out('o', stat->st_mode&~S_IFMT);
74   else if (type == 'A') {
75     char str[11];
76
77     mode_to_string(stat->st_mode, str);
78     xprintf("%s", str);
79   } else if (type == 'b') out('u', stat->st_blocks);
80   else if (type == 'B') out('u', stat->st_blksize);
81   else if (type == 'd') out('d', stat->st_dev);
82   else if (type == 'D') out('x', stat->st_dev);
83   else if (type == 'f') out('x', stat->st_mode);
84   else if (type == 'F') {
85     char *t = "character device\0directory\0block device\0" \
86               "regular file\0symbolic link\0socket\0FIFO (named pipe)";
87     int i, filetype = stat->st_mode & S_IFMT;
88
89     for (i = 1; filetype != (i*8192) && i < 7; i++) t += strlen(t)+1;
90     if (!stat->st_size && filetype == S_IFREG) t = "regular empty file";
91     xprintf("%s", t);
92   } else if (type == 'g') out('u', stat->st_gid);
93   else if (type == 'G') xprintf("%8s", TT.group_name->gr_name);
94   else if (type == 'h') out('u', stat->st_nlink);
95   else if (type == 'i') out('u', stat->st_ino);
96   else if (type == 'N') {
97     xprintf("`%s'", *toys.optargs);
98     if (S_ISLNK(stat->st_mode))
99       if (0<readlink(*toys.optargs, toybuf, sizeof(toybuf)))
100         xprintf(" -> `%s'", toybuf);
101   } else if (type == 'o') out('u', stat->st_blksize);
102   else if (type == 's') out('u', stat->st_size);
103   else if (type == 'u') out('u', stat->st_uid);
104   else if (type == 'U') xprintf("%8s", TT.user_name->pw_name);
105   else if (type == 'x') date_stat_format((void *)&stat->st_atime);
106   else if (type == 'X') out('u', stat->st_atime);
107   else if (type == 'y') date_stat_format((void *)&stat->st_mtime);
108   else if (type == 'Y') out('u', stat->st_mtime);
109   else if (type == 'z') date_stat_format((void *)&stat->st_ctime);
110   else if (type == 'Z') out('u', stat->st_ctime);
111   else xprintf("?");
112 }
113
114 static void print_statfs(char type) {
115   struct statfs *statfs = (struct statfs *)&TT.stat;
116
117   if (type == 'a') out('u', statfs->f_bavail);
118   else if (type == 'b') out('u', statfs->f_blocks);
119   else if (type == 'c') out('u', statfs->f_files);
120   else if (type == 'd') out('u', statfs->f_ffree);
121   else if (type == 'f') out('u', statfs->f_bfree);
122   else if (type == 'l') out('d', statfs->f_namelen);
123   else if (type == 't') out('x', statfs->f_type);
124   else if (type == 'T') {
125     char *s = "unknown";
126     struct {unsigned num; char *name;} nn[] = {
127       {0xADFF, "affs"}, {0x5346544e, "ntfs"}, {0x1Cd1, "devpts"},
128       {0x137D, "ext"}, {0xEF51, "ext2"}, {0xEF53, "ext3"},
129       {0x1BADFACE, "bfs"}, {0x9123683E, "btrfs"}, {0x28cd3d45, "cramfs"},
130       {0x3153464a, "jfs"}, {0x7275, "romfs"}, {0x01021994, "tmpfs"},
131       {0x3434, "nilfs"}, {0x6969, "nfs"}, {0x9fa0, "proc"},
132       {0x534F434B, "sockfs"}, {0x62656572, "sysfs"}, {0x517B, "smb"},
133       {0x4d44, "msdos"}, {0x4006, "fat"}, {0x43415d53, "smackfs"},
134       {0x73717368, "squashfs"}
135     };
136     int i;
137
138     for (i=0; i<ARRAY_LEN(nn); i++)
139       if (nn[i].num == statfs->f_type) s = nn[i].name;
140     fputs(s, stdout);
141   } else if (type == 'i')
142     xprintf("%08x%08x", statfs->f_fsid.__val[0], statfs->f_fsid.__val[1]);
143   else if (type == 's') out('d', statfs->f_frsize);
144   else if (type == 'S') out('d', statfs->f_bsize);
145   else xprintf("?");
146 }
147
148 void stat_main(void)
149 {
150   int flagf = toys.optflags & FLAG_f;
151   char *format = flagf
152     ? "  File: \"%n\"\n    ID: %i Namelen: %l    Type: %t\n"
153       "Block Size: %s    Fundamental block size: %S\n"
154       "Blocks: Total: %b\tFree: %f\tAvailable: %a\n"
155       "Inodes: Total: %c\tFree: %d"
156     : "  File: %N\n  Size: %s\t Blocks: %b\t IO Blocks: %B\t%F\n"
157       "Device: %Dh/%dd\t Inode: %i\t Links: %h\n"
158       "Access: (%a/%A)\tUid: (%u/%U)\tGid: (%g/%G)\n"
159       "Access: %x\nModify: %y\nChange: %z";
160   char *terse_format = "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o";
161
162   if (toys.optflags & FLAG_c) format = TT.fmt;
163   if (toys.optflags & FLAG_t) format = terse_format;
164
165   for (; *toys.optargs; toys.optargs++) {
166     char *f;
167
168     if (flagf && !statfs(*toys.optargs, (void *)&TT.stat));
169     else if (!flagf && !lstat(*toys.optargs, (void *)&TT.stat)) {
170       struct stat *stat = (struct stat*)&TT.stat;
171
172       // check user and group name
173       TT.user_name = getpwuid(stat->st_uid);
174       TT.group_name = getgrgid(stat->st_gid);
175     } else {
176       perror_msg("'%s'", *toys.optargs);
177       continue;
178     }
179
180     for (f = format; *f; f++) {
181       if (*f != '%') putchar(*f);
182       else {
183         if (*++f == 'n') xprintf("%s", *toys.optargs);
184         else if (flagf) print_statfs(*f);
185         else print_stat(*f);
186       }
187     }
188     xputc('\n');
189   }
190 }