1 /* $NetBSD: stat.c,v 1.43 2017/09/21 22:53:19 kre Exp $ */
4 * Copyright (c) 2002-2011 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/types.h>
35 #include <sys/mkdev.h>
57 #define st_atimespec st_atim
58 #define st_ctimespec st_ctim
59 #define st_mtimespec st_mtim
61 #define st_atimensec st_atim.tv_nsec
62 #define st_mtimensec st_mtim.tv_nsec
63 #define st_ctimensec st_ctim.tv_nsec
66 "%d %i %Sp %l %Su %Sg %r %z \"%Sa\" \"%Sm\" \"%Sc\" " DEF_B \
68 #define RAW_FORMAT "%d %i %#p %l %u %g %r %z %a %m %c " RAW_B \
70 #define LS_FORMAT "%Sp %l %Su %Sg %Z %Sm %N%SY"
71 #define LSF_FORMAT "%Sp %l %Su %Sg %Z %Sm %N%T%SY"
72 #define SHELL_FORMAT \
73 "st_dev=%d st_ino=%i st_mode=%#p st_nlink=%l " \
74 "st_uid=%u st_gid=%g st_rdev=%r st_size=%z " \
75 "st_atime=%Sa st_mtime=%Sm st_ctime=%Sc " SHELL_B \
76 "st_blksize=%k st_blocks=%b" SHELL_F
77 #define LINUX_FORMAT \
79 " Size: %-11z Blocks: %-11b IO Block: %-11k %HT%n" \
80 "Device: %Hd,%Ld Inode: %i Links: %l%n" \
81 " Mode: (%Mp%03OLp/%.10Sp) Uid: (%5u/%8Su) Gid: (%5g/%8Sg)%n" \
87 #define TIME_FORMAT "%b %e %T %Y"
89 #define FLAG_POUND 0x01
90 #define FLAG_SPACE 0x02
91 #define FLAG_PLUS 0x04
92 #define FLAG_ZERO 0x08
93 #define FLAG_MINUS 0x10
96 * These format characters must all be unique, except the magic one.
101 #define SIMPLE_NEWLINE 'n'
102 #define SIMPLE_TAB 't'
103 #define SIMPLE_PERCENT '%'
104 #define SIMPLE_NUMBER '@'
106 #define FMT_POUND '#'
107 #define FMT_SPACE ' '
110 #define FMT_MINUS '-'
112 #define FMT_DECIMAL 'D'
113 #define FMT_OCTAL 'O'
114 #define FMT_UNSIGNED 'U'
116 #define FMT_FLOAT 'F'
117 #define FMT_STRING 'S'
119 #define FMTF_DECIMAL 0x01
120 #define FMTF_OCTAL 0x02
121 #define FMTF_UNSIGNED 0x04
122 #define FMTF_HEX 0x08
123 #define FMTF_FLOAT 0x10
124 #define FMTF_STRING 0x20
126 #define HIGH_PIECE 'H'
127 #define MIDDLE_PIECE 'M'
128 #define LOW_PIECE 'L'
130 #define SHOW_realpath 'R'
131 #define SHOW_st_dev 'd'
132 #define SHOW_st_ino 'i'
133 #define SHOW_st_mode 'p'
134 #define SHOW_st_nlink 'l'
135 #define SHOW_st_uid 'u'
136 #define SHOW_st_gid 'g'
137 #define SHOW_st_rdev 'r'
138 #define SHOW_st_atime 'a'
139 #define SHOW_st_mtime 'm'
140 #define SHOW_st_ctime 'c'
141 #define SHOW_st_btime 'B'
142 #define SHOW_st_size 'z'
143 #define SHOW_st_blocks 'b'
144 #define SHOW_st_blksize 'k'
145 #define SHOW_symlink 'Y'
146 #define SHOW_filetype 'T'
147 #define SHOW_filename 'N'
148 #define SHOW_sizerdev 'Z'
150 static void usage(const char *);
151 static void output(const struct stat
*, const char *,
152 const char *, int, int, int);
153 static int format1(const struct stat
*, /* stat info */
154 const char *, /* the file name */
155 const char *, int, /* the format string itself */
156 char *, size_t, /* a place to put the output */
157 int, int, int, int, /* the parsed format */
160 static const char *timefmt
;
163 #define addchar(s, c, nl) \
165 (void)fputc((c), (s)); \
166 (*nl) = ((c) == '\n'); \
167 } while (0/*CONSTCOND*/)
170 main(int argc
, char *argv
[])
173 int ch
, rc
, errs
, am_readlink
;
174 int lsF
, fmtchar
, usestat
, fn
, nonl
, quiet
;
175 const char *statfmt
, *options
, *synopsis
;
187 setprogname(argv
[0]);
189 if (strcmp(getprogname(), "readlink") == 0) {
192 synopsis
= "[-fnqsv] [file ...]";
197 options
= "f:FlLnqrst:x";
198 synopsis
= "[-FlLnqrsx] [-f format] [-t timefmt] [file ...]";
201 while ((ch
= getopt(argc
, argv
, options
)) != -1)
232 errx(1, "can't use format '%c' with '%c'",
250 if (fmtchar
== '\0') {
255 statfmt
= DEF_FORMAT
;
259 if (lsF
&& fmtchar
!= 'l')
260 errx(1, "can't use format '%c' with -F", fmtchar
);
264 /* statfmt already set */
267 statfmt
= lsF
? LSF_FORMAT
: LS_FORMAT
;
270 statfmt
= RAW_FORMAT
;
273 statfmt
= SHELL_FORMAT
;
278 statfmt
= LINUX_FORMAT
;
280 timefmt
= "%Y-%m-%d %H:%M:%S.%f %z";
288 timefmt
= TIME_FORMAT
;
293 rc
= fstat(STDIN_FILENO
, &st
);
296 * Try stat() and if it fails, fall back to
297 * lstat() just in case we're examining a
300 if ((rc
= stat(argv
[0], &st
)) == -1 &&
302 (rc
= lstat(argv
[0], &st
)) == -1)
306 rc
= lstat(argv
[0], &st
);
313 argc
== 0 ? "(stdin)" : argv
[0],
314 usestat
? "stat" : "lstat");
317 output(&st
, argv
[0], statfmt
, fn
, nonl
, quiet
);
324 return (am_readlink
? linkfail
: errs
);
328 usage(const char *synopsis
)
331 (void)fprintf(stderr
, "usage: %s %s\n", getprogname(), synopsis
);
335 strmode(/* mode_t */ int mode
, char *p
)
338 switch (mode
& S_IFMT
) {
339 case S_IFDIR
: /* directory */
342 case S_IFCHR
: /* character special */
345 case S_IFBLK
: /* block special */
348 case S_IFREG
: /* regular */
351 case S_IFLNK
: /* symbolic link */
354 case S_IFSOCK
: /* socket */
358 case S_IFIFO
: /* fifo */
363 case S_IFWHT
: /* whiteout */
367 default: /* unknown */
380 switch (mode
& (S_IXUSR
| S_ISUID
)) {
390 case S_IXUSR
| S_ISUID
:
403 switch (mode
& (S_IXGRP
| S_ISGID
)) {
413 case S_IXGRP
| S_ISGID
:
426 switch (mode
& (S_IXOTH
| S_ISVTX
)) {
436 case S_IXOTH
| S_ISVTX
:
445 * Parses a format string.
448 output(const struct stat
*st
, const char *file
,
449 const char *statfmt
, int fn
, int nonl
, int quiet
)
451 int flags
, size
, prec
, ofmt
, hilo
, what
;
453 * buf size is enough for an item of length PATH_MAX,
454 * multiplied by 4 for vis encoding, plus 4 for symlink
455 * " -> " prefix, plus 1 for \0 terminator.
457 char buf
[PATH_MAX
* 4 + 4 + 1];
462 while (*statfmt
!= '\0') {
465 * Non-format characters go straight out.
467 if (*statfmt
!= FMT_MAGIC
) {
468 addchar(stdout
, *statfmt
, &nl
);
474 * The current format "substring" starts here,
475 * and then we skip the magic.
481 * Some simple one-character "formats".
485 addchar(stdout
, '\n', &nl
);
489 addchar(stdout
, '\t', &nl
);
493 addchar(stdout
, '%', &nl
);
496 case SIMPLE_NUMBER
: {
499 snprintf(num
, sizeof(num
), "%d", fn
);
500 for (p
= &num
[0]; *p
; p
++)
501 addchar(stdout
, *p
, &nl
);
508 * This must be an actual format string. Format strings are
509 * similar to printf(3) formats up to a point, and are of
512 * % required start of format
513 * [-# +0] opt. format characters
514 * size opt. field width
515 * . opt. decimal separator, followed by
516 * prec opt. precision
517 * fmt opt. output specifier (string, numeric, etc.)
518 * sub opt. sub field specifier (high, middle, low)
519 * datum required field specifier (size, mode, etc)
521 * Only the % and the datum selector are required. All data
522 * have reasonable default output forms. The "sub" specifier
523 * only applies to certain data (mode, dev, rdev, filetype).
524 * The symlink output defaults to STRING, yet will only emit
525 * the leading " -> " if STRING is explicitly specified. The
526 * sizerdev datum will generate rdev output for character or
527 * block devices, and size output for all others.
528 * For STRING output, the # format requests vis encoding.
532 if (*statfmt
== FMT_POUND
)
534 else if (*statfmt
== FMT_SPACE
)
536 else if (*statfmt
== FMT_PLUS
)
538 else if (*statfmt
== FMT_ZERO
)
540 else if (*statfmt
== FMT_MINUS
)
545 } while (1/*CONSTCOND*/);
548 if (isdigit((unsigned)*statfmt
)) {
550 while (isdigit((unsigned)*statfmt
)) {
551 size
= (size
* 10) + (*statfmt
- '0');
559 if (*statfmt
== FMT_DOT
) {
563 while (isdigit((unsigned)*statfmt
)) {
564 prec
= (prec
* 10) + (*statfmt
- '0');
571 #define fmtcase(x, y) case (y): (x) = (y); statfmt++; break
572 #define fmtcasef(x, y, z) case (y): (x) = (z); statfmt++; break
574 fmtcasef(ofmt
, FMT_DECIMAL
, FMTF_DECIMAL
);
575 fmtcasef(ofmt
, FMT_OCTAL
, FMTF_OCTAL
);
576 fmtcasef(ofmt
, FMT_UNSIGNED
, FMTF_UNSIGNED
);
577 fmtcasef(ofmt
, FMT_HEX
, FMTF_HEX
);
578 fmtcasef(ofmt
, FMT_FLOAT
, FMTF_FLOAT
);
579 fmtcasef(ofmt
, FMT_STRING
, FMTF_STRING
);
586 fmtcase(hilo
, HIGH_PIECE
);
587 fmtcase(hilo
, MIDDLE_PIECE
);
588 fmtcase(hilo
, LOW_PIECE
);
595 fmtcase(what
, SHOW_realpath
);
596 fmtcase(what
, SHOW_st_dev
);
597 fmtcase(what
, SHOW_st_ino
);
598 fmtcase(what
, SHOW_st_mode
);
599 fmtcase(what
, SHOW_st_nlink
);
600 fmtcase(what
, SHOW_st_uid
);
601 fmtcase(what
, SHOW_st_gid
);
602 fmtcase(what
, SHOW_st_rdev
);
603 fmtcase(what
, SHOW_st_atime
);
604 fmtcase(what
, SHOW_st_mtime
);
605 fmtcase(what
, SHOW_st_ctime
);
606 fmtcase(what
, SHOW_st_btime
);
607 fmtcase(what
, SHOW_st_size
);
608 fmtcase(what
, SHOW_st_blocks
);
609 fmtcase(what
, SHOW_st_blksize
);
610 fmtcase(what
, SHOW_symlink
);
611 fmtcase(what
, SHOW_filetype
);
612 fmtcase(what
, SHOW_filename
);
613 fmtcase(what
, SHOW_sizerdev
);
622 subfmt
, statfmt
- subfmt
,
624 flags
, size
, prec
, ofmt
, hilo
, what
, quiet
);
626 for (i
= 0; i
< t
&& i
< (int)(sizeof(buf
) - 1); i
++)
627 addchar(stdout
, buf
[i
], &nl
);
632 errx(1, "%.*s: bad format",
633 (int)(statfmt
- subfmt
+ 1), subfmt
);
637 (void)fputc('\n', stdout
);
638 (void)fflush(stdout
);
642 fmttime(char *buf
, size_t len
, const char *fmt
, time_t secs
, long nsecs
)
645 const char *fpb
, *fp1
, *fp2
; /* pointers into fmt */
646 char *fmt2
= NULL
; /* replacement fmt (if not NULL) */
647 /* XXX init of next twp for stupid gcc only */
648 char *f2p
= NULL
; /* ptr into fmt2 - last added */
653 if (localtime_r(&secs
, &tm
) == NULL
) {
655 (void)localtime_r(&secs
, &tm
);
657 for (fp1
= fpb
= fmt
; (fp2
= strchr(fp1
, '%')) != NULL
; ) {
659 /* make sure we don't find the 2nd '%' in "%%" */
660 fp1
= fp2
+ 1 + (fp2
[1] != '\0');
664 /* allow for ~100 %f's in the format ... */
665 flen
= strlen(fmt
) + 1024;
667 if ((fmt2
= calloc(flen
, 1)) == NULL
) {
673 o
= (size_t)(fp2
- fpb
);
674 memcpy(f2p
, fpb
, o
); /* must fit */
677 o
= (size_t)(fp2
- fpb
);
681 if (flen
< o
+ 10) { /* 9 digits + \0 == 10 */
687 sl
= snprintf(f2p
, flen
, "%.9ld", nsecs
);
704 (void)strftime(buf
, len
, fmt
, &tm
);
710 * Arranges output according to a single parsed format substring.
713 format1(const struct stat
*st
,
715 const char *fmt
, int flen
,
716 char *buf
, size_t blen
,
717 int flags
, int size
, int prec
, int ofmt
,
718 int hilo
, int what
, int quiet
)
721 char *stmp
, lfmt
[24], tmp
[20];
723 char smode
[12], sid
[12], path
[PATH_MAX
+ 4], visbuf
[PATH_MAX
* 4 + 4];
729 int formats
; /* bitmap of allowed formats for this datum */
730 int small
; /* true if datum is a small integer */
731 int gottime
; /* true if secs and nsecs are valid */
732 int shift
; /* powers of 2 to scale numbers before printing */
733 size_t prefixlen
; /* length of constant prefix for string data */
744 * First, pick out the data and tweak it based on hilo or
745 * specified output format (symlink output only).
750 small
= (sizeof(st
->st_dev
) == 4);
751 data
= (what
== SHOW_st_dev
) ? st
->st_dev
: st
->st_rdev
;
752 if (hilo
== HIGH_PIECE
) {
756 else if (hilo
== LOW_PIECE
) {
757 data
= minor((unsigned)data
);
760 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
|
763 if (data
== (uint64_t)-1)
766 ofmt
= FMTF_UNSIGNED
;
770 small
= (sizeof(st
->st_ino
) == 4);
773 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
775 ofmt
= FMTF_UNSIGNED
;
778 small
= (sizeof(st
->st_mode
) == 4);
780 strmode(st
->st_mode
, smode
);
783 if (stmp
[l
- 1] == ' ')
785 if (hilo
== HIGH_PIECE
) {
791 else if (hilo
== MIDDLE_PIECE
) {
792 data
= (data
>> 9) & 07;
797 else if (hilo
== LOW_PIECE
) {
804 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
|
810 small
= (sizeof(st
->st_dev
) == 4);
813 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
815 ofmt
= FMTF_UNSIGNED
;
818 small
= (sizeof(st
->st_uid
) == 4);
820 if ((pw
= getpwuid(st
->st_uid
)) != NULL
)
823 snprintf(sid
, sizeof(sid
), "(%ld)", (long)st
->st_uid
);
826 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
|
829 ofmt
= FMTF_UNSIGNED
;
832 small
= (sizeof(st
->st_gid
) == 4);
834 if ((gr
= getgrgid(st
->st_gid
)) != NULL
)
837 snprintf(sid
, sizeof(sid
), "(%ld)", (long)st
->st_gid
);
840 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
|
843 ofmt
= FMTF_UNSIGNED
;
848 nsecs
= st
->st_atimensec
;
854 nsecs
= st
->st_mtimensec
;
861 nsecs
= st
->st_ctimensec
;
864 small
= (sizeof(secs
) == 4);
866 sdata
= fmttime(path
, sizeof(path
), timefmt
, secs
, nsecs
);
868 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
|
869 FMTF_FLOAT
| FMTF_STRING
;
874 small
= (sizeof(st
->st_size
) == 4);
877 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
879 ofmt
= FMTF_UNSIGNED
;
882 shift
= 30; /* gigabytes */
886 shift
= 20; /* megabytes */
890 shift
= 10; /* kilobytes */
896 small
= (sizeof(st
->st_blocks
) == 4);
897 data
= st
->st_blocks
;
899 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
901 ofmt
= FMTF_UNSIGNED
;
903 case SHOW_st_blksize
:
904 small
= (sizeof(st
->st_blksize
) == 4);
905 data
= st
->st_blksize
;
907 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
909 ofmt
= FMTF_UNSIGNED
;
915 (void)strlcpy(path
, "(stdin)", sizeof(path
));
918 snprintf(path
, sizeof(path
), " -> ");
919 if (realpath(file
, path
+ 4) == NULL
) {
921 warn("realpath `%s'", file
);
926 sdata
= path
+ (ofmt
== FMTF_STRING
? 0 : 4);
927 prefixlen
= (ofmt
== FMTF_STRING
? 4 : 0);
930 formats
= FMTF_STRING
;
937 if (S_ISLNK(st
->st_mode
)) {
938 snprintf(path
, sizeof(path
), " -> ");
939 l
= readlink(file
, path
+ 4, sizeof(path
) - 4 - 1);
942 warn("readlink `%s'", file
);
948 sdata
= path
+ (ofmt
== FMTF_STRING
? 0 : 4);
949 prefixlen
= (ofmt
== FMTF_STRING
? 4 : 0);
955 formats
= FMTF_STRING
;
963 if (hilo
== 0 || hilo
== LOW_PIECE
) {
964 switch (st
->st_mode
& S_IFMT
) {
965 case S_IFIFO
: sdata
= "|"; break;
966 case S_IFDIR
: sdata
= "/"; break;
969 (S_IXUSR
| S_IXGRP
| S_IXOTH
))
972 case S_IFLNK
: sdata
= "@"; break;
974 case S_IFSOCK
: sdata
= "="; break;
977 case S_IFWHT
: sdata
= "%"; break;
980 case S_IFDOOR
: sdata
= ">"; break;
981 #endif /* S_IFDOOR */
985 else if (hilo
== HIGH_PIECE
) {
986 switch (st
->st_mode
& S_IFMT
) {
987 case S_IFIFO
: sdata
= "Fifo File"; break;
988 case S_IFCHR
: sdata
= "Character Device"; break;
989 case S_IFDIR
: sdata
= "Directory"; break;
990 case S_IFBLK
: sdata
= "Block Device"; break;
991 case S_IFREG
: sdata
= "Regular File"; break;
992 case S_IFLNK
: sdata
= "Symbolic Link"; break;
994 case S_IFSOCK
: sdata
= "Socket"; break;
997 case S_IFWHT
: sdata
= "Whiteout File"; break;
1000 case S_IFDOOR
: sdata
= "Door"; break;
1001 #endif /* S_IFDOOR */
1002 default: sdata
= "???"; break;
1006 formats
= FMTF_STRING
;
1014 (void)strlcpy(path
, "(stdin)", sizeof(path
));
1015 if (hilo
== HIGH_PIECE
|| hilo
== LOW_PIECE
)
1019 (void)strlcpy(path
, file
, sizeof(path
));
1022 (void)strlcpy(path
, file
, sizeof(path
));
1023 s
= strrchr(path
, '/');
1025 /* trim off trailing /'s */
1027 s
[0] == '/' && s
[1] == '\0')
1029 s
= strrchr(path
, '/');
1031 if (hilo
== HIGH_PIECE
) {
1033 (void)strlcpy(path
, ".", sizeof(path
));
1035 while (s
!= path
&& s
[0] == '/')
1040 else if (hilo
== LOW_PIECE
) {
1041 if (s
!= NULL
&& s
[1] != '\0')
1042 (void)strlcpy(path
, s
+ 1,
1048 formats
= FMTF_STRING
;
1053 if (S_ISCHR(st
->st_mode
) || S_ISBLK(st
->st_mode
)) {
1054 char majdev
[20], mindev
[20];
1060 majdev
, sizeof(majdev
),
1062 ofmt
, HIGH_PIECE
, SHOW_st_rdev
, quiet
);
1066 mindev
, sizeof(mindev
),
1068 ofmt
, LOW_PIECE
, SHOW_st_rdev
, quiet
);
1069 return (snprintf(buf
, blen
, "%.*s,%.*s",
1070 l1
, majdev
, l2
, mindev
));
1078 ofmt
, 0, SHOW_st_size
, quiet
));
1082 errx(1, "%.*s: bad format", (int)flen
, fmt
);
1086 * If a subdatum was specified but not supported, or an output
1087 * format was selected that is not supported, that's an error.
1089 if (hilo
!= 0 || (ofmt
& formats
) == 0)
1090 errx(1, "%.*s: bad format", (int)flen
, fmt
);
1094 * Assemble the format string for passing to printf(3).
1097 (void)strcat(lfmt
, "%");
1098 if (flags
& FLAG_POUND
)
1099 (void)strcat(lfmt
, "#");
1100 if (flags
& FLAG_SPACE
)
1101 (void)strcat(lfmt
, " ");
1102 if (flags
& FLAG_PLUS
)
1103 (void)strcat(lfmt
, "+");
1104 if (flags
& FLAG_MINUS
)
1105 (void)strcat(lfmt
, "-");
1106 if (flags
& FLAG_ZERO
)
1107 (void)strcat(lfmt
, "0");
1110 * Only the timespecs support the FLOAT output format, and that
1111 * requires work that differs from the other formats.
1113 if (ofmt
== FMTF_FLOAT
) {
1115 * Nothing after the decimal point, so just print seconds.
1119 (void)snprintf(tmp
, sizeof(tmp
), "%d", size
);
1120 (void)strcat(lfmt
, tmp
);
1122 (void)strcat(lfmt
, "lld");
1123 return (snprintf(buf
, blen
, lfmt
,
1128 * Unspecified precision gets all the precision we have:
1135 * Adjust the size for the decimal point and the digits
1141 * Any leftover size that's legitimate will be used.
1144 (void)snprintf(tmp
, sizeof(tmp
), "%d", size
);
1145 (void)strcat(lfmt
, tmp
);
1147 /* Seconds: time_t cast to long long. */
1148 (void)strcat(lfmt
, "lld");
1151 * The stuff after the decimal point always needs zero
1154 (void)strcat(lfmt
, ".%0");
1157 * We can "print" at most nine digits of precision. The
1158 * rest we will pad on at the end.
1160 * Nanoseconds: long.
1162 (void)snprintf(tmp
, sizeof(tmp
), "%dld", prec
> 9 ? 9 : prec
);
1163 (void)strcat(lfmt
, tmp
);
1166 * For precision of less that nine digits, trim off the
1167 * less significant figures.
1169 for (; prec
< 9; prec
++)
1173 * Use the format, and then tack on any zeroes that
1174 * might be required to make up the requested precision.
1176 l
= snprintf(buf
, blen
, lfmt
, (long long)secs
, nsecs
);
1177 for (; prec
> 9 && l
< (int)blen
; prec
--, l
++)
1178 (void)strcat(buf
, "0");
1183 * Add on size and precision, if specified, to the format.
1186 (void)snprintf(tmp
, sizeof(tmp
), "%d", size
);
1187 (void)strcat(lfmt
, tmp
);
1190 (void)snprintf(tmp
, sizeof(tmp
), ".%d", prec
);
1191 (void)strcat(lfmt
, tmp
);
1195 * String output uses the temporary sdata.
1197 if (ofmt
== FMTF_STRING
) {
1199 errx(1, "%.*s: bad format", (int)flen
, fmt
);
1200 (void)strcat(lfmt
, "s");
1201 return (snprintf(buf
, blen
, lfmt
, sdata
));
1205 * Ensure that sign extension does not cause bad looking output
1208 if (small
&& ofmt
!= FMTF_DECIMAL
)
1209 data
= (uint32_t)data
;
1212 * The four "numeric" output forms.
1214 (void)strcat(lfmt
, "ll");
1216 case FMTF_DECIMAL
: (void)strcat(lfmt
, "d"); break;
1217 case FMTF_OCTAL
: (void)strcat(lfmt
, "o"); break;
1218 case FMTF_UNSIGNED
: (void)strcat(lfmt
, "u"); break;
1219 case FMTF_HEX
: (void)strcat(lfmt
, "x"); break;
1223 * shift and round to nearest for kilobytes, megabytes,
1227 data
>>= (shift
- 1);
1232 return (snprintf(buf
, blen
, lfmt
, data
));