2 * SPDX-License-Identifier: BSD-2-Clause
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 #include <sys/cdefs.h>
35 __RCSID("$NetBSD: stat.c,v 1.33 2011/01/15 22:54:10 njoly Exp $"
36 "$OpenBSD: stat.c,v 1.14 2009/06/24 09:44:25 sobrado Exp $");
40 __FBSDID("$FreeBSD$");
44 #else /* HAVE_CONFIG_H */
45 #define HAVE_STRUCT_STAT_ST_FLAGS 1
46 #define HAVE_STRUCT_STAT_ST_GEN 1
47 #define HAVE_STRUCT_STAT_ST_BIRTHTIME 1
48 #define HAVE_STRUCT_STAT_ST_MTIMENSEC 1
49 #define HAVE_DEVNAME 1
50 #endif /* HAVE_CONFIG_H */
52 #include <sys/param.h>
53 #include <sys/types.h>
55 #include <sys/mount.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: (%OMp%03OLp/%.10Sp) Uid: (%5u/%8Su) Gid: (%5g/%8Sg)%n" \
113 "Device: %Hd,%Ld Inode: %i Links: %l%n" \
119 #define TIME_FORMAT "%b %e %T %Y"
121 #define FLAG_POUND 0x01
122 #define FLAG_SPACE 0x02
123 #define FLAG_PLUS 0x04
124 #define FLAG_ZERO 0x08
125 #define FLAG_MINUS 0x10
128 * These format characters must all be unique, except the magic one.
130 #define FMT_MAGIC '%'
133 #define SIMPLE_NEWLINE 'n'
134 #define SIMPLE_TAB 't'
135 #define SIMPLE_PERCENT '%'
136 #define SIMPLE_NUMBER '@'
138 #define FMT_POUND '#'
139 #define FMT_SPACE ' '
142 #define FMT_MINUS '-'
144 #define FMT_DECIMAL 'D'
145 #define FMT_OCTAL 'O'
146 #define FMT_UNSIGNED 'U'
148 #define FMT_FLOAT 'F'
149 #define FMT_STRING 'S'
151 #define FMTF_DECIMAL 0x01
152 #define FMTF_OCTAL 0x02
153 #define FMTF_UNSIGNED 0x04
154 #define FMTF_HEX 0x08
155 #define FMTF_FLOAT 0x10
156 #define FMTF_STRING 0x20
158 #define HIGH_PIECE 'H'
159 #define MIDDLE_PIECE 'M'
160 #define LOW_PIECE 'L'
162 #define SHOW_realpath 'R'
163 #define SHOW_st_dev 'd'
164 #define SHOW_st_ino 'i'
165 #define SHOW_st_mode 'p'
166 #define SHOW_st_nlink 'l'
167 #define SHOW_st_uid 'u'
168 #define SHOW_st_gid 'g'
169 #define SHOW_st_rdev 'r'
170 #define SHOW_st_atime 'a'
171 #define SHOW_st_mtime 'm'
172 #define SHOW_st_ctime 'c'
173 #define SHOW_st_btime 'B'
174 #define SHOW_st_size 'z'
175 #define SHOW_st_blocks 'b'
176 #define SHOW_st_blksize 'k'
177 #define SHOW_st_flags 'f'
178 #define SHOW_st_gen 'v'
179 #define SHOW_symlink 'Y'
180 #define SHOW_filetype 'T'
181 #define SHOW_filename 'N'
182 #define SHOW_sizerdev 'Z'
184 void usage(const char *);
185 void output(const struct stat *, const char *,
186 const char *, int, int);
187 int format1(const struct stat *, /* stat info */
188 const char *, /* the file name */
189 const char *, int, /* the format string itself */
190 char *, size_t, /* a place to put the output */
191 int, int, int, int, /* the parsed format */
193 int hex2byte(const char [2]);
194 #if HAVE_STRUCT_STAT_ST_FLAGS
195 char *xfflagstostr(unsigned long);
198 static const char *timefmt;
201 #define addchar(s, c, nl) \
203 (void)fputc((c), (s)); \
204 (*nl) = ((c) == '\n'); \
205 } while (0/*CONSTCOND*/)
208 main(int argc, char *argv[])
211 int ch, rc, errs, am_readlink;
212 int lsF, fmtchar, usestat, nfs_handle, fn, nonl, quiet;
213 const char *statfmt, *options, *synopsis;
214 char dname[sizeof _PATH_DEV + SPECNAMELEN] = _PATH_DEV;
229 if (strcmp(getprogname(), "readlink") == 0) {
232 synopsis = "[-fn] [file ...]";
237 options = "f:FHlLnqrst:x";
238 synopsis = "[-FLnq] [-f format | -l | -r | -s | -x] "
239 "[-t timefmt] [file|handle ...]";
242 while ((ch = getopt(argc, argv, options)) != -1)
271 errx(1, "can't use format '%c' with '%c'",
286 if (fmtchar == '\0') {
291 statfmt = DEF_FORMAT;
295 if (lsF && fmtchar != 'l')
296 errx(1, "can't use format '%c' with -F", fmtchar);
300 /* statfmt already set */
303 statfmt = lsF ? LSF_FORMAT : LS_FORMAT;
306 statfmt = RAW_FORMAT;
309 statfmt = SHELL_FORMAT;
312 statfmt = LINUX_FORMAT;
322 timefmt = TIME_FORMAT;
327 if (fdevname_r(STDIN_FILENO, dname +
328 sizeof _PATH_DEV - 1, SPECNAMELEN) != NULL)
332 rc = fstat(STDIN_FILENO, &st);
339 bzero(&fhnd, sizeof(fhnd));
340 j = MIN(2 * sizeof(fhnd), strlen(file));
345 rc = hex2byte(&file[j - 2]);
348 ((char*) &fhnd)[j / 2 - 1] = rc;
355 rc = fhstat(&fhnd, &st);
357 } else if (usestat) {
359 * Try stat() and if it fails, fall back to
360 * lstat() just in case we're examining a
363 if ((rc = stat(file, &st)) == -1 &&
365 (rc = lstat(file, &st)) == -1)
369 rc = lstat(file, &st);
379 output(&st, file, statfmt, fn, nonl);
386 return (am_readlink ? linkfail : errs);
389 #if HAVE_STRUCT_STAT_ST_FLAGS
391 * fflagstostr() wrapper that leaks only once
394 xfflagstostr(unsigned long fflags)
396 static char *str = NULL;
401 str = fflagstostr(fflags);
403 err(1, "fflagstostr");
406 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
409 usage(const char *synopsis)
412 (void)fprintf(stderr, "usage: %s %s\n", getprogname(), synopsis);
417 * Parses a format string.
420 output(const struct stat *st, const char *file,
421 const char *statfmt, int fn, int nonl)
423 int flags, size, prec, ofmt, hilo, what;
424 char buf[PATH_MAX + 4 + 1];
429 while (*statfmt != '\0') {
432 * Non-format characters go straight out.
434 if (*statfmt != FMT_MAGIC) {
435 addchar(stdout, *statfmt, &nl);
441 * The current format "substring" starts here,
442 * and then we skip the magic.
448 * Some simple one-character "formats".
452 addchar(stdout, '\n', &nl);
456 addchar(stdout, '\t', &nl);
460 addchar(stdout, '%', &nl);
463 case SIMPLE_NUMBER: {
466 snprintf(num, sizeof(num), "%d", fn);
467 for (p = &num[0]; *p; p++)
468 addchar(stdout, *p, &nl);
475 * This must be an actual format string. Format strings are
476 * similar to printf(3) formats up to a point, and are of
479 * % required start of format
480 * [-# +0] opt. format characters
481 * size opt. field width
482 * . opt. decimal separator, followed by
483 * prec opt. precision
484 * fmt opt. output specifier (string, numeric, etc.)
485 * sub opt. sub field specifier (high, middle, low)
486 * datum required field specifier (size, mode, etc)
488 * Only the % and the datum selector are required. All data
489 * have reasonable default output forms. The "sub" specifier
490 * only applies to certain data (mode, dev, rdev, filetype).
491 * The symlink output defaults to STRING, yet will only emit
492 * the leading " -> " if STRING is explicitly specified. The
493 * sizerdev datum will generate rdev output for character or
494 * block devices, and size output for all others.
498 if (*statfmt == FMT_POUND)
500 else if (*statfmt == FMT_SPACE)
502 else if (*statfmt == FMT_PLUS)
504 else if (*statfmt == FMT_ZERO)
506 else if (*statfmt == FMT_MINUS)
511 } while (1/*CONSTCOND*/);
514 if (isdigit((unsigned)*statfmt)) {
516 while (isdigit((unsigned)*statfmt)) {
517 size = (size * 10) + (*statfmt - '0');
525 if (*statfmt == FMT_DOT) {
529 while (isdigit((unsigned)*statfmt)) {
530 prec = (prec * 10) + (*statfmt - '0');
537 #define fmtcase(x, y) case (y): (x) = (y); statfmt++; break
538 #define fmtcasef(x, y, z) case (y): (x) = (z); statfmt++; break
540 fmtcasef(ofmt, FMT_DECIMAL, FMTF_DECIMAL);
541 fmtcasef(ofmt, FMT_OCTAL, FMTF_OCTAL);
542 fmtcasef(ofmt, FMT_UNSIGNED, FMTF_UNSIGNED);
543 fmtcasef(ofmt, FMT_HEX, FMTF_HEX);
544 fmtcasef(ofmt, FMT_FLOAT, FMTF_FLOAT);
545 fmtcasef(ofmt, FMT_STRING, FMTF_STRING);
552 fmtcase(hilo, HIGH_PIECE);
553 fmtcase(hilo, MIDDLE_PIECE);
554 fmtcase(hilo, LOW_PIECE);
561 fmtcase(what, SHOW_realpath);
562 fmtcase(what, SHOW_st_dev);
563 fmtcase(what, SHOW_st_ino);
564 fmtcase(what, SHOW_st_mode);
565 fmtcase(what, SHOW_st_nlink);
566 fmtcase(what, SHOW_st_uid);
567 fmtcase(what, SHOW_st_gid);
568 fmtcase(what, SHOW_st_rdev);
569 fmtcase(what, SHOW_st_atime);
570 fmtcase(what, SHOW_st_mtime);
571 fmtcase(what, SHOW_st_ctime);
572 fmtcase(what, SHOW_st_btime);
573 fmtcase(what, SHOW_st_size);
574 fmtcase(what, SHOW_st_blocks);
575 fmtcase(what, SHOW_st_blksize);
576 fmtcase(what, SHOW_st_flags);
577 fmtcase(what, SHOW_st_gen);
578 fmtcase(what, SHOW_symlink);
579 fmtcase(what, SHOW_filetype);
580 fmtcase(what, SHOW_filename);
581 fmtcase(what, SHOW_sizerdev);
590 subfmt, statfmt - subfmt,
592 flags, size, prec, ofmt, hilo, what);
594 for (i = 0; i < t && i < (int)(sizeof(buf) - 1); i++)
595 addchar(stdout, buf[i], &nl);
600 errx(1, "%.*s: bad format",
601 (int)(statfmt - subfmt + 1), subfmt);
605 (void)fputc('\n', stdout);
606 (void)fflush(stdout);
610 * Arranges output according to a single parsed format substring.
613 format1(const struct stat *st,
615 const char *fmt, int flen,
616 char *buf, size_t blen,
617 int flags, int size, int prec, int ofmt,
621 char *stmp, lfmt[24], tmp[20];
623 char smode[12], sid[12], path[PATH_MAX + 4];
624 const struct timespec *tsp;
627 int l, small, formats;
634 * First, pick out the data and tweak it based on hilo or
635 * specified output format (symlink output only).
640 small = (sizeof(st->st_dev) == 4);
641 data = (what == SHOW_st_dev) ? st->st_dev : st->st_rdev;
643 sdata = devname(what == SHOW_st_dev ? st->st_dev :
644 st->st_rdev, S_ISCHR(st->st_mode) ? S_IFCHR :
645 (S_ISBLK(st->st_mode) ? S_IFBLK : 0));
646 #endif /* HAVE_DEVNAME */
647 if (hilo == HIGH_PIECE) {
651 else if (hilo == LOW_PIECE) {
652 data = minor((unsigned)data);
655 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
658 #else /* HAVE_DEVNAME */
660 #endif /* HAVE_DEVNAME */
662 ofmt = FMTF_UNSIGNED;
665 small = (sizeof(st->st_ino) == 4);
668 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
670 ofmt = FMTF_UNSIGNED;
673 small = (sizeof(st->st_mode) == 4);
675 strmode(st->st_mode, smode);
678 if (stmp[l - 1] == ' ')
680 if (hilo == HIGH_PIECE) {
686 else if (hilo == MIDDLE_PIECE) {
687 data = (data >> 9) & 07;
692 else if (hilo == LOW_PIECE) {
699 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
705 small = (sizeof(st->st_dev) == 4);
708 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
710 ofmt = FMTF_UNSIGNED;
713 small = (sizeof(st->st_uid) == 4);
715 sdata = user_from_uid(st->st_uid, 1);
717 snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_uid);
720 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
723 ofmt = FMTF_UNSIGNED;
726 small = (sizeof(st->st_gid) == 4);
728 sdata = group_from_gid(st->st_gid, 1);
730 snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_gid);
733 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
736 ofmt = FMTF_UNSIGNED;
739 tsp = &st->st_atimespec;
743 tsp = &st->st_mtimespec;
747 tsp = &st->st_ctimespec;
749 #if HAVE_STRUCT_STAT_ST_BIRTHTIME
752 tsp = &st->st_birthtimespec;
753 #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
754 ts = *tsp; /* copy so we can muck with it */
755 small = (sizeof(ts.tv_sec) == 4);
757 tm = localtime(&ts.tv_sec);
760 tm = localtime(&ts.tv_sec);
762 (void)setlocale(LC_TIME, "");
763 (void)strftime(path, sizeof(path), timefmt, tm);
765 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
766 FMTF_FLOAT | FMTF_STRING;
771 small = (sizeof(st->st_size) == 4);
774 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
776 ofmt = FMTF_UNSIGNED;
779 small = (sizeof(st->st_blocks) == 4);
780 data = st->st_blocks;
782 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
784 ofmt = FMTF_UNSIGNED;
786 case SHOW_st_blksize:
787 small = (sizeof(st->st_blksize) == 4);
788 data = st->st_blksize;
790 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
792 ofmt = FMTF_UNSIGNED;
794 #if HAVE_STRUCT_STAT_ST_FLAGS
796 small = (sizeof(st->st_flags) == 4);
798 sdata = xfflagstostr(st->st_flags);
801 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
804 ofmt = FMTF_UNSIGNED;
806 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
807 #if HAVE_STRUCT_STAT_ST_GEN
809 small = (sizeof(st->st_gen) == 4);
812 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
814 ofmt = FMTF_UNSIGNED;
816 #endif /* HAVE_STRUCT_STAT_ST_GEN */
821 (void)strlcpy(path, "(stdin)", sizeof(path));
824 snprintf(path, sizeof(path), " -> ");
825 if (realpath(file, path + 4) == NULL) {
830 sdata = path + (ofmt == FMTF_STRING ? 0 : 4);
833 formats = FMTF_STRING;
840 if (S_ISLNK(st->st_mode)) {
841 snprintf(path, sizeof(path), " -> ");
842 l = readlink(file, path + 4, sizeof(path) - 4 - 1);
849 sdata = path + (ofmt == FMTF_STRING ? 0 : 4);
855 formats = FMTF_STRING;
863 if (hilo == 0 || hilo == LOW_PIECE) {
864 switch (st->st_mode & S_IFMT) {
865 case S_IFIFO: sdata = "|"; break;
866 case S_IFDIR: sdata = "/"; break;
869 (S_IXUSR | S_IXGRP | S_IXOTH))
872 case S_IFLNK: sdata = "@"; break;
873 case S_IFSOCK: sdata = "="; break;
875 case S_IFWHT: sdata = "%"; break;
878 case S_IFDOOR: sdata = ">"; break;
879 #endif /* S_IFDOOR */
883 else if (hilo == HIGH_PIECE) {
884 switch (st->st_mode & S_IFMT) {
885 case S_IFIFO: sdata = "Fifo File"; break;
886 case S_IFCHR: sdata = "Character Device"; break;
887 case S_IFDIR: sdata = "Directory"; break;
888 case S_IFBLK: sdata = "Block Device"; break;
889 case S_IFREG: sdata = "Regular File"; break;
890 case S_IFLNK: sdata = "Symbolic Link"; break;
891 case S_IFSOCK: sdata = "Socket"; break;
893 case S_IFWHT: sdata = "Whiteout File"; break;
896 case S_IFDOOR: sdata = "Door"; break;
897 #endif /* S_IFDOOR */
898 default: sdata = "???"; break;
902 formats = FMTF_STRING;
909 (void)strlcpy(path, file, sizeof(path));
911 formats = FMTF_STRING;
916 if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
917 char majdev[20], mindev[20];
923 majdev, sizeof(majdev),
925 ofmt, HIGH_PIECE, SHOW_st_rdev);
929 mindev, sizeof(mindev),
931 ofmt, LOW_PIECE, SHOW_st_rdev);
932 return (snprintf(buf, blen, "%.*s,%.*s",
933 l1, majdev, l2, mindev));
941 ofmt, 0, SHOW_st_size));
945 errx(1, "%.*s: bad format", (int)flen, fmt);
949 * If a subdatum was specified but not supported, or an output
950 * format was selected that is not supported, that's an error.
952 if (hilo != 0 || (ofmt & formats) == 0)
953 errx(1, "%.*s: bad format", (int)flen, fmt);
956 * Assemble the format string for passing to printf(3).
959 (void)strcat(lfmt, "%");
960 if (flags & FLAG_POUND)
961 (void)strcat(lfmt, "#");
962 if (flags & FLAG_SPACE)
963 (void)strcat(lfmt, " ");
964 if (flags & FLAG_PLUS)
965 (void)strcat(lfmt, "+");
966 if (flags & FLAG_MINUS)
967 (void)strcat(lfmt, "-");
968 if (flags & FLAG_ZERO)
969 (void)strcat(lfmt, "0");
972 * Only the timespecs support the FLOAT output format, and that
973 * requires work that differs from the other formats.
975 if (ofmt == FMTF_FLOAT) {
977 * Nothing after the decimal point, so just print seconds.
981 (void)snprintf(tmp, sizeof(tmp), "%d", size);
982 (void)strcat(lfmt, tmp);
984 (void)strcat(lfmt, "lld");
985 return (snprintf(buf, blen, lfmt,
986 (long long)ts.tv_sec));
990 * Unspecified precision gets all the precision we have:
997 * Adjust the size for the decimal point and the digits
1003 * Any leftover size that's legitimate will be used.
1006 (void)snprintf(tmp, sizeof(tmp), "%d", size);
1007 (void)strcat(lfmt, tmp);
1009 /* Seconds: time_t cast to long long. */
1010 (void)strcat(lfmt, "lld");
1013 * The stuff after the decimal point always needs zero
1016 (void)strcat(lfmt, ".%0");
1019 * We can "print" at most nine digits of precision. The
1020 * rest we will pad on at the end.
1022 * Nanoseconds: long.
1024 (void)snprintf(tmp, sizeof(tmp), "%dld", MIN(prec, 9));
1025 (void)strcat(lfmt, tmp);
1028 * For precision of less that nine digits, trim off the
1029 * less significant figures.
1031 for (; prec < 9; prec++)
1035 * Use the format, and then tack on any zeroes that
1036 * might be required to make up the requested precision.
1038 l = snprintf(buf, blen, lfmt, (long long)ts.tv_sec, ts.tv_nsec);
1039 for (; prec > 9 && l < (int)blen; prec--, l++)
1040 (void)strcat(buf, "0");
1045 * Add on size and precision, if specified, to the format.
1048 (void)snprintf(tmp, sizeof(tmp), "%d", size);
1049 (void)strcat(lfmt, tmp);
1052 (void)snprintf(tmp, sizeof(tmp), ".%d", prec);
1053 (void)strcat(lfmt, tmp);
1057 * String output uses the temporary sdata.
1059 if (ofmt == FMTF_STRING) {
1061 errx(1, "%.*s: bad format", (int)flen, fmt);
1062 (void)strcat(lfmt, "s");
1063 return (snprintf(buf, blen, lfmt, sdata));
1067 * Ensure that sign extension does not cause bad looking output
1070 if (small && ofmt != FMTF_DECIMAL)
1071 data = (u_int32_t)data;
1074 * The four "numeric" output forms.
1076 (void)strcat(lfmt, "ll");
1078 case FMTF_DECIMAL: (void)strcat(lfmt, "d"); break;
1079 case FMTF_OCTAL: (void)strcat(lfmt, "o"); break;
1080 case FMTF_UNSIGNED: (void)strcat(lfmt, "u"); break;
1081 case FMTF_HEX: (void)strcat(lfmt, "x"); break;
1084 return (snprintf(buf, blen, lfmt, data));
1088 #define hex2nibble(c) (c <= '9' ? c - '0' : toupper(c) - 'A' + 10)
1090 hex2byte(const char c[2]) {
1091 if (!(ishexnumber(c[0]) && ishexnumber(c[1])))
1093 return (hex2nibble(c[0]) << 4) + hex2nibble(c[1]);