]>
git.cameronkatri.com Git - apple_cmds.git/blob - file_cmds/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_STRUCT_STAT_ST_ATIM 0
54 #define HAVE_DEVNAME 1
55 #endif /* HAVE_CONFIG_H */
57 #include <sys/types.h>
71 #if HAVE_STRUCT_STAT_ST_FLAGS
74 #define SHELL_F " st_flags=%f"
75 #else /* HAVE_STRUCT_STAT_ST_FLAGS */
79 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
81 #if HAVE_STRUCT_STAT_ST_BIRTHTIME
82 #define DEF_B "\"%SB\" "
84 #define SHELL_B "st_birthtime=%B "
85 #else /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
89 #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
91 #if HAVE_STRUCT_STAT_ST_ATIM
92 #define st_atimespec st_atim
93 #define st_ctimespec st_ctim
94 #define st_mtimespec st_mtim
95 #endif /* HAVE_STRUCT_STAT_ST_ATIM */
98 "%d %i %Sp %l %Su %Sg %r %z \"%Sa\" \"%Sm\" \"%Sc\" " DEF_B \
100 #define RAW_FORMAT "%d %i %#p %l %u %g %r %z %a %m %c " RAW_B \
102 #define LS_FORMAT "%Sp %l %Su %Sg %Z %Sm %N%SY"
103 #define LSF_FORMAT "%Sp %l %Su %Sg %Z %Sm %N%T%SY"
104 #define SHELL_FORMAT \
105 "st_dev=%d st_ino=%i st_mode=%#p st_nlink=%l " \
106 "st_uid=%u st_gid=%g st_rdev=%r st_size=%z " \
107 "st_atime=%a st_mtime=%m st_ctime=%c " SHELL_B \
108 "st_blksize=%k st_blocks=%b" SHELL_F
109 #define LINUX_FORMAT \
111 " Size: %-11z FileType: %HT%n" \
112 " Mode: (%04OA/%.10Sp) Uid: (%5u/%8Su) Gid: (%5g/%8Sg)%n" \
113 "Device: %Hd,%Ld Inode: %i Links: %l%n" \
118 #define TIME_FORMAT "%b %e %T %Y"
120 #define FLAG_POUND 0x01
121 #define FLAG_SPACE 0x02
122 #define FLAG_PLUS 0x04
123 #define FLAG_ZERO 0x08
124 #define FLAG_MINUS 0x10
127 * These format characters must all be unique, except the magic one.
129 #define FMT_MAGIC '%'
132 #define SIMPLE_NEWLINE 'n'
133 #define SIMPLE_TAB 't'
134 #define SIMPLE_PERCENT '%'
135 #define SIMPLE_NUMBER '@'
137 #define FMT_POUND '#'
138 #define FMT_SPACE ' '
141 #define FMT_MINUS '-'
143 #define FMT_DECIMAL 'D'
144 #define FMT_OCTAL 'O'
145 #define FMT_UNSIGNED 'U'
147 #define FMT_FLOAT 'F'
148 #define FMT_STRING 'S'
150 #define FMTF_DECIMAL 0x01
151 #define FMTF_OCTAL 0x02
152 #define FMTF_UNSIGNED 0x04
153 #define FMTF_HEX 0x08
154 #define FMTF_FLOAT 0x10
155 #define FMTF_STRING 0x20
157 #define HIGH_PIECE 'H'
158 #define MIDDLE_PIECE 'M'
159 #define LOW_PIECE 'L'
161 #define SHOW_st_dev 'd'
162 #define SHOW_st_ino 'i'
163 #define SHOW_st_mode 'p'
164 #define SHOW_st_mode2 'A'
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_mode2
);
491 fmtcase(what
, SHOW_st_nlink
);
492 fmtcase(what
, SHOW_st_uid
);
493 fmtcase(what
, SHOW_st_gid
);
494 fmtcase(what
, SHOW_st_rdev
);
495 fmtcase(what
, SHOW_st_atime
);
496 fmtcase(what
, SHOW_st_mtime
);
497 fmtcase(what
, SHOW_st_ctime
);
498 fmtcase(what
, SHOW_st_btime
);
499 fmtcase(what
, SHOW_st_size
);
500 fmtcase(what
, SHOW_st_blocks
);
501 fmtcase(what
, SHOW_st_blksize
);
502 fmtcase(what
, SHOW_st_flags
);
503 fmtcase(what
, SHOW_st_gen
);
504 fmtcase(what
, SHOW_symlink
);
505 fmtcase(what
, SHOW_filetype
);
506 fmtcase(what
, SHOW_filename
);
507 fmtcase(what
, SHOW_sizerdev
);
516 subfmt
, statfmt
- subfmt
,
518 flags
, size
, prec
, ofmt
, hilo
, what
);
520 for (i
= 0; i
< t
&& i
< sizeof(buf
); i
++)
521 addchar(stdout
, buf
[i
], &nl
);
526 errx(1, "%.*s: bad format",
527 (int)(statfmt
- subfmt
+ 1), subfmt
);
531 (void)fputc('\n', stdout
);
532 (void)fflush(stdout
);
536 * Arranges output according to a single parsed format substring.
539 format1(const struct stat
*st
,
541 const char *fmt
, int flen
,
542 char *buf
, size_t blen
,
543 int flags
, int size
, int prec
, int ofmt
,
547 char *sdata
, lfmt
[24], tmp
[20];
548 char smode
[12], sid
[12], path
[PATH_MAX
+ 4];
551 const struct timespec
*tsp
;
552 struct timespec ts
= {0,0};
554 int l
, small
, formats
;
561 * First, pick out the data and tweak it based on hilo or
562 * specified output format (symlink output only).
567 small
= (sizeof(st
->st_dev
) == 4);
568 data
= (what
== SHOW_st_dev
) ? st
->st_dev
: st
->st_rdev
;
570 sdata
= (what
== SHOW_st_dev
) ?
571 devname(st
->st_dev
, S_IFBLK
) :
573 S_ISCHR(st
->st_mode
) ? S_IFCHR
:
574 S_ISBLK(st
->st_mode
) ? S_IFBLK
:
578 #endif /* HAVE_DEVNAME */
579 if (hilo
== HIGH_PIECE
) {
583 else if (hilo
== LOW_PIECE
) {
584 data
= minor((unsigned)data
);
587 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
|
590 #else /* HAVE_DEVNAME */
592 #endif /* HAVE_DEVNAME */
594 ofmt
= FMTF_UNSIGNED
;
597 small
= (sizeof(st
->st_ino
) == 4);
600 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
602 ofmt
= FMTF_UNSIGNED
;
606 small
= (sizeof(st
->st_mode
) == 4);
608 strmode(st
->st_mode
, smode
);
611 if (sdata
[l
- 1] == ' ')
613 if (what
== SHOW_st_mode2
)
615 if (hilo
== HIGH_PIECE
) {
621 else if (hilo
== MIDDLE_PIECE
) {
622 data
= (data
>> 9) & 07;
627 else if (hilo
== LOW_PIECE
) {
633 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
|
639 small
= (sizeof(st
->st_dev
) == 4);
642 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
644 ofmt
= FMTF_UNSIGNED
;
647 small
= (sizeof(st
->st_uid
) == 4);
649 if ((pw
= getpwuid(st
->st_uid
)) != NULL
)
652 snprintf(sid
, sizeof(sid
), "(%ld)", (long)st
->st_uid
);
655 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
|
658 ofmt
= FMTF_UNSIGNED
;
661 small
= (sizeof(st
->st_gid
) == 4);
663 if ((gr
= getgrgid(st
->st_gid
)) != NULL
)
666 snprintf(sid
, sizeof(sid
), "(%ld)", (long)st
->st_gid
);
669 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
|
672 ofmt
= FMTF_UNSIGNED
;
675 tsp
= &st
->st_atimespec
;
679 tsp
= &st
->st_mtimespec
;
683 tsp
= &st
->st_ctimespec
;
685 #if HAVE_STRUCT_STAT_ST_BIRTHTIME
688 tsp
= &st
->st_birthtimespec
;
689 #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
690 ts
= *tsp
; /* copy so we can muck with it */
691 small
= (sizeof(ts
.tv_sec
) == 4);
693 tm
= localtime(&ts
.tv_sec
);
696 tm
= localtime(&ts
.tv_sec
);
698 (void)strftime(path
, sizeof(path
), timefmt
, tm
);
700 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
|
701 FMTF_FLOAT
| FMTF_STRING
;
706 small
= (sizeof(st
->st_size
) == 4);
709 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
711 ofmt
= FMTF_UNSIGNED
;
714 small
= (sizeof(st
->st_blocks
) == 4);
715 data
= st
->st_blocks
;
717 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
719 ofmt
= FMTF_UNSIGNED
;
721 case SHOW_st_blksize
:
722 small
= (sizeof(st
->st_blksize
) == 4);
723 data
= st
->st_blksize
;
725 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
727 ofmt
= FMTF_UNSIGNED
;
729 #if HAVE_STRUCT_STAT_ST_FLAGS
731 small
= (sizeof(st
->st_flags
) == 4);
734 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
|
736 if (ofmt
== FMTF_STRING
) {
739 snprintf(path
, sizeof(path
), "%s", fflagstostr(st
->st_flags
));
743 ofmt
= FMTF_UNSIGNED
;
745 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
746 #if HAVE_STRUCT_STAT_ST_GEN
748 small
= (sizeof(st
->st_gen
) == 4);
751 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
753 ofmt
= FMTF_UNSIGNED
;
755 #endif /* HAVE_STRUCT_STAT_ST_GEN */
759 if (S_ISLNK(st
->st_mode
)) {
760 snprintf(path
, sizeof(path
), " -> ");
761 l
= readlink(file
, path
+ 4, sizeof(path
) - 4 - 1);
768 sdata
= path
+ (ofmt
== FMTF_STRING
? 0 : 4);
774 formats
= FMTF_STRING
;
783 if (hilo
== 0 || hilo
== LOW_PIECE
) {
784 switch (st
->st_mode
& S_IFMT
) {
785 case S_IFIFO
: (void)strcat(sdata
, "|"); break;
786 case S_IFDIR
: (void)strcat(sdata
, "/"); break;
789 (S_IXUSR
| S_IXGRP
| S_IXOTH
))
790 (void)strcat(sdata
, "*");
792 case S_IFLNK
: (void)strcat(sdata
, "@"); break;
793 case S_IFSOCK
: (void)strcat(sdata
, "="); break;
795 case S_IFWHT
: (void)strcat(sdata
, "%"); break;
798 case S_IFDOOR
: (void)strcat(sdata
, ">"); break;
799 #endif /* S_IFDOOR */
803 else if (hilo
== HIGH_PIECE
) {
804 switch (st
->st_mode
& S_IFMT
) {
805 case S_IFIFO
: sdata
= "Fifo File"; break;
806 case S_IFCHR
: sdata
= "Character Device"; break;
807 case S_IFDIR
: sdata
= "Directory"; break;
808 case S_IFBLK
: sdata
= "Block Device"; break;
809 case S_IFREG
: sdata
= "Regular File"; break;
810 case S_IFLNK
: sdata
= "Symbolic Link"; break;
811 case S_IFSOCK
: sdata
= "Socket"; break;
813 case S_IFWHT
: sdata
= "Whiteout File"; break;
816 case S_IFDOOR
: sdata
= "Door"; break;
817 #endif /* S_IFDOOR */
818 default: sdata
= "???"; break;
822 formats
= FMTF_STRING
;
830 (void)strncpy(path
, "(stdin)", sizeof(path
));
832 (void)strncpy(path
, file
, sizeof(path
));
834 formats
= FMTF_STRING
;
839 if (S_ISCHR(st
->st_mode
) || S_ISBLK(st
->st_mode
)) {
840 char majdev
[20], mindev
[20];
846 majdev
, sizeof(majdev
),
848 ofmt
, HIGH_PIECE
, SHOW_st_rdev
);
852 mindev
, sizeof(mindev
),
854 ofmt
, LOW_PIECE
, SHOW_st_rdev
);
855 return (snprintf(buf
, blen
, "%.*s,%.*s",
856 l1
, majdev
, l2
, mindev
));
864 ofmt
, 0, SHOW_st_size
));
868 errx(1, "%.*s: bad format", (int)flen
, fmt
);
872 * If a subdatum was specified but not supported, or an output
873 * format was selected that is not supported, that's an error.
875 if (hilo
!= 0 || (ofmt
& formats
) == 0)
876 errx(1, "%.*s: bad format", (int)flen
, fmt
);
879 * Assemble the format string for passing to printf(3).
882 (void)strcat(lfmt
, "%");
883 if (flags
& FLAG_POUND
)
884 (void)strcat(lfmt
, "#");
885 if (flags
& FLAG_SPACE
)
886 (void)strcat(lfmt
, " ");
887 if (flags
& FLAG_PLUS
)
888 (void)strcat(lfmt
, "+");
889 if (flags
& FLAG_MINUS
)
890 (void)strcat(lfmt
, "-");
891 if (flags
& FLAG_ZERO
)
892 (void)strcat(lfmt
, "0");
895 * Only the timespecs support the FLOAT output format, and that
896 * requires work that differs from the other formats.
898 if (ofmt
== FMTF_FLOAT
) {
900 * Nothing after the decimal point, so just print seconds.
904 (void)snprintf(tmp
, sizeof(tmp
), "%d", size
);
905 (void)strcat(lfmt
, tmp
);
907 (void)strcat(lfmt
, "d");
908 return (snprintf(buf
, blen
, lfmt
, ts
.tv_sec
));
912 * Unspecified precision gets all the precision we have:
919 * Adjust the size for the decimal point and the digits
925 * Any leftover size that's legitimate will be used.
928 (void)snprintf(tmp
, sizeof(tmp
), "%d", size
);
929 (void)strcat(lfmt
, tmp
);
931 (void)strcat(lfmt
, "d");
934 * The stuff after the decimal point always needs zero
937 (void)strcat(lfmt
, ".%0");
940 * We can "print" at most nine digits of precision. The
941 * rest we will pad on at the end.
943 (void)snprintf(tmp
, sizeof(tmp
), "%dd", prec
> 9 ? 9 : prec
);
944 (void)strcat(lfmt
, tmp
);
947 * For precision of less that nine digits, trim off the
948 * less significant figures.
950 for (; prec
< 9; prec
++)
954 * Use the format, and then tack on any zeroes that
955 * might be required to make up the requested precision.
957 l
= snprintf(buf
, blen
, lfmt
, ts
.tv_sec
, ts
.tv_nsec
);
958 for (; prec
> 9 && l
< blen
; prec
--, l
++)
959 (void)strcat(buf
, "0");
964 * Add on size and precision, if specified, to the format.
967 (void)snprintf(tmp
, sizeof(tmp
), "%d", size
);
968 (void)strcat(lfmt
, tmp
);
971 (void)snprintf(tmp
, sizeof(tmp
), ".%d", prec
);
972 (void)strcat(lfmt
, tmp
);
976 * String output uses the temporary sdata.
978 if (ofmt
== FMTF_STRING
) {
980 errx(1, "%.*s: bad format", (int)flen
, fmt
);
981 (void)strcat(lfmt
, "s");
982 return (snprintf(buf
, blen
, lfmt
, sdata
));
986 * Ensure that sign extension does not cause bad looking output
989 if (small
&& ofmt
!= FMTF_DECIMAL
)
990 data
= (u_int32_t
)data
;
993 * The four "numeric" output forms.
995 (void)strcat(lfmt
, "ll");
997 case FMTF_DECIMAL
: (void)strcat(lfmt
, "d"); break;
998 case FMTF_OCTAL
: (void)strcat(lfmt
, "o"); break;
999 case FMTF_UNSIGNED
: (void)strcat(lfmt
, "u"); break;
1000 case FMTF_HEX
: (void)strcat(lfmt
, "x"); break;
1003 return (snprintf(buf
, blen
, lfmt
, data
));