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 */
368 case S_IFDOOR
: /* door */
372 default: /* unknown */
385 switch (mode
& (S_IXUSR
| S_ISUID
)) {
395 case S_IXUSR
| S_ISUID
:
408 switch (mode
& (S_IXGRP
| S_ISGID
)) {
418 case S_IXGRP
| S_ISGID
:
431 switch (mode
& (S_IXOTH
| S_ISVTX
)) {
441 case S_IXOTH
| S_ISVTX
:
450 * Parses a format string.
453 output(const struct stat
*st
, const char *file
,
454 const char *statfmt
, int fn
, int nonl
, int quiet
)
456 int flags
, size
, prec
, ofmt
, hilo
, what
;
458 * buf size is enough for an item of length PATH_MAX,
459 * multiplied by 4 for vis encoding, plus 4 for symlink
460 * " -> " prefix, plus 1 for \0 terminator.
462 char buf
[PATH_MAX
* 4 + 4 + 1];
467 while (*statfmt
!= '\0') {
470 * Non-format characters go straight out.
472 if (*statfmt
!= FMT_MAGIC
) {
473 addchar(stdout
, *statfmt
, &nl
);
479 * The current format "substring" starts here,
480 * and then we skip the magic.
486 * Some simple one-character "formats".
490 addchar(stdout
, '\n', &nl
);
494 addchar(stdout
, '\t', &nl
);
498 addchar(stdout
, '%', &nl
);
501 case SIMPLE_NUMBER
: {
504 snprintf(num
, sizeof(num
), "%d", fn
);
505 for (p
= &num
[0]; *p
; p
++)
506 addchar(stdout
, *p
, &nl
);
513 * This must be an actual format string. Format strings are
514 * similar to printf(3) formats up to a point, and are of
517 * % required start of format
518 * [-# +0] opt. format characters
519 * size opt. field width
520 * . opt. decimal separator, followed by
521 * prec opt. precision
522 * fmt opt. output specifier (string, numeric, etc.)
523 * sub opt. sub field specifier (high, middle, low)
524 * datum required field specifier (size, mode, etc)
526 * Only the % and the datum selector are required. All data
527 * have reasonable default output forms. The "sub" specifier
528 * only applies to certain data (mode, dev, rdev, filetype).
529 * The symlink output defaults to STRING, yet will only emit
530 * the leading " -> " if STRING is explicitly specified. The
531 * sizerdev datum will generate rdev output for character or
532 * block devices, and size output for all others.
533 * For STRING output, the # format requests vis encoding.
537 if (*statfmt
== FMT_POUND
)
539 else if (*statfmt
== FMT_SPACE
)
541 else if (*statfmt
== FMT_PLUS
)
543 else if (*statfmt
== FMT_ZERO
)
545 else if (*statfmt
== FMT_MINUS
)
550 } while (1/*CONSTCOND*/);
553 if (isdigit((unsigned)*statfmt
)) {
555 while (isdigit((unsigned)*statfmt
)) {
556 size
= (size
* 10) + (*statfmt
- '0');
564 if (*statfmt
== FMT_DOT
) {
568 while (isdigit((unsigned)*statfmt
)) {
569 prec
= (prec
* 10) + (*statfmt
- '0');
576 #define fmtcase(x, y) case (y): (x) = (y); statfmt++; break
577 #define fmtcasef(x, y, z) case (y): (x) = (z); statfmt++; break
579 fmtcasef(ofmt
, FMT_DECIMAL
, FMTF_DECIMAL
);
580 fmtcasef(ofmt
, FMT_OCTAL
, FMTF_OCTAL
);
581 fmtcasef(ofmt
, FMT_UNSIGNED
, FMTF_UNSIGNED
);
582 fmtcasef(ofmt
, FMT_HEX
, FMTF_HEX
);
583 fmtcasef(ofmt
, FMT_FLOAT
, FMTF_FLOAT
);
584 fmtcasef(ofmt
, FMT_STRING
, FMTF_STRING
);
591 fmtcase(hilo
, HIGH_PIECE
);
592 fmtcase(hilo
, MIDDLE_PIECE
);
593 fmtcase(hilo
, LOW_PIECE
);
600 fmtcase(what
, SHOW_realpath
);
601 fmtcase(what
, SHOW_st_dev
);
602 fmtcase(what
, SHOW_st_ino
);
603 fmtcase(what
, SHOW_st_mode
);
604 fmtcase(what
, SHOW_st_nlink
);
605 fmtcase(what
, SHOW_st_uid
);
606 fmtcase(what
, SHOW_st_gid
);
607 fmtcase(what
, SHOW_st_rdev
);
608 fmtcase(what
, SHOW_st_atime
);
609 fmtcase(what
, SHOW_st_mtime
);
610 fmtcase(what
, SHOW_st_ctime
);
611 fmtcase(what
, SHOW_st_btime
);
612 fmtcase(what
, SHOW_st_size
);
613 fmtcase(what
, SHOW_st_blocks
);
614 fmtcase(what
, SHOW_st_blksize
);
615 fmtcase(what
, SHOW_symlink
);
616 fmtcase(what
, SHOW_filetype
);
617 fmtcase(what
, SHOW_filename
);
618 fmtcase(what
, SHOW_sizerdev
);
627 subfmt
, statfmt
- subfmt
,
629 flags
, size
, prec
, ofmt
, hilo
, what
, quiet
);
631 for (i
= 0; i
< t
&& i
< (int)(sizeof(buf
) - 1); i
++)
632 addchar(stdout
, buf
[i
], &nl
);
637 errx(1, "%.*s: bad format",
638 (int)(statfmt
- subfmt
+ 1), subfmt
);
642 (void)fputc('\n', stdout
);
643 (void)fflush(stdout
);
647 fmttime(char *buf
, size_t len
, const char *fmt
, time_t secs
, long nsecs
)
650 const char *fpb
, *fp1
, *fp2
; /* pointers into fmt */
651 char *fmt2
= NULL
; /* replacement fmt (if not NULL) */
652 /* XXX init of next twp for stupid gcc only */
653 char *f2p
= NULL
; /* ptr into fmt2 - last added */
658 if (localtime_r(&secs
, &tm
) == NULL
) {
660 (void)localtime_r(&secs
, &tm
);
662 for (fp1
= fpb
= fmt
; (fp2
= strchr(fp1
, '%')) != NULL
; ) {
664 /* make sure we don't find the 2nd '%' in "%%" */
665 fp1
= fp2
+ 1 + (fp2
[1] != '\0');
669 /* allow for ~100 %f's in the format ... */
670 flen
= strlen(fmt
) + 1024;
672 if ((fmt2
= calloc(flen
, 1)) == NULL
) {
678 o
= (size_t)(fp2
- fpb
);
679 memcpy(f2p
, fpb
, o
); /* must fit */
682 o
= (size_t)(fp2
- fpb
);
686 if (flen
< o
+ 10) { /* 9 digits + \0 == 10 */
692 sl
= snprintf(f2p
, flen
, "%.9ld", nsecs
);
709 (void)strftime(buf
, len
, fmt
, &tm
);
715 * Arranges output according to a single parsed format substring.
718 format1(const struct stat
*st
,
720 const char *fmt
, int flen
,
721 char *buf
, size_t blen
,
722 int flags
, int size
, int prec
, int ofmt
,
723 int hilo
, int what
, int quiet
)
726 char *stmp
, lfmt
[24], tmp
[20];
728 char smode
[12], sid
[12], path
[PATH_MAX
+ 4], visbuf
[PATH_MAX
* 4 + 4];
734 int formats
; /* bitmap of allowed formats for this datum */
735 int small
; /* true if datum is a small integer */
736 int gottime
; /* true if secs and nsecs are valid */
737 int shift
; /* powers of 2 to scale numbers before printing */
738 size_t prefixlen
; /* length of constant prefix for string data */
749 * First, pick out the data and tweak it based on hilo or
750 * specified output format (symlink output only).
755 small
= (sizeof(st
->st_dev
) == 4);
756 data
= (what
== SHOW_st_dev
) ? st
->st_dev
: st
->st_rdev
;
757 if (hilo
== HIGH_PIECE
) {
761 else if (hilo
== LOW_PIECE
) {
762 data
= minor((unsigned)data
);
765 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
|
768 if (data
== (uint64_t)-1)
771 ofmt
= FMTF_UNSIGNED
;
775 small
= (sizeof(st
->st_ino
) == 4);
778 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
780 ofmt
= FMTF_UNSIGNED
;
783 small
= (sizeof(st
->st_mode
) == 4);
785 strmode(st
->st_mode
, smode
);
788 if (stmp
[l
- 1] == ' ')
790 if (hilo
== HIGH_PIECE
) {
796 else if (hilo
== MIDDLE_PIECE
) {
797 data
= (data
>> 9) & 07;
802 else if (hilo
== LOW_PIECE
) {
809 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
|
815 small
= (sizeof(st
->st_dev
) == 4);
818 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
820 ofmt
= FMTF_UNSIGNED
;
823 small
= (sizeof(st
->st_uid
) == 4);
825 if ((pw
= getpwuid(st
->st_uid
)) != NULL
)
828 snprintf(sid
, sizeof(sid
), "(%ld)", (long)st
->st_uid
);
831 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
|
834 ofmt
= FMTF_UNSIGNED
;
837 small
= (sizeof(st
->st_gid
) == 4);
839 if ((gr
= getgrgid(st
->st_gid
)) != NULL
)
842 snprintf(sid
, sizeof(sid
), "(%ld)", (long)st
->st_gid
);
845 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
|
848 ofmt
= FMTF_UNSIGNED
;
853 nsecs
= st
->st_atimensec
;
859 nsecs
= st
->st_mtimensec
;
866 nsecs
= st
->st_ctimensec
;
869 small
= (sizeof(secs
) == 4);
871 sdata
= fmttime(path
, sizeof(path
), timefmt
, secs
, nsecs
);
873 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
|
874 FMTF_FLOAT
| FMTF_STRING
;
879 small
= (sizeof(st
->st_size
) == 4);
882 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
884 ofmt
= FMTF_UNSIGNED
;
887 shift
= 30; /* gigabytes */
891 shift
= 20; /* megabytes */
895 shift
= 10; /* kilobytes */
901 small
= (sizeof(st
->st_blocks
) == 4);
902 data
= st
->st_blocks
;
904 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
906 ofmt
= FMTF_UNSIGNED
;
908 case SHOW_st_blksize
:
909 small
= (sizeof(st
->st_blksize
) == 4);
910 data
= st
->st_blksize
;
912 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
914 ofmt
= FMTF_UNSIGNED
;
920 (void)strlcpy(path
, "(stdin)", sizeof(path
));
923 snprintf(path
, sizeof(path
), " -> ");
924 if (realpath(file
, path
+ 4) == NULL
) {
926 warn("realpath `%s'", file
);
931 sdata
= path
+ (ofmt
== FMTF_STRING
? 0 : 4);
932 prefixlen
= (ofmt
== FMTF_STRING
? 4 : 0);
935 formats
= FMTF_STRING
;
942 if (S_ISLNK(st
->st_mode
)) {
943 snprintf(path
, sizeof(path
), " -> ");
944 l
= readlink(file
, path
+ 4, sizeof(path
) - 4 - 1);
947 warn("readlink `%s'", file
);
953 sdata
= path
+ (ofmt
== FMTF_STRING
? 0 : 4);
954 prefixlen
= (ofmt
== FMTF_STRING
? 4 : 0);
960 formats
= FMTF_STRING
;
968 if (hilo
== 0 || hilo
== LOW_PIECE
) {
969 switch (st
->st_mode
& S_IFMT
) {
970 case S_IFIFO
: sdata
= "|"; break;
971 case S_IFDIR
: sdata
= "/"; break;
974 (S_IXUSR
| S_IXGRP
| S_IXOTH
))
977 case S_IFLNK
: sdata
= "@"; break;
979 case S_IFSOCK
: sdata
= "="; break;
982 case S_IFWHT
: sdata
= "%"; break;
985 case S_IFDOOR
: sdata
= ">"; break;
986 #endif /* S_IFDOOR */
990 else if (hilo
== HIGH_PIECE
) {
991 switch (st
->st_mode
& S_IFMT
) {
992 case S_IFIFO
: sdata
= "Fifo File"; break;
993 case S_IFCHR
: sdata
= "Character Device"; break;
994 case S_IFDIR
: sdata
= "Directory"; break;
995 case S_IFBLK
: sdata
= "Block Device"; break;
996 case S_IFREG
: sdata
= "Regular File"; break;
997 case S_IFLNK
: sdata
= "Symbolic Link"; break;
999 case S_IFSOCK
: sdata
= "Socket"; break;
1002 case S_IFWHT
: sdata
= "Whiteout File"; break;
1003 #endif /* S_IFWHT */
1005 case S_IFDOOR
: sdata
= "Door"; break;
1006 #endif /* S_IFDOOR */
1007 default: sdata
= "???"; break;
1011 formats
= FMTF_STRING
;
1019 (void)strlcpy(path
, "(stdin)", sizeof(path
));
1020 if (hilo
== HIGH_PIECE
|| hilo
== LOW_PIECE
)
1024 (void)strlcpy(path
, file
, sizeof(path
));
1027 (void)strlcpy(path
, file
, sizeof(path
));
1028 s
= strrchr(path
, '/');
1030 /* trim off trailing /'s */
1032 s
[0] == '/' && s
[1] == '\0')
1034 s
= strrchr(path
, '/');
1036 if (hilo
== HIGH_PIECE
) {
1038 (void)strlcpy(path
, ".", sizeof(path
));
1040 while (s
!= path
&& s
[0] == '/')
1045 else if (hilo
== LOW_PIECE
) {
1046 if (s
!= NULL
&& s
[1] != '\0')
1047 (void)strlcpy(path
, s
+ 1,
1053 formats
= FMTF_STRING
;
1058 if (S_ISCHR(st
->st_mode
) || S_ISBLK(st
->st_mode
)) {
1059 char majdev
[20], mindev
[20];
1065 majdev
, sizeof(majdev
),
1067 ofmt
, HIGH_PIECE
, SHOW_st_rdev
, quiet
);
1071 mindev
, sizeof(mindev
),
1073 ofmt
, LOW_PIECE
, SHOW_st_rdev
, quiet
);
1074 return (snprintf(buf
, blen
, "%.*s,%.*s",
1075 l1
, majdev
, l2
, mindev
));
1083 ofmt
, 0, SHOW_st_size
, quiet
));
1087 errx(1, "%.*s: bad format", (int)flen
, fmt
);
1091 * If a subdatum was specified but not supported, or an output
1092 * format was selected that is not supported, that's an error.
1094 if (hilo
!= 0 || (ofmt
& formats
) == 0)
1095 errx(1, "%.*s: bad format", (int)flen
, fmt
);
1099 * Assemble the format string for passing to printf(3).
1102 (void)strcat(lfmt
, "%");
1103 if (flags
& FLAG_POUND
)
1104 (void)strcat(lfmt
, "#");
1105 if (flags
& FLAG_SPACE
)
1106 (void)strcat(lfmt
, " ");
1107 if (flags
& FLAG_PLUS
)
1108 (void)strcat(lfmt
, "+");
1109 if (flags
& FLAG_MINUS
)
1110 (void)strcat(lfmt
, "-");
1111 if (flags
& FLAG_ZERO
)
1112 (void)strcat(lfmt
, "0");
1115 * Only the timespecs support the FLOAT output format, and that
1116 * requires work that differs from the other formats.
1118 if (ofmt
== FMTF_FLOAT
) {
1120 * Nothing after the decimal point, so just print seconds.
1124 (void)snprintf(tmp
, sizeof(tmp
), "%d", size
);
1125 (void)strcat(lfmt
, tmp
);
1127 (void)strcat(lfmt
, "lld");
1128 return (snprintf(buf
, blen
, lfmt
,
1133 * Unspecified precision gets all the precision we have:
1140 * Adjust the size for the decimal point and the digits
1146 * Any leftover size that's legitimate will be used.
1149 (void)snprintf(tmp
, sizeof(tmp
), "%d", size
);
1150 (void)strcat(lfmt
, tmp
);
1152 /* Seconds: time_t cast to long long. */
1153 (void)strcat(lfmt
, "lld");
1156 * The stuff after the decimal point always needs zero
1159 (void)strcat(lfmt
, ".%0");
1162 * We can "print" at most nine digits of precision. The
1163 * rest we will pad on at the end.
1165 * Nanoseconds: long.
1167 (void)snprintf(tmp
, sizeof(tmp
), "%dld", prec
> 9 ? 9 : prec
);
1168 (void)strcat(lfmt
, tmp
);
1171 * For precision of less that nine digits, trim off the
1172 * less significant figures.
1174 for (; prec
< 9; prec
++)
1178 * Use the format, and then tack on any zeroes that
1179 * might be required to make up the requested precision.
1181 l
= snprintf(buf
, blen
, lfmt
, (long long)secs
, nsecs
);
1182 for (; prec
> 9 && l
< (int)blen
; prec
--, l
++)
1183 (void)strcat(buf
, "0");
1188 * Add on size and precision, if specified, to the format.
1191 (void)snprintf(tmp
, sizeof(tmp
), "%d", size
);
1192 (void)strcat(lfmt
, tmp
);
1195 (void)snprintf(tmp
, sizeof(tmp
), ".%d", prec
);
1196 (void)strcat(lfmt
, tmp
);
1200 * String output uses the temporary sdata.
1202 if (ofmt
== FMTF_STRING
) {
1204 errx(1, "%.*s: bad format", (int)flen
, fmt
);
1205 (void)strcat(lfmt
, "s");
1206 return (snprintf(buf
, blen
, lfmt
, sdata
));
1210 * Ensure that sign extension does not cause bad looking output
1213 if (small
&& ofmt
!= FMTF_DECIMAL
)
1214 data
= (uint32_t)data
;
1217 * The four "numeric" output forms.
1219 (void)strcat(lfmt
, "ll");
1221 case FMTF_DECIMAL
: (void)strcat(lfmt
, "d"); break;
1222 case FMTF_OCTAL
: (void)strcat(lfmt
, "o"); break;
1223 case FMTF_UNSIGNED
: (void)strcat(lfmt
, "u"); break;
1224 case FMTF_HEX
: (void)strcat(lfmt
, "x"); break;
1228 * shift and round to nearest for kilobytes, megabytes,
1232 data
>>= (shift
- 1);
1237 return (snprintf(buf
, blen
, lfmt
, data
));