annotate toys/pending/stat.c @ 914:91d15ead5602

Stat cleanup. Put global stat info in a union so we don't have to malloc it and thus don't need an explicit size tracked in main(). Make date_stat_format() take a timespec and take advantage of stat having an embedded timespec (nanosecond printing), typecast the long long prints for XYZ because on some 32 bit platforms it's an int.
author Rob Landley <rob@landley.net>
date Sat, 01 Jun 2013 20:47:16 -0500
parents f4f5132d5ac7
children b92cb3cc9696
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
914
91d15ead5602 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 912
diff changeset
41 union {
91d15ead5602 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 912
diff changeset
42 struct stat st;
91d15ead5602 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 912
diff changeset
43 struct statfs sf;
91d15ead5602 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 912
diff changeset
44 } stat;
912
f4f5132d5ac7 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 911
diff changeset
45 struct passwd *user_name;
f4f5132d5ac7 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 911
diff changeset
46 struct group *group_name;
f4f5132d5ac7 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 911
diff changeset
47 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
48 )
68d6c1ce7bba Add stat submission to new "pending" directory, along with infrastructure to support pending.
Rob Landley <rob@landley.net>
parents:
diff changeset
49
68d6c1ce7bba Add stat submission to new "pending" directory, along with infrastructure to support pending.
Rob Landley <rob@landley.net>
parents:
diff changeset
50
914
91d15ead5602 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 912
diff changeset
51 // Note: the atime, mtime, and ctime fields in struct stat are the start
91d15ead5602 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 912
diff changeset
52 // of embedded struct timespec, but posix won't let them use that
91d15ead5602 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 912
diff changeset
53 // struct definition for legacy/namespace reasons.
91d15ead5602 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 912
diff changeset
54
91d15ead5602 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 912
diff changeset
55 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
56 {
914
91d15ead5602 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 912
diff changeset
57 strftime(toybuf, sizeof(toybuf), "%Y-%m-%d %H:%M:%S",
91d15ead5602 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 912
diff changeset
58 localtime(&(ts->tv_sec)));
91d15ead5602 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 912
diff changeset
59 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
60 }
68d6c1ce7bba Add stat submission to new "pending" directory, along with infrastructure to support pending.
Rob Landley <rob@landley.net>
parents:
diff changeset
61
912
f4f5132d5ac7 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 911
diff changeset
62 static int print_stat(char type)
f4f5132d5ac7 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 911
diff changeset
63 {
914
91d15ead5602 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 912
diff changeset
64 struct stat *stat = (struct stat *)&TT.stat;
912
f4f5132d5ac7 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 911
diff changeset
65
911
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
66 switch (type) {
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
67 case 'a':
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
68 xprintf("%04lo", stat->st_mode & ~S_IFMT);
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
69 break;
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
70 case 'A':
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
71 xprintf("%s", TT.access_str);
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
72 break;
914
91d15ead5602 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 912
diff changeset
73
911
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
74 case 'b':
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
75 xprintf("%llu", stat->st_blocks);
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
76 break;
914
91d15ead5602 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 912
diff changeset
77
911
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
78 case 'B':
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
79 xprintf("%lu", stat->st_blksize);
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 'd':
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
82 xprintf("%ldd", stat->st_dev);
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 'D':
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
85 xprintf("%llxh", stat->st_dev);
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 'f':
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
88 xprintf("%lx", stat->st_mode);
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 'F':
912
f4f5132d5ac7 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 911
diff changeset
91 xprintf("%s", TT.ftname);
911
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 'g':
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
94 xprintf("%lu", stat->st_gid);
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 'G':
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
97 xprintf("%8s", TT.user_name->pw_name);
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 'h':
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
100 xprintf("%lu", stat->st_nlink);
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 'i':
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
103 xprintf("%llu", stat->st_ino);
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 'N':
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
106 xprintf("`%s'", *toys.optargs);
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 'o':
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
109 xprintf("%lu", stat->st_blksize);
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 's':
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
112 xprintf("%llu", stat->st_size);
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 'u':
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
115 xprintf("%lu", stat->st_uid);
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 'U':
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
118 xprintf("%8s", TT.user_name->pw_name);
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
119 break;
914
91d15ead5602 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 912
diff changeset
120
911
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
121 case 'x':
914
91d15ead5602 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 912
diff changeset
122 date_stat_format((void *)&stat->st_atime);
911
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
123 break;
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
124 case 'X':
914
91d15ead5602 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 912
diff changeset
125 xprintf("%llu", (long long)stat->st_atime);
911
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
126 break;
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
127 case 'y':
914
91d15ead5602 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 912
diff changeset
128 date_stat_format((void *)&stat->st_mtime);
911
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
129 break;
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
130 case 'Y':
914
91d15ead5602 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 912
diff changeset
131 xprintf("%llu", (long long)stat->st_mtime);
911
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
132 break;
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
133 case 'z':
914
91d15ead5602 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 912
diff changeset
134 date_stat_format((void *)&stat->st_ctime);
911
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
135 break;
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
136 case 'Z':
914
91d15ead5602 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 912
diff changeset
137 xprintf("%llu", (long long)stat->st_ctime);
911
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
138 break;
914
91d15ead5602 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 912
diff changeset
139
911
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
140 default:
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
141 return 1;
871
8aa07b575cd6 stat: Reindent from 4 to 2 spaces
Felix Janda <felix.janda@posteo.de>
parents: 811
diff changeset
142 }
911
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
143 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
144 }
68d6c1ce7bba Add stat submission to new "pending" directory, along with infrastructure to support pending.
Rob Landley <rob@landley.net>
parents:
diff changeset
145
911
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
146 static int print_statfs(char type) {
914
91d15ead5602 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 912
diff changeset
147 struct statfs *statfs = (struct statfs *)&TT.stat;
912
f4f5132d5ac7 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 911
diff changeset
148
911
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
149 switch (type) {
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
150 case 'a':
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
151 xprintf("%lu", statfs->f_bavail);
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
152 break;
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
153 case 'b':
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
154 xprintf("%lu", statfs->f_blocks);
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
155 break;
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
156 case 'c':
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
157 xprintf("%lu", statfs->f_files);
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
158 break;
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
159 case 'd':
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
160 xprintf("%lu", statfs->f_ffree);
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
161 break;
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
162 case 'f':
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
163 xprintf("%lu", statfs->f_bfree);
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
164 break;
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
165 case 'i':
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
166 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
167 break;
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
168 case 'l':
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
169 xprintf("%ld", statfs->f_namelen);
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
170 break;
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
171 case 's':
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
172 xprintf("%d", statfs->f_frsize);
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
173 break;
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
174 case 'S':
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
175 xprintf("%d", statfs->f_bsize);
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
176 break;
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
177 case 't':
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
178 xprintf("%lx", statfs->f_type);
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
179 break;
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
180 default:
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
181 return 1;
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
182 }
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
183 return 0;
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
184 }
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
185
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
186 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
187 {
914
91d15ead5602 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 912
diff changeset
188 struct stat *statf = (struct stat*)&TT.stat;
912
f4f5132d5ac7 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 911
diff changeset
189 char *types = "character device\0directory\0block device\0" \
f4f5132d5ac7 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 911
diff changeset
190 "regular file\0symbolic link\0socket\0FIFO (named pipe)";
f4f5132d5ac7 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 911
diff changeset
191 int i, filetype;
910
1b77a81859f6 stat: Add support for stat'ing multiple files
Felix Janda <felix.janda@posteo.de>
parents: 886
diff changeset
192
911
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
193 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
194
912
f4f5132d5ac7 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 911
diff changeset
195 filetype = statf->st_mode & S_IFMT;
f4f5132d5ac7 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 911
diff changeset
196 TT.ftname = types;
f4f5132d5ac7 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 911
diff changeset
197 for (i = 1; filetype != (i*8192) && i < 7; i++)
f4f5132d5ac7 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 911
diff changeset
198 TT.ftname += strlen(TT.ftname)+1;
f4f5132d5ac7 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 911
diff changeset
199 if (!statf->st_size && filetype == S_IFREG)
f4f5132d5ac7 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 911
diff changeset
200 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
201
1b77a81859f6 stat: Add support for stat'ing multiple files
Felix Janda <felix.janda@posteo.de>
parents: 886
diff changeset
202 // check user and group name
911
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
203 TT.user_name = getpwuid(statf->st_uid);
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
204 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
205 // 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
206 format_mode(&TT.access_str, statf->st_mode);
912
f4f5132d5ac7 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 911
diff changeset
207
910
1b77a81859f6 stat: Add support for stat'ing multiple files
Felix Janda <felix.janda@posteo.de>
parents: 886
diff changeset
208 return 0;
1b77a81859f6 stat: Add support for stat'ing multiple files
Felix Janda <felix.janda@posteo.de>
parents: 886
diff changeset
209 }
1b77a81859f6 stat: Add support for stat'ing multiple files
Felix Janda <felix.janda@posteo.de>
parents: 886
diff changeset
210
911
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
211 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
212 {
914
91d15ead5602 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 912
diff changeset
213 return statfs(path, (void *)&TT.stat) < 0;
910
1b77a81859f6 stat: Add support for stat'ing multiple files
Felix Janda <felix.janda@posteo.de>
parents: 886
diff changeset
214 }
1b77a81859f6 stat: Add support for stat'ing multiple files
Felix Janda <felix.janda@posteo.de>
parents: 886
diff changeset
215
810
874d2e646f2d Fix whitespace in submitted stat command.
Rob Landley <rob@landley.net>
parents: 747
diff changeset
216 void stat_main(void)
874d2e646f2d Fix whitespace in submitted stat command.
Rob Landley <rob@landley.net>
parents: 747
diff changeset
217 {
911
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
218 struct {
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
219 char *fmt;
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
220 int (*do_it)(char*);
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
221 int (*print_it)(char);
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
222 } d, ds[2] = {
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
223 {" File: %N\n"
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
224 " 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
225 "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
226 "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
227 "Access: %x\nModify: %y\nChange: %z",
914
91d15ead5602 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 912
diff changeset
228 do_stat, print_stat},
911
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
229 {" File: \"%n\"\n"
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
230 " ID: %i Namelen: %l Type: %t\n"
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
231 "Block Size: %s Fundamental block size: %S\n"
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
232 "Blocks: Total: %b\tFree: %f\tAvailable: %a\n"
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
233 "Inodes: Total: %c\tFree: %d",
914
91d15ead5602 Stat cleanup.
Rob Landley <rob@landley.net>
parents: 912
diff changeset
234 do_statfs, print_statfs}
911
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
235 };
872
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
236
911
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
237 d = ds[toys.optflags & FLAG_f];
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
238 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
239
1b77a81859f6 stat: Add support for stat'ing multiple files
Felix Janda <felix.janda@posteo.de>
parents: 886
diff changeset
240 for (; *toys.optargs; toys.optargs++) {
911
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
241 char *format = d.fmt;
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
242 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
243 perror_msg("'%s'", *toys.optargs);
1b77a81859f6 stat: Add support for stat'ing multiple files
Felix Janda <felix.janda@posteo.de>
parents: 886
diff changeset
244 continue;
1b77a81859f6 stat: Add support for stat'ing multiple files
Felix Janda <felix.janda@posteo.de>
parents: 886
diff changeset
245 }
911
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
246 for (; *format; format++) {
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
247 if (*format != '%') {
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
248 xputc(*format);
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
249 continue;
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
250 }
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
251 format++;
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
252 if (*format == 'n') xprintf("%s", *toys.optargs);
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
253 else if (d.print_it(*format)) xputc(*format);
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
254 }
accabaaac666 stat: Separate stat and statfs
Felix Janda <felix.janda@posteo.de>
parents: 910
diff changeset
255 xputc('\n');
910
1b77a81859f6 stat: Add support for stat'ing multiple files
Felix Janda <felix.janda@posteo.de>
parents: 886
diff changeset
256 }
747
68d6c1ce7bba Add stat submission to new "pending" directory, along with infrastructure to support pending.
Rob Landley <rob@landley.net>
parents:
diff changeset
257 }