2 * SPDX-License-Identifier: BSD-2-Clause-NetBSD
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>
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: (%OMp%03OLp/%.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 */
191 int hex2byte(const char [2]);
192 #if HAVE_STRUCT_STAT_ST_FLAGS
193 char *xfflagstostr(unsigned long);
196 static const char *timefmt;
199 #define addchar(s, c, nl) \
201 (void)fputc((c), (s)); \
202 (*nl) = ((c) == '\n'); \
203 } while (0/*CONSTCOND*/)
206 main(int argc, char *argv[])
209 int ch, rc, errs, am_readlink;
210 int lsF, fmtchar, usestat, nfs_handle, fn, nonl, quiet;
211 const char *statfmt, *options, *synopsis;
212 char dname[sizeof _PATH_DEV + SPECNAMELEN] = _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;
325 if (fdevname_r(STDIN_FILENO, dname +
326 sizeof _PATH_DEV - 1, SPECNAMELEN) != NULL)
330 rc = fstat(STDIN_FILENO, &st);
337 bzero(&fhnd, sizeof(fhnd));
338 j = MIN(2 * sizeof(fhnd), strlen(file));
343 rc = hex2byte(&file[j - 2]);
346 ((char*) &fhnd)[j / 2 - 1] = rc;
353 rc = fhstat(&fhnd, &st);
355 } else if (usestat) {
357 * Try stat() and if it fails, fall back to
358 * lstat() just in case we're examining a
361 if ((rc = stat(file, &st)) == -1 &&
363 (rc = lstat(file, &st)) == -1)
367 rc = lstat(file, &st);
374 warn("%s: stat", file);
377 output(&st, file, statfmt, fn, nonl);
384 return (am_readlink ? linkfail : errs);
387 #if HAVE_STRUCT_STAT_ST_FLAGS
389 * fflagstostr() wrapper that leaks only once
392 xfflagstostr(unsigned long fflags)
394 static char *str = NULL;
399 str = fflagstostr(fflags);
401 err(1, "fflagstostr");
404 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
407 usage(const char *synopsis)
410 (void)fprintf(stderr, "usage: %s %s\n", getprogname(), synopsis);
415 * Parses a format string.
418 output(const struct stat *st, const char *file,
419 const char *statfmt, int fn, int nonl)
421 int flags, size, prec, ofmt, hilo, what;
422 char buf[PATH_MAX + 4 + 1];
427 while (*statfmt != '\0') {
430 * Non-format characters go straight out.
432 if (*statfmt != FMT_MAGIC) {
433 addchar(stdout, *statfmt, &nl);
439 * The current format "substring" starts here,
440 * and then we skip the magic.
446 * Some simple one-character "formats".
450 addchar(stdout, '\n', &nl);
454 addchar(stdout, '\t', &nl);
458 addchar(stdout, '%', &nl);
461 case SIMPLE_NUMBER: {
464 snprintf(num, sizeof(num), "%d", fn);
465 for (p = &num[0]; *p; p++)
466 addchar(stdout, *p, &nl);
473 * This must be an actual format string. Format strings are
474 * similar to printf(3) formats up to a point, and are of
477 * % required start of format
478 * [-# +0] opt. format characters
479 * size opt. field width
480 * . opt. decimal separator, followed by
481 * prec opt. precision
482 * fmt opt. output specifier (string, numeric, etc.)
483 * sub opt. sub field specifier (high, middle, low)
484 * datum required field specifier (size, mode, etc)
486 * Only the % and the datum selector are required. All data
487 * have reasonable default output forms. The "sub" specifier
488 * only applies to certain data (mode, dev, rdev, filetype).
489 * The symlink output defaults to STRING, yet will only emit
490 * the leading " -> " if STRING is explicitly specified. The
491 * sizerdev datum will generate rdev output for character or
492 * block devices, and size output for all others.
496 if (*statfmt == FMT_POUND)
498 else if (*statfmt == FMT_SPACE)
500 else if (*statfmt == FMT_PLUS)
502 else if (*statfmt == FMT_ZERO)
504 else if (*statfmt == FMT_MINUS)
509 } while (1/*CONSTCOND*/);
512 if (isdigit((unsigned)*statfmt)) {
514 while (isdigit((unsigned)*statfmt)) {
515 size = (size * 10) + (*statfmt - '0');
523 if (*statfmt == FMT_DOT) {
527 while (isdigit((unsigned)*statfmt)) {
528 prec = (prec * 10) + (*statfmt - '0');
535 #define fmtcase(x, y) case (y): (x) = (y); statfmt++; break
536 #define fmtcasef(x, y, z) case (y): (x) = (z); statfmt++; break
538 fmtcasef(ofmt, FMT_DECIMAL, FMTF_DECIMAL);
539 fmtcasef(ofmt, FMT_OCTAL, FMTF_OCTAL);
540 fmtcasef(ofmt, FMT_UNSIGNED, FMTF_UNSIGNED);
541 fmtcasef(ofmt, FMT_HEX, FMTF_HEX);
542 fmtcasef(ofmt, FMT_FLOAT, FMTF_FLOAT);
543 fmtcasef(ofmt, FMT_STRING, FMTF_STRING);
550 fmtcase(hilo, HIGH_PIECE);
551 fmtcase(hilo, MIDDLE_PIECE);
552 fmtcase(hilo, LOW_PIECE);
559 fmtcase(what, SHOW_realpath);
560 fmtcase(what, SHOW_st_dev);
561 fmtcase(what, SHOW_st_ino);
562 fmtcase(what, SHOW_st_mode);
563 fmtcase(what, SHOW_st_nlink);
564 fmtcase(what, SHOW_st_uid);
565 fmtcase(what, SHOW_st_gid);
566 fmtcase(what, SHOW_st_rdev);
567 fmtcase(what, SHOW_st_atime);
568 fmtcase(what, SHOW_st_mtime);
569 fmtcase(what, SHOW_st_ctime);
570 fmtcase(what, SHOW_st_btime);
571 fmtcase(what, SHOW_st_size);
572 fmtcase(what, SHOW_st_blocks);
573 fmtcase(what, SHOW_st_blksize);
574 fmtcase(what, SHOW_st_flags);
575 fmtcase(what, SHOW_st_gen);
576 fmtcase(what, SHOW_symlink);
577 fmtcase(what, SHOW_filetype);
578 fmtcase(what, SHOW_filename);
579 fmtcase(what, SHOW_sizerdev);
588 subfmt, statfmt - subfmt,
590 flags, size, prec, ofmt, hilo, what);
592 for (i = 0; i < t && i < (int)(sizeof(buf) - 1); i++)
593 addchar(stdout, buf[i], &nl);
598 errx(1, "%.*s: bad format",
599 (int)(statfmt - subfmt + 1), subfmt);
603 (void)fputc('\n', stdout);
604 (void)fflush(stdout);
608 * Arranges output according to a single parsed format substring.
611 format1(const struct stat *st,
613 const char *fmt, int flen,
614 char *buf, size_t blen,
615 int flags, int size, int prec, int ofmt,
619 char *stmp, lfmt[24], tmp[20];
621 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 = (what == SHOW_st_dev) ?
644 devname(st->st_dev, S_IFBLK) :
646 S_ISCHR(st->st_mode) ? S_IFCHR :
647 S_ISBLK(st->st_mode) ? S_IFBLK :
651 #endif /* HAVE_DEVNAME */
652 if (hilo == HIGH_PIECE) {
656 else if (hilo == LOW_PIECE) {
657 data = minor((unsigned)data);
660 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
663 #else /* HAVE_DEVNAME */
665 #endif /* HAVE_DEVNAME */
667 ofmt = FMTF_UNSIGNED;
670 small = (sizeof(st->st_ino) == 4);
673 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
675 ofmt = FMTF_UNSIGNED;
678 small = (sizeof(st->st_mode) == 4);
680 strmode(st->st_mode, smode);
683 if (stmp[l - 1] == ' ')
685 if (hilo == HIGH_PIECE) {
691 else if (hilo == MIDDLE_PIECE) {
692 data = (data >> 9) & 07;
697 else if (hilo == LOW_PIECE) {
704 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
710 small = (sizeof(st->st_dev) == 4);
713 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
715 ofmt = FMTF_UNSIGNED;
718 small = (sizeof(st->st_uid) == 4);
720 if ((pw = getpwuid(st->st_uid)) != NULL)
723 snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_uid);
726 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
729 ofmt = FMTF_UNSIGNED;
732 small = (sizeof(st->st_gid) == 4);
734 if ((gr = getgrgid(st->st_gid)) != NULL)
737 snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_gid);
740 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
743 ofmt = FMTF_UNSIGNED;
746 tsp = &st->st_atimespec;
750 tsp = &st->st_mtimespec;
754 tsp = &st->st_ctimespec;
756 #if HAVE_STRUCT_STAT_ST_BIRTHTIME
759 tsp = &st->st_birthtimespec;
760 #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
761 ts = *tsp; /* copy so we can muck with it */
762 small = (sizeof(ts.tv_sec) == 4);
764 tm = localtime(&ts.tv_sec);
767 tm = localtime(&ts.tv_sec);
769 (void)strftime(path, sizeof(path), timefmt, tm);
771 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
772 FMTF_FLOAT | FMTF_STRING;
777 small = (sizeof(st->st_size) == 4);
780 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
782 ofmt = FMTF_UNSIGNED;
785 small = (sizeof(st->st_blocks) == 4);
786 data = st->st_blocks;
788 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
790 ofmt = FMTF_UNSIGNED;
792 case SHOW_st_blksize:
793 small = (sizeof(st->st_blksize) == 4);
794 data = st->st_blksize;
796 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
798 ofmt = FMTF_UNSIGNED;
800 #if HAVE_STRUCT_STAT_ST_FLAGS
802 small = (sizeof(st->st_flags) == 4);
804 sdata = xfflagstostr(st->st_flags);
807 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
810 ofmt = FMTF_UNSIGNED;
812 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
813 #if HAVE_STRUCT_STAT_ST_GEN
815 small = (sizeof(st->st_gen) == 4);
818 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
820 ofmt = FMTF_UNSIGNED;
822 #endif /* HAVE_STRUCT_STAT_ST_GEN */
827 (void)strlcpy(path, "(stdin)", sizeof(path));
830 snprintf(path, sizeof(path), " -> ");
831 if (realpath(file, path + 4) == NULL) {
836 sdata = path + (ofmt == FMTF_STRING ? 0 : 4);
839 formats = FMTF_STRING;
846 if (S_ISLNK(st->st_mode)) {
847 snprintf(path, sizeof(path), " -> ");
848 l = readlink(file, path + 4, sizeof(path) - 4 - 1);
855 sdata = path + (ofmt == FMTF_STRING ? 0 : 4);
861 formats = FMTF_STRING;
869 if (hilo == 0 || hilo == LOW_PIECE) {
870 switch (st->st_mode & S_IFMT) {
871 case S_IFIFO: sdata = "|"; break;
872 case S_IFDIR: sdata = "/"; break;
875 (S_IXUSR | S_IXGRP | S_IXOTH))
878 case S_IFLNK: sdata = "@"; break;
879 case S_IFSOCK: sdata = "="; break;
881 case S_IFWHT: sdata = "%"; break;
884 case S_IFDOOR: sdata = ">"; break;
885 #endif /* S_IFDOOR */
889 else if (hilo == HIGH_PIECE) {
890 switch (st->st_mode & S_IFMT) {
891 case S_IFIFO: sdata = "Fifo File"; break;
892 case S_IFCHR: sdata = "Character Device"; break;
893 case S_IFDIR: sdata = "Directory"; break;
894 case S_IFBLK: sdata = "Block Device"; break;
895 case S_IFREG: sdata = "Regular File"; break;
896 case S_IFLNK: sdata = "Symbolic Link"; break;
897 case S_IFSOCK: sdata = "Socket"; break;
899 case S_IFWHT: sdata = "Whiteout File"; break;
902 case S_IFDOOR: sdata = "Door"; break;
903 #endif /* S_IFDOOR */
904 default: sdata = "???"; break;
908 formats = FMTF_STRING;
915 (void)strlcpy(path, file, sizeof(path));
917 formats = FMTF_STRING;
922 if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
923 char majdev[20], mindev[20];
929 majdev, sizeof(majdev),
931 ofmt, HIGH_PIECE, SHOW_st_rdev);
935 mindev, sizeof(mindev),
937 ofmt, LOW_PIECE, SHOW_st_rdev);
938 return (snprintf(buf, blen, "%.*s,%.*s",
939 l1, majdev, l2, mindev));
947 ofmt, 0, SHOW_st_size));
951 errx(1, "%.*s: bad format", (int)flen, fmt);
955 * If a subdatum was specified but not supported, or an output
956 * format was selected that is not supported, that's an error.
958 if (hilo != 0 || (ofmt & formats) == 0)
959 errx(1, "%.*s: bad format", (int)flen, fmt);
962 * Assemble the format string for passing to printf(3).
965 (void)strcat(lfmt, "%");
966 if (flags & FLAG_POUND)
967 (void)strcat(lfmt, "#");
968 if (flags & FLAG_SPACE)
969 (void)strcat(lfmt, " ");
970 if (flags & FLAG_PLUS)
971 (void)strcat(lfmt, "+");
972 if (flags & FLAG_MINUS)
973 (void)strcat(lfmt, "-");
974 if (flags & FLAG_ZERO)
975 (void)strcat(lfmt, "0");
978 * Only the timespecs support the FLOAT output format, and that
979 * requires work that differs from the other formats.
981 if (ofmt == FMTF_FLOAT) {
983 * Nothing after the decimal point, so just print seconds.
987 (void)snprintf(tmp, sizeof(tmp), "%d", size);
988 (void)strcat(lfmt, tmp);
990 (void)strcat(lfmt, "lld");
991 return (snprintf(buf, blen, lfmt,
992 (long long)ts.tv_sec));
996 * Unspecified precision gets all the precision we have:
1003 * Adjust the size for the decimal point and the digits
1009 * Any leftover size that's legitimate will be used.
1012 (void)snprintf(tmp, sizeof(tmp), "%d", size);
1013 (void)strcat(lfmt, tmp);
1015 /* Seconds: time_t cast to long long. */
1016 (void)strcat(lfmt, "lld");
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 * Nanoseconds: long.
1030 (void)snprintf(tmp, sizeof(tmp), "%dld", MIN(prec, 9));
1031 (void)strcat(lfmt, tmp);
1034 * For precision of less that nine digits, trim off the
1035 * less significant figures.
1037 for (; prec < 9; prec++)
1041 * Use the format, and then tack on any zeroes that
1042 * might be required to make up the requested precision.
1044 l = snprintf(buf, blen, lfmt, (long long)ts.tv_sec, ts.tv_nsec);
1045 for (; prec > 9 && l < (int)blen; prec--, l++)
1046 (void)strcat(buf, "0");
1051 * Add on size and precision, if specified, to the format.
1054 (void)snprintf(tmp, sizeof(tmp), "%d", size);
1055 (void)strcat(lfmt, tmp);
1058 (void)snprintf(tmp, sizeof(tmp), ".%d", prec);
1059 (void)strcat(lfmt, tmp);
1063 * String output uses the temporary sdata.
1065 if (ofmt == FMTF_STRING) {
1067 errx(1, "%.*s: bad format", (int)flen, fmt);
1068 (void)strcat(lfmt, "s");
1069 return (snprintf(buf, blen, lfmt, sdata));
1073 * Ensure that sign extension does not cause bad looking output
1076 if (small && ofmt != FMTF_DECIMAL)
1077 data = (u_int32_t)data;
1080 * The four "numeric" output forms.
1082 (void)strcat(lfmt, "ll");
1084 case FMTF_DECIMAL: (void)strcat(lfmt, "d"); break;
1085 case FMTF_OCTAL: (void)strcat(lfmt, "o"); break;
1086 case FMTF_UNSIGNED: (void)strcat(lfmt, "u"); break;
1087 case FMTF_HEX: (void)strcat(lfmt, "x"); break;
1090 return (snprintf(buf, blen, lfmt, data));
1094 #define hex2nibble(c) (c <= '9' ? c - '0' : toupper(c) - 'A' + 10)
1096 hex2byte(const char c[2]) {
1097 if (!(ishexnumber(c[0]) && ishexnumber(c[1])))
1099 return (hex2nibble(c[0]) << 4) + hex2nibble(c[1]);