]>
git.saurik.com Git - apple/file_cmds.git/blob - stat/stat.c
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.
37 #include <sys/cdefs.h>
40 __RCSID("$NetBSD: stat.c,v 1.13 2003/07/25 03:21:17 atatat Exp $");
44 /* Commenting FBSDID, as it is not needed in OSX.
45 __FBSDID("$FreeBSD: src/usr.bin/stat/stat.c,v 1.6 2003/10/06 01:55:17 dougb Exp $");
50 #else /* HAVE_CONFIG_H */
51 #define HAVE_STRUCT_STAT_ST_FLAGS 1
52 #define HAVE_STRUCT_STAT_ST_GEN 1
53 #define HAVE_STRUCT_STAT_ST_BIRTHTIME 0 /* was 1; not needed if ! __BSD_VISIBLE ? */
54 #define HAVE_STRUCT_STAT_ST_MTIMENSEC 1
55 #define HAVE_DEVNAME 1
56 #endif /* HAVE_CONFIG_H */
58 #include <sys/types.h>
72 #if HAVE_STRUCT_STAT_ST_FLAGS
75 #define SHELL_F " st_flags=%f"
76 #else /* HAVE_STRUCT_STAT_ST_FLAGS */
80 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
82 #if HAVE_STRUCT_STAT_ST_BIRTHTIME
83 #define DEF_B "\"%SB\" "
85 #define SHELL_B "st_birthtime=%B "
86 #else /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
90 #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
92 #if HAVE_STRUCT_STAT_ST_ATIM
93 #define st_atimespec st_atim
94 #define st_ctimespec st_ctim
95 #define st_mtimespec st_mtim
96 #endif /* HAVE_STRUCT_STAT_ST_ATIM */
99 "%d %i %Sp %l %Su %Sg %r %z \"%Sa\" \"%Sm\" \"%Sc\" " DEF_B \
101 #define RAW_FORMAT "%d %i %#p %l %u %g %r %z %a %m %c " RAW_B \
103 #define LS_FORMAT "%Sp %l %Su %Sg %Z %Sm %N%SY"
104 #define LSF_FORMAT "%Sp %l %Su %Sg %Z %Sm %N%T%SY"
105 #define SHELL_FORMAT \
106 "st_dev=%d st_ino=%i st_mode=%#p st_nlink=%l " \
107 "st_uid=%u st_gid=%g st_rdev=%r st_size=%z " \
108 "st_atime=%a st_mtime=%m st_ctime=%c " SHELL_B \
109 "st_blksize=%k st_blocks=%b" SHELL_F
110 #define LINUX_FORMAT \
112 " Size: %-11z FileType: %HT%n" \
113 " Mode: (%04OLp/%.10Sp) Uid: (%5u/%8Su) Gid: (%5g/%8Sg)%n" \
114 "Device: %Hd,%Ld Inode: %i Links: %l%n" \
119 #define TIME_FORMAT "%b %e %T %Y"
121 #define FLAG_POUND 0x01
122 #define FLAG_SPACE 0x02
123 #define FLAG_PLUS 0x04
124 #define FLAG_ZERO 0x08
125 #define FLAG_MINUS 0x10
128 * These format characters must all be unique, except the magic one.
130 #define FMT_MAGIC '%'
133 #define SIMPLE_NEWLINE 'n'
134 #define SIMPLE_TAB 't'
135 #define SIMPLE_PERCENT '%'
136 #define SIMPLE_NUMBER '@'
138 #define FMT_POUND '#'
139 #define FMT_SPACE ' '
142 #define FMT_MINUS '-'
144 #define FMT_DECIMAL 'D'
145 #define FMT_OCTAL 'O'
146 #define FMT_UNSIGNED 'U'
148 #define FMT_FLOAT 'F'
149 #define FMT_STRING 'S'
151 #define FMTF_DECIMAL 0x01
152 #define FMTF_OCTAL 0x02
153 #define FMTF_UNSIGNED 0x04
154 #define FMTF_HEX 0x08
155 #define FMTF_FLOAT 0x10
156 #define FMTF_STRING 0x20
158 #define HIGH_PIECE 'H'
159 #define MIDDLE_PIECE 'M'
160 #define LOW_PIECE 'L'
162 #define SHOW_st_dev 'd'
163 #define SHOW_st_ino 'i'
164 #define SHOW_st_mode 'p'
165 #define SHOW_st_nlink 'l'
166 #define SHOW_st_uid 'u'
167 #define SHOW_st_gid 'g'
168 #define SHOW_st_rdev 'r'
169 #define SHOW_st_atime 'a'
170 #define SHOW_st_mtime 'm'
171 #define SHOW_st_ctime 'c'
172 #define SHOW_st_btime 'B'
173 #define SHOW_st_size 'z'
174 #define SHOW_st_blocks 'b'
175 #define SHOW_st_blksize 'k'
176 #define SHOW_st_flags 'f'
177 #define SHOW_st_gen 'v'
178 #define SHOW_symlink 'Y'
179 #define SHOW_filetype 'T'
180 #define SHOW_filename 'N'
181 #define SHOW_sizerdev 'Z'
183 void usage(const char *);
184 void output(const struct stat
*, const char *,
185 const char *, int, int, int);
186 int format1(const struct stat
*, /* stat info */
187 const char *, /* the file name */
188 const char *, int, /* the format string itself */
189 char *, size_t, /* a place to put the output */
190 int, int, int, int, /* the parsed format */
196 #define addchar(s, c, nl) \
198 (void)fputc((c), (s)); \
199 (*nl) = ((c) == '\n'); \
200 } while (0/*CONSTCOND*/)
203 main(int argc
, char *argv
[])
206 int ch
, rc
, errs
, am_readlink
;
207 int lsF
, fmtchar
, usestat
, fn
, nonl
, quiet
;
208 char *statfmt
, *options
, *synopsis
;
220 if (strcmp(getprogname(), "readlink") == 0) {
223 synopsis
= "[-n] [file ...]";
228 options
= "f:FlLnqrst:x";
229 synopsis
= "[-FlLnqrsx] [-f format] [-t timefmt] [file ...]";
232 while ((ch
= getopt(argc
, argv
, options
)) != -1)
254 errx(1, "can't use format '%c' with '%c'",
269 if (fmtchar
== '\0') {
274 statfmt
= DEF_FORMAT
;
278 if (lsF
&& fmtchar
!= 'l')
279 errx(1, "can't use format '%c' with -F", fmtchar
);
283 /* statfmt already set */
286 statfmt
= lsF
? LSF_FORMAT
: LS_FORMAT
;
289 statfmt
= RAW_FORMAT
;
292 statfmt
= SHELL_FORMAT
;
295 statfmt
= LINUX_FORMAT
;
305 timefmt
= TIME_FORMAT
;
310 rc
= fstat(STDIN_FILENO
, &st
);
312 rc
= stat(argv
[0], &st
);
314 rc
= lstat(argv
[0], &st
);
321 argc
== 0 ? "(stdin)" : argv
[0]);
324 output(&st
, argv
[0], statfmt
, fn
, nonl
, quiet
);
331 return (am_readlink
? linkfail
: errs
);
335 usage(const char *synopsis
)
338 (void)fprintf(stderr
, "usage: %s %s\n", getprogname(), synopsis
);
343 * Parses a format string.
346 output(const struct stat
*st
, const char *file
,
347 const char *statfmt
, int fn
, int nonl
, int quiet
)
349 int flags
, size
, prec
, ofmt
, hilo
, what
;
355 while (*statfmt
!= '\0') {
358 * Non-format characters go straight out.
360 if (*statfmt
!= FMT_MAGIC
) {
361 addchar(stdout
, *statfmt
, &nl
);
367 * The current format "substring" starts here,
368 * and then we skip the magic.
374 * Some simple one-character "formats".
378 addchar(stdout
, '\n', &nl
);
382 addchar(stdout
, '\t', &nl
);
386 addchar(stdout
, '%', &nl
);
389 case SIMPLE_NUMBER
: {
392 snprintf(num
, sizeof(num
), "%d", fn
);
393 for (p
= &num
[0]; *p
; p
++)
394 addchar(stdout
, *p
, &nl
);
401 * This must be an actual format string. Format strings are
402 * similar to printf(3) formats up to a point, and are of
405 * % required start of format
406 * [-# +0] opt. format characters
407 * size opt. field width
408 * . opt. decimal separator, followed by
409 * prec opt. precision
410 * fmt opt. output specifier (string, numeric, etc.)
411 * sub opt. sub field specifier (high, middle, low)
412 * datum required field specifier (size, mode, etc)
414 * Only the % and the datum selector are required. All data
415 * have reasonable default output forms. The "sub" specifier
416 * only applies to certain data (mode, dev, rdev, filetype).
417 * The symlink output defaults to STRING, yet will only emit
418 * the leading " -> " if STRING is explicitly specified. The
419 * sizerdev datum will generate rdev output for character or
420 * block devices, and size output for all others.
424 if (*statfmt
== FMT_POUND
)
426 else if (*statfmt
== FMT_SPACE
)
428 else if (*statfmt
== FMT_PLUS
)
430 else if (*statfmt
== FMT_ZERO
)
432 else if (*statfmt
== FMT_MINUS
)
437 } while (1/*CONSTCOND*/);
440 if (isdigit((unsigned)*statfmt
)) {
442 while (isdigit((unsigned)*statfmt
)) {
443 size
= (size
* 10) + (*statfmt
- '0');
451 if (*statfmt
== FMT_DOT
) {
455 while (isdigit((unsigned)*statfmt
)) {
456 prec
= (prec
* 10) + (*statfmt
- '0');
463 #define fmtcase(x, y) case (y): (x) = (y); statfmt++; break
464 #define fmtcasef(x, y, z) case (y): (x) = (z); statfmt++; break
466 fmtcasef(ofmt
, FMT_DECIMAL
, FMTF_DECIMAL
);
467 fmtcasef(ofmt
, FMT_OCTAL
, FMTF_OCTAL
);
468 fmtcasef(ofmt
, FMT_UNSIGNED
, FMTF_UNSIGNED
);
469 fmtcasef(ofmt
, FMT_HEX
, FMTF_HEX
);
470 fmtcasef(ofmt
, FMT_FLOAT
, FMTF_FLOAT
);
471 fmtcasef(ofmt
, FMT_STRING
, FMTF_STRING
);
478 fmtcase(hilo
, HIGH_PIECE
);
479 fmtcase(hilo
, MIDDLE_PIECE
);
480 fmtcase(hilo
, LOW_PIECE
);
487 fmtcase(what
, SHOW_st_dev
);
488 fmtcase(what
, SHOW_st_ino
);
489 fmtcase(what
, SHOW_st_mode
);
490 fmtcase(what
, SHOW_st_nlink
);
491 fmtcase(what
, SHOW_st_uid
);
492 fmtcase(what
, SHOW_st_gid
);
493 fmtcase(what
, SHOW_st_rdev
);
494 fmtcase(what
, SHOW_st_atime
);
495 fmtcase(what
, SHOW_st_mtime
);
496 fmtcase(what
, SHOW_st_ctime
);
497 fmtcase(what
, SHOW_st_btime
);
498 fmtcase(what
, SHOW_st_size
);
499 fmtcase(what
, SHOW_st_blocks
);
500 fmtcase(what
, SHOW_st_blksize
);
501 fmtcase(what
, SHOW_st_flags
);
502 fmtcase(what
, SHOW_st_gen
);
503 fmtcase(what
, SHOW_symlink
);
504 fmtcase(what
, SHOW_filetype
);
505 fmtcase(what
, SHOW_filename
);
506 fmtcase(what
, SHOW_sizerdev
);
515 subfmt
, statfmt
- subfmt
,
517 flags
, size
, prec
, ofmt
, hilo
, what
);
519 for (i
= 0; i
< t
&& i
< sizeof(buf
); i
++)
520 addchar(stdout
, buf
[i
], &nl
);
525 errx(1, "%.*s: bad format",
526 (int)(statfmt
- subfmt
+ 1), subfmt
);
530 (void)fputc('\n', stdout
);
531 (void)fflush(stdout
);
535 * Arranges output according to a single parsed format substring.
538 format1(const struct stat
*st
,
540 const char *fmt
, int flen
,
541 char *buf
, size_t blen
,
542 int flags
, int size
, int prec
, int ofmt
,
546 char *sdata
, lfmt
[24], tmp
[20];
547 char smode
[12], sid
[12], path
[PATH_MAX
+ 4];
550 const struct timespec
*tsp
;
553 int l
, small
, formats
;
560 * First, pick out the data and tweak it based on hilo or
561 * specified output format (symlink output only).
566 small
= (sizeof(st
->st_dev
) == 4);
567 data
= (what
== SHOW_st_dev
) ? st
->st_dev
: st
->st_rdev
;
569 sdata
= (what
== SHOW_st_dev
) ?
570 devname(st
->st_dev
, S_IFBLK
) :
572 S_ISCHR(st
->st_mode
) ? S_IFCHR
:
573 S_ISBLK(st
->st_mode
) ? S_IFBLK
:
577 #endif /* HAVE_DEVNAME */
578 if (hilo
== HIGH_PIECE
) {
582 else if (hilo
== LOW_PIECE
) {
583 data
= minor((unsigned)data
);
586 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
|
589 #else /* HAVE_DEVNAME */
591 #endif /* HAVE_DEVNAME */
593 ofmt
= FMTF_UNSIGNED
;
596 small
= (sizeof(st
->st_ino
) == 4);
599 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
601 ofmt
= FMTF_UNSIGNED
;
604 small
= (sizeof(st
->st_mode
) == 4);
606 strmode(st
->st_mode
, smode
);
609 if (sdata
[l
- 1] == ' ')
611 if (hilo
== HIGH_PIECE
) {
617 else if (hilo
== MIDDLE_PIECE
) {
618 data
= (data
>> 9) & 07;
623 else if (hilo
== LOW_PIECE
) {
629 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
|
635 small
= (sizeof(st
->st_dev
) == 4);
638 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
640 ofmt
= FMTF_UNSIGNED
;
643 small
= (sizeof(st
->st_uid
) == 4);
645 if ((pw
= getpwuid(st
->st_uid
)) != NULL
)
648 snprintf(sid
, sizeof(sid
), "(%ld)", (long)st
->st_uid
);
651 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
|
654 ofmt
= FMTF_UNSIGNED
;
657 small
= (sizeof(st
->st_gid
) == 4);
659 if ((gr
= getgrgid(st
->st_gid
)) != NULL
)
662 snprintf(sid
, sizeof(sid
), "(%ld)", (long)st
->st_gid
);
665 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
|
668 ofmt
= FMTF_UNSIGNED
;
671 tsp
= &st
->st_atimespec
;
675 tsp
= &st
->st_mtimespec
;
679 tsp
= &st
->st_ctimespec
;
681 #if HAVE_STRUCT_STAT_ST_BIRTHTIME
684 tsp
= &st
->st_birthtimespec
;
685 #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
686 ts
= *tsp
; /* copy so we can muck with it */
687 small
= (sizeof(ts
.tv_sec
) == 4);
690 tm
= localtime(&ts
.tv_sec
);
691 (void)strftime(path
, sizeof(path
), timefmt
, tm
);
693 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
|
694 FMTF_FLOAT
| FMTF_STRING
;
699 small
= (sizeof(st
->st_size
) == 4);
702 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
704 ofmt
= FMTF_UNSIGNED
;
707 small
= (sizeof(st
->st_blocks
) == 4);
708 data
= st
->st_blocks
;
710 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
712 ofmt
= FMTF_UNSIGNED
;
714 case SHOW_st_blksize
:
715 small
= (sizeof(st
->st_blksize
) == 4);
716 data
= st
->st_blksize
;
718 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
720 ofmt
= FMTF_UNSIGNED
;
722 #if HAVE_STRUCT_STAT_ST_FLAGS
724 small
= (sizeof(st
->st_flags
) == 4);
727 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
729 ofmt
= FMTF_UNSIGNED
;
731 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
732 #if HAVE_STRUCT_STAT_ST_GEN
734 small
= (sizeof(st
->st_gen
) == 4);
737 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
739 ofmt
= FMTF_UNSIGNED
;
741 #endif /* HAVE_STRUCT_STAT_ST_GEN */
745 if (S_ISLNK(st
->st_mode
)) {
746 snprintf(path
, sizeof(path
), " -> ");
747 l
= readlink(file
, path
+ 4, sizeof(path
) - 4 - 1);
754 sdata
= path
+ (ofmt
== FMTF_STRING
? 0 : 4);
760 formats
= FMTF_STRING
;
769 if (hilo
== 0 || hilo
== LOW_PIECE
) {
770 switch (st
->st_mode
& S_IFMT
) {
771 case S_IFIFO
: (void)strcat(sdata
, "|"); break;
772 case S_IFDIR
: (void)strcat(sdata
, "/"); break;
775 (S_IXUSR
| S_IXGRP
| S_IXOTH
))
776 (void)strcat(sdata
, "*");
778 case S_IFLNK
: (void)strcat(sdata
, "@"); break;
779 case S_IFSOCK
: (void)strcat(sdata
, "="); break;
781 case S_IFWHT
: (void)strcat(sdata
, "%"); break;
784 case S_IFDOOR
: (void)strcat(sdata
, ">"); break;
785 #endif /* S_IFDOOR */
789 else if (hilo
== HIGH_PIECE
) {
790 switch (st
->st_mode
& S_IFMT
) {
791 case S_IFIFO
: sdata
= "Fifo File"; break;
792 case S_IFCHR
: sdata
= "Character Device"; break;
793 case S_IFDIR
: sdata
= "Directory"; break;
794 case S_IFBLK
: sdata
= "Block Device"; break;
795 case S_IFREG
: sdata
= "Regular File"; break;
796 case S_IFLNK
: sdata
= "Symbolic Link"; break;
797 case S_IFSOCK
: sdata
= "Socket"; break;
799 case S_IFWHT
: sdata
= "Whiteout File"; break;
802 case S_IFDOOR
: sdata
= "Door"; break;
803 #endif /* S_IFDOOR */
804 default: sdata
= "???"; break;
808 formats
= FMTF_STRING
;
816 (void)strncpy(path
, "(stdin)", sizeof(path
));
818 (void)strncpy(path
, file
, sizeof(path
));
820 formats
= FMTF_STRING
;
825 if (S_ISCHR(st
->st_mode
) || S_ISBLK(st
->st_mode
)) {
826 char majdev
[20], mindev
[20];
832 majdev
, sizeof(majdev
),
834 ofmt
, HIGH_PIECE
, SHOW_st_rdev
);
838 mindev
, sizeof(mindev
),
840 ofmt
, LOW_PIECE
, SHOW_st_rdev
);
841 return (snprintf(buf
, blen
, "%.*s,%.*s",
842 l1
, majdev
, l2
, mindev
));
850 ofmt
, 0, SHOW_st_size
));
854 errx(1, "%.*s: bad format", (int)flen
, fmt
);
858 * If a subdatum was specified but not supported, or an output
859 * format was selected that is not supported, that's an error.
861 if (hilo
!= 0 || (ofmt
& formats
) == 0)
862 errx(1, "%.*s: bad format", (int)flen
, fmt
);
865 * Assemble the format string for passing to printf(3).
868 (void)strcat(lfmt
, "%");
869 if (flags
& FLAG_POUND
)
870 (void)strcat(lfmt
, "#");
871 if (flags
& FLAG_SPACE
)
872 (void)strcat(lfmt
, " ");
873 if (flags
& FLAG_PLUS
)
874 (void)strcat(lfmt
, "+");
875 if (flags
& FLAG_MINUS
)
876 (void)strcat(lfmt
, "-");
877 if (flags
& FLAG_ZERO
)
878 (void)strcat(lfmt
, "0");
881 * Only the timespecs support the FLOAT output format, and that
882 * requires work that differs from the other formats.
884 if (ofmt
== FMTF_FLOAT
) {
886 * Nothing after the decimal point, so just print seconds.
890 (void)snprintf(tmp
, sizeof(tmp
), "%d", size
);
891 (void)strcat(lfmt
, tmp
);
893 (void)strcat(lfmt
, "d");
894 return (snprintf(buf
, blen
, lfmt
, ts
.tv_sec
));
898 * Unspecified precision gets all the precision we have:
905 * Adjust the size for the decimal point and the digits
911 * Any leftover size that's legitimate will be used.
914 (void)snprintf(tmp
, sizeof(tmp
), "%d", size
);
915 (void)strcat(lfmt
, tmp
);
917 (void)strcat(lfmt
, "d");
920 * The stuff after the decimal point always needs zero
923 (void)strcat(lfmt
, ".%0");
926 * We can "print" at most nine digits of precision. The
927 * rest we will pad on at the end.
929 (void)snprintf(tmp
, sizeof(tmp
), "%dd", prec
> 9 ? 9 : prec
);
930 (void)strcat(lfmt
, tmp
);
933 * For precision of less that nine digits, trim off the
934 * less significant figures.
936 for (; prec
< 9; prec
++)
940 * Use the format, and then tack on any zeroes that
941 * might be required to make up the requested precision.
943 l
= snprintf(buf
, blen
, lfmt
, ts
.tv_sec
, ts
.tv_nsec
);
944 for (; prec
> 9 && l
< blen
; prec
--, l
++)
945 (void)strcat(buf
, "0");
950 * Add on size and precision, if specified, to the format.
953 (void)snprintf(tmp
, sizeof(tmp
), "%d", size
);
954 (void)strcat(lfmt
, tmp
);
957 (void)snprintf(tmp
, sizeof(tmp
), ".%d", prec
);
958 (void)strcat(lfmt
, tmp
);
962 * String output uses the temporary sdata.
964 if (ofmt
== FMTF_STRING
) {
966 errx(1, "%.*s: bad format", (int)flen
, fmt
);
967 (void)strcat(lfmt
, "s");
968 return (snprintf(buf
, blen
, lfmt
, sdata
));
972 * Ensure that sign extension does not cause bad looking output
975 if (small
&& ofmt
!= FMTF_DECIMAL
)
976 data
= (u_int32_t
)data
;
979 * The four "numeric" output forms.
981 (void)strcat(lfmt
, "ll");
983 case FMTF_DECIMAL
: (void)strcat(lfmt
, "d"); break;
984 case FMTF_OCTAL
: (void)strcat(lfmt
, "o"); break;
985 case FMTF_UNSIGNED
: (void)strcat(lfmt
, "u"); break;
986 case FMTF_HEX
: (void)strcat(lfmt
, "x"); break;
989 return (snprintf(buf
, blen
, lfmt
, data
));