1 /* stat.c Feb 1987 - main, printit, statit
3 * stat - a program to perform what the stat(2) call does.
5 * usage: stat [-] [-all] -<field> [-<field> ...] [file1 file2 file3 ...]
7 * where <field> is one of the struct stat fields without the leading "st_".
8 * The three times can be printed out as human times by requesting
9 * -Ctime instead of -ctime (upper case 1st letter).
10 * - means take the file names from stdin.
12 * no files means all fds.
14 * output: if only one field is specified, that fields' contents are printed.
15 * if more than one field is specified, the output is
16 * file filed1: f1val, field2: f2val, etc
18 * written: Larry McVoy, (mcvoy@rsch.wisc.edu)
21 # define ALLDEF /* Make -all default. (kjb) */
23 # include <sys/types.h>
31 # include <sys/stat.h>
32 # define addr(x) ((void*) &sbuf.x)
33 # define size(x) sizeof(sbuf.x)
34 # define equal(s, t) (strcmp(s, t) == 0)
36 # define PATH_MAX 1024
38 # undef LS_ADDS_SPACE /* AT&T Unix PC, ls prints "file[* /]" */
39 /* This makes stat fail. */
41 # ifndef _MINIX /* All but Minix have u_* and st_blocks */
46 # define S_IREAD S_IRUSR
47 # define S_IWRITE S_IWUSR
48 # define S_IEXEC S_IXUSR
60 char* f_name
; /* field name in stat */
61 u_char
* f_addr
; /* address of the field in sbuf */
62 u_short f_size
; /* size of the object, needed for pointer arith */
63 u_short f_print
; /* show this field? */
65 { "dev", addr(st_dev
), size(st_dev
), 0 },
66 { "ino", addr(st_ino
), size(st_ino
), 0 },
67 { "mode", addr(st_mode
), size(st_mode
), 0 },
68 { "nlink", addr(st_nlink
), size(st_nlink
), 0 },
69 { "uid", addr(st_uid
), size(st_uid
), 0 },
70 { "gid", addr(st_gid
), size(st_gid
), 0 },
71 { "rdev", addr(st_rdev
), size(st_rdev
), 0 },
72 { "size", addr(st_size
), size(st_size
), 0 },
73 { "Atime", addr(st_atime
), size(st_atime
), 0 },
74 { "atime", addr(st_atime
), size(st_atime
), 0 },
75 { "Mtime", addr(st_mtime
), size(st_mtime
), 0 },
76 { "mtime", addr(st_mtime
), size(st_mtime
), 0 },
77 { "Ctime", addr(st_ctime
), size(st_ctime
), 0 },
78 { "ctime", addr(st_ctime
), size(st_ctime
), 0 },
80 { "blksize", addr(st_blksize
), size(st_blksize
), 0 },
81 { "blocks", addr(st_blocks
), size(st_blocks
), 0 },
86 void printstat(struct stat
*sbuf
, int nprint
);
87 void printit(struct stat
* sb
, struct field
* f
, int n
);
88 void rwx(mode_t mode
, char *bit
);
93 int main(int ac
, char** av
)
95 int i
, j
, nprint
= 0, files
= 0;
96 char buf
[PATH_MAX
], *check
;
97 int sym
=0, ret
=0, from_stdin
= 0;
101 if ((arg0
= strrchr(av
[0], '/')) == NULL
) arg0
= av
[0]; else arg0
++;
103 if (equal(arg0
, "lstat")) sym
= 1;
104 if (equal(arg0
, "readlink")) do_readlink
= 1;
107 if (ac
> 1 && equal(av
[i
= 1], "-"))
110 for (i
= 1; i
<ac
; i
++) {
111 if (av
[i
][0] == '-') {
112 if (equal(av
[i
], "-")) {
117 if (equal("-all", av
[i
])) {
118 for (j
=0; fields
[j
].f_name
; j
++)
119 nprint
++, fields
[j
].f_print
++;
122 if (equal("-s", av
[i
])) {
128 fd
= strtoul(av
[i
]+1, &check
, 0);
129 if (check
!= av
[i
]+1 && *check
== '\0')
134 for (j
=0; fields
[j
].f_name
; j
++)
135 if (equal(fields
[j
].f_name
, &av
[i
][1])) {
136 nprint
++, fields
[j
].f_print
++;
139 if (!fields
[j
].f_name
) {
140 if (!equal("-?", av
[i
])) {
141 fprintf(stderr
, "stat: %s: bad field\n", av
[i
]);
153 for (j
=0; fields
[j
].f_name
; j
++)
154 nprint
++, fields
[j
].f_print
++;
159 files
++; /* We don't know how many files come from stdin. */
161 if (files
== 0) { /* Stat all file descriptors. */
162 if(do_readlink
) return 0;
163 for (i
= 0; i
<OPEN_MAX
; i
++) {
164 err
= fstat(i
, &sbuf
);
165 if (err
== -1 && errno
== EBADF
)
168 if (!first_file
) fputc('\n', stdout
);
169 printf("fd %d:\n", i
);
170 printstat(&sbuf
, nprint
);
173 fprintf(stderr
, "%s: fd %d: %s\n", arg0
, i
, strerror(errno
));
180 for (i
=1; i
<ac
; i
++) {
181 if (equal(av
[i
], "-")) {
182 while (fgets(buf
, sizeof(buf
), stdin
)) {
183 char *p
= strchr(buf
, '\n');
189 if((n
=readlink(buf
, sbuf
, sizeof(sbuf
)-1)) < 0) {
194 printf("%s: %s\n", buf
, sbuf
);
198 if (!sym
) err
= stat(av
[i
], &sbuf
);
199 if (sym
|| (err
!= 0 && errno
== ENOENT
)) {
200 err
= lstat(av
[i
], &sbuf
);
203 fprintf(stderr
, "%s: %s: %s\n",
204 arg0
, av
[i
], strerror(errno
));
208 if (!first_file
) fputc('\n', stdout
);
209 printf("%s:\n", buf
);
210 printstat(&sbuf
, nprint
);
215 if (av
[i
][0] == '-') {
216 fd
= strtoul(av
[i
]+1, &check
, 10);
217 if (check
== av
[i
]+1 || *check
!= '\0') continue;
223 err
= fstat((int) fd
, &sbuf
);
226 if (!first_file
) fputc('\n', stdout
);
227 if (files
!= 1) printf("fd %lu:\n", fd
);
228 printstat(&sbuf
, nprint
);
231 fprintf(stderr
, "fd %lu: %s\n", fd
, strerror(errno
));
239 if((n
=err
=readlink(av
[i
], sbuf
, sizeof(sbuf
)-1)) < 0) {
244 printf("%s: %s\n", av
[i
], sbuf
);
247 if (!sym
) err
= stat(av
[i
], &sbuf
);
248 if (sym
|| (err
!= 0 && errno
== ENOENT
)) err
= lstat(av
[i
], &sbuf
);
250 if (!first_file
) fputc('\n', stdout
);
251 if (files
!= 1) printf("%s:\n", av
[i
]);
252 printstat(&sbuf
, nprint
);
255 fprintf(stderr
, "%s: %s: %s\n", arg0
, av
[i
], strerror(errno
));
262 /*------------------------------------------------------------------30/Jan/87-*
263 * printstat(file, nprint) - do the work
264 *----------------------------------------------------------------larry mcvoy-*/
265 void printstat(struct stat
*sbuf
, int nprint
)
270 for (j
=0; fields
[j
].f_name
; j
++) {
271 if (fields
[j
].f_print
) {
272 if (!first_field
) fputc('\n', stdout
);
273 printit(sbuf
, &fields
[j
], nprint
);
281 /*------------------------------------------------------------------30/Jan/87-*
282 * printit(sb, f, n) - print the field
284 * Inputs -> (struct stat*), (struct field*), (int)
286 * Results -> Displays the field, with special handling of weird fields like
287 * mode and mtime. The mode field is dumped in octal, followed
288 * by one or more of the S_IF<X> and/or S_I<X> values.
289 *----------------------------------------------------------------larry mcvoy-*/
290 void printit(struct stat
* sb
, struct field
* f
, int n
)
293 printf("%s: ", f
->f_name
);
294 if (equal(f
->f_name
, "mode")) {
295 /* This lot changed to my personal liking. (kjb) */
298 printf("%07lo, ", (u_long
) sb
->st_mode
);
300 strcpy(bit
, "----------");
302 switch (sb
->st_mode
&S_IFMT
) {
303 case S_IFDIR
: bit
[0]='d'; break;
305 case S_IFFIFO
: bit
[0]='p'; break;
307 case S_IFCHR
: bit
[0]='c'; break;
308 case S_IFBLK
: bit
[0]='b'; break;
310 case S_IFSOCK
: bit
[0]='S'; break;
313 case S_IFMPC
: bit
[0]='C'; break;
316 case S_IFMPB
: bit
[0]='B'; break;
319 case S_IFLNK
: bit
[0]='l'; break;
322 rwx(sb
->st_mode
, bit
+1);
323 rwx(sb
->st_mode
<<3, bit
+4);
324 rwx(sb
->st_mode
<<6, bit
+7);
325 if (sb
->st_mode
&S_ISUID
) bit
[3]='s';
326 if (sb
->st_mode
&S_ISGID
) bit
[6]='s';
327 if (sb
->st_mode
&S_ISVTX
) bit
[9]='t';
328 printf("\"%s\"", bit
);
330 /* times in human form, uppercase first letter */
331 else if (equal("Ctime", f
->f_name
)) {
332 printf("%.24s (%lu)", ctime(&sb
->st_ctime
), (u_long
) sb
->st_ctime
);
335 else if (equal("Mtime", f
->f_name
)) {
336 printf("%.24s (%lu)", ctime(&sb
->st_mtime
), (u_long
) sb
->st_mtime
);
339 else if (equal("Atime", f
->f_name
)) {
340 printf("%.24s (%lu)", ctime(&sb
->st_atime
), (u_long
) sb
->st_atime
);
343 else if (equal("ctime", f
->f_name
)) {
344 printf("%lu", (u_long
) sb
->st_ctime
);
346 else if (equal("mtime", f
->f_name
)) {
347 printf("%lu", (u_long
) sb
->st_mtime
);
349 else if (equal("atime", f
->f_name
)) {
350 printf("%lu", (u_long
) sb
->st_atime
);
355 printf("%d", * (u_char
*) f
->f_addr
);
358 printf("%u", (u_int
) * (u_short
*) f
->f_addr
);
360 #if INT_MAX != SHRT_MAX
362 printf("%u", * (u_int
*) f
->f_addr
);
365 #if LONG_MAX != INT_MAX && LONG_MAX != SHRT_MAX
367 printf("%lu", * (u_long
*) f
->f_addr
);
371 fprintf(stderr
, "\nProgram error: bad '%s' field size %d\n",
372 f
->f_name
, f
->f_size
);
378 void rwx(mode_t mode
, char *bit
)
380 if (mode
&S_IREAD
) bit
[0]='r';
381 if (mode
&S_IWRITE
) bit
[1]='w';
382 if (mode
&S_IEXEC
) bit
[2]='x';
388 "Usage: %s [-] [-fd] [-all] [-s] [-field ...] [file1 ...]\n",