1 /* $NetBSD: stat.c,v 1.27 2008/05/16 17:58:33 atatat Exp $ */
4 * Copyright (c) 2002 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
36 #include <sys/cdefs.h>
38 __RCSID("$NetBSD: stat.c,v 1.27 2008/05/16 17:58:33 atatat Exp $");
41 #if ! HAVE_NBTOOL_CONFIG_H
42 #define HAVE_STRUCT_STAT_ST_FLAGS 1
43 #define HAVE_STRUCT_STAT_ST_GEN 1
44 #define HAVE_STRUCT_STAT_ST_BIRTHTIME 1
45 #define HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC 1
46 #define HAVE_STRUCT_STAT_ST_MTIMENSEC 1
47 #define HAVE_DEVNAME 1
48 #endif /* HAVE_NBTOOL_CONFIG_H */
50 #include <sys/types.h>
65 #if HAVE_STRUCT_STAT_ST_FLAGS
68 #define SHELL_F " st_flags=%f"
69 #else /* HAVE_STRUCT_STAT_ST_FLAGS */
73 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
75 #if HAVE_STRUCT_STAT_ST_BIRTHTIME
76 #define DEF_B "\"%SB\" "
78 #define SHELL_B "st_birthtime=%B "
79 #else /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
83 #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
85 #if HAVE_STRUCT_STAT_ST_ATIM
86 #define st_atimespec st_atim
87 #define st_ctimespec st_ctim
88 #define st_mtimespec st_mtim
89 #endif /* HAVE_STRUCT_STAT_ST_ATIM */
92 "%d %i %Sp %l %Su %Sg %r %z \"%Sa\" \"%Sm\" \"%Sc\" " DEF_B \
94 #define RAW_FORMAT "%d %i %#p %l %u %g %r %z %a %m %c " RAW_B \
96 #define LS_FORMAT "%Sp %l %Su %Sg %Z %Sm %N%SY"
97 #define LSF_FORMAT "%Sp %l %Su %Sg %Z %Sm %N%T%SY"
98 #define SHELL_FORMAT \
99 "st_dev=%d st_ino=%i st_mode=%#p st_nlink=%l " \
100 "st_uid=%u st_gid=%g st_rdev=%r st_size=%z " \
101 "st_atime=%a st_mtime=%m st_ctime=%c " SHELL_B \
102 "st_blksize=%k st_blocks=%b" SHELL_F
103 #define LINUX_FORMAT \
105 " Size: %-11z FileType: %HT%n" \
106 " Mode: (%04OLp/%.10Sp) Uid: (%5u/%8Su) Gid: (%5g/%8Sg)%n" \
107 "Device: %Hd,%Ld Inode: %i Links: %l%n" \
112 #define TIME_FORMAT "%b %e %T %Y"
114 #define FLAG_POUND 0x01
115 #define FLAG_SPACE 0x02
116 #define FLAG_PLUS 0x04
117 #define FLAG_ZERO 0x08
118 #define FLAG_MINUS 0x10
121 * These format characters must all be unique, except the magic one.
123 #define FMT_MAGIC '%'
126 #define SIMPLE_NEWLINE 'n'
127 #define SIMPLE_TAB 't'
128 #define SIMPLE_PERCENT '%'
129 #define SIMPLE_NUMBER '@'
131 #define FMT_POUND '#'
132 #define FMT_SPACE ' '
135 #define FMT_MINUS '-'
137 #define FMT_DECIMAL 'D'
138 #define FMT_OCTAL 'O'
139 #define FMT_UNSIGNED 'U'
141 #define FMT_FLOAT 'F'
142 #define FMT_STRING 'S'
144 #define FMTF_DECIMAL 0x01
145 #define FMTF_OCTAL 0x02
146 #define FMTF_UNSIGNED 0x04
147 #define FMTF_HEX 0x08
148 #define FMTF_FLOAT 0x10
149 #define FMTF_STRING 0x20
151 #define HIGH_PIECE 'H'
152 #define MIDDLE_PIECE 'M'
153 #define LOW_PIECE 'L'
155 #define SHOW_realpath 'R'
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 const char *statfmt
, *options
, *synopsis
;
214 if (strcmp(getprogname(), "readlink") == 0) {
217 synopsis
= "[-fn] [file ...]";
222 options
= "f:FlLnqrst:x";
223 synopsis
= "[-FlLnqrsx] [-f format] [-t timefmt] [file ...]";
226 while ((ch
= getopt(argc
, argv
, options
)) != -1)
252 errx(1, "can't use format '%c' with '%c'",
267 if (fmtchar
== '\0') {
272 statfmt
= DEF_FORMAT
;
276 if (lsF
&& fmtchar
!= 'l')
277 errx(1, "can't use format '%c' with -F", fmtchar
);
281 /* statfmt already set */
284 statfmt
= lsF
? LSF_FORMAT
: LS_FORMAT
;
287 statfmt
= RAW_FORMAT
;
290 statfmt
= SHELL_FORMAT
;
293 statfmt
= LINUX_FORMAT
;
303 timefmt
= TIME_FORMAT
;
308 rc
= fstat(STDIN_FILENO
, &st
);
311 * Try stat() and if it fails, fall back to
312 * lstat() just in case we're examining a
315 if ((rc
= stat(argv
[0], &st
)) == -1 &&
317 (rc
= lstat(argv
[0], &st
)) == -1)
321 rc
= lstat(argv
[0], &st
);
328 argc
== 0 ? "(stdin)" : argv
[0],
329 usestat
? "stat" : "lstat");
332 output(&st
, argv
[0], statfmt
, fn
, nonl
, quiet
);
339 return (am_readlink
? linkfail
: errs
);
343 usage(const char *synopsis
)
346 (void)fprintf(stderr
, "usage: %s %s\n", getprogname(), synopsis
);
351 * Parses a format string.
354 output(const struct stat
*st
, const char *file
,
355 const char *statfmt
, int fn
, int nonl
, int quiet
)
357 int flags
, size
, prec
, ofmt
, hilo
, what
;
358 char buf
[PATH_MAX
+ 4 + 1];
363 while (*statfmt
!= '\0') {
366 * Non-format characters go straight out.
368 if (*statfmt
!= FMT_MAGIC
) {
369 addchar(stdout
, *statfmt
, &nl
);
375 * The current format "substring" starts here,
376 * and then we skip the magic.
382 * Some simple one-character "formats".
386 addchar(stdout
, '\n', &nl
);
390 addchar(stdout
, '\t', &nl
);
394 addchar(stdout
, '%', &nl
);
397 case SIMPLE_NUMBER
: {
400 snprintf(num
, sizeof(num
), "%d", fn
);
401 for (p
= &num
[0]; *p
; p
++)
402 addchar(stdout
, *p
, &nl
);
409 * This must be an actual format string. Format strings are
410 * similar to printf(3) formats up to a point, and are of
413 * % required start of format
414 * [-# +0] opt. format characters
415 * size opt. field width
416 * . opt. decimal separator, followed by
417 * prec opt. precision
418 * fmt opt. output specifier (string, numeric, etc.)
419 * sub opt. sub field specifier (high, middle, low)
420 * datum required field specifier (size, mode, etc)
422 * Only the % and the datum selector are required. All data
423 * have reasonable default output forms. The "sub" specifier
424 * only applies to certain data (mode, dev, rdev, filetype).
425 * The symlink output defaults to STRING, yet will only emit
426 * the leading " -> " if STRING is explicitly specified. The
427 * sizerdev datum will generate rdev output for character or
428 * block devices, and size output for all others.
432 if (*statfmt
== FMT_POUND
)
434 else if (*statfmt
== FMT_SPACE
)
436 else if (*statfmt
== FMT_PLUS
)
438 else if (*statfmt
== FMT_ZERO
)
440 else if (*statfmt
== FMT_MINUS
)
445 } while (1/*CONSTCOND*/);
448 if (isdigit((unsigned)*statfmt
)) {
450 while (isdigit((unsigned)*statfmt
)) {
451 size
= (size
* 10) + (*statfmt
- '0');
459 if (*statfmt
== FMT_DOT
) {
463 while (isdigit((unsigned)*statfmt
)) {
464 prec
= (prec
* 10) + (*statfmt
- '0');
471 #define fmtcase(x, y) case (y): (x) = (y); statfmt++; break
472 #define fmtcasef(x, y, z) case (y): (x) = (z); statfmt++; break
474 fmtcasef(ofmt
, FMT_DECIMAL
, FMTF_DECIMAL
);
475 fmtcasef(ofmt
, FMT_OCTAL
, FMTF_OCTAL
);
476 fmtcasef(ofmt
, FMT_UNSIGNED
, FMTF_UNSIGNED
);
477 fmtcasef(ofmt
, FMT_HEX
, FMTF_HEX
);
478 fmtcasef(ofmt
, FMT_FLOAT
, FMTF_FLOAT
);
479 fmtcasef(ofmt
, FMT_STRING
, FMTF_STRING
);
486 fmtcase(hilo
, HIGH_PIECE
);
487 fmtcase(hilo
, MIDDLE_PIECE
);
488 fmtcase(hilo
, LOW_PIECE
);
495 fmtcase(what
, SHOW_realpath
);
496 fmtcase(what
, SHOW_st_dev
);
497 fmtcase(what
, SHOW_st_ino
);
498 fmtcase(what
, SHOW_st_mode
);
499 fmtcase(what
, SHOW_st_nlink
);
500 fmtcase(what
, SHOW_st_uid
);
501 fmtcase(what
, SHOW_st_gid
);
502 fmtcase(what
, SHOW_st_rdev
);
503 fmtcase(what
, SHOW_st_atime
);
504 fmtcase(what
, SHOW_st_mtime
);
505 fmtcase(what
, SHOW_st_ctime
);
506 fmtcase(what
, SHOW_st_btime
);
507 fmtcase(what
, SHOW_st_size
);
508 fmtcase(what
, SHOW_st_blocks
);
509 fmtcase(what
, SHOW_st_blksize
);
510 fmtcase(what
, SHOW_st_flags
);
511 fmtcase(what
, SHOW_st_gen
);
512 fmtcase(what
, SHOW_symlink
);
513 fmtcase(what
, SHOW_filetype
);
514 fmtcase(what
, SHOW_filename
);
515 fmtcase(what
, SHOW_sizerdev
);
524 subfmt
, statfmt
- subfmt
,
526 flags
, size
, prec
, ofmt
, hilo
, what
);
528 for (i
= 0; i
< t
&& i
< (int)(sizeof(buf
) - 1); i
++)
529 addchar(stdout
, buf
[i
], &nl
);
534 errx(1, "%.*s: bad format",
535 (int)(statfmt
- subfmt
+ 1), subfmt
);
539 (void)fputc('\n', stdout
);
540 (void)fflush(stdout
);
544 * Arranges output according to a single parsed format substring.
547 format1(const struct stat
*st
,
549 const char *fmt
, int flen
,
550 char *buf
, size_t blen
,
551 int flags
, int size
, int prec
, int ofmt
,
555 char *stmp
, lfmt
[24], tmp
[20];
557 char smode
[12], sid
[12], path
[PATH_MAX
+ 4];
563 int l
, small
, formats
, gottime
, shift
;
573 * First, pick out the data and tweak it based on hilo or
574 * specified output format (symlink output only).
579 small
= (sizeof(st
->st_dev
) == 4);
580 data
= (what
== SHOW_st_dev
) ? st
->st_dev
: st
->st_rdev
;
582 sdata
= (what
== SHOW_st_dev
) ?
583 devname(st
->st_dev
, S_IFBLK
) :
585 S_ISCHR(st
->st_mode
) ? S_IFCHR
:
586 S_ISBLK(st
->st_mode
) ? S_IFBLK
:
590 #endif /* HAVE_DEVNAME */
591 if (hilo
== HIGH_PIECE
) {
595 else if (hilo
== LOW_PIECE
) {
596 data
= minor((unsigned)data
);
599 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
|
602 #else /* HAVE_DEVNAME */
604 #endif /* HAVE_DEVNAME */
606 ofmt
= FMTF_UNSIGNED
;
609 small
= (sizeof(st
->st_ino
) == 4);
612 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
614 ofmt
= FMTF_UNSIGNED
;
617 small
= (sizeof(st
->st_mode
) == 4);
619 strmode(st
->st_mode
, smode
);
622 if (stmp
[l
- 1] == ' ')
624 if (hilo
== HIGH_PIECE
) {
630 else if (hilo
== MIDDLE_PIECE
) {
631 data
= (data
>> 9) & 07;
636 else if (hilo
== LOW_PIECE
) {
643 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
|
649 small
= (sizeof(st
->st_dev
) == 4);
652 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
654 ofmt
= FMTF_UNSIGNED
;
657 small
= (sizeof(st
->st_uid
) == 4);
659 if ((pw
= getpwuid(st
->st_uid
)) != NULL
)
662 snprintf(sid
, sizeof(sid
), "(%ld)", (long)st
->st_uid
);
665 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
|
668 ofmt
= FMTF_UNSIGNED
;
671 small
= (sizeof(st
->st_gid
) == 4);
673 if ((gr
= getgrgid(st
->st_gid
)) != NULL
)
676 snprintf(sid
, sizeof(sid
), "(%ld)", (long)st
->st_gid
);
679 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
|
682 ofmt
= FMTF_UNSIGNED
;
687 #if HAVE_STRUCT_STAT_ST_MTIMENSEC
688 nsecs
= st
->st_atimensec
;
695 #if HAVE_STRUCT_STAT_ST_MTIMENSEC
696 nsecs
= st
->st_mtimensec
;
704 #if HAVE_STRUCT_STAT_ST_MTIMENSEC
705 nsecs
= st
->st_ctimensec
;
709 #if HAVE_STRUCT_STAT_ST_BIRTHTIME
713 secs
= st
->st_birthtime
;
714 #if HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC
715 nsecs
= st
->st_birthtimensec
;
716 #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC */
718 #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
719 small
= (sizeof(secs
) == 4);
722 tm
= localtime(&secs
);
723 (void)strftime(path
, sizeof(path
), timefmt
, tm
);
725 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
|
726 FMTF_FLOAT
| FMTF_STRING
;
731 small
= (sizeof(st
->st_size
) == 4);
734 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
736 ofmt
= FMTF_UNSIGNED
;
739 shift
= 30; /* gigabytes */
743 shift
= 20; /* megabytes */
747 shift
= 10; /* kilobytes */
753 small
= (sizeof(st
->st_blocks
) == 4);
754 data
= st
->st_blocks
;
756 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
758 ofmt
= FMTF_UNSIGNED
;
760 case SHOW_st_blksize
:
761 small
= (sizeof(st
->st_blksize
) == 4);
762 data
= st
->st_blksize
;
764 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
766 ofmt
= FMTF_UNSIGNED
;
768 #if HAVE_STRUCT_STAT_ST_FLAGS
770 small
= (sizeof(st
->st_flags
) == 4);
773 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
775 ofmt
= FMTF_UNSIGNED
;
777 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
778 #if HAVE_STRUCT_STAT_ST_GEN
780 small
= (sizeof(st
->st_gen
) == 4);
783 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
785 ofmt
= FMTF_UNSIGNED
;
787 #endif /* HAVE_STRUCT_STAT_ST_GEN */
792 (void)strncpy(path
, "(stdin)", sizeof(path
));
795 snprintf(path
, sizeof(path
), " -> ");
796 if (realpath(file
, path
+ 4) == NULL
) {
801 sdata
= path
+ (ofmt
== FMTF_STRING
? 0 : 4);
804 formats
= FMTF_STRING
;
811 if (S_ISLNK(st
->st_mode
)) {
812 snprintf(path
, sizeof(path
), " -> ");
813 l
= readlink(file
, path
+ 4, sizeof(path
) - 4 - 1);
820 sdata
= path
+ (ofmt
== FMTF_STRING
? 0 : 4);
826 formats
= FMTF_STRING
;
834 if (hilo
== 0 || hilo
== LOW_PIECE
) {
835 switch (st
->st_mode
& S_IFMT
) {
836 case S_IFIFO
: sdata
= "|"; break;
837 case S_IFDIR
: sdata
= "/"; break;
840 (S_IXUSR
| S_IXGRP
| S_IXOTH
))
843 case S_IFLNK
: sdata
= "@"; break;
845 case S_IFSOCK
: sdata
= "="; break;
848 case S_IFWHT
: sdata
= "%"; break;
851 case S_IFDOOR
: sdata
= ">"; break;
852 #endif /* S_IFDOOR */
856 else if (hilo
== HIGH_PIECE
) {
857 switch (st
->st_mode
& S_IFMT
) {
858 case S_IFIFO
: sdata
= "Fifo File"; break;
859 case S_IFCHR
: sdata
= "Character Device"; break;
860 case S_IFDIR
: sdata
= "Directory"; break;
861 case S_IFBLK
: sdata
= "Block Device"; break;
862 case S_IFREG
: sdata
= "Regular File"; break;
863 case S_IFLNK
: sdata
= "Symbolic Link"; break;
865 case S_IFSOCK
: sdata
= "Socket"; break;
868 case S_IFWHT
: sdata
= "Whiteout File"; break;
871 case S_IFDOOR
: sdata
= "Door"; break;
872 #endif /* S_IFDOOR */
873 default: sdata
= "???"; break;
877 formats
= FMTF_STRING
;
885 (void)strncpy(path
, "(stdin)", sizeof(path
));
886 if (hilo
== HIGH_PIECE
|| hilo
== LOW_PIECE
)
890 (void)strncpy(path
, file
, sizeof(path
));
893 (void)strncpy(path
, file
, sizeof(path
));
894 s
= strrchr(path
, '/');
896 /* trim off trailing /'s */
898 s
[0] == '/' && s
[1] == '\0')
900 s
= strrchr(path
, '/');
902 if (hilo
== HIGH_PIECE
) {
904 (void)strncpy(path
, ".", sizeof(path
));
906 while (s
!= path
&& s
[0] == '/')
911 else if (hilo
== LOW_PIECE
) {
912 if (s
!= NULL
&& s
[1] != '\0')
913 (void)strncpy(path
, s
+ 1,
919 formats
= FMTF_STRING
;
924 if (S_ISCHR(st
->st_mode
) || S_ISBLK(st
->st_mode
)) {
925 char majdev
[20], mindev
[20];
931 majdev
, sizeof(majdev
),
933 ofmt
, HIGH_PIECE
, SHOW_st_rdev
);
937 mindev
, sizeof(mindev
),
939 ofmt
, LOW_PIECE
, SHOW_st_rdev
);
940 return (snprintf(buf
, blen
, "%.*s,%.*s",
941 l1
, majdev
, l2
, mindev
));
949 ofmt
, 0, SHOW_st_size
));
953 errx(1, "%.*s: bad format", (int)flen
, fmt
);
957 * If a subdatum was specified but not supported, or an output
958 * format was selected that is not supported, that's an error.
960 if (hilo
!= 0 || (ofmt
& formats
) == 0)
961 errx(1, "%.*s: bad format", (int)flen
, fmt
);
964 * Assemble the format string for passing to printf(3).
967 (void)strcat(lfmt
, "%");
968 if (flags
& FLAG_POUND
)
969 (void)strcat(lfmt
, "#");
970 if (flags
& FLAG_SPACE
)
971 (void)strcat(lfmt
, " ");
972 if (flags
& FLAG_PLUS
)
973 (void)strcat(lfmt
, "+");
974 if (flags
& FLAG_MINUS
)
975 (void)strcat(lfmt
, "-");
976 if (flags
& FLAG_ZERO
)
977 (void)strcat(lfmt
, "0");
980 * Only the timespecs support the FLOAT output format, and that
981 * requires work that differs from the other formats.
983 if (ofmt
== FMTF_FLOAT
) {
985 * Nothing after the decimal point, so just print seconds.
989 (void)snprintf(tmp
, sizeof(tmp
), "%d", size
);
990 (void)strcat(lfmt
, tmp
);
992 (void)strcat(lfmt
, "d");
993 return (snprintf(buf
, blen
, lfmt
, secs
));
997 * Unspecified precision gets all the precision we have:
1004 * Adjust the size for the decimal point and the digits
1010 * Any leftover size that's legitimate will be used.
1013 (void)snprintf(tmp
, sizeof(tmp
), "%d", size
);
1014 (void)strcat(lfmt
, tmp
);
1016 (void)strcat(lfmt
, "d");
1019 * The stuff after the decimal point always needs zero
1022 (void)strcat(lfmt
, ".%0");
1025 * We can "print" at most nine digits of precision. The
1026 * rest we will pad on at the end.
1028 (void)snprintf(tmp
, sizeof(tmp
), "%dd", prec
> 9 ? 9 : prec
);
1029 (void)strcat(lfmt
, tmp
);
1032 * For precision of less that nine digits, trim off the
1033 * less significant figures.
1035 for (; prec
< 9; prec
++)
1039 * Use the format, and then tack on any zeroes that
1040 * might be required to make up the requested precision.
1042 l
= snprintf(buf
, blen
, lfmt
, secs
, nsecs
);
1043 for (; prec
> 9 && l
< (int)blen
; prec
--, l
++)
1044 (void)strcat(buf
, "0");
1049 * Add on size and precision, if specified, to the format.
1052 (void)snprintf(tmp
, sizeof(tmp
), "%d", size
);
1053 (void)strcat(lfmt
, tmp
);
1056 (void)snprintf(tmp
, sizeof(tmp
), ".%d", prec
);
1057 (void)strcat(lfmt
, tmp
);
1061 * String output uses the temporary sdata.
1063 if (ofmt
== FMTF_STRING
) {
1065 errx(1, "%.*s: bad format", (int)flen
, fmt
);
1066 (void)strcat(lfmt
, "s");
1067 return (snprintf(buf
, blen
, lfmt
, sdata
));
1071 * Ensure that sign extension does not cause bad looking output
1074 if (small
&& ofmt
!= FMTF_DECIMAL
)
1075 data
= (u_int32_t
)data
;
1078 * The four "numeric" output forms.
1080 (void)strcat(lfmt
, "ll");
1082 case FMTF_DECIMAL
: (void)strcat(lfmt
, "d"); break;
1083 case FMTF_OCTAL
: (void)strcat(lfmt
, "o"); break;
1084 case FMTF_UNSIGNED
: (void)strcat(lfmt
, "u"); break;
1085 case FMTF_HEX
: (void)strcat(lfmt
, "x"); break;
1089 * shift and round to nearest for kilobytes, megabytes,
1093 data
>>= (shift
- 1);
1098 return (snprintf(buf
, blen
, lfmt
, data
));