1 /* $NetBSD: stat.c,v 1.33 2011/01/15 22:54:10 njoly 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.33 2011/01/15 22:54:10 njoly 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
48 /* Not supported in Minix. */
49 #define HAVE_DEVNAME 0
51 #define HAVE_DEVNAME 1
53 #endif /* HAVE_NBTOOL_CONFIG_H */
55 #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: (%04OLp/%.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_realpath 'R'
161 #define SHOW_st_dev 'd'
162 #define SHOW_st_ino 'i'
163 #define SHOW_st_mode 'p'
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);
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 const char *statfmt
, *options
, *synopsis
;
219 if (strcmp(getprogname(), "readlink") == 0) {
222 synopsis
= "[-fn] [file ...]";
227 options
= "f:FlLnqrst:x";
228 synopsis
= "[-FlLnqrsx] [-f format] [-t timefmt] [file ...]";
231 while ((ch
= getopt(argc
, argv
, options
)) != -1)
257 errx(1, "can't use format '%c' with '%c'",
272 if (fmtchar
== '\0') {
277 statfmt
= DEF_FORMAT
;
281 if (lsF
&& fmtchar
!= 'l')
282 errx(1, "can't use format '%c' with -F", fmtchar
);
286 /* statfmt already set */
289 statfmt
= lsF
? LSF_FORMAT
: LS_FORMAT
;
292 statfmt
= RAW_FORMAT
;
295 statfmt
= SHELL_FORMAT
;
298 statfmt
= LINUX_FORMAT
;
308 timefmt
= TIME_FORMAT
;
313 rc
= fstat(STDIN_FILENO
, &st
);
316 * Try stat() and if it fails, fall back to
317 * lstat() just in case we're examining a
320 if ((rc
= stat(argv
[0], &st
)) == -1 &&
322 (rc
= lstat(argv
[0], &st
)) == -1)
326 rc
= lstat(argv
[0], &st
);
333 argc
== 0 ? "(stdin)" : argv
[0],
334 usestat
? "stat" : "lstat");
337 output(&st
, argv
[0], statfmt
, fn
, nonl
);
344 return (am_readlink
? linkfail
: errs
);
348 usage(const char *synopsis
)
351 (void)fprintf(stderr
, "usage: %s %s\n", getprogname(), synopsis
);
356 * Parses a format string.
359 output(const struct stat
*st
, const char *file
,
360 const char *statfmt
, int fn
, int nonl
)
362 int flags
, size
, prec
, ofmt
, hilo
, what
;
363 char buf
[PATH_MAX
+ 4 + 1];
368 while (*statfmt
!= '\0') {
371 * Non-format characters go straight out.
373 if (*statfmt
!= FMT_MAGIC
) {
374 addchar(stdout
, *statfmt
, &nl
);
380 * The current format "substring" starts here,
381 * and then we skip the magic.
387 * Some simple one-character "formats".
391 addchar(stdout
, '\n', &nl
);
395 addchar(stdout
, '\t', &nl
);
399 addchar(stdout
, '%', &nl
);
402 case SIMPLE_NUMBER
: {
405 snprintf(num
, sizeof(num
), "%d", fn
);
406 for (p
= &num
[0]; *p
; p
++)
407 addchar(stdout
, *p
, &nl
);
414 * This must be an actual format string. Format strings are
415 * similar to printf(3) formats up to a point, and are of
418 * % required start of format
419 * [-# +0] opt. format characters
420 * size opt. field width
421 * . opt. decimal separator, followed by
422 * prec opt. precision
423 * fmt opt. output specifier (string, numeric, etc.)
424 * sub opt. sub field specifier (high, middle, low)
425 * datum required field specifier (size, mode, etc)
427 * Only the % and the datum selector are required. All data
428 * have reasonable default output forms. The "sub" specifier
429 * only applies to certain data (mode, dev, rdev, filetype).
430 * The symlink output defaults to STRING, yet will only emit
431 * the leading " -> " if STRING is explicitly specified. The
432 * sizerdev datum will generate rdev output for character or
433 * block devices, and size output for all others.
437 if (*statfmt
== FMT_POUND
)
439 else if (*statfmt
== FMT_SPACE
)
441 else if (*statfmt
== FMT_PLUS
)
443 else if (*statfmt
== FMT_ZERO
)
445 else if (*statfmt
== FMT_MINUS
)
450 } while (1/*CONSTCOND*/);
453 if (isdigit((unsigned)*statfmt
)) {
455 while (isdigit((unsigned)*statfmt
)) {
456 size
= (size
* 10) + (*statfmt
- '0');
464 if (*statfmt
== FMT_DOT
) {
468 while (isdigit((unsigned)*statfmt
)) {
469 prec
= (prec
* 10) + (*statfmt
- '0');
476 #define fmtcase(x, y) case (y): (x) = (y); statfmt++; break
477 #define fmtcasef(x, y, z) case (y): (x) = (z); statfmt++; break
479 fmtcasef(ofmt
, FMT_DECIMAL
, FMTF_DECIMAL
);
480 fmtcasef(ofmt
, FMT_OCTAL
, FMTF_OCTAL
);
481 fmtcasef(ofmt
, FMT_UNSIGNED
, FMTF_UNSIGNED
);
482 fmtcasef(ofmt
, FMT_HEX
, FMTF_HEX
);
483 fmtcasef(ofmt
, FMT_FLOAT
, FMTF_FLOAT
);
484 fmtcasef(ofmt
, FMT_STRING
, FMTF_STRING
);
491 fmtcase(hilo
, HIGH_PIECE
);
492 fmtcase(hilo
, MIDDLE_PIECE
);
493 fmtcase(hilo
, LOW_PIECE
);
500 fmtcase(what
, SHOW_realpath
);
501 fmtcase(what
, SHOW_st_dev
);
502 fmtcase(what
, SHOW_st_ino
);
503 fmtcase(what
, SHOW_st_mode
);
504 fmtcase(what
, SHOW_st_nlink
);
505 fmtcase(what
, SHOW_st_uid
);
506 fmtcase(what
, SHOW_st_gid
);
507 fmtcase(what
, SHOW_st_rdev
);
508 fmtcase(what
, SHOW_st_atime
);
509 fmtcase(what
, SHOW_st_mtime
);
510 fmtcase(what
, SHOW_st_ctime
);
511 fmtcase(what
, SHOW_st_btime
);
512 fmtcase(what
, SHOW_st_size
);
513 fmtcase(what
, SHOW_st_blocks
);
514 fmtcase(what
, SHOW_st_blksize
);
515 fmtcase(what
, SHOW_st_flags
);
516 fmtcase(what
, SHOW_st_gen
);
517 fmtcase(what
, SHOW_symlink
);
518 fmtcase(what
, SHOW_filetype
);
519 fmtcase(what
, SHOW_filename
);
520 fmtcase(what
, SHOW_sizerdev
);
529 subfmt
, statfmt
- subfmt
,
531 flags
, size
, prec
, ofmt
, hilo
, what
);
533 for (i
= 0; i
< t
&& i
< (int)(sizeof(buf
) - 1); i
++)
534 addchar(stdout
, buf
[i
], &nl
);
539 errx(1, "%.*s: bad format",
540 (int)(statfmt
- subfmt
+ 1), subfmt
);
544 (void)fputc('\n', stdout
);
545 (void)fflush(stdout
);
549 * Arranges output according to a single parsed format substring.
552 format1(const struct stat
*st
,
554 const char *fmt
, int flen
,
555 char *buf
, size_t blen
,
556 int flags
, int size
, int prec
, int ofmt
,
560 char *stmp
, lfmt
[24], tmp
[20];
562 char smode
[12], sid
[12], path
[PATH_MAX
+ 4];
568 int l
, small
, formats
, gottime
, shift
;
578 * First, pick out the data and tweak it based on hilo or
579 * specified output format (symlink output only).
584 small
= (sizeof(st
->st_dev
) == 4);
585 data
= (what
== SHOW_st_dev
) ? st
->st_dev
: st
->st_rdev
;
587 sdata
= (what
== SHOW_st_dev
) ?
588 devname(st
->st_dev
, S_IFBLK
) :
590 S_ISCHR(st
->st_mode
) ? S_IFCHR
:
591 S_ISBLK(st
->st_mode
) ? S_IFBLK
:
595 #endif /* HAVE_DEVNAME */
596 if (hilo
== HIGH_PIECE
) {
600 else if (hilo
== LOW_PIECE
) {
601 data
= minor((unsigned)data
);
604 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
|
607 #else /* HAVE_DEVNAME */
609 #endif /* HAVE_DEVNAME */
611 ofmt
= FMTF_UNSIGNED
;
614 small
= (sizeof(st
->st_ino
) == 4);
617 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
619 ofmt
= FMTF_UNSIGNED
;
622 small
= (sizeof(st
->st_mode
) == 4);
624 strmode(st
->st_mode
, smode
);
627 if (stmp
[l
- 1] == ' ')
629 if (hilo
== HIGH_PIECE
) {
635 else if (hilo
== MIDDLE_PIECE
) {
636 data
= (data
>> 9) & 07;
641 else if (hilo
== LOW_PIECE
) {
648 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
|
654 small
= (sizeof(st
->st_dev
) == 4);
657 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
659 ofmt
= FMTF_UNSIGNED
;
662 small
= (sizeof(st
->st_uid
) == 4);
664 if ((pw
= getpwuid(st
->st_uid
)) != NULL
)
667 snprintf(sid
, sizeof(sid
), "(%ld)", (long)st
->st_uid
);
670 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
|
673 ofmt
= FMTF_UNSIGNED
;
676 small
= (sizeof(st
->st_gid
) == 4);
678 if ((gr
= getgrgid(st
->st_gid
)) != NULL
)
681 snprintf(sid
, sizeof(sid
), "(%ld)", (long)st
->st_gid
);
684 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
|
687 ofmt
= FMTF_UNSIGNED
;
692 #if HAVE_STRUCT_STAT_ST_MTIMENSEC
693 nsecs
= st
->st_atimensec
;
700 #if HAVE_STRUCT_STAT_ST_MTIMENSEC
701 nsecs
= st
->st_mtimensec
;
709 #if HAVE_STRUCT_STAT_ST_MTIMENSEC
710 nsecs
= st
->st_ctimensec
;
714 #if HAVE_STRUCT_STAT_ST_BIRTHTIME
718 secs
= st
->st_birthtime
;
719 #if HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC
720 nsecs
= st
->st_birthtimensec
;
721 #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC */
723 #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
724 small
= (sizeof(secs
) == 4);
726 tm
= localtime(&secs
);
729 tm
= localtime(&secs
);
731 (void)strftime(path
, sizeof(path
), timefmt
, tm
);
733 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
|
734 FMTF_FLOAT
| FMTF_STRING
;
739 small
= (sizeof(st
->st_size
) == 4);
742 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
744 ofmt
= FMTF_UNSIGNED
;
747 shift
= 30; /* gigabytes */
751 shift
= 20; /* megabytes */
755 shift
= 10; /* kilobytes */
761 small
= (sizeof(st
->st_blocks
) == 4);
762 data
= st
->st_blocks
;
764 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
766 ofmt
= FMTF_UNSIGNED
;
768 case SHOW_st_blksize
:
769 small
= (sizeof(st
->st_blksize
) == 4);
770 data
= st
->st_blksize
;
772 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
774 ofmt
= FMTF_UNSIGNED
;
776 #if HAVE_STRUCT_STAT_ST_FLAGS
778 small
= (sizeof(st
->st_flags
) == 4);
781 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
783 ofmt
= FMTF_UNSIGNED
;
785 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
786 #if HAVE_STRUCT_STAT_ST_GEN
788 small
= (sizeof(st
->st_gen
) == 4);
791 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
793 ofmt
= FMTF_UNSIGNED
;
795 #endif /* HAVE_STRUCT_STAT_ST_GEN */
800 (void)strlcpy(path
, "(stdin)", sizeof(path
));
803 snprintf(path
, sizeof(path
), " -> ");
804 if (realpath(file
, path
+ 4) == NULL
) {
809 sdata
= path
+ (ofmt
== FMTF_STRING
? 0 : 4);
812 formats
= FMTF_STRING
;
819 if (S_ISLNK(st
->st_mode
)) {
820 snprintf(path
, sizeof(path
), " -> ");
821 l
= readlink(file
, path
+ 4, sizeof(path
) - 4 - 1);
828 sdata
= path
+ (ofmt
== FMTF_STRING
? 0 : 4);
834 formats
= FMTF_STRING
;
842 if (hilo
== 0 || hilo
== LOW_PIECE
) {
843 switch (st
->st_mode
& S_IFMT
) {
844 case S_IFIFO
: sdata
= "|"; break;
845 case S_IFDIR
: sdata
= "/"; break;
848 (S_IXUSR
| S_IXGRP
| S_IXOTH
))
851 case S_IFLNK
: sdata
= "@"; break;
853 case S_IFSOCK
: sdata
= "="; break;
856 case S_IFWHT
: sdata
= "%"; break;
859 case S_IFDOOR
: sdata
= ">"; break;
860 #endif /* S_IFDOOR */
864 else if (hilo
== HIGH_PIECE
) {
865 switch (st
->st_mode
& S_IFMT
) {
866 case S_IFIFO
: sdata
= "Fifo File"; break;
867 case S_IFCHR
: sdata
= "Character Device"; break;
868 case S_IFDIR
: sdata
= "Directory"; break;
869 case S_IFBLK
: sdata
= "Block Device"; break;
870 case S_IFREG
: sdata
= "Regular File"; break;
871 case S_IFLNK
: sdata
= "Symbolic Link"; break;
873 case S_IFSOCK
: sdata
= "Socket"; break;
876 case S_IFWHT
: sdata
= "Whiteout File"; break;
879 case S_IFDOOR
: sdata
= "Door"; break;
880 #endif /* S_IFDOOR */
881 default: sdata
= "???"; break;
885 formats
= FMTF_STRING
;
893 (void)strlcpy(path
, "(stdin)", sizeof(path
));
894 if (hilo
== HIGH_PIECE
|| hilo
== LOW_PIECE
)
898 (void)strlcpy(path
, file
, sizeof(path
));
901 (void)strlcpy(path
, file
, sizeof(path
));
902 s
= strrchr(path
, '/');
904 /* trim off trailing /'s */
906 s
[0] == '/' && s
[1] == '\0')
908 s
= strrchr(path
, '/');
910 if (hilo
== HIGH_PIECE
) {
912 (void)strlcpy(path
, ".", sizeof(path
));
914 while (s
!= path
&& s
[0] == '/')
919 else if (hilo
== LOW_PIECE
) {
920 if (s
!= NULL
&& s
[1] != '\0')
921 (void)strlcpy(path
, s
+ 1,
927 formats
= FMTF_STRING
;
932 if (S_ISCHR(st
->st_mode
) || S_ISBLK(st
->st_mode
)) {
933 char majdev
[20], mindev
[20];
939 majdev
, sizeof(majdev
),
941 ofmt
, HIGH_PIECE
, SHOW_st_rdev
);
945 mindev
, sizeof(mindev
),
947 ofmt
, LOW_PIECE
, SHOW_st_rdev
);
948 return (snprintf(buf
, blen
, "%.*s,%.*s",
949 l1
, majdev
, l2
, mindev
));
957 ofmt
, 0, SHOW_st_size
));
961 errx(1, "%.*s: bad format", (int)flen
, fmt
);
965 * If a subdatum was specified but not supported, or an output
966 * format was selected that is not supported, that's an error.
968 if (hilo
!= 0 || (ofmt
& formats
) == 0)
969 errx(1, "%.*s: bad format", (int)flen
, fmt
);
972 * Assemble the format string for passing to printf(3).
975 (void)strcat(lfmt
, "%");
976 if (flags
& FLAG_POUND
)
977 (void)strcat(lfmt
, "#");
978 if (flags
& FLAG_SPACE
)
979 (void)strcat(lfmt
, " ");
980 if (flags
& FLAG_PLUS
)
981 (void)strcat(lfmt
, "+");
982 if (flags
& FLAG_MINUS
)
983 (void)strcat(lfmt
, "-");
984 if (flags
& FLAG_ZERO
)
985 (void)strcat(lfmt
, "0");
988 * Only the timespecs support the FLOAT output format, and that
989 * requires work that differs from the other formats.
991 if (ofmt
== FMTF_FLOAT
) {
993 * Nothing after the decimal point, so just print seconds.
997 (void)snprintf(tmp
, sizeof(tmp
), "%d", size
);
998 (void)strcat(lfmt
, tmp
);
1000 (void)strcat(lfmt
, "lld");
1001 return (snprintf(buf
, blen
, lfmt
,
1006 * Unspecified precision gets all the precision we have:
1013 * Adjust the size for the decimal point and the digits
1019 * Any leftover size that's legitimate will be used.
1022 (void)snprintf(tmp
, sizeof(tmp
), "%d", size
);
1023 (void)strcat(lfmt
, tmp
);
1025 /* Seconds: time_t cast to long long. */
1026 (void)strcat(lfmt
, "lld");
1029 * The stuff after the decimal point always needs zero
1032 (void)strcat(lfmt
, ".%0");
1035 * We can "print" at most nine digits of precision. The
1036 * rest we will pad on at the end.
1038 * Nanoseconds: long.
1040 (void)snprintf(tmp
, sizeof(tmp
), "%dld", prec
> 9 ? 9 : prec
);
1041 (void)strcat(lfmt
, tmp
);
1044 * For precision of less that nine digits, trim off the
1045 * less significant figures.
1047 for (; prec
< 9; prec
++)
1051 * Use the format, and then tack on any zeroes that
1052 * might be required to make up the requested precision.
1054 l
= snprintf(buf
, blen
, lfmt
, (long long)secs
, nsecs
);
1055 for (; prec
> 9 && l
< (int)blen
; prec
--, l
++)
1056 (void)strcat(buf
, "0");
1061 * Add on size and precision, if specified, to the format.
1064 (void)snprintf(tmp
, sizeof(tmp
), "%d", size
);
1065 (void)strcat(lfmt
, tmp
);
1068 (void)snprintf(tmp
, sizeof(tmp
), ".%d", prec
);
1069 (void)strcat(lfmt
, tmp
);
1073 * String output uses the temporary sdata.
1075 if (ofmt
== FMTF_STRING
) {
1077 errx(1, "%.*s: bad format", (int)flen
, fmt
);
1078 (void)strcat(lfmt
, "s");
1079 return (snprintf(buf
, blen
, lfmt
, sdata
));
1083 * Ensure that sign extension does not cause bad looking output
1086 if (small
&& ofmt
!= FMTF_DECIMAL
)
1087 data
= (u_int32_t
)data
;
1090 * The four "numeric" output forms.
1092 (void)strcat(lfmt
, "ll");
1094 case FMTF_DECIMAL
: (void)strcat(lfmt
, "d"); break;
1095 case FMTF_OCTAL
: (void)strcat(lfmt
, "o"); break;
1096 case FMTF_UNSIGNED
: (void)strcat(lfmt
, "u"); break;
1097 case FMTF_HEX
: (void)strcat(lfmt
, "x"); break;
1101 * shift and round to nearest for kilobytes, megabytes,
1105 data
>>= (shift
- 1);
1110 return (snprintf(buf
, blen
, lfmt
, data
));