annotate toys/pending/stat.c @ 912:f4f5132d5ac7

Stat cleanup. From the mailing list: Ok, first thing: clean up the help text. I realize what's there is copied verbatim from the man page, but that man page sucks. ("modification time" vs "change time"?) Took a bit of finagling to fit it in 80x24, but just made it. GLOBALS() indent was still tab, change to two spaces. And I tend to put a blank line between options lib/args.c automatically fills out and normal globals. We never do anything with date_stat_format() but immediately print it, might as well make the function do it. The types[] array in do_stat() is a rough edge. Hmmm... there's no else case that sets the type in case it was unknown (such as 0). In theory, this never happens. In practice it means I can cheat slightly, given this observation: $ find linux -name stat.h | xargs grep 'S_IF[A-Z]*[ \t]' linux/include/uapi/linux/stat.h:#define S_IFMT 00170000 linux/include/uapi/linux/stat.h:#define S_IFSOCK 0140000 linux/include/uapi/linux/stat.h:#define S_IFLNK 0120000 linux/include/uapi/linux/stat.h:#define S_IFREG 0100000 linux/include/uapi/linux/stat.h:#define S_IFBLK 0060000 linux/include/uapi/linux/stat.h:#define S_IFDIR 0040000 linux/include/uapi/linux/stat.h:#define S_IFCHR 0020000 linux/include/uapi/linux/stat.h:#define S_IFIFO 0010000 I.E. the only place the I_IFBLAH constants occur a stat.h header in current linux code is in the generic stuff, it doesn't vary per target. (The access permission bits are actually subtly standardized in posix due to the command line arguments to chmod, although I'm sure cygwin finds a way to break. But the type fields, not so much. But linux has to be binary compatible with itself foreverish, and that's all I really care about.) So, we have ALMOST have this going by twos, except there's no 8 and there is a 1. so let's make the 1 the default, feed a blank string into the 8... No, duh: octal. So it's actually 2, 4, 6, 8, 10, 12. So make the loop look like: filetype = statf->st_mode & S_IFMT; TT.ftname = types; for (i = 1; filetype != (i*8192) && i < 7; i++) TT.ftname += strlen(TT.ftname)+1; Yes that's linux-specific, and I think I'm ok with that. Printing all zeroes and pretending that's nanosecond resolution... either support it or don't. Let's see, supporting it is stat->st_atim.tv_nsec and similar... no mention of nanoseconds in strftime() (et tu, posix2008?) so pass it as a second argument and append it by hand... (Need to test that against musl...) When we hit an unknown type in print_it() we print the literal character, which is right for %% but what about an unknown option? $ stat -c %q / ? Eh, I guess that's a "don't care". It didn't die with an error, that's the important thing. I have a horrible idea for compressing the switch/case blocks, but should probably check this in and get some sleep for right now...
author Rob Landley <rob@landley.net>
date Tue, 28 May 2013 00:28:45 -0500
parents accabaaac666
children 91d15ead5602
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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 * anand.sinha85@gmail.com
874d2e646f2d Fix whitespace in submitted stat command.
Rob Landley <rob@landley.net>
parents: 747
diff changeset
3 * Copyright 2012 <warior.linux@gmail.com>
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
f4f5132d5ac7 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 911
diff changeset
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
f4f5132d5ac7 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 911
diff changeset
15 -f display filesystem status instead of file status
f4f5132d5ac7 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 911
diff changeset
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
f4f5132d5ac7 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 911
diff changeset
18 The valid format escape sequences for files:
f4f5132d5ac7 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 911
diff changeset
19 %a Access bits (octal) |%A Access bits (flags)|%b Blocks allocated
f4f5132d5ac7 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 911
diff changeset
20 %B Bytes per block |%d Device ID (dec) |%D Device ID (hex)
f4f5132d5ac7 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 911
diff changeset
21 %f All mode bits (hex) |%F File type |%g Group ID
f4f5132d5ac7 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 911
diff changeset
22 %G Group name |%h Hard links |%i Inode
f4f5132d5ac7 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 911
diff changeset
23 %n Filename |%N Long filename |%o I/O block size
f4f5132d5ac7 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 911
diff changeset
24 %s Size (bytes) |%u User ID |%U User name
f4f5132d5ac7 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 911
diff changeset
25 %x Access time |%X Access unix time |%y File write time
f4f5132d5ac7 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 911
diff changeset
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
f4f5132d5ac7 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 911
diff changeset
28 The valid format escape sequences for filesystems:
f4f5132d5ac7 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 911
diff changeset
29 %a Available blocks |%b Total blocks |%c Total inodes
f4f5132d5ac7 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 911
diff changeset
30 %d Free inodes |%f Free blocks |%i File system ID
f4f5132d5ac7 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 911
diff changeset
31 %l Max filename length |%n File name |%s Fragment size
f4f5132d5ac7 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 911
diff changeset
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
f4f5132d5ac7 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 911
diff changeset
39 char *fmt;
f4f5132d5ac7 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 911
diff changeset
40
f4f5132d5ac7 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 911
diff changeset
41 void *stat;
f4f5132d5ac7 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 911
diff changeset
42 struct passwd *user_name;
f4f5132d5ac7 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 911
diff changeset
43 struct group *group_name;
f4f5132d5ac7 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 911
diff changeset
44 char *ftname, access_str[11];
747
68d6c1ce7bba Add stat submission to new "pending" directory, along with infrastructure to support pending.
Rob Landley <rob@landley.net>
parents:
diff changeset
45 )
68d6c1ce7bba Add stat submission to new "pending" directory, along with infrastructure to support pending.
Rob Landley <rob@landley.net>
parents:
diff changeset
46
68d6c1ce7bba Add stat submission to new "pending" directory, along with infrastructure to support pending.
Rob Landley <rob@landley.net>
parents:
diff changeset
47
912
f4f5132d5ac7 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 911
diff changeset
48 static void date_stat_format(time_t time, int nano)
810
874d2e646f2d Fix whitespace in submitted stat command.
Rob Landley <rob@landley.net>
parents: 747
diff changeset
49 {
872
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
50 static char buf[36];
912
f4f5132d5ac7 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 911
diff changeset
51 int len;
811
7a983e09efad Call stat "stat" instead of "st" in menuconfig. Use xmalloc() instead of malloc.
Rob Landley <rob@landley.net>
parents: 810
diff changeset
52
912
f4f5132d5ac7 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 911
diff changeset
53 len = strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S.", localtime(&time));
f4f5132d5ac7 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 911
diff changeset
54 sprintf(buf+len, "%09d", nano);
f4f5132d5ac7 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 911
diff changeset
55 xprintf("%s", buf);
747
68d6c1ce7bba Add stat submission to new "pending" directory, along with infrastructure to support pending.
Rob Landley <rob@landley.net>
parents:
diff changeset
56 }
68d6c1ce7bba Add stat submission to new "pending" directory, along with infrastructure to support pending.
Rob Landley <rob@landley.net>
parents:
diff changeset
57
912
f4f5132d5ac7 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 911
diff changeset
58 static int print_stat(char type)
f4f5132d5ac7 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 911
diff changeset
59 {
911
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
60 struct stat *stat = (struct stat*)TT.stat;
912
f4f5132d5ac7 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 911
diff changeset
61
911
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
62 switch (type) {
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
63 case 'a':
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
64 xprintf("%04lo", stat->st_mode & ~S_IFMT);
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
65 break;
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
66 case 'A':
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
67 xprintf("%s", TT.access_str);
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
68 break;
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
69 case 'b':
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
70 xprintf("%llu", stat->st_blocks);
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
71 break;
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
72 case 'B':
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
73 xprintf("%lu", stat->st_blksize);
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
74 break;
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
75 case 'd':
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
76 xprintf("%ldd", stat->st_dev);
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
77 break;
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
78 case 'D':
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
79 xprintf("%llxh", stat->st_dev);
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
80 break;
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
81 case 'f':
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
82 xprintf("%lx", stat->st_mode);
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
83 break;
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
84 case 'F':
912
f4f5132d5ac7 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 911
diff changeset
85 xprintf("%s", TT.ftname);
911
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
86 break;
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
87 case 'g':
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
88 xprintf("%lu", stat->st_gid);
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
89 break;
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
90 case 'G':
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
91 xprintf("%8s", TT.user_name->pw_name);
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
92 break;
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
93 case 'h':
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
94 xprintf("%lu", stat->st_nlink);
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
95 break;
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
96 case 'i':
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
97 xprintf("%llu", stat->st_ino);
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
98 break;
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
99 case 'N':
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
100 xprintf("`%s'", *toys.optargs);
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
101 break;
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
102 case 'o':
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
103 xprintf("%lu", stat->st_blksize);
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
104 break;
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
105 case 's':
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
106 xprintf("%llu", stat->st_size);
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
107 break;
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
108 case 'u':
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
109 xprintf("%lu", stat->st_uid);
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
110 break;
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
111 case 'U':
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
112 xprintf("%8s", TT.user_name->pw_name);
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
113 break;
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
114 case 'x':
912
f4f5132d5ac7 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 911
diff changeset
115 date_stat_format(stat->st_atime, stat->st_atim.tv_nsec);
911
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
116 break;
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
117 case 'X':
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
118 xprintf("%llu", stat->st_atime);
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
119 break;
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
120 case 'y':
912
f4f5132d5ac7 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 911
diff changeset
121 date_stat_format(stat->st_mtime, stat->st_mtim.tv_nsec);
911
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
122 break;
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
123 case 'Y':
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
124 xprintf("%llu", stat->st_mtime);
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
125 break;
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
126 case 'z':
912
f4f5132d5ac7 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 911
diff changeset
127 date_stat_format(stat->st_ctime, stat->st_ctim.tv_nsec);
911
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
128 break;
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
129 case 'Z':
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
130 xprintf("%llu", stat->st_ctime);
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
131 break;
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
132 default:
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
133 return 1;
871
8aa07b575cd6 stat: Reindent from 4 to 2 spaces
Felix Janda <felix.janda@posteo.de>
parents: 811
diff changeset
134 }
911
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
135 return 0;
747
68d6c1ce7bba Add stat submission to new "pending" directory, along with infrastructure to support pending.
Rob Landley <rob@landley.net>
parents:
diff changeset
136 }
68d6c1ce7bba Add stat submission to new "pending" directory, along with infrastructure to support pending.
Rob Landley <rob@landley.net>
parents:
diff changeset
137
911
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
138 static int print_statfs(char type) {
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
139 struct statfs *statfs = (struct statfs*)TT.stat;
912
f4f5132d5ac7 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 911
diff changeset
140
911
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
141 switch (type) {
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
142 case 'a':
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
143 xprintf("%lu", statfs->f_bavail);
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
144 break;
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
145 case 'b':
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
146 xprintf("%lu", statfs->f_blocks);
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
147 break;
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
148 case 'c':
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
149 xprintf("%lu", statfs->f_files);
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
150 break;
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
151 case 'd':
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
152 xprintf("%lu", statfs->f_ffree);
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
153 break;
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
154 case 'f':
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
155 xprintf("%lu", statfs->f_bfree);
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
156 break;
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
157 case 'i':
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
158 xprintf("%x%x", statfs->f_fsid.__val[0], statfs->f_fsid.__val[1]);
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
159 break;
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
160 case 'l':
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
161 xprintf("%ld", statfs->f_namelen);
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
162 break;
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
163 case 's':
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
164 xprintf("%d", statfs->f_frsize);
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
165 break;
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
166 case 'S':
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
167 xprintf("%d", statfs->f_bsize);
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
168 break;
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
169 case 't':
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
170 xprintf("%lx", statfs->f_type);
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
171 break;
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
172 default:
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
173 return 1;
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
174 }
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
175 return 0;
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
176 }
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
177
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
178 static int do_stat(char *path)
910
1b77a81859f6 stat: Add support for stat'ing multiple files
Felix Janda <felix.janda@posteo.de>
parents: 886
diff changeset
179 {
911
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
180 struct stat *statf = (struct stat*)TT.stat;
912
f4f5132d5ac7 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 911
diff changeset
181 char *types = "character device\0directory\0block device\0" \
f4f5132d5ac7 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 911
diff changeset
182 "regular file\0symbolic link\0socket\0FIFO (named pipe)";
f4f5132d5ac7 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 911
diff changeset
183 int i, filetype;
910
1b77a81859f6 stat: Add support for stat'ing multiple files
Felix Janda <felix.janda@posteo.de>
parents: 886
diff changeset
184
911
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
185 if (stat(path, statf) < 0) return 1;
910
1b77a81859f6 stat: Add support for stat'ing multiple files
Felix Janda <felix.janda@posteo.de>
parents: 886
diff changeset
186
912
f4f5132d5ac7 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 911
diff changeset
187 filetype = statf->st_mode & S_IFMT;
f4f5132d5ac7 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 911
diff changeset
188 TT.ftname = types;
f4f5132d5ac7 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 911
diff changeset
189 for (i = 1; filetype != (i*8192) && i < 7; i++)
f4f5132d5ac7 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 911
diff changeset
190 TT.ftname += strlen(TT.ftname)+1;
f4f5132d5ac7 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 911
diff changeset
191 if (!statf->st_size && filetype == S_IFREG)
f4f5132d5ac7 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 911
diff changeset
192 TT.ftname = "regular empty file";
910
1b77a81859f6 stat: Add support for stat'ing multiple files
Felix Janda <felix.janda@posteo.de>
parents: 886
diff changeset
193
1b77a81859f6 stat: Add support for stat'ing multiple files
Felix Janda <felix.janda@posteo.de>
parents: 886
diff changeset
194 // check user and group name
911
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
195 TT.user_name = getpwuid(statf->st_uid);
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
196 TT.group_name = getgrgid(statf->st_gid);
910
1b77a81859f6 stat: Add support for stat'ing multiple files
Felix Janda <felix.janda@posteo.de>
parents: 886
diff changeset
197 // function to get access in human readable format
911
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
198 format_mode(&TT.access_str, statf->st_mode);
912
f4f5132d5ac7 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 911
diff changeset
199
910
1b77a81859f6 stat: Add support for stat'ing multiple files
Felix Janda <felix.janda@posteo.de>
parents: 886
diff changeset
200 return 0;
1b77a81859f6 stat: Add support for stat'ing multiple files
Felix Janda <felix.janda@posteo.de>
parents: 886
diff changeset
201 }
1b77a81859f6 stat: Add support for stat'ing multiple files
Felix Janda <felix.janda@posteo.de>
parents: 886
diff changeset
202
911
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
203 static int do_statfs(char *path)
910
1b77a81859f6 stat: Add support for stat'ing multiple files
Felix Janda <felix.janda@posteo.de>
parents: 886
diff changeset
204 {
911
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
205 return statfs(path, TT.stat) < 0;
910
1b77a81859f6 stat: Add support for stat'ing multiple files
Felix Janda <felix.janda@posteo.de>
parents: 886
diff changeset
206 }
1b77a81859f6 stat: Add support for stat'ing multiple files
Felix Janda <felix.janda@posteo.de>
parents: 886
diff changeset
207
810
874d2e646f2d Fix whitespace in submitted stat command.
Rob Landley <rob@landley.net>
parents: 747
diff changeset
208 void stat_main(void)
874d2e646f2d Fix whitespace in submitted stat command.
Rob Landley <rob@landley.net>
parents: 747
diff changeset
209 {
911
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
210 struct {
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
211 char *fmt;
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
212 int (*do_it)(char*);
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
213 int (*print_it)(char);
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
214 size_t size;
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
215 } d, ds[2] = {
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
216 {" File: %N\n"
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
217 " Size: %s\t Blocks: %b\t IO Blocks: %B\t%F\n"
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
218 "Device: %D\t Inode: %i\t Links: %h\n"
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
219 "Access: (%a/%A)\tUid: (%u/%U)\tGid: (%g/%G)\n"
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
220 "Access: %x\nModify: %y\nChange: %z",
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
221 do_stat, print_stat, sizeof(struct stat)},
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
222 {" File: \"%n\"\n"
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
223 " ID: %i Namelen: %l Type: %t\n"
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
224 "Block Size: %s Fundamental block size: %S\n"
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
225 "Blocks: Total: %b\tFree: %f\tAvailable: %a\n"
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
226 "Inodes: Total: %c\tFree: %d",
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
227 do_statfs, print_statfs, sizeof(struct statfs)}
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
228 };
872
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
229
911
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
230 d = ds[toys.optflags & FLAG_f];
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
231 TT.stat = xmalloc(d.size);
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
232 if (toys.optflags & FLAG_c) d.fmt = TT.fmt;
910
1b77a81859f6 stat: Add support for stat'ing multiple files
Felix Janda <felix.janda@posteo.de>
parents: 886
diff changeset
233
1b77a81859f6 stat: Add support for stat'ing multiple files
Felix Janda <felix.janda@posteo.de>
parents: 886
diff changeset
234 for (; *toys.optargs; toys.optargs++) {
911
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
235 char *format = d.fmt;
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
236 if (d.do_it(*toys.optargs)) {
910
1b77a81859f6 stat: Add support for stat'ing multiple files
Felix Janda <felix.janda@posteo.de>
parents: 886
diff changeset
237 perror_msg("'%s'", *toys.optargs);
1b77a81859f6 stat: Add support for stat'ing multiple files
Felix Janda <felix.janda@posteo.de>
parents: 886
diff changeset
238 continue;
1b77a81859f6 stat: Add support for stat'ing multiple files
Felix Janda <felix.janda@posteo.de>
parents: 886
diff changeset
239 }
911
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
240 for (; *format; format++) {
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
241 if (*format != '%') {
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
242 xputc(*format);
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
243 continue;
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
244 }
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
245 format++;
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
246 if (*format == 'n') xprintf("%s", *toys.optargs);
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
247 else if (d.print_it(*format)) xputc(*format);
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
248 }
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
249 xputc('\n');
910
1b77a81859f6 stat: Add support for stat'ing multiple files
Felix Janda <felix.janda@posteo.de>
parents: 886
diff changeset
250 }
911
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
251
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
252 if(CFG_TOYBOX_FREE) free(TT.stat);
747
68d6c1ce7bba Add stat submission to new "pending" directory, along with infrastructure to support pending.
Rob Landley <rob@landley.net>
parents:
diff changeset
253 }