kernel: Fix buildkernel without INVARIANTS.
[dragonfly.git] / usr.bin / stat / stat.c
blobcc16ed53c0de5f80f9a4838c8788ec8b8d3261d9
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 $
40 #if HAVE_CONFIG_H
41 #include "config.h"
42 #else /* HAVE_CONFIG_H */
43 #define HAVE_STRUCT_STAT_ST_FLAGS 1
44 #define HAVE_STRUCT_STAT_ST_GEN 1
45 #define HAVE_STRUCT_STAT_ST_BIRTHTIME 0
46 #define HAVE_STRUCT_STAT_ST_MTIMENSEC 1
47 #define HAVE_DEVNAME 1
48 #endif /* HAVE_CONFIG_H */
50 #include <sys/types.h>
51 #include <sys/stat.h>
53 #include <ctype.h>
54 #include <err.h>
55 #include <grp.h>
56 #include <limits.h>
57 #include <pwd.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <time.h>
62 #include <unistd.h>
64 #if HAVE_STRUCT_STAT_ST_FLAGS
65 #define DEF_F "%#Xf "
66 #define RAW_F "%f "
67 #define SHELL_F " st_flags=%f"
68 #else /* HAVE_STRUCT_STAT_ST_FLAGS */
69 #define DEF_F
70 #define RAW_F
71 #define SHELL_F
72 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
74 #if HAVE_STRUCT_STAT_ST_BIRTHTIME
75 #define DEF_B "\"%SB\" "
76 #define RAW_B "%B "
77 #define SHELL_B "st_birthtime=%B "
78 #else /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
79 #define DEF_B
80 #define RAW_B
81 #define SHELL_B
82 #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
84 #if HAVE_STRUCT_STAT_ST_ATIM
85 #define st_atimespec st_atim
86 #define st_ctimespec st_ctim
87 #define st_mtimespec st_mtim
88 #endif /* HAVE_STRUCT_STAT_ST_ATIM */
90 #define DEF_FORMAT \
91 "%d %i %Sp %l %Su %Sg %r %z \"%Sa\" \"%Sm\" \"%Sc\" " DEF_B \
92 "%k %b " DEF_F "%N"
93 #define RAW_FORMAT "%d %i %#p %l %u %g %r %z %a %m %c " RAW_B \
94 "%k %b " RAW_F "%N"
95 #define LS_FORMAT "%Sp %l %Su %Sg %Z %Sm %N%SY"
96 #define LSF_FORMAT "%Sp %l %Su %Sg %Z %Sm %N%T%SY"
97 #define SHELL_FORMAT \
98 "st_dev=%d st_ino=%i st_mode=%#p st_nlink=%l " \
99 "st_uid=%u st_gid=%g st_rdev=%r st_size=%z " \
100 "st_atime=%a st_mtime=%m st_ctime=%c " SHELL_B \
101 "st_blksize=%k st_blocks=%b" SHELL_F
102 #define LINUX_FORMAT \
103 " File: \"%N\"%n" \
104 " Size: %-11z FileType: %HT%n" \
105 " Mode: (%04OLp/%.10Sp) Uid: (%5u/%8Su) Gid: (%5g/%8Sg)%n" \
106 "Device: %Hd,%Ld Inode: %i Links: %l%n" \
107 "Access: %Sa%n" \
108 "Modify: %Sm%n" \
109 "Change: %Sc"
111 #define TIME_FORMAT "%b %e %T %Y"
113 #define FLAG_POUND 0x01
114 #define FLAG_SPACE 0x02
115 #define FLAG_PLUS 0x04
116 #define FLAG_ZERO 0x08
117 #define FLAG_MINUS 0x10
120 * These format characters must all be unique, except the magic one.
122 #define FMT_MAGIC '%'
123 #define FMT_DOT '.'
125 #define SIMPLE_NEWLINE 'n'
126 #define SIMPLE_TAB 't'
127 #define SIMPLE_PERCENT '%'
128 #define SIMPLE_NUMBER '@'
130 #define FMT_POUND '#'
131 #define FMT_SPACE ' '
132 #define FMT_PLUS '+'
133 #define FMT_ZERO '0'
134 #define FMT_MINUS '-'
136 #define FMT_DECIMAL 'D'
137 #define FMT_OCTAL 'O'
138 #define FMT_UNSIGNED 'U'
139 #define FMT_HEX 'X'
140 #define FMT_FLOAT 'F'
141 #define FMT_STRING 'S'
143 #define FMTF_DECIMAL 0x01
144 #define FMTF_OCTAL 0x02
145 #define FMTF_UNSIGNED 0x04
146 #define FMTF_HEX 0x08
147 #define FMTF_FLOAT 0x10
148 #define FMTF_STRING 0x20
150 #define HIGH_PIECE 'H'
151 #define MIDDLE_PIECE 'M'
152 #define LOW_PIECE 'L'
154 #define SHOW_st_dev 'd'
155 #define SHOW_st_ino 'i'
156 #define SHOW_st_mode 'p'
157 #define SHOW_st_nlink 'l'
158 #define SHOW_st_uid 'u'
159 #define SHOW_st_gid 'g'
160 #define SHOW_st_rdev 'r'
161 #define SHOW_st_atime 'a'
162 #define SHOW_st_mtime 'm'
163 #define SHOW_st_ctime 'c'
164 #define SHOW_st_btime 'B'
165 #define SHOW_st_size 'z'
166 #define SHOW_st_blocks 'b'
167 #define SHOW_st_blksize 'k'
168 #define SHOW_st_flags 'f'
169 #define SHOW_st_gen 'v'
170 #define SHOW_symlink 'Y'
171 #define SHOW_filetype 'T'
172 #define SHOW_filename 'N'
173 #define SHOW_sizerdev 'Z'
175 void usage(const char *);
176 void output(const struct stat *, const char *,
177 const char *, int, int, int);
178 int format1(const struct stat *, /* stat info */
179 const char *, /* the file name */
180 const char *, int, /* the format string itself */
181 char *, size_t, /* a place to put the output */
182 int, int, int, int, /* the parsed format */
183 int, int);
185 char *timefmt;
186 int linkfail;
188 #define addchar(s, c, nl) \
189 do { \
190 (void)fputc((c), (s)); \
191 (*nl) = ((c) == '\n'); \
192 } while (0/*CONSTCOND*/)
195 main(int argc, char *argv[])
197 struct stat st;
198 int ch, rc, errs, am_readlink;
199 int lsF, fmtchar, usestat, fn, nonl, quiet;
200 char *statfmt, *options, *synopsis;
202 am_readlink = 0;
203 lsF = 0;
204 fmtchar = '\0';
205 usestat = 0;
206 nonl = 0;
207 quiet = 0;
208 linkfail = 0;
209 statfmt = NULL;
210 timefmt = NULL;
212 if (strcmp(getprogname(), "readlink") == 0) {
213 am_readlink = 1;
214 options = "n";
215 synopsis = "[-n] [file ...]";
216 statfmt = "%Y";
217 fmtchar = 'f';
218 quiet = 1;
219 } else {
220 options = "f:FlLnqrst:x";
221 synopsis = "[-FlLnqrsx] [-f format] [-t timefmt] [file ...]";
224 while ((ch = getopt(argc, argv, options)) != -1)
225 switch (ch) {
226 case 'F':
227 lsF = 1;
228 break;
229 case 'L':
230 usestat = 1;
231 break;
232 case 'n':
233 nonl = 1;
234 break;
235 case 'q':
236 quiet = 1;
237 break;
238 case 'f':
239 statfmt = optarg;
240 /* FALLTHROUGH */
241 case 'l':
242 case 'r':
243 case 's':
244 case 'x':
245 if (fmtchar != 0)
246 errx(1, "can't use format '%c' with '%c'",
247 fmtchar, ch);
248 fmtchar = ch;
249 break;
250 case 't':
251 timefmt = optarg;
252 break;
253 default:
254 usage(synopsis);
257 argc -= optind;
258 argv += optind;
259 fn = 1;
261 if (fmtchar == '\0') {
262 if (lsF)
263 fmtchar = 'l';
264 else {
265 fmtchar = 'f';
266 statfmt = DEF_FORMAT;
270 if (lsF && fmtchar != 'l')
271 errx(1, "can't use format '%c' with -F", fmtchar);
273 switch (fmtchar) {
274 case 'f':
275 /* statfmt already set */
276 break;
277 case 'l':
278 statfmt = lsF ? LSF_FORMAT : LS_FORMAT;
279 break;
280 case 'r':
281 statfmt = RAW_FORMAT;
282 break;
283 case 's':
284 statfmt = SHELL_FORMAT;
285 break;
286 case 'x':
287 statfmt = LINUX_FORMAT;
288 if (timefmt == NULL)
289 timefmt = "%c";
290 break;
291 default:
292 usage(synopsis);
293 /*NOTREACHED*/
296 if (timefmt == NULL)
297 timefmt = TIME_FORMAT;
299 errs = 0;
300 do {
301 if (argc == 0)
302 rc = fstat(STDIN_FILENO, &st);
303 else if (usestat)
304 rc = stat(argv[0], &st);
305 else
306 rc = lstat(argv[0], &st);
308 if (rc == -1) {
309 errs = 1;
310 linkfail = 1;
311 if (!quiet)
312 warn("%s: stat",
313 argc == 0 ? "(stdin)" : argv[0]);
315 else
316 output(&st, argv[0], statfmt, fn, nonl, quiet);
318 argv++;
319 argc--;
320 fn++;
321 } while (argc > 0);
323 return (am_readlink ? linkfail : errs);
326 void
327 usage(const char *synopsis)
330 (void)fprintf(stderr, "usage: %s %s\n", getprogname(), synopsis);
331 exit(1);
335 * Parses a format string.
337 void
338 output(const struct stat *st, const char *file,
339 const char *statfmt, int fn, int nonl, int quiet)
341 int flags, size, prec, ofmt, hilo, what;
342 char buf[PATH_MAX];
343 const char *subfmt;
344 int nl, t, i;
346 nl = 1;
347 while (*statfmt != '\0') {
350 * Non-format characters go straight out.
352 if (*statfmt != FMT_MAGIC) {
353 addchar(stdout, *statfmt, &nl);
354 statfmt++;
355 continue;
359 * The current format "substring" starts here,
360 * and then we skip the magic.
362 subfmt = statfmt;
363 statfmt++;
366 * Some simple one-character "formats".
368 switch (*statfmt) {
369 case SIMPLE_NEWLINE:
370 addchar(stdout, '\n', &nl);
371 statfmt++;
372 continue;
373 case SIMPLE_TAB:
374 addchar(stdout, '\t', &nl);
375 statfmt++;
376 continue;
377 case SIMPLE_PERCENT:
378 addchar(stdout, '%', &nl);
379 statfmt++;
380 continue;
381 case SIMPLE_NUMBER: {
382 char num[12], *p;
384 snprintf(num, sizeof(num), "%d", fn);
385 for (p = &num[0]; *p; p++)
386 addchar(stdout, *p, &nl);
387 statfmt++;
388 continue;
393 * This must be an actual format string. Format strings are
394 * similar to printf(3) formats up to a point, and are of
395 * the form:
397 * % required start of format
398 * [-# +0] opt. format characters
399 * size opt. field width
400 * . opt. decimal separator, followed by
401 * prec opt. precision
402 * fmt opt. output specifier (string, numeric, etc.)
403 * sub opt. sub field specifier (high, middle, low)
404 * datum required field specifier (size, mode, etc)
406 * Only the % and the datum selector are required. All data
407 * have reasonable default output forms. The "sub" specifier
408 * only applies to certain data (mode, dev, rdev, filetype).
409 * The symlink output defaults to STRING, yet will only emit
410 * the leading " -> " if STRING is explicitly specified. The
411 * sizerdev datum will generate rdev output for character or
412 * block devices, and size output for all others.
414 flags = 0;
415 do {
416 if (*statfmt == FMT_POUND)
417 flags |= FLAG_POUND;
418 else if (*statfmt == FMT_SPACE)
419 flags |= FLAG_SPACE;
420 else if (*statfmt == FMT_PLUS)
421 flags |= FLAG_PLUS;
422 else if (*statfmt == FMT_ZERO)
423 flags |= FLAG_ZERO;
424 else if (*statfmt == FMT_MINUS)
425 flags |= FLAG_MINUS;
426 else
427 break;
428 statfmt++;
429 } while (1/*CONSTCOND*/);
431 size = -1;
432 if (isdigit((unsigned)*statfmt)) {
433 size = 0;
434 while (isdigit((unsigned)*statfmt)) {
435 size = (size * 10) + (*statfmt - '0');
436 statfmt++;
437 if (size < 0)
438 goto badfmt;
442 prec = -1;
443 if (*statfmt == FMT_DOT) {
444 statfmt++;
446 prec = 0;
447 while (isdigit((unsigned)*statfmt)) {
448 prec = (prec * 10) + (*statfmt - '0');
449 statfmt++;
450 if (prec < 0)
451 goto badfmt;
455 #define fmtcase(x, y) case (y): (x) = (y); statfmt++; break
456 #define fmtcasef(x, y, z) case (y): (x) = (z); statfmt++; break
457 switch (*statfmt) {
458 fmtcasef(ofmt, FMT_DECIMAL, FMTF_DECIMAL);
459 fmtcasef(ofmt, FMT_OCTAL, FMTF_OCTAL);
460 fmtcasef(ofmt, FMT_UNSIGNED, FMTF_UNSIGNED);
461 fmtcasef(ofmt, FMT_HEX, FMTF_HEX);
462 fmtcasef(ofmt, FMT_FLOAT, FMTF_FLOAT);
463 fmtcasef(ofmt, FMT_STRING, FMTF_STRING);
464 default:
465 ofmt = 0;
466 break;
469 switch (*statfmt) {
470 fmtcase(hilo, HIGH_PIECE);
471 fmtcase(hilo, MIDDLE_PIECE);
472 fmtcase(hilo, LOW_PIECE);
473 default:
474 hilo = 0;
475 break;
478 switch (*statfmt) {
479 fmtcase(what, SHOW_st_dev);
480 fmtcase(what, SHOW_st_ino);
481 fmtcase(what, SHOW_st_mode);
482 fmtcase(what, SHOW_st_nlink);
483 fmtcase(what, SHOW_st_uid);
484 fmtcase(what, SHOW_st_gid);
485 fmtcase(what, SHOW_st_rdev);
486 fmtcase(what, SHOW_st_atime);
487 fmtcase(what, SHOW_st_mtime);
488 fmtcase(what, SHOW_st_ctime);
489 fmtcase(what, SHOW_st_btime);
490 fmtcase(what, SHOW_st_size);
491 fmtcase(what, SHOW_st_blocks);
492 fmtcase(what, SHOW_st_blksize);
493 fmtcase(what, SHOW_st_flags);
494 fmtcase(what, SHOW_st_gen);
495 fmtcase(what, SHOW_symlink);
496 fmtcase(what, SHOW_filetype);
497 fmtcase(what, SHOW_filename);
498 fmtcase(what, SHOW_sizerdev);
499 default:
500 goto badfmt;
502 #undef fmtcasef
503 #undef fmtcase
505 t = format1(st,
506 file,
507 subfmt, statfmt - subfmt,
508 buf, sizeof(buf),
509 flags, size, prec, ofmt, hilo, what);
511 for (i = 0; i < t && i < sizeof(buf); i++)
512 addchar(stdout, buf[i], &nl);
514 continue;
516 badfmt:
517 errx(1, "%.*s: bad format",
518 (int)(statfmt - subfmt + 1), subfmt);
521 if (!nl && !nonl)
522 (void)fputc('\n', stdout);
523 (void)fflush(stdout);
527 * Arranges output according to a single parsed format substring.
530 format1(const struct stat *st,
531 const char *file,
532 const char *fmt, int flen,
533 char *buf, size_t blen,
534 int flags, int size, int prec, int ofmt,
535 int hilo, int what)
537 u_int64_t data;
538 char *sdata, lfmt[24], tmp[20];
539 char smode[12], sid[12], path[PATH_MAX + 4];
540 struct passwd *pw;
541 struct group *gr;
542 const struct timespec *tsp;
543 struct timespec ts;
544 struct tm *tm;
545 int l, small, formats;
547 tsp = NULL;
548 formats = 0;
549 small = 0;
552 * First, pick out the data and tweak it based on hilo or
553 * specified output format (symlink output only).
555 switch (what) {
556 case SHOW_st_dev:
557 case SHOW_st_rdev:
558 small = (sizeof(st->st_dev) == 4);
559 data = (what == SHOW_st_dev) ? st->st_dev : st->st_rdev;
560 #if HAVE_DEVNAME
561 sdata = (what == SHOW_st_dev) ?
562 devname(st->st_dev, S_IFBLK) :
563 devname(st->st_rdev,
564 S_ISCHR(st->st_mode) ? S_IFCHR :
565 S_ISBLK(st->st_mode) ? S_IFBLK :
566 0U);
567 if (sdata == NULL)
568 sdata = "???";
569 #endif /* HAVE_DEVNAME */
570 if (hilo == HIGH_PIECE) {
571 data = major(data);
572 hilo = 0;
574 else if (hilo == LOW_PIECE) {
575 data = minor((unsigned)data);
576 hilo = 0;
578 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
579 #if HAVE_DEVNAME
580 FMTF_STRING;
581 #else /* HAVE_DEVNAME */
583 #endif /* HAVE_DEVNAME */
584 if (ofmt == 0)
585 ofmt = FMTF_UNSIGNED;
586 break;
587 case SHOW_st_ino:
588 small = (sizeof(st->st_ino) == 4);
589 data = st->st_ino;
590 sdata = NULL;
591 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
592 if (ofmt == 0)
593 ofmt = FMTF_UNSIGNED;
594 break;
595 case SHOW_st_mode:
596 small = (sizeof(st->st_mode) == 4);
597 data = st->st_mode;
598 strmode(st->st_mode, smode);
599 sdata = smode;
600 l = strlen(sdata);
601 if (sdata[l - 1] == ' ')
602 sdata[--l] = '\0';
603 if (hilo == HIGH_PIECE) {
604 data >>= 12;
605 sdata += 1;
606 sdata[3] = '\0';
607 hilo = 0;
609 else if (hilo == MIDDLE_PIECE) {
610 data = (data >> 9) & 07;
611 sdata += 4;
612 sdata[3] = '\0';
613 hilo = 0;
615 else if (hilo == LOW_PIECE) {
616 data &= 0777;
617 sdata += 7;
618 sdata[3] = '\0';
619 hilo = 0;
621 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
622 FMTF_STRING;
623 if (ofmt == 0)
624 ofmt = FMTF_OCTAL;
625 break;
626 case SHOW_st_nlink:
627 small = (sizeof(st->st_dev) == 4);
628 data = st->st_nlink;
629 sdata = NULL;
630 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
631 if (ofmt == 0)
632 ofmt = FMTF_UNSIGNED;
633 break;
634 case SHOW_st_uid:
635 small = (sizeof(st->st_uid) == 4);
636 data = st->st_uid;
637 if ((pw = getpwuid(st->st_uid)) != NULL)
638 sdata = pw->pw_name;
639 else {
640 snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_uid);
641 sdata = sid;
643 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
644 FMTF_STRING;
645 if (ofmt == 0)
646 ofmt = FMTF_UNSIGNED;
647 break;
648 case SHOW_st_gid:
649 small = (sizeof(st->st_gid) == 4);
650 data = st->st_gid;
651 if ((gr = getgrgid(st->st_gid)) != NULL)
652 sdata = gr->gr_name;
653 else {
654 snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_gid);
655 sdata = sid;
657 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
658 FMTF_STRING;
659 if (ofmt == 0)
660 ofmt = FMTF_UNSIGNED;
661 break;
662 case SHOW_st_atime:
663 tsp = &st->st_atimespec;
664 /* FALLTHROUGH */
665 case SHOW_st_mtime:
666 if (tsp == NULL)
667 tsp = &st->st_mtimespec;
668 /* FALLTHROUGH */
669 case SHOW_st_ctime:
670 if (tsp == NULL)
671 tsp = &st->st_ctimespec;
672 /* FALLTHROUGH */
673 #if HAVE_STRUCT_STAT_ST_BIRTHTIME
674 case SHOW_st_btime:
675 if (tsp == NULL)
676 tsp = &st->st_birthtimespec;
677 #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
678 ts = *tsp; /* copy so we can muck with it */
679 small = (sizeof(ts.tv_sec) == 4);
680 data = ts.tv_sec;
681 small = 1;
682 tm = localtime(&ts.tv_sec);
683 strftime(path, sizeof(path), timefmt, tm);
684 sdata = path;
685 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
686 FMTF_FLOAT | FMTF_STRING;
687 if (ofmt == 0)
688 ofmt = FMTF_DECIMAL;
689 break;
690 case SHOW_st_size:
691 small = (sizeof(st->st_size) == 4);
692 data = st->st_size;
693 sdata = NULL;
694 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
695 if (ofmt == 0)
696 ofmt = FMTF_UNSIGNED;
697 break;
698 case SHOW_st_blocks:
699 small = (sizeof(st->st_blocks) == 4);
700 data = st->st_blocks;
701 sdata = NULL;
702 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
703 if (ofmt == 0)
704 ofmt = FMTF_UNSIGNED;
705 break;
706 case SHOW_st_blksize:
707 small = (sizeof(st->st_blksize) == 4);
708 data = st->st_blksize;
709 sdata = NULL;
710 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
711 if (ofmt == 0)
712 ofmt = FMTF_UNSIGNED;
713 break;
714 #if HAVE_STRUCT_STAT_ST_FLAGS
715 case SHOW_st_flags:
716 small = (sizeof(st->st_flags) == 4);
717 data = st->st_flags;
718 sdata = NULL;
719 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
720 if (ofmt == 0)
721 ofmt = FMTF_UNSIGNED;
722 break;
723 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
724 #if HAVE_STRUCT_STAT_ST_GEN
725 case SHOW_st_gen:
726 small = (sizeof(st->st_gen) == 4);
727 data = st->st_gen;
728 sdata = NULL;
729 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
730 if (ofmt == 0)
731 ofmt = FMTF_UNSIGNED;
732 break;
733 #endif /* HAVE_STRUCT_STAT_ST_GEN */
734 case SHOW_symlink:
735 small = 0;
736 data = 0;
737 if (S_ISLNK(st->st_mode)) {
738 snprintf(path, sizeof(path), " -> ");
739 l = readlink(file, path + 4, sizeof(path) - 4 - 1);
740 if (l == -1) {
741 linkfail = 1;
742 l = 0;
743 path[0] = '\0';
745 path[l + 4] = '\0';
746 sdata = path + (ofmt == FMTF_STRING ? 0 : 4);
748 else {
749 linkfail = 1;
750 sdata = "";
752 formats = FMTF_STRING;
753 if (ofmt == 0)
754 ofmt = FMTF_STRING;
755 break;
756 case SHOW_filetype:
757 small = 0;
758 data = 0;
759 sdata = smode;
760 sdata[0] = '\0';
761 if (hilo == 0 || hilo == LOW_PIECE) {
762 switch (st->st_mode & S_IFMT) {
763 case S_IFIFO: (void)strcat(sdata, "|"); break;
764 case S_IFDIR: (void)strcat(sdata, "/"); break;
765 case S_IFREG:
766 if (st->st_mode &
767 (S_IXUSR | S_IXGRP | S_IXOTH))
768 (void)strcat(sdata, "*");
769 break;
770 case S_IFLNK: (void)strcat(sdata, "@"); break;
771 case S_IFSOCK: (void)strcat(sdata, "="); break;
772 #ifdef S_IFWHT
773 case S_IFWHT: (void)strcat(sdata, "%"); break;
774 #endif /* S_IFWHT */
775 #ifdef S_IFDOOR
776 case S_IFDOOR: (void)strcat(sdata, ">"); break;
777 #endif /* S_IFDOOR */
779 hilo = 0;
781 else if (hilo == HIGH_PIECE) {
782 switch (st->st_mode & S_IFMT) {
783 case S_IFIFO: sdata = "Fifo File"; break;
784 case S_IFCHR: sdata = "Character Device"; break;
785 case S_IFDIR: sdata = "Directory"; break;
786 case S_IFBLK: sdata = "Block Device"; break;
787 case S_IFREG: sdata = "Regular File"; break;
788 case S_IFLNK: sdata = "Symbolic Link"; break;
789 case S_IFSOCK: sdata = "Socket"; break;
790 #ifdef S_IFWHT
791 case S_IFWHT: sdata = "Whiteout File"; break;
792 #endif /* S_IFWHT */
793 #ifdef S_IFDOOR
794 case S_IFDOOR: sdata = "Door"; break;
795 #endif /* S_IFDOOR */
796 default: sdata = "???"; break;
798 hilo = 0;
800 formats = FMTF_STRING;
801 if (ofmt == 0)
802 ofmt = FMTF_STRING;
803 break;
804 case SHOW_filename:
805 small = 0;
806 data = 0;
807 if (file == NULL)
808 (void)strncpy(path, "(stdin)", sizeof(path));
809 else
810 (void)strncpy(path, file, sizeof(path));
811 sdata = path;
812 formats = FMTF_STRING;
813 if (ofmt == 0)
814 ofmt = FMTF_STRING;
815 break;
816 case SHOW_sizerdev:
817 if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
818 char majdev[20], mindev[20];
819 int l1, l2;
821 l1 = format1(st,
822 file,
823 fmt, flen,
824 majdev, sizeof(majdev),
825 flags, size, prec,
826 ofmt, HIGH_PIECE, SHOW_st_rdev);
827 l2 = format1(st,
828 file,
829 fmt, flen,
830 mindev, sizeof(mindev),
831 flags, size, prec,
832 ofmt, LOW_PIECE, SHOW_st_rdev);
833 return (snprintf(buf, blen, "%.*s,%.*s",
834 l1, majdev, l2, mindev));
836 else {
837 return (format1(st,
838 file,
839 fmt, flen,
840 buf, blen,
841 flags, size, prec,
842 ofmt, 0, SHOW_st_size));
844 /*NOTREACHED*/
845 default:
846 errx(1, "%.*s: bad format", (int)flen, fmt);
850 * If a subdatum was specified but not supported, or an output
851 * format was selected that is not supported, that's an error.
853 if (hilo != 0 || (ofmt & formats) == 0)
854 errx(1, "%.*s: bad format", (int)flen, fmt);
857 * Assemble the format string for passing to printf(3).
859 lfmt[0] = '\0';
860 (void)strcat(lfmt, "%");
861 if (flags & FLAG_POUND)
862 (void)strcat(lfmt, "#");
863 if (flags & FLAG_SPACE)
864 (void)strcat(lfmt, " ");
865 if (flags & FLAG_PLUS)
866 (void)strcat(lfmt, "+");
867 if (flags & FLAG_MINUS)
868 (void)strcat(lfmt, "-");
869 if (flags & FLAG_ZERO)
870 (void)strcat(lfmt, "0");
873 * Only the timespecs support the FLOAT output format, and that
874 * requires work that differs from the other formats.
876 if (ofmt == FMTF_FLOAT) {
878 * Nothing after the decimal point, so just print seconds.
880 if (prec == 0) {
881 if (size != -1) {
882 (void)snprintf(tmp, sizeof(tmp), "%d", size);
883 (void)strcat(lfmt, tmp);
885 (void)strcat(lfmt, "d");
886 return (snprintf(buf, blen, lfmt, ts.tv_sec));
890 * Unspecified precision gets all the precision we have:
891 * 9 digits.
893 if (prec == -1)
894 prec = 9;
897 * Adjust the size for the decimal point and the digits
898 * that will follow.
900 size -= prec + 1;
903 * Any leftover size that's legitimate will be used.
905 if (size > 0) {
906 (void)snprintf(tmp, sizeof(tmp), "%d", size);
907 (void)strcat(lfmt, tmp);
909 (void)strcat(lfmt, "d");
912 * The stuff after the decimal point always needs zero
913 * filling.
915 (void)strcat(lfmt, ".%0");
918 * We can "print" at most nine digits of precision. The
919 * rest we will pad on at the end.
921 (void)snprintf(tmp, sizeof(tmp), "%dd", prec > 9 ? 9 : prec);
922 (void)strcat(lfmt, tmp);
925 * For precision of less that nine digits, trim off the
926 * less significant figures.
928 for (; prec < 9; prec++)
929 ts.tv_nsec /= 10;
932 * Use the format, and then tack on any zeroes that
933 * might be required to make up the requested precision.
935 l = snprintf(buf, blen, lfmt, ts.tv_sec, ts.tv_nsec);
936 for (; prec > 9 && l < blen; prec--, l++)
937 (void)strcat(buf, "0");
938 return (l);
942 * Add on size and precision, if specified, to the format.
944 if (size != -1) {
945 (void)snprintf(tmp, sizeof(tmp), "%d", size);
946 (void)strcat(lfmt, tmp);
948 if (prec != -1) {
949 (void)snprintf(tmp, sizeof(tmp), ".%d", prec);
950 (void)strcat(lfmt, tmp);
954 * String output uses the temporary sdata.
956 if (ofmt == FMTF_STRING) {
957 if (sdata == NULL)
958 errx(1, "%.*s: bad format", (int)flen, fmt);
959 (void)strcat(lfmt, "s");
960 return (snprintf(buf, blen, lfmt, sdata));
964 * Ensure that sign extension does not cause bad looking output
965 * for some forms.
967 if (small && ofmt != FMTF_DECIMAL)
968 data = (u_int32_t)data;
971 * The four "numeric" output forms.
973 (void)strcat(lfmt, "ll");
974 switch (ofmt) {
975 case FMTF_DECIMAL: (void)strcat(lfmt, "d"); break;
976 case FMTF_OCTAL: (void)strcat(lfmt, "o"); break;
977 case FMTF_UNSIGNED: (void)strcat(lfmt, "u"); break;
978 case FMTF_HEX: (void)strcat(lfmt, "x"); break;
981 return (snprintf(buf, blen, lfmt, data));