]>
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 __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>
70 #if HAVE_STRUCT_STAT_ST_FLAGS
73 #define SHELL_F " st_flags=%f"
74 #else /* HAVE_STRUCT_STAT_ST_FLAGS */
78 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
80 #if HAVE_STRUCT_STAT_ST_BIRTHTIME
81 #define DEF_B "\"%SB\" "
83 #define SHELL_B "st_birthtime=%B "
84 #else /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
88 #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
90 #if HAVE_STRUCT_STAT_ST_ATIM
91 #define st_atimespec st_atim
92 #define st_ctimespec st_ctim
93 #define st_mtimespec st_mtim
94 #endif /* HAVE_STRUCT_STAT_ST_ATIM */
97 "%d %i %Sp %l %Su %Sg %r %z \"%Sa\" \"%Sm\" \"%Sc\" " DEF_B \
99 #define RAW_FORMAT "%d %i %#p %l %u %g %r %z %a %m %c " RAW_B \
101 #define LS_FORMAT "%Sp %l %Su %Sg %Z %Sm %N%SY"
102 #define LSF_FORMAT "%Sp %l %Su %Sg %Z %Sm %N%T%SY"
103 #define SHELL_FORMAT \
104 "st_dev=%d st_ino=%i st_mode=%#p st_nlink=%l " \
105 "st_uid=%u st_gid=%g st_rdev=%r st_size=%z " \
106 "st_atime=%a st_mtime=%m st_ctime=%c " SHELL_B \
107 "st_blksize=%k st_blocks=%b" SHELL_F
108 #define LINUX_FORMAT \
110 " Size: %-11z FileType: %HT%n" \
111 " Mode: (%04OA/%.10Sp) Uid: (%5u/%8Su) Gid: (%5g/%8Sg)%n" \
112 "Device: %Hd,%Ld Inode: %i Links: %l%n" \
117 #define TIME_FORMAT "%b %e %T %Y"
119 #define FLAG_POUND 0x01
120 #define FLAG_SPACE 0x02
121 #define FLAG_PLUS 0x04
122 #define FLAG_ZERO 0x08
123 #define FLAG_MINUS 0x10
126 * These format characters must all be unique, except the magic one.
128 #define FMT_MAGIC '%'
131 #define SIMPLE_NEWLINE 'n'
132 #define SIMPLE_TAB 't'
133 #define SIMPLE_PERCENT '%'
134 #define SIMPLE_NUMBER '@'
136 #define FMT_POUND '#'
137 #define FMT_SPACE ' '
140 #define FMT_MINUS '-'
142 #define FMT_DECIMAL 'D'
143 #define FMT_OCTAL 'O'
144 #define FMT_UNSIGNED 'U'
146 #define FMT_FLOAT 'F'
147 #define FMT_STRING 'S'
149 #define FMTF_DECIMAL 0x01
150 #define FMTF_OCTAL 0x02
151 #define FMTF_UNSIGNED 0x04
152 #define FMTF_HEX 0x08
153 #define FMTF_FLOAT 0x10
154 #define FMTF_STRING 0x20
156 #define HIGH_PIECE 'H'
157 #define MIDDLE_PIECE 'M'
158 #define LOW_PIECE 'L'
160 #define SHOW_st_dev 'd'
161 #define SHOW_st_ino 'i'
162 #define SHOW_st_mode 'p'
163 #define SHOW_st_mode2 'A'
164 #define SHOW_st_nlink 'l'
165 #define SHOW_st_uid 'u'
166 #define SHOW_st_gid 'g'
167 #define SHOW_st_rdev 'r'
168 #define SHOW_st_atime 'a'
169 #define SHOW_st_mtime 'm'
170 #define SHOW_st_ctime 'c'
171 #define SHOW_st_btime 'B'
172 #define SHOW_st_size 'z'
173 #define SHOW_st_blocks 'b'
174 #define SHOW_st_blksize 'k'
175 #define SHOW_st_flags 'f'
176 #define SHOW_st_gen 'v'
177 #define SHOW_symlink 'Y'
178 #define SHOW_filetype 'T'
179 #define SHOW_filename 'N'
180 #define SHOW_sizerdev 'Z'
182 void usage(const char *);
183 void output(const struct stat
*, const char *,
184 const char *, int, int, int);
185 int format1(const struct stat
*, /* stat info */
186 const char *, /* the file name */
187 const char *, int, /* the format string itself */
188 char *, size_t, /* a place to put the output */
189 int, int, int, int, /* the parsed format */
195 #define addchar(s, c, nl) \
197 (void)fputc((c), (s)); \
198 (*nl) = ((c) == '\n'); \
199 } while (0/*CONSTCOND*/)
202 main(int argc
, char *argv
[])
205 int ch
, rc
, errs
, am_readlink
;
206 int lsF
, fmtchar
, usestat
, fn
, nonl
, quiet
;
207 char *statfmt
, *options
, *synopsis
;
219 if (strcmp(getprogname(), "readlink") == 0) {
222 synopsis
= "[-n] [file ...]";
227 options
= "f:FlLnqrst:x";
228 synopsis
= "[-FlLnqrsx] [-f format] [-t timefmt] [file ...]";
231 while ((ch
= getopt(argc
, argv
, options
)) != -1)
253 errx(1, "can't use format '%c' with '%c'",
268 if (fmtchar
== '\0') {
273 statfmt
= DEF_FORMAT
;
277 if (lsF
&& fmtchar
!= 'l')
278 errx(1, "can't use format '%c' with -F", fmtchar
);
282 /* statfmt already set */
285 statfmt
= lsF
? LSF_FORMAT
: LS_FORMAT
;
288 statfmt
= RAW_FORMAT
;
291 statfmt
= SHELL_FORMAT
;
294 statfmt
= LINUX_FORMAT
;
304 timefmt
= TIME_FORMAT
;
309 rc
= fstat(STDIN_FILENO
, &st
);
311 rc
= stat(argv
[0], &st
);
313 rc
= lstat(argv
[0], &st
);
320 argc
== 0 ? "(stdin)" : argv
[0]);
323 output(&st
, argv
[0], statfmt
, fn
, nonl
, quiet
);
330 return (am_readlink
? linkfail
: errs
);
334 usage(const char *synopsis
)
337 (void)fprintf(stderr
, "usage: %s %s\n", getprogname(), synopsis
);
342 * Parses a format string.
345 output(const struct stat
*st
, const char *file
,
346 const char *statfmt
, int fn
, int nonl
, int quiet
)
348 int flags
, size
, prec
, ofmt
, hilo
, what
;
354 while (*statfmt
!= '\0') {
357 * Non-format characters go straight out.
359 if (*statfmt
!= FMT_MAGIC
) {
360 addchar(stdout
, *statfmt
, &nl
);
366 * The current format "substring" starts here,
367 * and then we skip the magic.
373 * Some simple one-character "formats".
377 addchar(stdout
, '\n', &nl
);
381 addchar(stdout
, '\t', &nl
);
385 addchar(stdout
, '%', &nl
);
388 case SIMPLE_NUMBER
: {
391 snprintf(num
, sizeof(num
), "%d", fn
);
392 for (p
= &num
[0]; *p
; p
++)
393 addchar(stdout
, *p
, &nl
);
400 * This must be an actual format string. Format strings are
401 * similar to printf(3) formats up to a point, and are of
404 * % required start of format
405 * [-# +0] opt. format characters
406 * size opt. field width
407 * . opt. decimal separator, followed by
408 * prec opt. precision
409 * fmt opt. output specifier (string, numeric, etc.)
410 * sub opt. sub field specifier (high, middle, low)
411 * datum required field specifier (size, mode, etc)
413 * Only the % and the datum selector are required. All data
414 * have reasonable default output forms. The "sub" specifier
415 * only applies to certain data (mode, dev, rdev, filetype).
416 * The symlink output defaults to STRING, yet will only emit
417 * the leading " -> " if STRING is explicitly specified. The
418 * sizerdev datum will generate rdev output for character or
419 * block devices, and size output for all others.
423 if (*statfmt
== FMT_POUND
)
425 else if (*statfmt
== FMT_SPACE
)
427 else if (*statfmt
== FMT_PLUS
)
429 else if (*statfmt
== FMT_ZERO
)
431 else if (*statfmt
== FMT_MINUS
)
436 } while (1/*CONSTCOND*/);
439 if (isdigit((unsigned)*statfmt
)) {
441 while (isdigit((unsigned)*statfmt
)) {
442 size
= (size
* 10) + (*statfmt
- '0');
450 if (*statfmt
== FMT_DOT
) {
454 while (isdigit((unsigned)*statfmt
)) {
455 prec
= (prec
* 10) + (*statfmt
- '0');
462 #define fmtcase(x, y) case (y): (x) = (y); statfmt++; break
463 #define fmtcasef(x, y, z) case (y): (x) = (z); statfmt++; break
465 fmtcasef(ofmt
, FMT_DECIMAL
, FMTF_DECIMAL
);
466 fmtcasef(ofmt
, FMT_OCTAL
, FMTF_OCTAL
);
467 fmtcasef(ofmt
, FMT_UNSIGNED
, FMTF_UNSIGNED
);
468 fmtcasef(ofmt
, FMT_HEX
, FMTF_HEX
);
469 fmtcasef(ofmt
, FMT_FLOAT
, FMTF_FLOAT
);
470 fmtcasef(ofmt
, FMT_STRING
, FMTF_STRING
);
477 fmtcase(hilo
, HIGH_PIECE
);
478 fmtcase(hilo
, MIDDLE_PIECE
);
479 fmtcase(hilo
, LOW_PIECE
);
486 fmtcase(what
, SHOW_st_dev
);
487 fmtcase(what
, SHOW_st_ino
);
488 fmtcase(what
, SHOW_st_mode
);
489 fmtcase(what
, SHOW_st_mode2
);
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
;
605 small
= (sizeof(st
->st_mode
) == 4);
607 strmode(st
->st_mode
, smode
);
610 if (sdata
[l
- 1] == ' ')
612 if (what
== SHOW_st_mode2
)
614 if (hilo
== HIGH_PIECE
) {
620 else if (hilo
== MIDDLE_PIECE
) {
621 data
= (data
>> 9) & 07;
626 else if (hilo
== LOW_PIECE
) {
632 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
|
638 small
= (sizeof(st
->st_dev
) == 4);
641 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
643 ofmt
= FMTF_UNSIGNED
;
646 small
= (sizeof(st
->st_uid
) == 4);
648 if ((pw
= getpwuid(st
->st_uid
)) != NULL
)
651 snprintf(sid
, sizeof(sid
), "(%ld)", (long)st
->st_uid
);
654 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
|
657 ofmt
= FMTF_UNSIGNED
;
660 small
= (sizeof(st
->st_gid
) == 4);
662 if ((gr
= getgrgid(st
->st_gid
)) != NULL
)
665 snprintf(sid
, sizeof(sid
), "(%ld)", (long)st
->st_gid
);
668 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
|
671 ofmt
= FMTF_UNSIGNED
;
674 tsp
= &st
->st_atimespec
;
678 tsp
= &st
->st_mtimespec
;
682 tsp
= &st
->st_ctimespec
;
684 #if HAVE_STRUCT_STAT_ST_BIRTHTIME
687 tsp
= &st
->st_birthtimespec
;
688 #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
689 ts
= *tsp
; /* copy so we can muck with it */
690 small
= (sizeof(ts
.tv_sec
) == 4);
692 tm
= localtime(&ts
.tv_sec
);
695 tm
= localtime(&ts
.tv_sec
);
697 (void)strftime(path
, sizeof(path
), timefmt
, tm
);
699 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
|
700 FMTF_FLOAT
| FMTF_STRING
;
705 small
= (sizeof(st
->st_size
) == 4);
708 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
710 ofmt
= FMTF_UNSIGNED
;
713 small
= (sizeof(st
->st_blocks
) == 4);
714 data
= st
->st_blocks
;
716 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
718 ofmt
= FMTF_UNSIGNED
;
720 case SHOW_st_blksize
:
721 small
= (sizeof(st
->st_blksize
) == 4);
722 data
= st
->st_blksize
;
724 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
726 ofmt
= FMTF_UNSIGNED
;
728 #if HAVE_STRUCT_STAT_ST_FLAGS
730 small
= (sizeof(st
->st_flags
) == 4);
733 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
735 ofmt
= FMTF_UNSIGNED
;
737 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
738 #if HAVE_STRUCT_STAT_ST_GEN
740 small
= (sizeof(st
->st_gen
) == 4);
743 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
745 ofmt
= FMTF_UNSIGNED
;
747 #endif /* HAVE_STRUCT_STAT_ST_GEN */
751 if (S_ISLNK(st
->st_mode
)) {
752 snprintf(path
, sizeof(path
), " -> ");
753 l
= readlink(file
, path
+ 4, sizeof(path
) - 4 - 1);
760 sdata
= path
+ (ofmt
== FMTF_STRING
? 0 : 4);
766 formats
= FMTF_STRING
;
775 if (hilo
== 0 || hilo
== LOW_PIECE
) {
776 switch (st
->st_mode
& S_IFMT
) {
777 case S_IFIFO
: (void)strcat(sdata
, "|"); break;
778 case S_IFDIR
: (void)strcat(sdata
, "/"); break;
781 (S_IXUSR
| S_IXGRP
| S_IXOTH
))
782 (void)strcat(sdata
, "*");
784 case S_IFLNK
: (void)strcat(sdata
, "@"); break;
785 case S_IFSOCK
: (void)strcat(sdata
, "="); break;
787 case S_IFWHT
: (void)strcat(sdata
, "%"); break;
790 case S_IFDOOR
: (void)strcat(sdata
, ">"); break;
791 #endif /* S_IFDOOR */
795 else if (hilo
== HIGH_PIECE
) {
796 switch (st
->st_mode
& S_IFMT
) {
797 case S_IFIFO
: sdata
= "Fifo File"; break;
798 case S_IFCHR
: sdata
= "Character Device"; break;
799 case S_IFDIR
: sdata
= "Directory"; break;
800 case S_IFBLK
: sdata
= "Block Device"; break;
801 case S_IFREG
: sdata
= "Regular File"; break;
802 case S_IFLNK
: sdata
= "Symbolic Link"; break;
803 case S_IFSOCK
: sdata
= "Socket"; break;
805 case S_IFWHT
: sdata
= "Whiteout File"; break;
808 case S_IFDOOR
: sdata
= "Door"; break;
809 #endif /* S_IFDOOR */
810 default: sdata
= "???"; break;
814 formats
= FMTF_STRING
;
822 (void)strncpy(path
, "(stdin)", sizeof(path
));
824 (void)strncpy(path
, file
, sizeof(path
));
826 formats
= FMTF_STRING
;
831 if (S_ISCHR(st
->st_mode
) || S_ISBLK(st
->st_mode
)) {
832 char majdev
[20], mindev
[20];
838 majdev
, sizeof(majdev
),
840 ofmt
, HIGH_PIECE
, SHOW_st_rdev
);
844 mindev
, sizeof(mindev
),
846 ofmt
, LOW_PIECE
, SHOW_st_rdev
);
847 return (snprintf(buf
, blen
, "%.*s,%.*s",
848 l1
, majdev
, l2
, mindev
));
856 ofmt
, 0, SHOW_st_size
));
860 errx(1, "%.*s: bad format", (int)flen
, fmt
);
864 * If a subdatum was specified but not supported, or an output
865 * format was selected that is not supported, that's an error.
867 if (hilo
!= 0 || (ofmt
& formats
) == 0)
868 errx(1, "%.*s: bad format", (int)flen
, fmt
);
871 * Assemble the format string for passing to printf(3).
874 (void)strcat(lfmt
, "%");
875 if (flags
& FLAG_POUND
)
876 (void)strcat(lfmt
, "#");
877 if (flags
& FLAG_SPACE
)
878 (void)strcat(lfmt
, " ");
879 if (flags
& FLAG_PLUS
)
880 (void)strcat(lfmt
, "+");
881 if (flags
& FLAG_MINUS
)
882 (void)strcat(lfmt
, "-");
883 if (flags
& FLAG_ZERO
)
884 (void)strcat(lfmt
, "0");
887 * Only the timespecs support the FLOAT output format, and that
888 * requires work that differs from the other formats.
890 if (ofmt
== FMTF_FLOAT
) {
892 * Nothing after the decimal point, so just print seconds.
896 (void)snprintf(tmp
, sizeof(tmp
), "%d", size
);
897 (void)strcat(lfmt
, tmp
);
899 (void)strcat(lfmt
, "d");
900 return (snprintf(buf
, blen
, lfmt
, ts
.tv_sec
));
904 * Unspecified precision gets all the precision we have:
911 * Adjust the size for the decimal point and the digits
917 * Any leftover size that's legitimate will be used.
920 (void)snprintf(tmp
, sizeof(tmp
), "%d", size
);
921 (void)strcat(lfmt
, tmp
);
923 (void)strcat(lfmt
, "d");
926 * The stuff after the decimal point always needs zero
929 (void)strcat(lfmt
, ".%0");
932 * We can "print" at most nine digits of precision. The
933 * rest we will pad on at the end.
935 (void)snprintf(tmp
, sizeof(tmp
), "%dd", prec
> 9 ? 9 : prec
);
936 (void)strcat(lfmt
, tmp
);
939 * For precision of less that nine digits, trim off the
940 * less significant figures.
942 for (; prec
< 9; prec
++)
946 * Use the format, and then tack on any zeroes that
947 * might be required to make up the requested precision.
949 l
= snprintf(buf
, blen
, lfmt
, ts
.tv_sec
, ts
.tv_nsec
);
950 for (; prec
> 9 && l
< blen
; prec
--, l
++)
951 (void)strcat(buf
, "0");
956 * Add on size and precision, if specified, to the format.
959 (void)snprintf(tmp
, sizeof(tmp
), "%d", size
);
960 (void)strcat(lfmt
, tmp
);
963 (void)snprintf(tmp
, sizeof(tmp
), ".%d", prec
);
964 (void)strcat(lfmt
, tmp
);
968 * String output uses the temporary sdata.
970 if (ofmt
== FMTF_STRING
) {
972 errx(1, "%.*s: bad format", (int)flen
, fmt
);
973 (void)strcat(lfmt
, "s");
974 return (snprintf(buf
, blen
, lfmt
, sdata
));
978 * Ensure that sign extension does not cause bad looking output
981 if (small
&& ofmt
!= FMTF_DECIMAL
)
982 data
= (u_int32_t
)data
;
985 * The four "numeric" output forms.
987 (void)strcat(lfmt
, "ll");
989 case FMTF_DECIMAL
: (void)strcat(lfmt
, "d"); break;
990 case FMTF_OCTAL
: (void)strcat(lfmt
, "o"); break;
991 case FMTF_UNSIGNED
: (void)strcat(lfmt
, "u"); break;
992 case FMTF_HEX
: (void)strcat(lfmt
, "x"); break;
995 return (snprintf(buf
, blen
, lfmt
, data
));