stat.c revision 1.14 1 /* $NetBSD: stat.c,v 1.14 2003/10/26 20:43:13 chs Exp $ */
2
3 /*
4 * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Brown.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 #if defined(__RCSID) && !defined(lint)
41 __RCSID("$NetBSD: stat.c,v 1.14 2003/10/26 20:43:13 chs Exp $");
42 #endif
43
44 #if HAVE_CONFIG_H
45 #include "config.h"
46 #else /* HAVE_CONFIG_H */
47 #define HAVE_STRUCT_STAT_ST_FLAGS 1
48 #define HAVE_STRUCT_STAT_ST_GEN 1
49 #define HAVE_STRUCT_STAT_ST_BIRTHTIME 1
50 #define HAVE_STRUCT_STAT_ST_MTIMENSEC 1
51 #define HAVE_DEVNAME 1
52 #endif /* HAVE_CONFIG_H */
53
54 #include <sys/types.h>
55 #include <sys/stat.h>
56
57 #include <ctype.h>
58 #include <err.h>
59 #include <grp.h>
60 #include <limits.h>
61 #include <pwd.h>
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <string.h>
65 #include <time.h>
66 #include <unistd.h>
67
68 #if HAVE_STRUCT_STAT_ST_FLAGS
69 #define DEF_F "%#Xf "
70 #define RAW_F "%f "
71 #define SHELL_F " st_flags=%f"
72 #else /* HAVE_STRUCT_STAT_ST_FLAGS */
73 #define DEF_F
74 #define RAW_F
75 #define SHELL_F
76 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
77
78 #if HAVE_STRUCT_STAT_ST_BIRTHTIME
79 #define DEF_B "\"%SB\" "
80 #define RAW_B "%B "
81 #define SHELL_B "st_birthtime=%B "
82 #else /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
83 #define DEF_B
84 #define RAW_B
85 #define SHELL_B
86 #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
87
88 #if HAVE_STRUCT_STAT_ST_ATIM
89 #define st_atimespec st_atim
90 #define st_ctimespec st_ctim
91 #define st_mtimespec st_mtim
92 #endif /* HAVE_STRUCT_STAT_ST_ATIM */
93
94 #define DEF_FORMAT \
95 "%d %i %Sp %l %Su %Sg %r %z \"%Sa\" \"%Sm\" \"%Sc\" " DEF_B \
96 "%k %b " DEF_F "%N"
97 #define RAW_FORMAT "%d %i %#p %l %u %g %r %z %a %m %c " RAW_B \
98 "%k %b " RAW_F "%N"
99 #define LS_FORMAT "%Sp %l %Su %Sg %Z %Sm %N%SY"
100 #define LSF_FORMAT "%Sp %l %Su %Sg %Z %Sm %N%T%SY"
101 #define SHELL_FORMAT \
102 "st_dev=%d st_ino=%i st_mode=%#p st_nlink=%l " \
103 "st_uid=%u st_gid=%g st_rdev=%r st_size=%z " \
104 "st_atime=%a st_mtime=%m st_ctime=%c " SHELL_B \
105 "st_blksize=%k st_blocks=%b" SHELL_F
106 #define LINUX_FORMAT \
107 " File: \"%N\"%n" \
108 " Size: %-11z FileType: %HT%n" \
109 " Mode: (%04OLp/%.10Sp) Uid: (%5u/%8Su) Gid: (%5g/%8Sg)%n" \
110 "Device: %Hd,%Ld Inode: %i Links: %l%n" \
111 "Access: %Sa%n" \
112 "Modify: %Sm%n" \
113 "Change: %Sc"
114
115 #define TIME_FORMAT "%b %e %T %Y"
116
117 #define FLAG_POUND 0x01
118 #define FLAG_SPACE 0x02
119 #define FLAG_PLUS 0x04
120 #define FLAG_ZERO 0x08
121 #define FLAG_MINUS 0x10
122
123 /*
124 * These format characters must all be unique, except the magic one.
125 */
126 #define FMT_MAGIC '%'
127 #define FMT_DOT '.'
128
129 #define SIMPLE_NEWLINE 'n'
130 #define SIMPLE_TAB 't'
131 #define SIMPLE_PERCENT '%'
132 #define SIMPLE_NUMBER '@'
133
134 #define FMT_POUND '#'
135 #define FMT_SPACE ' '
136 #define FMT_PLUS '+'
137 #define FMT_ZERO '0'
138 #define FMT_MINUS '-'
139
140 #define FMT_DECIMAL 'D'
141 #define FMT_OCTAL 'O'
142 #define FMT_UNSIGNED 'U'
143 #define FMT_HEX 'X'
144 #define FMT_FLOAT 'F'
145 #define FMT_STRING 'S'
146
147 #define FMTF_DECIMAL 0x01
148 #define FMTF_OCTAL 0x02
149 #define FMTF_UNSIGNED 0x04
150 #define FMTF_HEX 0x08
151 #define FMTF_FLOAT 0x10
152 #define FMTF_STRING 0x20
153
154 #define HIGH_PIECE 'H'
155 #define MIDDLE_PIECE 'M'
156 #define LOW_PIECE 'L'
157
158 #define SHOW_st_dev 'd'
159 #define SHOW_st_ino 'i'
160 #define SHOW_st_mode 'p'
161 #define SHOW_st_nlink 'l'
162 #define SHOW_st_uid 'u'
163 #define SHOW_st_gid 'g'
164 #define SHOW_st_rdev 'r'
165 #define SHOW_st_atime 'a'
166 #define SHOW_st_mtime 'm'
167 #define SHOW_st_ctime 'c'
168 #define SHOW_st_btime 'B'
169 #define SHOW_st_size 'z'
170 #define SHOW_st_blocks 'b'
171 #define SHOW_st_blksize 'k'
172 #define SHOW_st_flags 'f'
173 #define SHOW_st_gen 'v'
174 #define SHOW_symlink 'Y'
175 #define SHOW_filetype 'T'
176 #define SHOW_filename 'N'
177 #define SHOW_sizerdev 'Z'
178
179 void usage(const char *);
180 void output(const struct stat *, const char *,
181 const char *, int, int, int);
182 int format1(const struct stat *, /* stat info */
183 const char *, /* the file name */
184 const char *, int, /* the format string itself */
185 char *, size_t, /* a place to put the output */
186 int, int, int, int, /* the parsed format */
187 int, int);
188
189 char *timefmt;
190 int linkfail;
191
192 #define addchar(s, c, nl) \
193 do { \
194 (void)fputc((c), (s)); \
195 (*nl) = ((c) == '\n'); \
196 } while (0/*CONSTCOND*/)
197
198 int
199 main(int argc, char *argv[])
200 {
201 struct stat st;
202 int ch, rc, errs, am_readlink;
203 int lsF, fmtchar, usestat, fn, nonl, quiet;
204 char *statfmt, *options, *synopsis;
205
206 am_readlink = 0;
207 lsF = 0;
208 fmtchar = '\0';
209 usestat = 0;
210 nonl = 0;
211 quiet = 0;
212 linkfail = 0;
213 statfmt = NULL;
214 timefmt = NULL;
215
216 if (strcmp(getprogname(), "readlink") == 0) {
217 am_readlink = 1;
218 options = "n";
219 synopsis = "[-n] [file ...]";
220 statfmt = "%Y";
221 fmtchar = 'f';
222 quiet = 1;
223 } else {
224 options = "f:FlLnqrst:x";
225 synopsis = "[-FlLnqrsx] [-f format] [-t timefmt] [file ...]";
226 }
227
228 while ((ch = getopt(argc, argv, options)) != -1)
229 switch (ch) {
230 case 'F':
231 lsF = 1;
232 break;
233 case 'L':
234 usestat = 1;
235 break;
236 case 'n':
237 nonl = 1;
238 break;
239 case 'q':
240 quiet = 1;
241 break;
242 case 'f':
243 statfmt = optarg;
244 /* FALLTHROUGH */
245 case 'l':
246 case 'r':
247 case 's':
248 case 'x':
249 if (fmtchar != 0)
250 errx(1, "can't use format '%c' with '%c'",
251 fmtchar, ch);
252 fmtchar = ch;
253 break;
254 case 't':
255 timefmt = optarg;
256 break;
257 default:
258 usage(synopsis);
259 }
260
261 argc -= optind;
262 argv += optind;
263 fn = 1;
264
265 if (fmtchar == '\0') {
266 if (lsF)
267 fmtchar = 'l';
268 else {
269 fmtchar = 'f';
270 statfmt = DEF_FORMAT;
271 }
272 }
273
274 if (lsF && fmtchar != 'l')
275 errx(1, "can't use format '%c' with -F", fmtchar);
276
277 switch (fmtchar) {
278 case 'f':
279 /* statfmt already set */
280 break;
281 case 'l':
282 statfmt = lsF ? LSF_FORMAT : LS_FORMAT;
283 break;
284 case 'r':
285 statfmt = RAW_FORMAT;
286 break;
287 case 's':
288 statfmt = SHELL_FORMAT;
289 break;
290 case 'x':
291 statfmt = LINUX_FORMAT;
292 if (timefmt == NULL)
293 timefmt = "%c";
294 break;
295 default:
296 usage(synopsis);
297 /*NOTREACHED*/
298 }
299
300 if (timefmt == NULL)
301 timefmt = TIME_FORMAT;
302
303 errs = 0;
304 do {
305 if (argc == 0)
306 rc = fstat(STDIN_FILENO, &st);
307 else if (usestat)
308 rc = stat(argv[0], &st);
309 else
310 rc = lstat(argv[0], &st);
311
312 if (rc == -1) {
313 errs = 1;
314 linkfail = 1;
315 if (!quiet)
316 warn("%s: stat",
317 argc == 0 ? "(stdin)" : argv[0]);
318 }
319 else
320 output(&st, argv[0], statfmt, fn, nonl, quiet);
321
322 argv++;
323 argc--;
324 fn++;
325 } while (argc > 0);
326
327 return (am_readlink ? linkfail : errs);
328 }
329
330 void
331 usage(const char *synopsis)
332 {
333
334 (void)fprintf(stderr, "usage: %s %s\n", getprogname(), synopsis);
335 exit(1);
336 }
337
338 /*
339 * Parses a format string.
340 */
341 void
342 output(const struct stat *st, const char *file,
343 const char *statfmt, int fn, int nonl, int quiet)
344 {
345 int flags, size, prec, ofmt, hilo, what;
346 char buf[PATH_MAX];
347 const char *subfmt;
348 int nl, t, i;
349
350 nl = 1;
351 while (*statfmt != '\0') {
352
353 /*
354 * Non-format characters go straight out.
355 */
356 if (*statfmt != FMT_MAGIC) {
357 addchar(stdout, *statfmt, &nl);
358 statfmt++;
359 continue;
360 }
361
362 /*
363 * The current format "substring" starts here,
364 * and then we skip the magic.
365 */
366 subfmt = statfmt;
367 statfmt++;
368
369 /*
370 * Some simple one-character "formats".
371 */
372 switch (*statfmt) {
373 case SIMPLE_NEWLINE:
374 addchar(stdout, '\n', &nl);
375 statfmt++;
376 continue;
377 case SIMPLE_TAB:
378 addchar(stdout, '\t', &nl);
379 statfmt++;
380 continue;
381 case SIMPLE_PERCENT:
382 addchar(stdout, '%', &nl);
383 statfmt++;
384 continue;
385 case SIMPLE_NUMBER: {
386 char num[12], *p;
387
388 snprintf(num, sizeof(num), "%d", fn);
389 for (p = &num[0]; *p; p++)
390 addchar(stdout, *p, &nl);
391 statfmt++;
392 continue;
393 }
394 }
395
396 /*
397 * This must be an actual format string. Format strings are
398 * similar to printf(3) formats up to a point, and are of
399 * the form:
400 *
401 * % required start of format
402 * [-# +0] opt. format characters
403 * size opt. field width
404 * . opt. decimal separator, followed by
405 * prec opt. precision
406 * fmt opt. output specifier (string, numeric, etc.)
407 * sub opt. sub field specifier (high, middle, low)
408 * datum required field specifier (size, mode, etc)
409 *
410 * Only the % and the datum selector are required. All data
411 * have reasonable default output forms. The "sub" specifier
412 * only applies to certain data (mode, dev, rdev, filetype).
413 * The symlink output defaults to STRING, yet will only emit
414 * the leading " -> " if STRING is explicitly specified. The
415 * sizerdev datum will generate rdev output for character or
416 * block devices, and size output for all others.
417 */
418 flags = 0;
419 do {
420 if (*statfmt == FMT_POUND)
421 flags |= FLAG_POUND;
422 else if (*statfmt == FMT_SPACE)
423 flags |= FLAG_SPACE;
424 else if (*statfmt == FMT_PLUS)
425 flags |= FLAG_PLUS;
426 else if (*statfmt == FMT_ZERO)
427 flags |= FLAG_ZERO;
428 else if (*statfmt == FMT_MINUS)
429 flags |= FLAG_MINUS;
430 else
431 break;
432 statfmt++;
433 } while (1/*CONSTCOND*/);
434
435 size = -1;
436 if (isdigit((unsigned)*statfmt)) {
437 size = 0;
438 while (isdigit((unsigned)*statfmt)) {
439 size = (size * 10) + (*statfmt - '0');
440 statfmt++;
441 if (size < 0)
442 goto badfmt;
443 }
444 }
445
446 prec = -1;
447 if (*statfmt == FMT_DOT) {
448 statfmt++;
449
450 prec = 0;
451 while (isdigit((unsigned)*statfmt)) {
452 prec = (prec * 10) + (*statfmt - '0');
453 statfmt++;
454 if (prec < 0)
455 goto badfmt;
456 }
457 }
458
459 #define fmtcase(x, y) case (y): (x) = (y); statfmt++; break
460 #define fmtcasef(x, y, z) case (y): (x) = (z); statfmt++; break
461 switch (*statfmt) {
462 fmtcasef(ofmt, FMT_DECIMAL, FMTF_DECIMAL);
463 fmtcasef(ofmt, FMT_OCTAL, FMTF_OCTAL);
464 fmtcasef(ofmt, FMT_UNSIGNED, FMTF_UNSIGNED);
465 fmtcasef(ofmt, FMT_HEX, FMTF_HEX);
466 fmtcasef(ofmt, FMT_FLOAT, FMTF_FLOAT);
467 fmtcasef(ofmt, FMT_STRING, FMTF_STRING);
468 default:
469 ofmt = 0;
470 break;
471 }
472
473 switch (*statfmt) {
474 fmtcase(hilo, HIGH_PIECE);
475 fmtcase(hilo, MIDDLE_PIECE);
476 fmtcase(hilo, LOW_PIECE);
477 default:
478 hilo = 0;
479 break;
480 }
481
482 switch (*statfmt) {
483 fmtcase(what, SHOW_st_dev);
484 fmtcase(what, SHOW_st_ino);
485 fmtcase(what, SHOW_st_mode);
486 fmtcase(what, SHOW_st_nlink);
487 fmtcase(what, SHOW_st_uid);
488 fmtcase(what, SHOW_st_gid);
489 fmtcase(what, SHOW_st_rdev);
490 fmtcase(what, SHOW_st_atime);
491 fmtcase(what, SHOW_st_mtime);
492 fmtcase(what, SHOW_st_ctime);
493 fmtcase(what, SHOW_st_btime);
494 fmtcase(what, SHOW_st_size);
495 fmtcase(what, SHOW_st_blocks);
496 fmtcase(what, SHOW_st_blksize);
497 fmtcase(what, SHOW_st_flags);
498 fmtcase(what, SHOW_st_gen);
499 fmtcase(what, SHOW_symlink);
500 fmtcase(what, SHOW_filetype);
501 fmtcase(what, SHOW_filename);
502 fmtcase(what, SHOW_sizerdev);
503 default:
504 goto badfmt;
505 }
506 #undef fmtcasef
507 #undef fmtcase
508
509 t = format1(st,
510 file,
511 subfmt, statfmt - subfmt,
512 buf, sizeof(buf),
513 flags, size, prec, ofmt, hilo, what);
514
515 for (i = 0; i < t && i < sizeof(buf); i++)
516 addchar(stdout, buf[i], &nl);
517
518 continue;
519
520 badfmt:
521 errx(1, "%.*s: bad format",
522 (int)(statfmt - subfmt + 1), subfmt);
523 }
524
525 if (!nl && !nonl)
526 (void)fputc('\n', stdout);
527 (void)fflush(stdout);
528 }
529
530 /*
531 * Arranges output according to a single parsed format substring.
532 */
533 int
534 format1(const struct stat *st,
535 const char *file,
536 const char *fmt, int flen,
537 char *buf, size_t blen,
538 int flags, int size, int prec, int ofmt,
539 int hilo, int what)
540 {
541 u_int64_t data;
542 char *sdata, lfmt[24], tmp[20];
543 char smode[12], sid[12], path[PATH_MAX + 4];
544 struct passwd *pw;
545 struct group *gr;
546 struct tm *tm;
547 time_t secs;
548 long nsecs;
549 int l, small, formats, gottime;
550
551 formats = 0;
552 small = 0;
553 gottime = 0;
554 secs = 0;
555 nsecs = 0;
556
557 /*
558 * First, pick out the data and tweak it based on hilo or
559 * specified output format (symlink output only).
560 */
561 switch (what) {
562 case SHOW_st_dev:
563 case SHOW_st_rdev:
564 small = (sizeof(st->st_dev) == 4);
565 data = (what == SHOW_st_dev) ? st->st_dev : st->st_rdev;
566 #if HAVE_DEVNAME
567 sdata = (what == SHOW_st_dev) ?
568 devname(st->st_dev, S_IFBLK) :
569 devname(st->st_rdev,
570 S_ISCHR(st->st_mode) ? S_IFCHR :
571 S_ISBLK(st->st_mode) ? S_IFBLK :
572 0U);
573 if (sdata == NULL)
574 sdata = "???";
575 #endif /* HAVE_DEVNAME */
576 if (hilo == HIGH_PIECE) {
577 data = major(data);
578 hilo = 0;
579 }
580 else if (hilo == LOW_PIECE) {
581 data = minor((unsigned)data);
582 hilo = 0;
583 }
584 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
585 #if HAVE_DEVNAME
586 FMTF_STRING;
587 #else /* HAVE_DEVNAME */
588 0;
589 #endif /* HAVE_DEVNAME */
590 if (ofmt == 0)
591 ofmt = FMTF_UNSIGNED;
592 break;
593 case SHOW_st_ino:
594 small = (sizeof(st->st_ino) == 4);
595 data = st->st_ino;
596 sdata = NULL;
597 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
598 if (ofmt == 0)
599 ofmt = FMTF_UNSIGNED;
600 break;
601 case SHOW_st_mode:
602 small = (sizeof(st->st_mode) == 4);
603 data = st->st_mode;
604 strmode(st->st_mode, smode);
605 sdata = smode;
606 l = strlen(sdata);
607 if (sdata[l - 1] == ' ')
608 sdata[--l] = '\0';
609 if (hilo == HIGH_PIECE) {
610 data >>= 12;
611 sdata += 1;
612 sdata[3] = '\0';
613 hilo = 0;
614 }
615 else if (hilo == MIDDLE_PIECE) {
616 data = (data >> 9) & 07;
617 sdata += 4;
618 sdata[3] = '\0';
619 hilo = 0;
620 }
621 else if (hilo == LOW_PIECE) {
622 data &= 0777;
623 sdata += 7;
624 sdata[3] = '\0';
625 hilo = 0;
626 }
627 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
628 FMTF_STRING;
629 if (ofmt == 0)
630 ofmt = FMTF_OCTAL;
631 break;
632 case SHOW_st_nlink:
633 small = (sizeof(st->st_dev) == 4);
634 data = st->st_nlink;
635 sdata = NULL;
636 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
637 if (ofmt == 0)
638 ofmt = FMTF_UNSIGNED;
639 break;
640 case SHOW_st_uid:
641 small = (sizeof(st->st_uid) == 4);
642 data = st->st_uid;
643 if ((pw = getpwuid(st->st_uid)) != NULL)
644 sdata = pw->pw_name;
645 else {
646 snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_uid);
647 sdata = sid;
648 }
649 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
650 FMTF_STRING;
651 if (ofmt == 0)
652 ofmt = FMTF_UNSIGNED;
653 break;
654 case SHOW_st_gid:
655 small = (sizeof(st->st_gid) == 4);
656 data = st->st_gid;
657 if ((gr = getgrgid(st->st_gid)) != NULL)
658 sdata = gr->gr_name;
659 else {
660 snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_gid);
661 sdata = sid;
662 }
663 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
664 FMTF_STRING;
665 if (ofmt == 0)
666 ofmt = FMTF_UNSIGNED;
667 break;
668 case SHOW_st_atime:
669 gottime = 1;
670 secs = st->st_atime;
671 #ifdef HAVE_STRUCT_STAT_ST_MTIMENSEC
672 nsecs = st->st_atimensec;
673 #endif
674 /* FALLTHROUGH */
675 case SHOW_st_mtime:
676 if (!gottime) {
677 secs = st->st_mtime;
678 #ifdef HAVE_STRUCT_STAT_ST_MTIMENSEC
679 nsecs = st->st_mtimensec;
680 #endif
681 }
682 /* FALLTHROUGH */
683 case SHOW_st_ctime:
684 if (!gottime) {
685 secs = st->st_ctime;
686 #ifdef HAVE_STRUCT_STAT_ST_MTIMENSEC
687 nsecs = st->st_ctimensec;
688 #endif
689 }
690 /* FALLTHROUGH */
691 #if HAVE_STRUCT_STAT_ST_BIRTHTIME
692 case SHOW_st_btime:
693 if (!gottime) {
694 secs = st->st_birthtimespec.tv_sec;
695 nsecs = st->st_birthtimespec.tv_nsec;
696 }
697 #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
698 small = (sizeof(secs) == 4);
699 data = secs;
700 small = 1;
701 tm = localtime(&secs);
702 (void)strftime(path, sizeof(path), timefmt, tm);
703 sdata = path;
704 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
705 FMTF_FLOAT | FMTF_STRING;
706 if (ofmt == 0)
707 ofmt = FMTF_DECIMAL;
708 break;
709 case SHOW_st_size:
710 small = (sizeof(st->st_size) == 4);
711 data = st->st_size;
712 sdata = NULL;
713 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
714 if (ofmt == 0)
715 ofmt = FMTF_UNSIGNED;
716 break;
717 case SHOW_st_blocks:
718 small = (sizeof(st->st_blocks) == 4);
719 data = st->st_blocks;
720 sdata = NULL;
721 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
722 if (ofmt == 0)
723 ofmt = FMTF_UNSIGNED;
724 break;
725 case SHOW_st_blksize:
726 small = (sizeof(st->st_blksize) == 4);
727 data = st->st_blksize;
728 sdata = NULL;
729 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
730 if (ofmt == 0)
731 ofmt = FMTF_UNSIGNED;
732 break;
733 #if HAVE_STRUCT_STAT_ST_FLAGS
734 case SHOW_st_flags:
735 small = (sizeof(st->st_flags) == 4);
736 data = st->st_flags;
737 sdata = NULL;
738 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
739 if (ofmt == 0)
740 ofmt = FMTF_UNSIGNED;
741 break;
742 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
743 #if HAVE_STRUCT_STAT_ST_GEN
744 case SHOW_st_gen:
745 small = (sizeof(st->st_gen) == 4);
746 data = st->st_gen;
747 sdata = NULL;
748 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
749 if (ofmt == 0)
750 ofmt = FMTF_UNSIGNED;
751 break;
752 #endif /* HAVE_STRUCT_STAT_ST_GEN */
753 case SHOW_symlink:
754 small = 0;
755 data = 0;
756 if (S_ISLNK(st->st_mode)) {
757 snprintf(path, sizeof(path), " -> ");
758 l = readlink(file, path + 4, sizeof(path) - 4 - 1);
759 if (l == -1) {
760 linkfail = 1;
761 l = 0;
762 path[0] = '\0';
763 }
764 path[l + 4] = '\0';
765 sdata = path + (ofmt == FMTF_STRING ? 0 : 4);
766 }
767 else {
768 linkfail = 1;
769 sdata = "";
770 }
771 formats = FMTF_STRING;
772 if (ofmt == 0)
773 ofmt = FMTF_STRING;
774 break;
775 case SHOW_filetype:
776 small = 0;
777 data = 0;
778 sdata = smode;
779 sdata[0] = '\0';
780 if (hilo == 0 || hilo == LOW_PIECE) {
781 switch (st->st_mode & S_IFMT) {
782 case S_IFIFO: (void)strcat(sdata, "|"); break;
783 case S_IFDIR: (void)strcat(sdata, "/"); break;
784 case S_IFREG:
785 if (st->st_mode &
786 (S_IXUSR | S_IXGRP | S_IXOTH))
787 (void)strcat(sdata, "*");
788 break;
789 case S_IFLNK: (void)strcat(sdata, "@"); break;
790 case S_IFSOCK: (void)strcat(sdata, "="); break;
791 #ifdef S_IFWHT
792 case S_IFWHT: (void)strcat(sdata, "%"); break;
793 #endif /* S_IFWHT */
794 #ifdef S_IFDOOR
795 case S_IFDOOR: (void)strcat(sdata, ">"); break;
796 #endif /* S_IFDOOR */
797 }
798 hilo = 0;
799 }
800 else if (hilo == HIGH_PIECE) {
801 switch (st->st_mode & S_IFMT) {
802 case S_IFIFO: sdata = "Fifo File"; break;
803 case S_IFCHR: sdata = "Character Device"; break;
804 case S_IFDIR: sdata = "Directory"; break;
805 case S_IFBLK: sdata = "Block Device"; break;
806 case S_IFREG: sdata = "Regular File"; break;
807 case S_IFLNK: sdata = "Symbolic Link"; break;
808 case S_IFSOCK: sdata = "Socket"; break;
809 #ifdef S_IFWHT
810 case S_IFWHT: sdata = "Whiteout File"; break;
811 #endif /* S_IFWHT */
812 #ifdef S_IFDOOR
813 case S_IFDOOR: sdata = "Door"; break;
814 #endif /* S_IFDOOR */
815 default: sdata = "???"; break;
816 }
817 hilo = 0;
818 }
819 formats = FMTF_STRING;
820 if (ofmt == 0)
821 ofmt = FMTF_STRING;
822 break;
823 case SHOW_filename:
824 small = 0;
825 data = 0;
826 if (file == NULL)
827 (void)strncpy(path, "(stdin)", sizeof(path));
828 else
829 (void)strncpy(path, file, sizeof(path));
830 sdata = path;
831 formats = FMTF_STRING;
832 if (ofmt == 0)
833 ofmt = FMTF_STRING;
834 break;
835 case SHOW_sizerdev:
836 if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
837 char majdev[20], mindev[20];
838 int l1, l2;
839
840 l1 = format1(st,
841 file,
842 fmt, flen,
843 majdev, sizeof(majdev),
844 flags, size, prec,
845 ofmt, HIGH_PIECE, SHOW_st_rdev);
846 l2 = format1(st,
847 file,
848 fmt, flen,
849 mindev, sizeof(mindev),
850 flags, size, prec,
851 ofmt, LOW_PIECE, SHOW_st_rdev);
852 return (snprintf(buf, blen, "%.*s,%.*s",
853 l1, majdev, l2, mindev));
854 }
855 else {
856 return (format1(st,
857 file,
858 fmt, flen,
859 buf, blen,
860 flags, size, prec,
861 ofmt, 0, SHOW_st_size));
862 }
863 /*NOTREACHED*/
864 default:
865 errx(1, "%.*s: bad format", (int)flen, fmt);
866 }
867
868 /*
869 * If a subdatum was specified but not supported, or an output
870 * format was selected that is not supported, that's an error.
871 */
872 if (hilo != 0 || (ofmt & formats) == 0)
873 errx(1, "%.*s: bad format", (int)flen, fmt);
874
875 /*
876 * Assemble the format string for passing to printf(3).
877 */
878 lfmt[0] = '\0';
879 (void)strcat(lfmt, "%");
880 if (flags & FLAG_POUND)
881 (void)strcat(lfmt, "#");
882 if (flags & FLAG_SPACE)
883 (void)strcat(lfmt, " ");
884 if (flags & FLAG_PLUS)
885 (void)strcat(lfmt, "+");
886 if (flags & FLAG_MINUS)
887 (void)strcat(lfmt, "-");
888 if (flags & FLAG_ZERO)
889 (void)strcat(lfmt, "0");
890
891 /*
892 * Only the timespecs support the FLOAT output format, and that
893 * requires work that differs from the other formats.
894 */
895 if (ofmt == FMTF_FLOAT) {
896 /*
897 * Nothing after the decimal point, so just print seconds.
898 */
899 if (prec == 0) {
900 if (size != -1) {
901 (void)snprintf(tmp, sizeof(tmp), "%d", size);
902 (void)strcat(lfmt, tmp);
903 }
904 (void)strcat(lfmt, "d");
905 return (snprintf(buf, blen, lfmt, secs));
906 }
907
908 /*
909 * Unspecified precision gets all the precision we have:
910 * 9 digits.
911 */
912 if (prec == -1)
913 prec = 9;
914
915 /*
916 * Adjust the size for the decimal point and the digits
917 * that will follow.
918 */
919 size -= prec + 1;
920
921 /*
922 * Any leftover size that's legitimate will be used.
923 */
924 if (size > 0) {
925 (void)snprintf(tmp, sizeof(tmp), "%d", size);
926 (void)strcat(lfmt, tmp);
927 }
928 (void)strcat(lfmt, "d");
929
930 /*
931 * The stuff after the decimal point always needs zero
932 * filling.
933 */
934 (void)strcat(lfmt, ".%0");
935
936 /*
937 * We can "print" at most nine digits of precision. The
938 * rest we will pad on at the end.
939 */
940 (void)snprintf(tmp, sizeof(tmp), "%dd", prec > 9 ? 9 : prec);
941 (void)strcat(lfmt, tmp);
942
943 /*
944 * For precision of less that nine digits, trim off the
945 * less significant figures.
946 */
947 for (; prec < 9; prec++)
948 nsecs /= 10;
949
950 /*
951 * Use the format, and then tack on any zeroes that
952 * might be required to make up the requested precision.
953 */
954 l = snprintf(buf, blen, lfmt, secs, nsecs);
955 for (; prec > 9 && l < blen; prec--, l++)
956 (void)strcat(buf, "0");
957 return (l);
958 }
959
960 /*
961 * Add on size and precision, if specified, to the format.
962 */
963 if (size != -1) {
964 (void)snprintf(tmp, sizeof(tmp), "%d", size);
965 (void)strcat(lfmt, tmp);
966 }
967 if (prec != -1) {
968 (void)snprintf(tmp, sizeof(tmp), ".%d", prec);
969 (void)strcat(lfmt, tmp);
970 }
971
972 /*
973 * String output uses the temporary sdata.
974 */
975 if (ofmt == FMTF_STRING) {
976 if (sdata == NULL)
977 errx(1, "%.*s: bad format", (int)flen, fmt);
978 (void)strcat(lfmt, "s");
979 return (snprintf(buf, blen, lfmt, sdata));
980 }
981
982 /*
983 * Ensure that sign extension does not cause bad looking output
984 * for some forms.
985 */
986 if (small && ofmt != FMTF_DECIMAL)
987 data = (u_int32_t)data;
988
989 /*
990 * The four "numeric" output forms.
991 */
992 (void)strcat(lfmt, "ll");
993 switch (ofmt) {
994 case FMTF_DECIMAL: (void)strcat(lfmt, "d"); break;
995 case FMTF_OCTAL: (void)strcat(lfmt, "o"); break;
996 case FMTF_UNSIGNED: (void)strcat(lfmt, "u"); break;
997 case FMTF_HEX: (void)strcat(lfmt, "x"); break;
998 }
999
1000 return (snprintf(buf, blen, lfmt, data));
1001 }
1002