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.
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
29 * $NetBSD: stat.c,v 1.33 2011/01/15 22:54:10 njoly Exp $
30 * $OpenBSD: stat.c,v 1.14 2009/06/24 09:44:25 sobrado Exp $
31 * $FreeBSD: head/usr.bin/stat/stat.c 265893 2014-05-11 18:49:18Z thomas $
34 #include <sys/param.h>
35 #include <sys/types.h>
37 #include <sys/mount.h>
53 #define HAVE_DEVNAME 0
54 #define HAVE_STRUCT_STAT_ST_FLAGS 0
55 #define HAVE_STRUCT_STAT_ST_GEN 0
56 #define HAVE_STRUCT_STAT_ST_BIRTHTIME 0
57 #define HAVE_STRUCT_STAT_ST_ATIM 1
59 #define HAVE_DEVNAME 1
60 #define HAVE_STRUCT_STAT_ST_FLAGS 1
61 #define HAVE_STRUCT_STAT_ST_GEN 1
62 #define HAVE_STRUCT_STAT_ST_BIRTHTIME 0
63 #define HAVE_STRUCT_STAT_ST_ATIM 1
66 #if HAVE_STRUCT_STAT_ST_FLAGS
69 #define SHELL_F " st_flags=%f"
70 #else /* HAVE_STRUCT_STAT_ST_FLAGS */
74 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
76 #if HAVE_STRUCT_STAT_ST_BIRTHTIME
77 #define DEF_B "\"%SB\" "
79 #define SHELL_B "st_birthtime=%B "
80 #else /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
84 #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
86 #if HAVE_STRUCT_STAT_ST_ATIM
87 #define st_atimespec st_atim
88 #define st_ctimespec st_ctim
89 #define st_mtimespec st_mtim
90 #endif /* HAVE_STRUCT_STAT_ST_ATIM */
93 "%d %i %Sp %l %Su %Sg %r %z \"%Sa\" \"%Sm\" \"%Sc\" " DEF_B \
95 #define RAW_FORMAT "%d %i %#p %l %u %g %r %z %a %m %c " RAW_B \
97 #define LS_FORMAT "%Sp %l %Su %Sg %Z %Sm %N%SY"
98 #define LSF_FORMAT "%Sp %l %Su %Sg %Z %Sm %N%T%SY"
99 #define SHELL_FORMAT \
100 "st_dev=%d st_ino=%i st_mode=%#p st_nlink=%l " \
101 "st_uid=%u st_gid=%g st_rdev=%r st_size=%z " \
102 "st_atime=%a st_mtime=%m st_ctime=%c " SHELL_B \
103 "st_blksize=%k st_blocks=%b" SHELL_F
104 #define LINUX_FORMAT \
106 " Size: %-11z FileType: %HT%n" \
107 " Mode: (%OMp%03OLp/%.10Sp) Uid: (%5u/%8Su) Gid: (%5g/%8Sg)%n" \
108 "Device: %Hd,%Ld Inode: %i Links: %l%n" \
113 #define TIME_FORMAT "%b %e %T %Y"
115 #define FLAG_POUND 0x01
116 #define FLAG_SPACE 0x02
117 #define FLAG_PLUS 0x04
118 #define FLAG_ZERO 0x08
119 #define FLAG_MINUS 0x10
122 * These format characters must all be unique, except the magic one.
124 #define FMT_MAGIC '%'
127 #define SIMPLE_NEWLINE 'n'
128 #define SIMPLE_TAB 't'
129 #define SIMPLE_PERCENT '%'
130 #define SIMPLE_NUMBER '@'
132 #define FMT_POUND '#'
133 #define FMT_SPACE ' '
136 #define FMT_MINUS '-'
138 #define FMT_DECIMAL 'D'
139 #define FMT_OCTAL 'O'
140 #define FMT_UNSIGNED 'U'
142 #define FMT_FLOAT 'F'
143 #define FMT_STRING 'S'
145 #define FMTF_DECIMAL 0x01
146 #define FMTF_OCTAL 0x02
147 #define FMTF_UNSIGNED 0x04
148 #define FMTF_HEX 0x08
149 #define FMTF_FLOAT 0x10
150 #define FMTF_STRING 0x20
152 #define HIGH_PIECE 'H'
153 #define MIDDLE_PIECE 'M'
154 #define LOW_PIECE 'L'
156 #define SHOW_realpath 'R'
157 #define SHOW_st_dev 'd'
158 #define SHOW_st_ino 'i'
159 #define SHOW_st_mode 'p'
160 #define SHOW_st_nlink 'l'
161 #define SHOW_st_uid 'u'
162 #define SHOW_st_gid 'g'
163 #define SHOW_st_rdev 'r'
164 #define SHOW_st_atime 'a'
165 #define SHOW_st_mtime 'm'
166 #define SHOW_st_ctime 'c'
167 #define SHOW_st_btime 'B'
168 #define SHOW_st_size 'z'
169 #define SHOW_st_blocks 'b'
170 #define SHOW_st_blksize 'k'
171 #define SHOW_st_flags 'f'
172 #define SHOW_st_gen 'v'
173 #define SHOW_symlink 'Y'
174 #define SHOW_filetype 'T'
175 #define SHOW_filename 'N'
176 #define SHOW_sizerdev 'Z'
178 static void usage(const char *) __dead2
;
179 static void output(const struct stat
*, const char *,
180 const char *, int, int);
181 static int format1(const struct stat
*, /* stat info */
182 const char *, /* the file name */
183 const char *, int, /* the format string itself */
184 char *, size_t, /* a place to put the output */
185 int, int, int, int, /* the parsed format */
188 static int hex2byte(const char [2]);
190 #if HAVE_STRUCT_STAT_ST_FLAGS
191 static char *xfflagstostr(unsigned long);
194 static const char *timefmt
;
197 #define addchar(s, c, nl) \
200 (*nl) = ((c) == '\n'); \
201 } while (0/*CONSTCOND*/)
204 main(int argc
, char *argv
[])
207 int ch
, rc
, errs
, am_readlink
;
208 int lsF
, fmtchar
, usestat
, nfs_handle
, fn
, nonl
, quiet
;
209 const char *statfmt
, *options
, *synopsis
;
211 char dname
[sizeof _PATH_DEV
+ MNAMELEN
] = _PATH_DEV
;
227 if (strcmp(getprogname(), "readlink") == 0) {
230 synopsis
= "[-fn] [file ...]";
235 options
= "f:FHlLnqrst:x";
236 synopsis
= "[-FLnq] [-f format | -l | -r | -s | -x] "
237 "[-t timefmt] [file|handle ...]";
240 while ((ch
= getopt(argc
, argv
, options
)) != -1)
269 errx(1, "can't use format '%c' with '%c'",
284 if (fmtchar
== '\0') {
289 statfmt
= DEF_FORMAT
;
293 if (lsF
&& fmtchar
!= 'l')
294 errx(1, "can't use format '%c' with -F", fmtchar
);
298 /* statfmt already set */
301 statfmt
= lsF
? LSF_FORMAT
: LS_FORMAT
;
304 statfmt
= RAW_FORMAT
;
307 statfmt
= SHELL_FORMAT
;
310 statfmt
= LINUX_FORMAT
;
320 timefmt
= TIME_FORMAT
;
326 if (fdevname_r(STDIN_FILENO
, dname
+
327 sizeof _PATH_DEV
- 1, MNAMELEN
) == 0)
332 rc
= fstat(STDIN_FILENO
, &st
);
340 bzero(&fhnd
, sizeof(fhnd
));
341 j
= MIN(2 * sizeof(fhnd
), strlen(file
));
346 rc
= hex2byte(&file
[j
- 2]);
349 ((char*) &fhnd
)[j
/ 2 - 1] = rc
;
356 rc
= fhstat(&fhnd
, &st
);
358 err(1, "-H is disabled in btools");
360 } else if (usestat
) {
362 * Try stat() and if it fails, fall back to
363 * lstat() just in case we're examining a
366 if ((rc
= stat(file
, &st
)) == -1 &&
368 (rc
= lstat(file
, &st
)) == -1)
372 rc
= lstat(file
, &st
);
379 warn("%s: stat", file
);
382 output(&st
, file
, statfmt
, fn
, nonl
);
389 return (am_readlink
? linkfail
: errs
);
392 #if HAVE_STRUCT_STAT_ST_FLAGS
394 * fflagstostr() wrapper that leaks only once
397 xfflagstostr(unsigned long fflags
)
399 static char *str
= NULL
;
404 str
= fflagstostr(fflags
);
406 err(1, "fflagstostr");
409 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
412 usage(const char *synopsis
)
415 fprintf(stderr
, "usage: %s %s\n", getprogname(), synopsis
);
420 * Parses a format string.
423 output(const struct stat
*st
, const char *file
,
424 const char *statfmt
, int fn
, int nonl
)
426 int flags
, size
, prec
, ofmt
, hilo
, what
;
427 char buf
[PATH_MAX
+ 4 + 1];
432 while (*statfmt
!= '\0') {
435 * Non-format characters go straight out.
437 if (*statfmt
!= FMT_MAGIC
) {
438 addchar(stdout
, *statfmt
, &nl
);
444 * The current format "substring" starts here,
445 * and then we skip the magic.
451 * Some simple one-character "formats".
455 addchar(stdout
, '\n', &nl
);
459 addchar(stdout
, '\t', &nl
);
463 addchar(stdout
, '%', &nl
);
466 case SIMPLE_NUMBER
: {
469 snprintf(num
, sizeof(num
), "%d", fn
);
470 for (p
= &num
[0]; *p
; p
++)
471 addchar(stdout
, *p
, &nl
);
478 * This must be an actual format string. Format strings are
479 * similar to printf(3) formats up to a point, and are of
482 * % required start of format
483 * [-# +0] opt. format characters
484 * size opt. field width
485 * . opt. decimal separator, followed by
486 * prec opt. precision
487 * fmt opt. output specifier (string, numeric, etc.)
488 * sub opt. sub field specifier (high, middle, low)
489 * datum required field specifier (size, mode, etc)
491 * Only the % and the datum selector are required. All data
492 * have reasonable default output forms. The "sub" specifier
493 * only applies to certain data (mode, dev, rdev, filetype).
494 * The symlink output defaults to STRING, yet will only emit
495 * the leading " -> " if STRING is explicitly specified. The
496 * sizerdev datum will generate rdev output for character or
497 * block devices, and size output for all others.
501 if (*statfmt
== FMT_POUND
)
503 else if (*statfmt
== FMT_SPACE
)
505 else if (*statfmt
== FMT_PLUS
)
507 else if (*statfmt
== FMT_ZERO
)
509 else if (*statfmt
== FMT_MINUS
)
514 } while (1/*CONSTCOND*/);
517 if (isdigit((unsigned)*statfmt
)) {
519 while (isdigit((unsigned)*statfmt
)) {
520 size
= (size
* 10) + (*statfmt
- '0');
528 if (*statfmt
== FMT_DOT
) {
532 while (isdigit((unsigned)*statfmt
)) {
533 prec
= (prec
* 10) + (*statfmt
- '0');
540 #define fmtcase(x, y) case (y): (x) = (y); statfmt++; break
541 #define fmtcasef(x, y, z) case (y): (x) = (z); statfmt++; break
543 fmtcasef(ofmt
, FMT_DECIMAL
, FMTF_DECIMAL
);
544 fmtcasef(ofmt
, FMT_OCTAL
, FMTF_OCTAL
);
545 fmtcasef(ofmt
, FMT_UNSIGNED
, FMTF_UNSIGNED
);
546 fmtcasef(ofmt
, FMT_HEX
, FMTF_HEX
);
547 fmtcasef(ofmt
, FMT_FLOAT
, FMTF_FLOAT
);
548 fmtcasef(ofmt
, FMT_STRING
, FMTF_STRING
);
555 fmtcase(hilo
, HIGH_PIECE
);
556 fmtcase(hilo
, MIDDLE_PIECE
);
557 fmtcase(hilo
, LOW_PIECE
);
564 fmtcase(what
, SHOW_realpath
);
565 fmtcase(what
, SHOW_st_dev
);
566 fmtcase(what
, SHOW_st_ino
);
567 fmtcase(what
, SHOW_st_mode
);
568 fmtcase(what
, SHOW_st_nlink
);
569 fmtcase(what
, SHOW_st_uid
);
570 fmtcase(what
, SHOW_st_gid
);
571 fmtcase(what
, SHOW_st_rdev
);
572 fmtcase(what
, SHOW_st_atime
);
573 fmtcase(what
, SHOW_st_mtime
);
574 fmtcase(what
, SHOW_st_ctime
);
575 fmtcase(what
, SHOW_st_btime
);
576 fmtcase(what
, SHOW_st_size
);
577 fmtcase(what
, SHOW_st_blocks
);
578 fmtcase(what
, SHOW_st_blksize
);
579 fmtcase(what
, SHOW_st_flags
);
580 fmtcase(what
, SHOW_st_gen
);
581 fmtcase(what
, SHOW_symlink
);
582 fmtcase(what
, SHOW_filetype
);
583 fmtcase(what
, SHOW_filename
);
584 fmtcase(what
, SHOW_sizerdev
);
593 subfmt
, statfmt
- subfmt
,
595 flags
, size
, prec
, ofmt
, hilo
, what
);
597 for (i
= 0; i
< t
&& i
< (int)(sizeof(buf
) - 1); i
++)
598 addchar(stdout
, buf
[i
], &nl
);
603 errx(1, "%.*s: bad format",
604 (int)(statfmt
- subfmt
+ 1), subfmt
);
613 * Arranges output according to a single parsed format substring.
616 format1(const struct stat
*st
,
618 const char *fmt
, int flen
,
619 char *buf
, size_t blen
,
620 int flags
, int size
, int prec
, int ofmt
,
624 char *stmp
, lfmt
[24], tmp
[20];
626 char smode
[12], sid
[12], path
[PATH_MAX
+ 4];
629 const struct timespec
*tsp
;
632 int l
, small
, formats
;
639 * First, pick out the data and tweak it based on hilo or
640 * specified output format (symlink output only).
645 small
= (sizeof(st
->st_dev
) == 4);
646 data
= (what
== SHOW_st_dev
) ? st
->st_dev
: st
->st_rdev
;
648 sdata
= (what
== SHOW_st_dev
) ?
649 devname(st
->st_dev
, S_IFBLK
) :
651 S_ISCHR(st
->st_mode
) ? S_IFCHR
:
652 S_ISBLK(st
->st_mode
) ? S_IFBLK
:
656 #endif /* HAVE_DEVNAME */
659 if (hilo
== HIGH_PIECE
) {
663 else if (hilo
== LOW_PIECE
) {
664 data
= minor((unsigned)data
);
667 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
|
670 #else /* HAVE_DEVNAME */
672 #endif /* HAVE_DEVNAME */
674 ofmt
= FMTF_UNSIGNED
;
677 small
= (sizeof(st
->st_ino
) == 4);
680 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
682 ofmt
= FMTF_UNSIGNED
;
685 small
= (sizeof(st
->st_mode
) == 4);
687 strmode(st
->st_mode
, smode
);
690 if (stmp
[l
- 1] == ' ')
692 if (hilo
== HIGH_PIECE
) {
698 else if (hilo
== MIDDLE_PIECE
) {
699 data
= (data
>> 9) & 07;
704 else if (hilo
== LOW_PIECE
) {
711 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
|
717 small
= (sizeof(st
->st_dev
) == 4);
720 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
722 ofmt
= FMTF_UNSIGNED
;
725 small
= (sizeof(st
->st_uid
) == 4);
727 if ((pw
= getpwuid(st
->st_uid
)) != NULL
)
730 snprintf(sid
, sizeof(sid
), "(%ld)", (long)st
->st_uid
);
733 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
|
736 ofmt
= FMTF_UNSIGNED
;
739 small
= (sizeof(st
->st_gid
) == 4);
741 if ((gr
= getgrgid(st
->st_gid
)) != NULL
)
744 snprintf(sid
, sizeof(sid
), "(%ld)", (long)st
->st_gid
);
747 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
|
750 ofmt
= FMTF_UNSIGNED
;
753 tsp
= &st
->st_atimespec
;
757 tsp
= &st
->st_mtimespec
;
761 tsp
= &st
->st_ctimespec
;
763 #if HAVE_STRUCT_STAT_ST_BIRTHTIME
766 tsp
= &st
->st_birthtimespec
;
767 #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
768 ts
= *tsp
; /* copy so we can muck with it */
769 small
= (sizeof(ts
.tv_sec
) == 4);
771 tm
= localtime(&ts
.tv_sec
);
774 tm
= localtime(&ts
.tv_sec
);
776 strftime(path
, sizeof(path
), timefmt
, tm
);
778 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
|
779 FMTF_FLOAT
| FMTF_STRING
;
784 small
= (sizeof(st
->st_size
) == 4);
787 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
789 ofmt
= FMTF_UNSIGNED
;
792 small
= (sizeof(st
->st_blocks
) == 4);
793 data
= st
->st_blocks
;
795 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
797 ofmt
= FMTF_UNSIGNED
;
799 case SHOW_st_blksize
:
800 small
= (sizeof(st
->st_blksize
) == 4);
801 data
= st
->st_blksize
;
803 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
805 ofmt
= FMTF_UNSIGNED
;
807 #if HAVE_STRUCT_STAT_ST_FLAGS
809 small
= (sizeof(st
->st_flags
) == 4);
811 sdata
= xfflagstostr(st
->st_flags
);
814 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
|
817 ofmt
= FMTF_UNSIGNED
;
819 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
820 #if HAVE_STRUCT_STAT_ST_GEN
822 small
= (sizeof(st
->st_gen
) == 4);
825 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
827 ofmt
= FMTF_UNSIGNED
;
829 #endif /* HAVE_STRUCT_STAT_ST_GEN */
834 strlcpy(path
, "(stdin)", sizeof(path
));
837 snprintf(path
, sizeof(path
), " -> ");
838 if (realpath(file
, path
+ 4) == NULL
) {
843 sdata
= path
+ (ofmt
== FMTF_STRING
? 0 : 4);
846 formats
= FMTF_STRING
;
853 if (S_ISLNK(st
->st_mode
)) {
854 snprintf(path
, sizeof(path
), " -> ");
855 l
= readlink(file
, path
+ 4, sizeof(path
) - 4 - 1);
862 sdata
= path
+ (ofmt
== FMTF_STRING
? 0 : 4);
868 formats
= FMTF_STRING
;
876 if (hilo
== 0 || hilo
== LOW_PIECE
) {
877 switch (st
->st_mode
& S_IFMT
) {
878 case S_IFIFO
: sdata
= "|"; break;
879 case S_IFDIR
: sdata
= "/"; break;
882 (S_IXUSR
| S_IXGRP
| S_IXOTH
))
885 case S_IFLNK
: sdata
= "@"; break;
886 case S_IFSOCK
: sdata
= "="; break;
888 case S_IFWHT
: sdata
= "%"; break;
891 case S_IFDOOR
: sdata
= ">"; break;
892 #endif /* S_IFDOOR */
896 else if (hilo
== HIGH_PIECE
) {
897 switch (st
->st_mode
& S_IFMT
) {
898 case S_IFIFO
: sdata
= "Fifo File"; break;
899 case S_IFCHR
: sdata
= "Character Device"; break;
900 case S_IFDIR
: sdata
= "Directory"; break;
901 case S_IFBLK
: sdata
= "Block Device"; break;
902 case S_IFREG
: sdata
= "Regular File"; break;
903 case S_IFLNK
: sdata
= "Symbolic Link"; break;
904 case S_IFSOCK
: sdata
= "Socket"; break;
906 case S_IFWHT
: sdata
= "Whiteout File"; break;
909 case S_IFDOOR
: sdata
= "Door"; break;
910 #endif /* S_IFDOOR */
911 default: sdata
= "???"; break;
915 formats
= FMTF_STRING
;
922 strlcpy(path
, file
, sizeof(path
));
924 formats
= FMTF_STRING
;
929 if (S_ISCHR(st
->st_mode
) || S_ISBLK(st
->st_mode
)) {
930 char majdev
[20], mindev
[20];
936 majdev
, sizeof(majdev
),
938 ofmt
, HIGH_PIECE
, SHOW_st_rdev
);
942 mindev
, sizeof(mindev
),
944 ofmt
, LOW_PIECE
, SHOW_st_rdev
);
945 return (snprintf(buf
, blen
, "%.*s,%.*s",
946 l1
, majdev
, l2
, mindev
));
954 ofmt
, 0, SHOW_st_size
));
958 errx(1, "%.*s: bad format", (int)flen
, fmt
);
962 * If a subdatum was specified but not supported, or an output
963 * format was selected that is not supported, that's an error.
965 if (hilo
!= 0 || (ofmt
& formats
) == 0)
966 errx(1, "%.*s: bad format", (int)flen
, fmt
);
969 * Assemble the format string for passing to printf(3).
973 if (flags
& FLAG_POUND
)
975 if (flags
& FLAG_SPACE
)
977 if (flags
& FLAG_PLUS
)
979 if (flags
& FLAG_MINUS
)
981 if (flags
& FLAG_ZERO
)
985 * Only the timespecs support the FLOAT output format, and that
986 * requires work that differs from the other formats.
988 if (ofmt
== FMTF_FLOAT
) {
990 * Nothing after the decimal point, so just print seconds.
994 snprintf(tmp
, sizeof(tmp
), "%d", size
);
998 return (snprintf(buf
, blen
, lfmt
,
999 (long long)ts
.tv_sec
));
1003 * Unspecified precision gets all the precision we have:
1010 * Adjust the size for the decimal point and the digits
1016 * Any leftover size that's legitimate will be used.
1019 snprintf(tmp
, sizeof(tmp
), "%d", size
);
1022 /* Seconds: time_t cast to long long. */
1023 strcat(lfmt
, "lld");
1026 * The stuff after the decimal point always needs zero
1029 strcat(lfmt
, ".%0");
1032 * We can "print" at most nine digits of precision. The
1033 * rest we will pad on at the end.
1035 * Nanoseconds: long.
1037 snprintf(tmp
, sizeof(tmp
), "%dld", prec
> 9 ? 9 : prec
);
1041 * For precision of less that nine digits, trim off the
1042 * less significant figures.
1044 for (; prec
< 9; prec
++)
1048 * Use the format, and then tack on any zeroes that
1049 * might be required to make up the requested precision.
1051 l
= snprintf(buf
, blen
, lfmt
, (long long)ts
.tv_sec
, ts
.tv_nsec
);
1052 for (; prec
> 9 && l
< (int)blen
; prec
--, l
++)
1058 * Add on size and precision, if specified, to the format.
1061 snprintf(tmp
, sizeof(tmp
), "%d", size
);
1065 snprintf(tmp
, sizeof(tmp
), ".%d", prec
);
1070 * String output uses the temporary sdata.
1072 if (ofmt
== FMTF_STRING
) {
1074 errx(1, "%.*s: bad format", (int)flen
, fmt
);
1076 return (snprintf(buf
, blen
, lfmt
, sdata
));
1080 * Ensure that sign extension does not cause bad looking output
1083 if (small
&& ofmt
!= FMTF_DECIMAL
)
1084 data
= (u_int32_t
)data
;
1087 * The four "numeric" output forms.
1091 case FMTF_DECIMAL
: strcat(lfmt
, "d"); break;
1092 case FMTF_OCTAL
: strcat(lfmt
, "o"); break;
1093 case FMTF_UNSIGNED
: strcat(lfmt
, "u"); break;
1094 case FMTF_HEX
: strcat(lfmt
, "x"); break;
1097 return (snprintf(buf
, blen
, lfmt
, data
));
1101 #define hex2nibble(c) (c <= '9' ? c - '0' : toupper(c) - 'A' + 10)
1103 hex2byte(const char c
[2]) {
1104 if (!(ishexnumber(c
[0]) && ishexnumber(c
[1])))
1106 return (hex2nibble(c
[0]) << 4) + hex2nibble(c
[1]);