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 $
36 #else /* HAVE_CONFIG_H */
37 #define HAVE_STRUCT_STAT_ST_FLAGS 1
38 #define HAVE_STRUCT_STAT_ST_GEN 1
39 #define HAVE_STRUCT_STAT_ST_BIRTHTIME 0
40 #define HAVE_STRUCT_STAT_ST_MTIMENSEC 1
41 #define HAVE_DEVNAME 1
42 #endif /* HAVE_CONFIG_H */
44 #include <sys/param.h>
45 #include <sys/types.h>
47 #include <sys/mount.h>
62 #if HAVE_STRUCT_STAT_ST_FLAGS
65 #define SHELL_F " st_flags=%f"
66 #else /* HAVE_STRUCT_STAT_ST_FLAGS */
70 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
72 #if HAVE_STRUCT_STAT_ST_BIRTHTIME
73 #define DEF_B "\"%SB\" "
75 #define SHELL_B "st_birthtime=%B "
76 #else /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
80 #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
82 #if HAVE_STRUCT_STAT_ST_ATIM
83 #define st_atimespec st_atim
84 #define st_ctimespec st_ctim
85 #define st_mtimespec st_mtim
86 #endif /* HAVE_STRUCT_STAT_ST_ATIM */
89 "%d %i %Sp %l %Su %Sg %r %z \"%Sa\" \"%Sm\" \"%Sc\" " DEF_B \
91 #define RAW_FORMAT "%d %i %#p %l %u %g %r %z %a %m %c " RAW_B \
93 #define LS_FORMAT "%Sp %l %Su %Sg %Z %Sm %N%SY"
94 #define LSF_FORMAT "%Sp %l %Su %Sg %Z %Sm %N%T%SY"
95 #define SHELL_FORMAT \
96 "st_dev=%d st_ino=%i st_mode=%#p st_nlink=%l " \
97 "st_uid=%u st_gid=%g st_rdev=%r st_size=%z " \
98 "st_atime=%a st_mtime=%m st_ctime=%c " SHELL_B \
99 "st_blksize=%k st_blocks=%b" SHELL_F
100 #define LINUX_FORMAT \
102 " Size: %-11z FileType: %HT%n" \
103 " Mode: (%OMp%03OLp/%.10Sp) Uid: (%5u/%8Su) Gid: (%5g/%8Sg)%n" \
104 "Device: %Hd,%Ld Inode: %i Links: %l%n" \
109 #define TIME_FORMAT "%b %e %T %Y"
111 #define FLAG_POUND 0x01
112 #define FLAG_SPACE 0x02
113 #define FLAG_PLUS 0x04
114 #define FLAG_ZERO 0x08
115 #define FLAG_MINUS 0x10
118 * These format characters must all be unique, except the magic one.
120 #define FMT_MAGIC '%'
123 #define SIMPLE_NEWLINE 'n'
124 #define SIMPLE_TAB 't'
125 #define SIMPLE_PERCENT '%'
126 #define SIMPLE_NUMBER '@'
128 #define FMT_POUND '#'
129 #define FMT_SPACE ' '
132 #define FMT_MINUS '-'
134 #define FMT_DECIMAL 'D'
135 #define FMT_OCTAL 'O'
136 #define FMT_UNSIGNED 'U'
138 #define FMT_FLOAT 'F'
139 #define FMT_STRING 'S'
141 #define FMTF_DECIMAL 0x01
142 #define FMTF_OCTAL 0x02
143 #define FMTF_UNSIGNED 0x04
144 #define FMTF_HEX 0x08
145 #define FMTF_FLOAT 0x10
146 #define FMTF_STRING 0x20
148 #define HIGH_PIECE 'H'
149 #define MIDDLE_PIECE 'M'
150 #define LOW_PIECE 'L'
152 #define SHOW_realpath 'R'
153 #define SHOW_st_dev 'd'
154 #define SHOW_st_ino 'i'
155 #define SHOW_st_mode 'p'
156 #define SHOW_st_nlink 'l'
157 #define SHOW_st_uid 'u'
158 #define SHOW_st_gid 'g'
159 #define SHOW_st_rdev 'r'
160 #define SHOW_st_atime 'a'
161 #define SHOW_st_mtime 'm'
162 #define SHOW_st_ctime 'c'
163 #define SHOW_st_btime 'B'
164 #define SHOW_st_size 'z'
165 #define SHOW_st_blocks 'b'
166 #define SHOW_st_blksize 'k'
167 #define SHOW_st_flags 'f'
168 #define SHOW_st_gen 'v'
169 #define SHOW_symlink 'Y'
170 #define SHOW_filetype 'T'
171 #define SHOW_filename 'N'
172 #define SHOW_sizerdev 'Z'
174 static void usage(const char *) __dead2
;
175 static void output(const struct stat
*, const char *,
176 const char *, int, int);
177 static int format1(const struct stat
*, /* stat info */
178 const char *, /* the file name */
179 const char *, int, /* the format string itself */
180 char *, size_t, /* a place to put the output */
181 int, int, int, int, /* the parsed format */
183 static int hex2byte(const char [2]);
184 #if HAVE_STRUCT_STAT_ST_FLAGS
185 static char *xfflagstostr(unsigned long);
188 static const char *timefmt
;
191 #define addchar(s, c, nl) \
194 (*nl) = ((c) == '\n'); \
195 } while (0/*CONSTCOND*/)
198 main(int argc
, char *argv
[])
201 int ch
, rc
, errs
, am_readlink
;
202 int lsF
, fmtchar
, usestat
, nfs_handle
, fn
, nonl
, quiet
;
203 const char *statfmt
, *options
, *synopsis
;
204 char dname
[sizeof _PATH_DEV
+ MNAMELEN
] = _PATH_DEV
;
219 if (strcmp(getprogname(), "readlink") == 0) {
222 synopsis
= "[-fn] [file ...]";
227 options
= "f:FHlLnqrst:x";
228 synopsis
= "[-FLnq] [-f format | -l | -r | -s | -x] "
229 "[-t timefmt] [file|handle ...]";
232 while ((ch
= getopt(argc
, argv
, options
)) != -1)
261 errx(1, "can't use format '%c' with '%c'",
276 if (fmtchar
== '\0') {
281 statfmt
= DEF_FORMAT
;
285 if (lsF
&& fmtchar
!= 'l')
286 errx(1, "can't use format '%c' with -F", fmtchar
);
290 /* statfmt already set */
293 statfmt
= lsF
? LSF_FORMAT
: LS_FORMAT
;
296 statfmt
= RAW_FORMAT
;
299 statfmt
= SHELL_FORMAT
;
302 statfmt
= LINUX_FORMAT
;
312 timefmt
= TIME_FORMAT
;
317 if (fdevname_r(STDIN_FILENO
, dname
+
318 sizeof _PATH_DEV
- 1, MNAMELEN
) == 0)
322 rc
= fstat(STDIN_FILENO
, &st
);
329 bzero(&fhnd
, sizeof(fhnd
));
330 j
= MIN(2 * sizeof(fhnd
), strlen(file
));
335 rc
= hex2byte(&file
[j
- 2]);
338 ((char*) &fhnd
)[j
/ 2 - 1] = rc
;
345 rc
= fhstat(&fhnd
, &st
);
347 } else if (usestat
) {
349 * Try stat() and if it fails, fall back to
350 * lstat() just in case we're examining a
353 if ((rc
= stat(file
, &st
)) == -1 &&
355 (rc
= lstat(file
, &st
)) == -1)
359 rc
= lstat(file
, &st
);
366 warn("%s: stat", file
);
369 output(&st
, file
, statfmt
, fn
, nonl
);
376 return (am_readlink
? linkfail
: errs
);
379 #if HAVE_STRUCT_STAT_ST_FLAGS
381 * fflagstostr() wrapper that leaks only once
384 xfflagstostr(unsigned long fflags
)
386 static char *str
= NULL
;
391 str
= fflagstostr(fflags
);
393 err(1, "fflagstostr");
396 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
399 usage(const char *synopsis
)
402 fprintf(stderr
, "usage: %s %s\n", getprogname(), synopsis
);
407 * Parses a format string.
410 output(const struct stat
*st
, const char *file
,
411 const char *statfmt
, int fn
, int nonl
)
413 int flags
, size
, prec
, ofmt
, hilo
, what
;
414 char buf
[PATH_MAX
+ 4 + 1];
419 while (*statfmt
!= '\0') {
422 * Non-format characters go straight out.
424 if (*statfmt
!= FMT_MAGIC
) {
425 addchar(stdout
, *statfmt
, &nl
);
431 * The current format "substring" starts here,
432 * and then we skip the magic.
438 * Some simple one-character "formats".
442 addchar(stdout
, '\n', &nl
);
446 addchar(stdout
, '\t', &nl
);
450 addchar(stdout
, '%', &nl
);
453 case SIMPLE_NUMBER
: {
456 snprintf(num
, sizeof(num
), "%d", fn
);
457 for (p
= &num
[0]; *p
; p
++)
458 addchar(stdout
, *p
, &nl
);
465 * This must be an actual format string. Format strings are
466 * similar to printf(3) formats up to a point, and are of
469 * % required start of format
470 * [-# +0] opt. format characters
471 * size opt. field width
472 * . opt. decimal separator, followed by
473 * prec opt. precision
474 * fmt opt. output specifier (string, numeric, etc.)
475 * sub opt. sub field specifier (high, middle, low)
476 * datum required field specifier (size, mode, etc)
478 * Only the % and the datum selector are required. All data
479 * have reasonable default output forms. The "sub" specifier
480 * only applies to certain data (mode, dev, rdev, filetype).
481 * The symlink output defaults to STRING, yet will only emit
482 * the leading " -> " if STRING is explicitly specified. The
483 * sizerdev datum will generate rdev output for character or
484 * block devices, and size output for all others.
488 if (*statfmt
== FMT_POUND
)
490 else if (*statfmt
== FMT_SPACE
)
492 else if (*statfmt
== FMT_PLUS
)
494 else if (*statfmt
== FMT_ZERO
)
496 else if (*statfmt
== FMT_MINUS
)
501 } while (1/*CONSTCOND*/);
504 if (isdigit((unsigned)*statfmt
)) {
506 while (isdigit((unsigned)*statfmt
)) {
507 size
= (size
* 10) + (*statfmt
- '0');
515 if (*statfmt
== FMT_DOT
) {
519 while (isdigit((unsigned)*statfmt
)) {
520 prec
= (prec
* 10) + (*statfmt
- '0');
527 #define fmtcase(x, y) case (y): (x) = (y); statfmt++; break
528 #define fmtcasef(x, y, z) case (y): (x) = (z); statfmt++; break
530 fmtcasef(ofmt
, FMT_DECIMAL
, FMTF_DECIMAL
);
531 fmtcasef(ofmt
, FMT_OCTAL
, FMTF_OCTAL
);
532 fmtcasef(ofmt
, FMT_UNSIGNED
, FMTF_UNSIGNED
);
533 fmtcasef(ofmt
, FMT_HEX
, FMTF_HEX
);
534 fmtcasef(ofmt
, FMT_FLOAT
, FMTF_FLOAT
);
535 fmtcasef(ofmt
, FMT_STRING
, FMTF_STRING
);
542 fmtcase(hilo
, HIGH_PIECE
);
543 fmtcase(hilo
, MIDDLE_PIECE
);
544 fmtcase(hilo
, LOW_PIECE
);
551 fmtcase(what
, SHOW_realpath
);
552 fmtcase(what
, SHOW_st_dev
);
553 fmtcase(what
, SHOW_st_ino
);
554 fmtcase(what
, SHOW_st_mode
);
555 fmtcase(what
, SHOW_st_nlink
);
556 fmtcase(what
, SHOW_st_uid
);
557 fmtcase(what
, SHOW_st_gid
);
558 fmtcase(what
, SHOW_st_rdev
);
559 fmtcase(what
, SHOW_st_atime
);
560 fmtcase(what
, SHOW_st_mtime
);
561 fmtcase(what
, SHOW_st_ctime
);
562 fmtcase(what
, SHOW_st_btime
);
563 fmtcase(what
, SHOW_st_size
);
564 fmtcase(what
, SHOW_st_blocks
);
565 fmtcase(what
, SHOW_st_blksize
);
566 fmtcase(what
, SHOW_st_flags
);
567 fmtcase(what
, SHOW_st_gen
);
568 fmtcase(what
, SHOW_symlink
);
569 fmtcase(what
, SHOW_filetype
);
570 fmtcase(what
, SHOW_filename
);
571 fmtcase(what
, SHOW_sizerdev
);
580 subfmt
, statfmt
- subfmt
,
582 flags
, size
, prec
, ofmt
, hilo
, what
);
584 for (i
= 0; i
< t
&& i
< (int)(sizeof(buf
) - 1); i
++)
585 addchar(stdout
, buf
[i
], &nl
);
590 errx(1, "%.*s: bad format",
591 (int)(statfmt
- subfmt
+ 1), subfmt
);
600 * Arranges output according to a single parsed format substring.
603 format1(const struct stat
*st
,
605 const char *fmt
, int flen
,
606 char *buf
, size_t blen
,
607 int flags
, int size
, int prec
, int ofmt
,
611 char *stmp
, lfmt
[24], tmp
[20];
613 char smode
[12], sid
[12], path
[PATH_MAX
+ 4];
616 const struct timespec
*tsp
;
619 int l
, small
, formats
;
626 * First, pick out the data and tweak it based on hilo or
627 * specified output format (symlink output only).
632 small
= (sizeof(st
->st_dev
) == 4);
633 data
= (what
== SHOW_st_dev
) ? st
->st_dev
: st
->st_rdev
;
635 sdata
= (what
== SHOW_st_dev
) ?
636 devname(st
->st_dev
, S_IFBLK
) :
638 S_ISCHR(st
->st_mode
) ? S_IFCHR
:
639 S_ISBLK(st
->st_mode
) ? S_IFBLK
:
643 #endif /* HAVE_DEVNAME */
644 if (hilo
== HIGH_PIECE
) {
648 else if (hilo
== LOW_PIECE
) {
649 data
= minor((unsigned)data
);
652 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
|
655 #else /* HAVE_DEVNAME */
657 #endif /* HAVE_DEVNAME */
659 ofmt
= FMTF_UNSIGNED
;
662 small
= (sizeof(st
->st_ino
) == 4);
665 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
667 ofmt
= FMTF_UNSIGNED
;
670 small
= (sizeof(st
->st_mode
) == 4);
672 strmode(st
->st_mode
, smode
);
675 if (stmp
[l
- 1] == ' ')
677 if (hilo
== HIGH_PIECE
) {
683 else if (hilo
== MIDDLE_PIECE
) {
684 data
= (data
>> 9) & 07;
689 else if (hilo
== LOW_PIECE
) {
696 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
|
702 small
= (sizeof(st
->st_dev
) == 4);
705 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
707 ofmt
= FMTF_UNSIGNED
;
710 small
= (sizeof(st
->st_uid
) == 4);
712 if ((pw
= getpwuid(st
->st_uid
)) != NULL
)
715 snprintf(sid
, sizeof(sid
), "(%ld)", (long)st
->st_uid
);
718 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
|
721 ofmt
= FMTF_UNSIGNED
;
724 small
= (sizeof(st
->st_gid
) == 4);
726 if ((gr
= getgrgid(st
->st_gid
)) != NULL
)
729 snprintf(sid
, sizeof(sid
), "(%ld)", (long)st
->st_gid
);
732 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
|
735 ofmt
= FMTF_UNSIGNED
;
738 tsp
= &st
->st_atimespec
;
742 tsp
= &st
->st_mtimespec
;
746 tsp
= &st
->st_ctimespec
;
748 #if HAVE_STRUCT_STAT_ST_BIRTHTIME
751 tsp
= &st
->st_birthtimespec
;
752 #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
753 ts
= *tsp
; /* copy so we can muck with it */
754 small
= (sizeof(ts
.tv_sec
) == 4);
756 tm
= localtime(&ts
.tv_sec
);
759 tm
= localtime(&ts
.tv_sec
);
761 strftime(path
, sizeof(path
), timefmt
, tm
);
763 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
|
764 FMTF_FLOAT
| FMTF_STRING
;
769 small
= (sizeof(st
->st_size
) == 4);
772 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
774 ofmt
= FMTF_UNSIGNED
;
777 small
= (sizeof(st
->st_blocks
) == 4);
778 data
= st
->st_blocks
;
780 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
782 ofmt
= FMTF_UNSIGNED
;
784 case SHOW_st_blksize
:
785 small
= (sizeof(st
->st_blksize
) == 4);
786 data
= st
->st_blksize
;
788 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
790 ofmt
= FMTF_UNSIGNED
;
792 #if HAVE_STRUCT_STAT_ST_FLAGS
794 small
= (sizeof(st
->st_flags
) == 4);
796 sdata
= xfflagstostr(st
->st_flags
);
799 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
|
802 ofmt
= FMTF_UNSIGNED
;
804 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
805 #if HAVE_STRUCT_STAT_ST_GEN
807 small
= (sizeof(st
->st_gen
) == 4);
810 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
812 ofmt
= FMTF_UNSIGNED
;
814 #endif /* HAVE_STRUCT_STAT_ST_GEN */
819 strlcpy(path
, "(stdin)", sizeof(path
));
822 snprintf(path
, sizeof(path
), " -> ");
823 if (realpath(file
, path
+ 4) == NULL
) {
828 sdata
= path
+ (ofmt
== FMTF_STRING
? 0 : 4);
831 formats
= FMTF_STRING
;
838 if (S_ISLNK(st
->st_mode
)) {
839 snprintf(path
, sizeof(path
), " -> ");
840 l
= readlink(file
, path
+ 4, sizeof(path
) - 4 - 1);
847 sdata
= path
+ (ofmt
== FMTF_STRING
? 0 : 4);
853 formats
= FMTF_STRING
;
861 if (hilo
== 0 || hilo
== LOW_PIECE
) {
862 switch (st
->st_mode
& S_IFMT
) {
863 case S_IFIFO
: sdata
= "|"; break;
864 case S_IFDIR
: sdata
= "/"; break;
867 (S_IXUSR
| S_IXGRP
| S_IXOTH
))
870 case S_IFLNK
: sdata
= "@"; break;
871 case S_IFSOCK
: sdata
= "="; break;
873 case S_IFWHT
: sdata
= "%"; break;
876 case S_IFDOOR
: sdata
= ">"; break;
877 #endif /* S_IFDOOR */
881 else if (hilo
== HIGH_PIECE
) {
882 switch (st
->st_mode
& S_IFMT
) {
883 case S_IFIFO
: sdata
= "Fifo File"; break;
884 case S_IFCHR
: sdata
= "Character Device"; break;
885 case S_IFDIR
: sdata
= "Directory"; break;
886 case S_IFBLK
: sdata
= "Block Device"; break;
887 case S_IFREG
: sdata
= "Regular File"; break;
888 case S_IFLNK
: sdata
= "Symbolic Link"; break;
889 case S_IFSOCK
: sdata
= "Socket"; break;
891 case S_IFWHT
: sdata
= "Whiteout File"; break;
894 case S_IFDOOR
: sdata
= "Door"; break;
895 #endif /* S_IFDOOR */
896 default: sdata
= "???"; break;
900 formats
= FMTF_STRING
;
907 strlcpy(path
, file
, sizeof(path
));
909 formats
= FMTF_STRING
;
914 if (S_ISCHR(st
->st_mode
) || S_ISBLK(st
->st_mode
)) {
915 char majdev
[20], mindev
[20];
921 majdev
, sizeof(majdev
),
923 ofmt
, HIGH_PIECE
, SHOW_st_rdev
);
927 mindev
, sizeof(mindev
),
929 ofmt
, LOW_PIECE
, SHOW_st_rdev
);
930 return (snprintf(buf
, blen
, "%.*s,%.*s",
931 l1
, majdev
, l2
, mindev
));
939 ofmt
, 0, SHOW_st_size
));
943 errx(1, "%.*s: bad format", (int)flen
, fmt
);
947 * If a subdatum was specified but not supported, or an output
948 * format was selected that is not supported, that's an error.
950 if (hilo
!= 0 || (ofmt
& formats
) == 0)
951 errx(1, "%.*s: bad format", (int)flen
, fmt
);
954 * Assemble the format string for passing to printf(3).
958 if (flags
& FLAG_POUND
)
960 if (flags
& FLAG_SPACE
)
962 if (flags
& FLAG_PLUS
)
964 if (flags
& FLAG_MINUS
)
966 if (flags
& FLAG_ZERO
)
970 * Only the timespecs support the FLOAT output format, and that
971 * requires work that differs from the other formats.
973 if (ofmt
== FMTF_FLOAT
) {
975 * Nothing after the decimal point, so just print seconds.
979 snprintf(tmp
, sizeof(tmp
), "%d", size
);
983 return (snprintf(buf
, blen
, lfmt
,
984 (long long)ts
.tv_sec
));
988 * Unspecified precision gets all the precision we have:
995 * Adjust the size for the decimal point and the digits
1001 * Any leftover size that's legitimate will be used.
1004 snprintf(tmp
, sizeof(tmp
), "%d", size
);
1007 /* Seconds: time_t cast to long long. */
1008 strcat(lfmt
, "lld");
1011 * The stuff after the decimal point always needs zero
1014 strcat(lfmt
, ".%0");
1017 * We can "print" at most nine digits of precision. The
1018 * rest we will pad on at the end.
1020 * Nanoseconds: long.
1022 snprintf(tmp
, sizeof(tmp
), "%dld", prec
> 9 ? 9 : prec
);
1026 * For precision of less that nine digits, trim off the
1027 * less significant figures.
1029 for (; prec
< 9; prec
++)
1033 * Use the format, and then tack on any zeroes that
1034 * might be required to make up the requested precision.
1036 l
= snprintf(buf
, blen
, lfmt
, (long long)ts
.tv_sec
, ts
.tv_nsec
);
1037 for (; prec
> 9 && l
< (int)blen
; prec
--, l
++)
1043 * Add on size and precision, if specified, to the format.
1046 snprintf(tmp
, sizeof(tmp
), "%d", size
);
1050 snprintf(tmp
, sizeof(tmp
), ".%d", prec
);
1055 * String output uses the temporary sdata.
1057 if (ofmt
== FMTF_STRING
) {
1059 errx(1, "%.*s: bad format", (int)flen
, fmt
);
1061 return (snprintf(buf
, blen
, lfmt
, sdata
));
1065 * Ensure that sign extension does not cause bad looking output
1068 if (small
&& ofmt
!= FMTF_DECIMAL
)
1069 data
= (u_int32_t
)data
;
1072 * The four "numeric" output forms.
1076 case FMTF_DECIMAL
: strcat(lfmt
, "d"); break;
1077 case FMTF_OCTAL
: strcat(lfmt
, "o"); break;
1078 case FMTF_UNSIGNED
: strcat(lfmt
, "u"); break;
1079 case FMTF_HEX
: strcat(lfmt
, "x"); break;
1082 return (snprintf(buf
, blen
, lfmt
, data
));
1086 #define hex2nibble(c) (c <= '9' ? c - '0' : toupper(c) - 'A' + 10)
1088 hex2byte(const char c
[2]) {
1089 if (!(ishexnumber(c
[0]) && ishexnumber(c
[1])))
1091 return (hex2nibble(c
[0]) << 4) + hex2nibble(c
[1]);