]>
git.saurik.com Git - apple/file_cmds.git/blob - stat/stat.c
5603a0596055d428b0c1e0327cd6822d3d53af12
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 __FBSDID("$FreeBSD: src/usr.bin/stat/stat.c,v 1.6 2003/10/06 01:55:17 dougb Exp $");
48 #else /* HAVE_CONFIG_H */
49 #define HAVE_STRUCT_STAT_ST_FLAGS 1
50 #define HAVE_STRUCT_STAT_ST_GEN 1
51 #define HAVE_STRUCT_STAT_ST_BIRTHTIME 1
52 #define HAVE_STRUCT_STAT_ST_MTIMENSEC 1
53 #define HAVE_DEVNAME 1
54 #endif /* HAVE_CONFIG_H */
56 #include <sys/types.h>
74 #endif /* __APPLE__ */
76 #if HAVE_STRUCT_STAT_ST_FLAGS
79 #define SHELL_F " st_flags=%f"
80 #else /* HAVE_STRUCT_STAT_ST_FLAGS */
84 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
86 #if HAVE_STRUCT_STAT_ST_BIRTHTIME
87 #define DEF_B "\"%SB\" "
89 #define SHELL_B "st_birthtime=%B "
90 #else /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
94 #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
96 #if HAVE_STRUCT_STAT_ST_ATIM
97 #define st_atimespec st_atim
98 #define st_ctimespec st_ctim
99 #define st_mtimespec st_mtim
100 #endif /* HAVE_STRUCT_STAT_ST_ATIM */
103 "%d %i %Sp %l %Su %Sg %r %z \"%Sa\" \"%Sm\" \"%Sc\" " DEF_B \
105 #define RAW_FORMAT "%d %i %#p %l %u %g %r %z %a %m %c " RAW_B \
107 #define LS_FORMAT "%Sp %l %Su %Sg %Z %Sm %N%SY"
108 #define LSF_FORMAT "%Sp %l %Su %Sg %Z %Sm %N%T%SY"
109 #define SHELL_FORMAT \
110 "st_dev=%d st_ino=%i st_mode=%#p st_nlink=%l " \
111 "st_uid=%u st_gid=%g st_rdev=%r st_size=%z " \
112 "st_atime=%a st_mtime=%m st_ctime=%c " SHELL_B \
113 "st_blksize=%k st_blocks=%b" SHELL_F
114 #define LINUX_FORMAT \
116 " Size: %-11z FileType: %HT%n" \
117 " Mode: (%04OA/%.10Sp) Uid: (%5u/%8Su) Gid: (%5g/%8Sg)%n" \
118 "Device: %Hd,%Ld Inode: %i Links: %l%n" \
123 #define TIME_FORMAT "%b %e %T %Y"
125 #define FLAG_POUND 0x01
126 #define FLAG_SPACE 0x02
127 #define FLAG_PLUS 0x04
128 #define FLAG_ZERO 0x08
129 #define FLAG_MINUS 0x10
132 * These format characters must all be unique, except the magic one.
134 #define FMT_MAGIC '%'
137 #define SIMPLE_NEWLINE 'n'
138 #define SIMPLE_TAB 't'
139 #define SIMPLE_PERCENT '%'
140 #define SIMPLE_NUMBER '@'
142 #define FMT_POUND '#'
143 #define FMT_SPACE ' '
146 #define FMT_MINUS '-'
148 #define FMT_DECIMAL 'D'
149 #define FMT_OCTAL 'O'
150 #define FMT_UNSIGNED 'U'
152 #define FMT_FLOAT 'F'
153 #define FMT_STRING 'S'
155 #define FMTF_DECIMAL 0x01
156 #define FMTF_OCTAL 0x02
157 #define FMTF_UNSIGNED 0x04
158 #define FMTF_HEX 0x08
159 #define FMTF_FLOAT 0x10
160 #define FMTF_STRING 0x20
162 #define HIGH_PIECE 'H'
163 #define MIDDLE_PIECE 'M'
164 #define LOW_PIECE 'L'
166 #define SHOW_st_dev 'd'
167 #define SHOW_st_ino 'i'
168 #define SHOW_st_mode 'p'
169 #define SHOW_st_mode2 'A'
170 #define SHOW_st_nlink 'l'
171 #define SHOW_st_uid 'u'
172 #define SHOW_st_gid 'g'
173 #define SHOW_st_rdev 'r'
174 #define SHOW_st_atime 'a'
175 #define SHOW_st_mtime 'm'
176 #define SHOW_st_ctime 'c'
177 #define SHOW_st_btime 'B'
178 #define SHOW_st_size 'z'
179 #define SHOW_st_blocks 'b'
180 #define SHOW_st_blksize 'k'
181 #define SHOW_st_flags 'f'
182 #define SHOW_st_gen 'v'
183 #define SHOW_symlink 'Y'
184 #define SHOW_filetype 'T'
185 #define SHOW_filename 'N'
186 #define SHOW_sizerdev 'Z'
188 void usage(const char *);
189 void output(const struct stat
*, const char *,
190 const char *, int, int, int);
191 int format1(const struct stat
*, /* stat info */
192 const char *, /* the file name */
193 const char *, int, /* the format string itself */
194 char *, size_t, /* a place to put the output */
195 int, int, int, int, /* the parsed format */
201 #define addchar(s, c, nl) \
203 (void)fputc((c), (s)); \
204 (*nl) = ((c) == '\n'); \
205 } while (0/*CONSTCOND*/)
208 main(int argc
, char *argv
[])
211 int ch
, rc
, errs
, am_readlink
;
212 int lsF
, fmtchar
, usestat
, fn
, nonl
, quiet
;
213 char *statfmt
, *options
, *synopsis
;
225 if (strcmp(getprogname(), "readlink") == 0) {
228 synopsis
= "[-n] [file ...]";
233 options
= "f:FlLnqrst:x";
234 synopsis
= "[-FlLnqrsx] [-f format] [-t timefmt] [file ...]";
237 while ((ch
= getopt(argc
, argv
, options
)) != -1)
259 errx(1, "can't use format '%c' with '%c'",
274 if (fmtchar
== '\0') {
279 statfmt
= DEF_FORMAT
;
283 if (lsF
&& fmtchar
!= 'l')
284 errx(1, "can't use format '%c' with -F", fmtchar
);
288 /* statfmt already set */
291 statfmt
= lsF
? LSF_FORMAT
: LS_FORMAT
;
294 statfmt
= RAW_FORMAT
;
297 statfmt
= SHELL_FORMAT
;
300 statfmt
= LINUX_FORMAT
;
310 timefmt
= TIME_FORMAT
;
315 rc
= fstat(STDIN_FILENO
, &st
);
317 rc
= stat(argv
[0], &st
);
319 rc
= lstat(argv
[0], &st
);
326 argc
== 0 ? "(stdin)" : argv
[0]);
329 output(&st
, argv
[0], statfmt
, fn
, nonl
, quiet
);
336 return (am_readlink
? linkfail
: errs
);
340 usage(const char *synopsis
)
343 (void)fprintf(stderr
, "usage: %s %s\n", getprogname(), synopsis
);
348 * Parses a format string.
351 output(const struct stat
*st
, const char *file
,
352 const char *statfmt
, int fn
, int nonl
, int quiet
)
354 int flags
, size
, prec
, ofmt
, hilo
, what
;
360 while (*statfmt
!= '\0') {
363 * Non-format characters go straight out.
365 if (*statfmt
!= FMT_MAGIC
) {
366 addchar(stdout
, *statfmt
, &nl
);
372 * The current format "substring" starts here,
373 * and then we skip the magic.
379 * Some simple one-character "formats".
383 addchar(stdout
, '\n', &nl
);
387 addchar(stdout
, '\t', &nl
);
391 addchar(stdout
, '%', &nl
);
394 case SIMPLE_NUMBER
: {
397 snprintf(num
, sizeof(num
), "%d", fn
);
398 for (p
= &num
[0]; *p
; p
++)
399 addchar(stdout
, *p
, &nl
);
406 * This must be an actual format string. Format strings are
407 * similar to printf(3) formats up to a point, and are of
410 * % required start of format
411 * [-# +0] opt. format characters
412 * size opt. field width
413 * . opt. decimal separator, followed by
414 * prec opt. precision
415 * fmt opt. output specifier (string, numeric, etc.)
416 * sub opt. sub field specifier (high, middle, low)
417 * datum required field specifier (size, mode, etc)
419 * Only the % and the datum selector are required. All data
420 * have reasonable default output forms. The "sub" specifier
421 * only applies to certain data (mode, dev, rdev, filetype).
422 * The symlink output defaults to STRING, yet will only emit
423 * the leading " -> " if STRING is explicitly specified. The
424 * sizerdev datum will generate rdev output for character or
425 * block devices, and size output for all others.
429 if (*statfmt
== FMT_POUND
)
431 else if (*statfmt
== FMT_SPACE
)
433 else if (*statfmt
== FMT_PLUS
)
435 else if (*statfmt
== FMT_ZERO
)
437 else if (*statfmt
== FMT_MINUS
)
442 } while (1/*CONSTCOND*/);
445 if (isdigit((unsigned)*statfmt
)) {
447 while (isdigit((unsigned)*statfmt
)) {
448 size
= (size
* 10) + (*statfmt
- '0');
456 if (*statfmt
== FMT_DOT
) {
460 while (isdigit((unsigned)*statfmt
)) {
461 prec
= (prec
* 10) + (*statfmt
- '0');
468 #define fmtcase(x, y) case (y): (x) = (y); statfmt++; break
469 #define fmtcasef(x, y, z) case (y): (x) = (z); statfmt++; break
471 fmtcasef(ofmt
, FMT_DECIMAL
, FMTF_DECIMAL
);
472 fmtcasef(ofmt
, FMT_OCTAL
, FMTF_OCTAL
);
473 fmtcasef(ofmt
, FMT_UNSIGNED
, FMTF_UNSIGNED
);
474 fmtcasef(ofmt
, FMT_HEX
, FMTF_HEX
);
475 fmtcasef(ofmt
, FMT_FLOAT
, FMTF_FLOAT
);
476 fmtcasef(ofmt
, FMT_STRING
, FMTF_STRING
);
483 fmtcase(hilo
, HIGH_PIECE
);
484 fmtcase(hilo
, MIDDLE_PIECE
);
485 fmtcase(hilo
, LOW_PIECE
);
492 fmtcase(what
, SHOW_st_dev
);
493 fmtcase(what
, SHOW_st_ino
);
494 fmtcase(what
, SHOW_st_mode
);
495 fmtcase(what
, SHOW_st_mode2
);
496 fmtcase(what
, SHOW_st_nlink
);
497 fmtcase(what
, SHOW_st_uid
);
498 fmtcase(what
, SHOW_st_gid
);
499 fmtcase(what
, SHOW_st_rdev
);
500 fmtcase(what
, SHOW_st_atime
);
501 fmtcase(what
, SHOW_st_mtime
);
502 fmtcase(what
, SHOW_st_ctime
);
503 fmtcase(what
, SHOW_st_btime
);
504 fmtcase(what
, SHOW_st_size
);
505 fmtcase(what
, SHOW_st_blocks
);
506 fmtcase(what
, SHOW_st_blksize
);
507 fmtcase(what
, SHOW_st_flags
);
508 fmtcase(what
, SHOW_st_gen
);
509 fmtcase(what
, SHOW_symlink
);
510 fmtcase(what
, SHOW_filetype
);
511 fmtcase(what
, SHOW_filename
);
512 fmtcase(what
, SHOW_sizerdev
);
521 subfmt
, statfmt
- subfmt
,
523 flags
, size
, prec
, ofmt
, hilo
, what
);
525 for (i
= 0; i
< t
&& i
< sizeof(buf
); i
++)
526 addchar(stdout
, buf
[i
], &nl
);
531 errx(1, "%.*s: bad format",
532 (int)(statfmt
- subfmt
+ 1), subfmt
);
536 (void)fputc('\n', stdout
);
537 (void)fflush(stdout
);
541 * Arranges output according to a single parsed format substring.
544 format1(const struct stat
*st
,
546 const char *fmt
, int flen
,
547 char *buf
, size_t blen
,
548 int flags
, int size
, int prec
, int ofmt
,
552 char *sdata
, lfmt
[24], tmp
[20];
553 char smode
[12], sid
[12], path
[PATH_MAX
+ 4];
556 const struct timespec
*tsp
;
559 int l
, small
, formats
;
566 * First, pick out the data and tweak it based on hilo or
567 * specified output format (symlink output only).
572 small
= (sizeof(st
->st_dev
) == 4);
573 data
= (what
== SHOW_st_dev
) ? st
->st_dev
: st
->st_rdev
;
575 sdata
= (what
== SHOW_st_dev
) ?
576 devname(st
->st_dev
, S_IFBLK
) :
578 S_ISCHR(st
->st_mode
) ? S_IFCHR
:
579 S_ISBLK(st
->st_mode
) ? S_IFBLK
:
583 #endif /* HAVE_DEVNAME */
584 if (hilo
== HIGH_PIECE
) {
588 else if (hilo
== LOW_PIECE
) {
589 data
= minor((unsigned)data
);
592 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
|
595 #else /* HAVE_DEVNAME */
597 #endif /* HAVE_DEVNAME */
599 ofmt
= FMTF_UNSIGNED
;
602 small
= (sizeof(st
->st_ino
) == 4);
605 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
607 ofmt
= FMTF_UNSIGNED
;
611 small
= (sizeof(st
->st_mode
) == 4);
613 strmode(st
->st_mode
, smode
);
616 if (sdata
[l
- 1] == ' ')
618 if (what
== SHOW_st_mode2
)
620 if (hilo
== HIGH_PIECE
) {
626 else if (hilo
== MIDDLE_PIECE
) {
627 data
= (data
>> 9) & 07;
632 else if (hilo
== LOW_PIECE
) {
638 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
|
644 small
= (sizeof(st
->st_dev
) == 4);
647 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
649 ofmt
= FMTF_UNSIGNED
;
652 small
= (sizeof(st
->st_uid
) == 4);
654 if ((pw
= getpwuid(st
->st_uid
)) != NULL
)
657 snprintf(sid
, sizeof(sid
), "(%ld)", (long)st
->st_uid
);
660 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
|
663 ofmt
= FMTF_UNSIGNED
;
666 small
= (sizeof(st
->st_gid
) == 4);
668 if ((gr
= getgrgid(st
->st_gid
)) != NULL
)
671 snprintf(sid
, sizeof(sid
), "(%ld)", (long)st
->st_gid
);
674 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
|
677 ofmt
= FMTF_UNSIGNED
;
680 tsp
= &st
->st_atimespec
;
684 tsp
= &st
->st_mtimespec
;
688 tsp
= &st
->st_ctimespec
;
690 #if HAVE_STRUCT_STAT_ST_BIRTHTIME
693 tsp
= &st
->st_birthtimespec
;
694 #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
695 ts
= *tsp
; /* copy so we can muck with it */
696 small
= (sizeof(ts
.tv_sec
) == 4);
699 tm
= localtime(&ts
.tv_sec
);
700 (void)strftime(path
, sizeof(path
), timefmt
, tm
);
702 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
|
703 FMTF_FLOAT
| FMTF_STRING
;
708 small
= (sizeof(st
->st_size
) == 4);
711 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
713 ofmt
= FMTF_UNSIGNED
;
716 small
= (sizeof(st
->st_blocks
) == 4);
717 data
= st
->st_blocks
;
719 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
721 ofmt
= FMTF_UNSIGNED
;
723 case SHOW_st_blksize
:
724 small
= (sizeof(st
->st_blksize
) == 4);
725 data
= st
->st_blksize
;
727 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
729 ofmt
= FMTF_UNSIGNED
;
731 #if HAVE_STRUCT_STAT_ST_FLAGS
733 small
= (sizeof(st
->st_flags
) == 4);
736 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
738 ofmt
= FMTF_UNSIGNED
;
740 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
741 #if HAVE_STRUCT_STAT_ST_GEN
743 small
= (sizeof(st
->st_gen
) == 4);
746 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
748 ofmt
= FMTF_UNSIGNED
;
750 #endif /* HAVE_STRUCT_STAT_ST_GEN */
754 if (S_ISLNK(st
->st_mode
)) {
755 snprintf(path
, sizeof(path
), " -> ");
756 l
= readlink(file
, path
+ 4, sizeof(path
) - 4 - 1);
763 sdata
= path
+ (ofmt
== FMTF_STRING
? 0 : 4);
769 formats
= FMTF_STRING
;
778 if (hilo
== 0 || hilo
== LOW_PIECE
) {
779 switch (st
->st_mode
& S_IFMT
) {
780 case S_IFIFO
: (void)strcat(sdata
, "|"); break;
781 case S_IFDIR
: (void)strcat(sdata
, "/"); break;
784 (S_IXUSR
| S_IXGRP
| S_IXOTH
))
785 (void)strcat(sdata
, "*");
787 case S_IFLNK
: (void)strcat(sdata
, "@"); break;
788 case S_IFSOCK
: (void)strcat(sdata
, "="); break;
790 case S_IFWHT
: (void)strcat(sdata
, "%"); break;
793 case S_IFDOOR
: (void)strcat(sdata
, ">"); break;
794 #endif /* S_IFDOOR */
798 else if (hilo
== HIGH_PIECE
) {
799 switch (st
->st_mode
& S_IFMT
) {
800 case S_IFIFO
: sdata
= "Fifo File"; break;
801 case S_IFCHR
: sdata
= "Character Device"; break;
802 case S_IFDIR
: sdata
= "Directory"; break;
803 case S_IFBLK
: sdata
= "Block Device"; break;
804 case S_IFREG
: sdata
= "Regular File"; break;
805 case S_IFLNK
: sdata
= "Symbolic Link"; break;
806 case S_IFSOCK
: sdata
= "Socket"; break;
808 case S_IFWHT
: sdata
= "Whiteout File"; break;
811 case S_IFDOOR
: sdata
= "Door"; break;
812 #endif /* S_IFDOOR */
813 default: sdata
= "???"; break;
817 formats
= FMTF_STRING
;
825 (void)strncpy(path
, "(stdin)", sizeof(path
));
827 (void)strncpy(path
, file
, sizeof(path
));
829 formats
= FMTF_STRING
;
834 if (S_ISCHR(st
->st_mode
) || S_ISBLK(st
->st_mode
)) {
835 char majdev
[20], mindev
[20];
841 majdev
, sizeof(majdev
),
843 ofmt
, HIGH_PIECE
, SHOW_st_rdev
);
847 mindev
, sizeof(mindev
),
849 ofmt
, LOW_PIECE
, SHOW_st_rdev
);
850 return (snprintf(buf
, blen
, "%.*s,%.*s",
851 l1
, majdev
, l2
, mindev
));
859 ofmt
, 0, SHOW_st_size
));
863 errx(1, "%.*s: bad format", (int)flen
, fmt
);
867 * If a subdatum was specified but not supported, or an output
868 * format was selected that is not supported, that's an error.
870 if (hilo
!= 0 || (ofmt
& formats
) == 0)
871 errx(1, "%.*s: bad format", (int)flen
, fmt
);
874 * Assemble the format string for passing to printf(3).
877 (void)strcat(lfmt
, "%");
878 if (flags
& FLAG_POUND
)
879 (void)strcat(lfmt
, "#");
880 if (flags
& FLAG_SPACE
)
881 (void)strcat(lfmt
, " ");
882 if (flags
& FLAG_PLUS
)
883 (void)strcat(lfmt
, "+");
884 if (flags
& FLAG_MINUS
)
885 (void)strcat(lfmt
, "-");
886 if (flags
& FLAG_ZERO
)
887 (void)strcat(lfmt
, "0");
890 * Only the timespecs support the FLOAT output format, and that
891 * requires work that differs from the other formats.
893 if (ofmt
== FMTF_FLOAT
) {
895 * Nothing after the decimal point, so just print seconds.
899 (void)snprintf(tmp
, sizeof(tmp
), "%d", size
);
900 (void)strcat(lfmt
, tmp
);
902 (void)strcat(lfmt
, "d");
903 return (snprintf(buf
, blen
, lfmt
, ts
.tv_sec
));
907 * Unspecified precision gets all the precision we have:
914 * Adjust the size for the decimal point and the digits
920 * Any leftover size that's legitimate will be used.
923 (void)snprintf(tmp
, sizeof(tmp
), "%d", size
);
924 (void)strcat(lfmt
, tmp
);
926 (void)strcat(lfmt
, "d");
929 * The stuff after the decimal point always needs zero
932 (void)strcat(lfmt
, ".%0");
935 * We can "print" at most nine digits of precision. The
936 * rest we will pad on at the end.
938 (void)snprintf(tmp
, sizeof(tmp
), "%dd", prec
> 9 ? 9 : prec
);
939 (void)strcat(lfmt
, tmp
);
942 * For precision of less that nine digits, trim off the
943 * less significant figures.
945 for (; prec
< 9; prec
++)
949 * Use the format, and then tack on any zeroes that
950 * might be required to make up the requested precision.
952 l
= snprintf(buf
, blen
, lfmt
, ts
.tv_sec
, ts
.tv_nsec
);
953 for (; prec
> 9 && l
< blen
; prec
--, l
++)
954 (void)strcat(buf
, "0");
959 * Add on size and precision, if specified, to the format.
962 (void)snprintf(tmp
, sizeof(tmp
), "%d", size
);
963 (void)strcat(lfmt
, tmp
);
966 (void)snprintf(tmp
, sizeof(tmp
), ".%d", prec
);
967 (void)strcat(lfmt
, tmp
);
971 * String output uses the temporary sdata.
973 if (ofmt
== FMTF_STRING
) {
975 errx(1, "%.*s: bad format", (int)flen
, fmt
);
976 (void)strcat(lfmt
, "s");
977 return (snprintf(buf
, blen
, lfmt
, sdata
));
981 * Ensure that sign extension does not cause bad looking output
984 if (small
&& ofmt
!= FMTF_DECIMAL
)
985 data
= (u_int32_t
)data
;
988 * The four "numeric" output forms.
990 (void)strcat(lfmt
, "ll");
992 case FMTF_DECIMAL
: (void)strcat(lfmt
, "d"); break;
993 case FMTF_OCTAL
: (void)strcat(lfmt
, "o"); break;
994 case FMTF_UNSIGNED
: (void)strcat(lfmt
, "u"); break;
995 case FMTF_HEX
: (void)strcat(lfmt
, "x"); break;
998 return (snprintf(buf
, blen
, lfmt
, data
));