Mercurial > hg > toybox
annotate toys/pending/stat.c @ 917:b2697351ce6d
Stat cleanup.
Move ftname out of GLOBALS into 'F' handler. Make 'i' zero pad output (zeroes
in middle of ID can get lost).
author | Rob Landley <rob@landley.net> |
---|---|
date | Sun, 02 Jun 2013 00:52:14 -0500 |
parents | b92cb3cc9696 |
children |
rev | line source |
---|---|
810
874d2e646f2d
Fix whitespace in submitted stat command.
Rob Landley <rob@landley.net>
parents:
747
diff
changeset
|
1 /* stat.c : display file or file system status |
874d2e646f2d
Fix whitespace in submitted stat command.
Rob Landley <rob@landley.net>
parents:
747
diff
changeset
|
2 * Copyright 2012 <warior.linux@gmail.com> |
917 | 3 * Copyright 2013 <anand.sinha85@gmail.com> |
810
874d2e646f2d
Fix whitespace in submitted stat command.
Rob Landley <rob@landley.net>
parents:
747
diff
changeset
|
4 |
886
6bb5c8ace240
stat: Remove unimplemented options and clean up help text
Felix Janda <felix.janda@posteo.de>
parents:
885
diff
changeset
|
5 USE_STAT(NEWTOY(stat, "c:f", TOYFLAG_BIN)) |
810
874d2e646f2d
Fix whitespace in submitted stat command.
Rob Landley <rob@landley.net>
parents:
747
diff
changeset
|
6 |
747
68d6c1ce7bba
Add stat submission to new "pending" directory, along with infrastructure to support pending.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
7 config STAT |
871
8aa07b575cd6
stat: Reindent from 4 to 2 spaces
Felix Janda <felix.janda@posteo.de>
parents:
811
diff
changeset
|
8 bool stat |
8aa07b575cd6
stat: Reindent from 4 to 2 spaces
Felix Janda <felix.janda@posteo.de>
parents:
811
diff
changeset
|
9 default n |
8aa07b575cd6
stat: Reindent from 4 to 2 spaces
Felix Janda <felix.janda@posteo.de>
parents:
811
diff
changeset
|
10 help |
886
6bb5c8ace240
stat: Remove unimplemented options and clean up help text
Felix Janda <felix.janda@posteo.de>
parents:
885
diff
changeset
|
11 usage: stat [-f] [-c FORMAT] FILE... |
6bb5c8ace240
stat: Remove unimplemented options and clean up help text
Felix Janda <felix.janda@posteo.de>
parents:
885
diff
changeset
|
12 |
912 | 13 Display status of files or filesystems. |
886
6bb5c8ace240
stat: Remove unimplemented options and clean up help text
Felix Janda <felix.janda@posteo.de>
parents:
885
diff
changeset
|
14 |
912 | 15 -f display filesystem status instead of file status |
16 -c Output specified FORMAT string instead of default | |
886
6bb5c8ace240
stat: Remove unimplemented options and clean up help text
Felix Janda <felix.janda@posteo.de>
parents:
885
diff
changeset
|
17 |
912 | 18 The valid format escape sequences for files: |
19 %a Access bits (octal) |%A Access bits (flags)|%b Blocks allocated | |
20 %B Bytes per block |%d Device ID (dec) |%D Device ID (hex) | |
21 %f All mode bits (hex) |%F File type |%g Group ID | |
22 %G Group name |%h Hard links |%i Inode | |
23 %n Filename |%N Long filename |%o I/O block size | |
24 %s Size (bytes) |%u User ID |%U User name | |
25 %x Access time |%X Access unix time |%y File write time | |
26 %Y File write unix time|%z Dir change time |%Z Dir change unix time | |
886
6bb5c8ace240
stat: Remove unimplemented options and clean up help text
Felix Janda <felix.janda@posteo.de>
parents:
885
diff
changeset
|
27 |
912 | 28 The valid format escape sequences for filesystems: |
29 %a Available blocks |%b Total blocks |%c Total inodes | |
30 %d Free inodes |%f Free blocks |%i File system ID | |
31 %l Max filename length |%n File name |%s Fragment size | |
32 %S Best transfer size |%t File system type | |
747
68d6c1ce7bba
Add stat submission to new "pending" directory, along with infrastructure to support pending.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
33 */ |
68d6c1ce7bba
Add stat submission to new "pending" directory, along with infrastructure to support pending.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
34 |
68d6c1ce7bba
Add stat submission to new "pending" directory, along with infrastructure to support pending.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
35 #define FOR_stat |
68d6c1ce7bba
Add stat submission to new "pending" directory, along with infrastructure to support pending.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
36 #include "toys.h" |
810
874d2e646f2d
Fix whitespace in submitted stat command.
Rob Landley <rob@landley.net>
parents:
747
diff
changeset
|
37 |
747
68d6c1ce7bba
Add stat submission to new "pending" directory, along with infrastructure to support pending.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
38 GLOBALS( |
912 | 39 char *fmt; |
40 | |
914 | 41 union { |
42 struct stat st; | |
43 struct statfs sf; | |
44 } stat; | |
912 | 45 struct passwd *user_name; |
46 struct group *group_name; | |
747
68d6c1ce7bba
Add stat submission to new "pending" directory, along with infrastructure to support pending.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
47 ) |
68d6c1ce7bba
Add stat submission to new "pending" directory, along with infrastructure to support pending.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
48 |
68d6c1ce7bba
Add stat submission to new "pending" directory, along with infrastructure to support pending.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
49 |
914 | 50 // Note: the atime, mtime, and ctime fields in struct stat are the start |
51 // of embedded struct timespec, but posix won't let them use that | |
52 // struct definition for legacy/namespace reasons. | |
53 | |
54 static void date_stat_format(struct timespec *ts) | |
810
874d2e646f2d
Fix whitespace in submitted stat command.
Rob Landley <rob@landley.net>
parents:
747
diff
changeset
|
55 { |
914 | 56 strftime(toybuf, sizeof(toybuf), "%Y-%m-%d %H:%M:%S", |
57 localtime(&(ts->tv_sec))); | |
58 xprintf("%s.%09d", toybuf, ts->tv_nsec); | |
747
68d6c1ce7bba
Add stat submission to new "pending" directory, along with infrastructure to support pending.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
59 } |
68d6c1ce7bba
Add stat submission to new "pending" directory, along with infrastructure to support pending.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
60 |
916 | 61 static void print_stat(char type) |
912 | 62 { |
914 | 63 struct stat *stat = (struct stat *)&TT.stat; |
912 | 64 |
916 | 65 if (type == 'a') xprintf("%04lo", stat->st_mode & ~S_IFMT); |
66 else if (type == 'A') { | |
67 char str[11]; | |
914 | 68 |
916 | 69 mode_to_string(stat->st_mode, str); |
70 xprintf("%s", str); | |
71 } else if (type == 'b') xprintf("%llu", stat->st_blocks); | |
72 else if (type == 'B') xprintf("%lu", stat->st_blksize); | |
73 else if (type == 'd') xprintf("%ldd", stat->st_dev); | |
74 else if (type == 'D') xprintf("%llxh", stat->st_dev); | |
75 else if (type == 'f') xprintf("%lx", stat->st_mode); | |
917 | 76 else if (type == 'F') { |
77 char *t = "character device\0directory\0block device\0" \ | |
78 "regular file\0symbolic link\0socket\0FIFO (named pipe)"; | |
79 int i, filetype = stat->st_mode & S_IFMT; | |
80 | |
81 for (i = 1; filetype != (i*8192) && i < 7; i++) t += strlen(t)+1; | |
82 if (!stat->st_size && filetype == S_IFREG) t = "regular empty file"; | |
83 xprintf("%s", t); | |
84 } else if (type == 'g') xprintf("%lu", stat->st_gid); | |
916 | 85 else if (type == 'G') xprintf("%8s", TT.user_name->pw_name); |
86 else if (type == 'h') xprintf("%lu", stat->st_nlink); | |
87 else if (type == 'i') xprintf("%llu", stat->st_ino); | |
88 else if (type == 'N') { | |
89 xprintf("`%s'", *toys.optargs); | |
90 if (S_ISLNK(stat->st_mode)) | |
91 if (0<readlink(*toys.optargs, toybuf, sizeof(toybuf))) | |
92 xprintf(" -> `%s'", toybuf); | |
93 } else if (type == 'o') xprintf("%lu", stat->st_blksize); | |
94 else if (type == 's') xprintf("%llu", stat->st_size); | |
95 else if (type == 'u') xprintf("%lu", stat->st_uid); | |
96 else if (type == 'U') xprintf("%8s", TT.user_name->pw_name); | |
97 else if (type == 'x') date_stat_format((void *)&stat->st_atime); | |
98 else if (type == 'X') xprintf("%llu", (long long)stat->st_atime); | |
99 else if (type == 'y') date_stat_format((void *)&stat->st_mtime); | |
100 else if (type == 'Y') xprintf("%llu", (long long)stat->st_mtime); | |
101 else if (type == 'z') date_stat_format((void *)&stat->st_ctime); | |
102 else if (type == 'Z') xprintf("%llu", (long long)stat->st_ctime); | |
103 else xprintf("?"); | |
747
68d6c1ce7bba
Add stat submission to new "pending" directory, along with infrastructure to support pending.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
104 } |
68d6c1ce7bba
Add stat submission to new "pending" directory, along with infrastructure to support pending.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
105 |
916 | 106 static void print_statfs(char type) { |
914 | 107 struct statfs *statfs = (struct statfs *)&TT.stat; |
912 | 108 |
916 | 109 if (type == 'a') xprintf("%lu", statfs->f_bavail); |
110 else if (type == 'b') xprintf("%lu", statfs->f_blocks); | |
111 else if (type == 'c') xprintf("%lu", statfs->f_files); | |
112 else if (type == 'd') xprintf("%lu", statfs->f_ffree); | |
113 else if (type == 'f') xprintf("%lu", statfs->f_bfree); | |
114 else if (type == 'l') xprintf("%ld", statfs->f_namelen); | |
115 else if (type == 't') xprintf("%lx", statfs->f_type); | |
116 else if (type == 'i') | |
917 | 117 xprintf("%08x%08x", statfs->f_fsid.__val[0], statfs->f_fsid.__val[1]); |
916 | 118 else if (type == 's') xprintf("%d", statfs->f_frsize); |
119 else if (type == 'S') xprintf("%d", statfs->f_bsize); | |
120 else xprintf("?"); | |
910
1b77a81859f6
stat: Add support for stat'ing multiple files
Felix Janda <felix.janda@posteo.de>
parents:
886
diff
changeset
|
121 } |
1b77a81859f6
stat: Add support for stat'ing multiple files
Felix Janda <felix.janda@posteo.de>
parents:
886
diff
changeset
|
122 |
810
874d2e646f2d
Fix whitespace in submitted stat command.
Rob Landley <rob@landley.net>
parents:
747
diff
changeset
|
123 void stat_main(void) |
874d2e646f2d
Fix whitespace in submitted stat command.
Rob Landley <rob@landley.net>
parents:
747
diff
changeset
|
124 { |
916 | 125 int flagf = toys.optflags & FLAG_f; |
126 char *format = flagf | |
127 ? " File: \"%n\"\n ID: %i Namelen: %l Type: %t\n" | |
128 "Block Size: %s Fundamental block size: %S\n" | |
129 "Blocks: Total: %b\tFree: %f\tAvailable: %a\n" | |
130 "Inodes: Total: %c\tFree: %d" | |
131 : " File: %N\n Size: %s\t Blocks: %b\t IO Blocks: %B\t%F\n" | |
132 "Device: %D\t Inode: %i\t Links: %h\n" | |
133 "Access: (%a/%A)\tUid: (%u/%U)\tGid: (%g/%G)\n" | |
134 "Access: %x\nModify: %y\nChange: %z"; | |
872 | 135 |
916 | 136 if (toys.optflags & FLAG_c) format = TT.fmt; |
910
1b77a81859f6
stat: Add support for stat'ing multiple files
Felix Janda <felix.janda@posteo.de>
parents:
886
diff
changeset
|
137 |
1b77a81859f6
stat: Add support for stat'ing multiple files
Felix Janda <felix.janda@posteo.de>
parents:
886
diff
changeset
|
138 for (; *toys.optargs; toys.optargs++) { |
916 | 139 char *f; |
140 | |
141 if (flagf && !statfs(*toys.optargs, (void *)&TT.stat)); | |
142 else if (!flagf && !lstat(*toys.optargs, (void *)&TT.stat)) { | |
143 struct stat *stat = (struct stat*)&TT.stat; | |
144 | |
145 // check user and group name | |
146 TT.user_name = getpwuid(stat->st_uid); | |
147 TT.group_name = getgrgid(stat->st_gid); | |
148 } else { | |
910
1b77a81859f6
stat: Add support for stat'ing multiple files
Felix Janda <felix.janda@posteo.de>
parents:
886
diff
changeset
|
149 perror_msg("'%s'", *toys.optargs); |
1b77a81859f6
stat: Add support for stat'ing multiple files
Felix Janda <felix.janda@posteo.de>
parents:
886
diff
changeset
|
150 continue; |
1b77a81859f6
stat: Add support for stat'ing multiple files
Felix Janda <felix.janda@posteo.de>
parents:
886
diff
changeset
|
151 } |
916 | 152 |
153 for (f = format; *f; f++) { | |
154 if (*f != '%') putchar(*f); | |
155 else { | |
156 if (*++f == 'n') xprintf("%s", *toys.optargs); | |
157 else if (flagf) print_statfs(*f); | |
158 else print_stat(*f); | |
911
accabaaac666
stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents:
910
diff
changeset
|
159 } |
accabaaac666
stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents:
910
diff
changeset
|
160 } |
accabaaac666
stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents:
910
diff
changeset
|
161 xputc('\n'); |
910
1b77a81859f6
stat: Add support for stat'ing multiple files
Felix Janda <felix.janda@posteo.de>
parents:
886
diff
changeset
|
162 } |
747
68d6c1ce7bba
Add stat submission to new "pending" directory, along with infrastructure to support pending.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
163 } |