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