2 * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 * This code is derived from software contributed to The NetBSD Foundation
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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>
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>
66 #if HAVE_STRUCT_STAT_ST_FLAGS
69 #define SHELL_F " st_flags=%f"
70 #else /* HAVE_STRUCT_STAT_ST_FLAGS */
74 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
76 #if HAVE_STRUCT_STAT_ST_BIRTHTIME
77 #define DEF_B "\"%SB\" "
79 #define SHELL_B "st_birthtime=%B "
80 #else /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
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 */
93 "%d %i %Sp %l %Su %Sg %r %z \"%Sa\" \"%Sm\" \"%Sc\" " DEF_B \
95 #define RAW_FORMAT "%d %i %#p %l %u %g %r %z %a %m %c " RAW_B \
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 \
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" \
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 '%'
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 ' '
136 #define FMT_MINUS '-'
138 #define FMT_DECIMAL 'D'
139 #define FMT_OCTAL 'O'
140 #define FMT_UNSIGNED 'U'
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 */
190 #define addchar(s, c, nl) \
192 (void)fputc((c), (s)); \
193 (*nl) = ((c) == '\n'); \
194 } while (0/*CONSTCOND*/)
197 main(int argc
, char *argv
[])
200 int ch
, rc
, errs
, am_readlink
;
201 int lsF
, fmtchar
, usestat
, fn
, nonl
, quiet
;
202 char *statfmt
, *options
, *synopsis
;
214 if (strcmp(getprogname(), "readlink") == 0) {
217 synopsis
= "[-n] [file ...]";
222 options
= "f:FlLnqrst:x";
223 synopsis
= "[-FlLnqrsx] [-f format] [-t timefmt] [file ...]";
226 while ((ch
= getopt(argc
, argv
, options
)) != -1)
248 errx(1, "can't use format '%c' with '%c'",
263 if (fmtchar
== '\0') {
268 statfmt
= DEF_FORMAT
;
272 if (lsF
&& fmtchar
!= 'l')
273 errx(1, "can't use format '%c' with -F", fmtchar
);
277 /* statfmt already set */
280 statfmt
= lsF
? LSF_FORMAT
: LS_FORMAT
;
283 statfmt
= RAW_FORMAT
;
286 statfmt
= SHELL_FORMAT
;
289 statfmt
= LINUX_FORMAT
;
299 timefmt
= TIME_FORMAT
;
304 rc
= fstat(STDIN_FILENO
, &st
);
306 rc
= stat(argv
[0], &st
);
308 rc
= lstat(argv
[0], &st
);
315 argc
== 0 ? "(stdin)" : argv
[0]);
318 output(&st
, argv
[0], statfmt
, fn
, nonl
, quiet
);
325 return (am_readlink
? linkfail
: errs
);
329 usage(const char *synopsis
)
332 (void)fprintf(stderr
, "usage: %s %s\n", getprogname(), synopsis
);
337 * Parses a format string.
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
;
349 while (*statfmt
!= '\0') {
352 * Non-format characters go straight out.
354 if (*statfmt
!= FMT_MAGIC
) {
355 addchar(stdout
, *statfmt
, &nl
);
361 * The current format "substring" starts here,
362 * and then we skip the magic.
368 * Some simple one-character "formats".
372 addchar(stdout
, '\n', &nl
);
376 addchar(stdout
, '\t', &nl
);
380 addchar(stdout
, '%', &nl
);
383 case SIMPLE_NUMBER
: {
386 snprintf(num
, sizeof(num
), "%d", fn
);
387 for (p
= &num
[0]; *p
; p
++)
388 addchar(stdout
, *p
, &nl
);
395 * This must be an actual format string. Format strings are
396 * similar to printf(3) formats up to a point, and are of
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.
418 if (*statfmt
== FMT_POUND
)
420 else if (*statfmt
== FMT_SPACE
)
422 else if (*statfmt
== FMT_PLUS
)
424 else if (*statfmt
== FMT_ZERO
)
426 else if (*statfmt
== FMT_MINUS
)
431 } while (1/*CONSTCOND*/);
434 if (isdigit((unsigned)*statfmt
)) {
436 while (isdigit((unsigned)*statfmt
)) {
437 size
= (size
* 10) + (*statfmt
- '0');
445 if (*statfmt
== FMT_DOT
) {
449 while (isdigit((unsigned)*statfmt
)) {
450 prec
= (prec
* 10) + (*statfmt
- '0');
457 #define fmtcase(x, y) case (y): (x) = (y); statfmt++; break
458 #define fmtcasef(x, y, z) case (y): (x) = (z); statfmt++; break
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
);
472 fmtcase(hilo
, HIGH_PIECE
);
473 fmtcase(hilo
, MIDDLE_PIECE
);
474 fmtcase(hilo
, LOW_PIECE
);
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
);
509 subfmt
, statfmt
- subfmt
,
511 flags
, size
, prec
, ofmt
, hilo
, what
);
513 for (i
= 0; i
< t
&& i
< sizeof(buf
); i
++)
514 addchar(stdout
, buf
[i
], &nl
);
519 errx(1, "%.*s: bad format",
520 (int)(statfmt
- subfmt
+ 1), subfmt
);
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
,
534 const char *fmt
, int flen
,
535 char *buf
, size_t blen
,
536 int flags
, int size
, int prec
, int ofmt
,
540 char *sdata
, lfmt
[24], tmp
[20];
541 char smode
[12], sid
[12], path
[PATH_MAX
+ 4];
544 const struct timespec
*tsp
;
547 int l
, small
, formats
;
554 * First, pick out the data and tweak it based on hilo or
555 * specified output format (symlink output only).
560 small
= (sizeof(st
->st_dev
) == 4);
561 data
= (what
== SHOW_st_dev
) ? st
->st_dev
: st
->st_rdev
;
563 sdata
= (what
== SHOW_st_dev
) ?
564 devname(st
->st_dev
, S_IFBLK
) :
566 S_ISCHR(st
->st_mode
) ? S_IFCHR
:
567 S_ISBLK(st
->st_mode
) ? S_IFBLK
:
571 #endif /* HAVE_DEVNAME */
572 if (hilo
== HIGH_PIECE
) {
576 else if (hilo
== LOW_PIECE
) {
577 data
= minor((unsigned)data
);
580 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
|
583 #else /* HAVE_DEVNAME */
585 #endif /* HAVE_DEVNAME */
587 ofmt
= FMTF_UNSIGNED
;
590 small
= (sizeof(st
->st_ino
) == 4);
593 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
595 ofmt
= FMTF_UNSIGNED
;
598 small
= (sizeof(st
->st_mode
) == 4);
600 strmode(st
->st_mode
, smode
);
603 if (sdata
[l
- 1] == ' ')
605 if (hilo
== HIGH_PIECE
) {
611 else if (hilo
== MIDDLE_PIECE
) {
612 data
= (data
>> 9) & 07;
617 else if (hilo
== LOW_PIECE
) {
623 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
|
629 small
= (sizeof(st
->st_dev
) == 4);
632 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
634 ofmt
= FMTF_UNSIGNED
;
637 small
= (sizeof(st
->st_uid
) == 4);
639 if ((pw
= getpwuid(st
->st_uid
)) != NULL
)
642 snprintf(sid
, sizeof(sid
), "(%ld)", (long)st
->st_uid
);
645 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
|
648 ofmt
= FMTF_UNSIGNED
;
651 small
= (sizeof(st
->st_gid
) == 4);
653 if ((gr
= getgrgid(st
->st_gid
)) != NULL
)
656 snprintf(sid
, sizeof(sid
), "(%ld)", (long)st
->st_gid
);
659 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
|
662 ofmt
= FMTF_UNSIGNED
;
665 tsp
= &st
->st_atimespec
;
669 tsp
= &st
->st_mtimespec
;
673 tsp
= &st
->st_ctimespec
;
675 #if HAVE_STRUCT_STAT_ST_BIRTHTIME
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);
684 tm
= localtime(&ts
.tv_sec
);
685 strftime(path
, sizeof(path
), timefmt
, tm
);
687 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
|
688 FMTF_FLOAT
| FMTF_STRING
;
693 small
= (sizeof(st
->st_size
) == 4);
696 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
698 ofmt
= FMTF_UNSIGNED
;
701 small
= (sizeof(st
->st_blocks
) == 4);
702 data
= st
->st_blocks
;
704 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
706 ofmt
= FMTF_UNSIGNED
;
708 case SHOW_st_blksize
:
709 small
= (sizeof(st
->st_blksize
) == 4);
710 data
= st
->st_blksize
;
712 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
714 ofmt
= FMTF_UNSIGNED
;
716 #if HAVE_STRUCT_STAT_ST_FLAGS
718 small
= (sizeof(st
->st_flags
) == 4);
721 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
723 ofmt
= FMTF_UNSIGNED
;
725 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
726 #if HAVE_STRUCT_STAT_ST_GEN
728 small
= (sizeof(st
->st_gen
) == 4);
731 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
733 ofmt
= FMTF_UNSIGNED
;
735 #endif /* HAVE_STRUCT_STAT_ST_GEN */
739 if (S_ISLNK(st
->st_mode
)) {
740 snprintf(path
, sizeof(path
), " -> ");
741 l
= readlink(file
, path
+ 4, sizeof(path
) - 4 - 1);
748 sdata
= path
+ (ofmt
== FMTF_STRING
? 0 : 4);
754 formats
= FMTF_STRING
;
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;
769 (S_IXUSR
| S_IXGRP
| S_IXOTH
))
770 (void)strcat(sdata
, "*");
772 case S_IFLNK
: (void)strcat(sdata
, "@"); break;
773 case S_IFSOCK
: (void)strcat(sdata
, "="); break;
775 case S_IFWHT
: (void)strcat(sdata
, "%"); break;
778 case S_IFDOOR
: (void)strcat(sdata
, ">"); break;
779 #endif /* S_IFDOOR */
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;
793 case S_IFWHT
: sdata
= "Whiteout File"; break;
796 case S_IFDOOR
: sdata
= "Door"; break;
797 #endif /* S_IFDOOR */
798 default: sdata
= "???"; break;
802 formats
= FMTF_STRING
;
810 (void)strncpy(path
, "(stdin)", sizeof(path
));
812 (void)strncpy(path
, file
, sizeof(path
));
814 formats
= FMTF_STRING
;
819 if (S_ISCHR(st
->st_mode
) || S_ISBLK(st
->st_mode
)) {
820 char majdev
[20], mindev
[20];
826 majdev
, sizeof(majdev
),
828 ofmt
, HIGH_PIECE
, SHOW_st_rdev
);
832 mindev
, sizeof(mindev
),
834 ofmt
, LOW_PIECE
, SHOW_st_rdev
);
835 return (snprintf(buf
, blen
, "%.*s,%.*s",
836 l1
, majdev
, l2
, mindev
));
844 ofmt
, 0, SHOW_st_size
));
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).
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.
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:
899 * Adjust the size for the decimal point and the digits
905 * Any leftover size that's legitimate will be used.
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
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
++)
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");
944 * Add on size and precision, if specified, to the format.
947 (void)snprintf(tmp
, sizeof(tmp
), "%d", size
);
948 (void)strcat(lfmt
, tmp
);
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
) {
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
969 if (small
&& ofmt
!= FMTF_DECIMAL
)
970 data
= (u_int32_t
)data
;
973 * The four "numeric" output forms.
975 (void)strcat(lfmt
, "ll");
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
));