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];
622 const struct timespec *tsp;
625 int l, small, formats;
632 * First, pick out the data and tweak it based on hilo or
633 * specified output format (symlink output only).
638 small = (sizeof(st->st_dev) == 4);
639 data = (what == SHOW_st_dev) ? st->st_dev : st->st_rdev;
641 sdata = (what == SHOW_st_dev) ?
642 devname(st->st_dev, S_IFBLK) :
644 S_ISCHR(st->st_mode) ? S_IFCHR :
645 S_ISBLK(st->st_mode) ? S_IFBLK :
649 #endif /* HAVE_DEVNAME */
650 if (hilo == HIGH_PIECE) {
654 else if (hilo == LOW_PIECE) {
655 data = minor((unsigned)data);
658 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
661 #else /* HAVE_DEVNAME */
663 #endif /* HAVE_DEVNAME */
665 ofmt = FMTF_UNSIGNED;
668 small = (sizeof(st->st_ino) == 4);
671 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
673 ofmt = FMTF_UNSIGNED;
676 small = (sizeof(st->st_mode) == 4);
678 strmode(st->st_mode, smode);
681 if (stmp[l - 1] == ' ')
683 if (hilo == HIGH_PIECE) {
689 else if (hilo == MIDDLE_PIECE) {
690 data = (data >> 9) & 07;
695 else if (hilo == LOW_PIECE) {
702 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
708 small = (sizeof(st->st_dev) == 4);
711 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
713 ofmt = FMTF_UNSIGNED;
716 small = (sizeof(st->st_uid) == 4);
718 sdata = user_from_uid(st->st_uid, 1);
720 snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_uid);
723 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
726 ofmt = FMTF_UNSIGNED;
729 small = (sizeof(st->st_gid) == 4);
731 sdata = group_from_gid(st->st_gid, 1);
733 snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_gid);
736 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
739 ofmt = FMTF_UNSIGNED;
742 tsp = &st->st_atimespec;
746 tsp = &st->st_mtimespec;
750 tsp = &st->st_ctimespec;
752 #if HAVE_STRUCT_STAT_ST_BIRTHTIME
755 tsp = &st->st_birthtimespec;
756 #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
757 ts = *tsp; /* copy so we can muck with it */
758 small = (sizeof(ts.tv_sec) == 4);
760 tm = localtime(&ts.tv_sec);
763 tm = localtime(&ts.tv_sec);
765 (void)strftime(path, sizeof(path), timefmt, tm);
767 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
768 FMTF_FLOAT | FMTF_STRING;
773 small = (sizeof(st->st_size) == 4);
776 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
778 ofmt = FMTF_UNSIGNED;
781 small = (sizeof(st->st_blocks) == 4);
782 data = st->st_blocks;
784 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
786 ofmt = FMTF_UNSIGNED;
788 case SHOW_st_blksize:
789 small = (sizeof(st->st_blksize) == 4);
790 data = st->st_blksize;
792 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
794 ofmt = FMTF_UNSIGNED;
796 #if HAVE_STRUCT_STAT_ST_FLAGS
798 small = (sizeof(st->st_flags) == 4);
800 sdata = xfflagstostr(st->st_flags);
803 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
806 ofmt = FMTF_UNSIGNED;
808 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
809 #if HAVE_STRUCT_STAT_ST_GEN
811 small = (sizeof(st->st_gen) == 4);
814 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
816 ofmt = FMTF_UNSIGNED;
818 #endif /* HAVE_STRUCT_STAT_ST_GEN */
823 (void)strlcpy(path, "(stdin)", sizeof(path));
826 snprintf(path, sizeof(path), " -> ");
827 if (realpath(file, path + 4) == NULL) {
832 sdata = path + (ofmt == FMTF_STRING ? 0 : 4);
835 formats = FMTF_STRING;
842 if (S_ISLNK(st->st_mode)) {
843 snprintf(path, sizeof(path), " -> ");
844 l = readlink(file, path + 4, sizeof(path) - 4 - 1);
851 sdata = path + (ofmt == FMTF_STRING ? 0 : 4);
857 formats = FMTF_STRING;
865 if (hilo == 0 || hilo == LOW_PIECE) {
866 switch (st->st_mode & S_IFMT) {
867 case S_IFIFO: sdata = "|"; break;
868 case S_IFDIR: sdata = "/"; break;
871 (S_IXUSR | S_IXGRP | S_IXOTH))
874 case S_IFLNK: sdata = "@"; break;
875 case S_IFSOCK: sdata = "="; break;
877 case S_IFWHT: sdata = "%"; break;
880 case S_IFDOOR: sdata = ">"; break;
881 #endif /* S_IFDOOR */
885 else if (hilo == HIGH_PIECE) {
886 switch (st->st_mode & S_IFMT) {
887 case S_IFIFO: sdata = "Fifo File"; break;
888 case S_IFCHR: sdata = "Character Device"; break;
889 case S_IFDIR: sdata = "Directory"; break;
890 case S_IFBLK: sdata = "Block Device"; break;
891 case S_IFREG: sdata = "Regular File"; break;
892 case S_IFLNK: sdata = "Symbolic Link"; break;
893 case S_IFSOCK: sdata = "Socket"; break;
895 case S_IFWHT: sdata = "Whiteout File"; break;
898 case S_IFDOOR: sdata = "Door"; break;
899 #endif /* S_IFDOOR */
900 default: sdata = "???"; break;
904 formats = FMTF_STRING;
911 (void)strlcpy(path, file, sizeof(path));
913 formats = FMTF_STRING;
918 if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
919 char majdev[20], mindev[20];
925 majdev, sizeof(majdev),
927 ofmt, HIGH_PIECE, SHOW_st_rdev);
931 mindev, sizeof(mindev),
933 ofmt, LOW_PIECE, SHOW_st_rdev);
934 return (snprintf(buf, blen, "%.*s,%.*s",
935 l1, majdev, l2, mindev));
943 ofmt, 0, SHOW_st_size));
947 errx(1, "%.*s: bad format", (int)flen, fmt);
951 * If a subdatum was specified but not supported, or an output
952 * format was selected that is not supported, that's an error.
954 if (hilo != 0 || (ofmt & formats) == 0)
955 errx(1, "%.*s: bad format", (int)flen, fmt);
958 * Assemble the format string for passing to printf(3).
961 (void)strcat(lfmt, "%");
962 if (flags & FLAG_POUND)
963 (void)strcat(lfmt, "#");
964 if (flags & FLAG_SPACE)
965 (void)strcat(lfmt, " ");
966 if (flags & FLAG_PLUS)
967 (void)strcat(lfmt, "+");
968 if (flags & FLAG_MINUS)
969 (void)strcat(lfmt, "-");
970 if (flags & FLAG_ZERO)
971 (void)strcat(lfmt, "0");
974 * Only the timespecs support the FLOAT output format, and that
975 * requires work that differs from the other formats.
977 if (ofmt == FMTF_FLOAT) {
979 * Nothing after the decimal point, so just print seconds.
983 (void)snprintf(tmp, sizeof(tmp), "%d", size);
984 (void)strcat(lfmt, tmp);
986 (void)strcat(lfmt, "lld");
987 return (snprintf(buf, blen, lfmt,
988 (long long)ts.tv_sec));
992 * Unspecified precision gets all the precision we have:
999 * Adjust the size for the decimal point and the digits
1005 * Any leftover size that's legitimate will be used.
1008 (void)snprintf(tmp, sizeof(tmp), "%d", size);
1009 (void)strcat(lfmt, tmp);
1011 /* Seconds: time_t cast to long long. */
1012 (void)strcat(lfmt, "lld");
1015 * The stuff after the decimal point always needs zero
1018 (void)strcat(lfmt, ".%0");
1021 * We can "print" at most nine digits of precision. The
1022 * rest we will pad on at the end.
1024 * Nanoseconds: long.
1026 (void)snprintf(tmp, sizeof(tmp), "%dld", MIN(prec, 9));
1027 (void)strcat(lfmt, tmp);
1030 * For precision of less that nine digits, trim off the
1031 * less significant figures.
1033 for (; prec < 9; prec++)
1037 * Use the format, and then tack on any zeroes that
1038 * might be required to make up the requested precision.
1040 l = snprintf(buf, blen, lfmt, (long long)ts.tv_sec, ts.tv_nsec);
1041 for (; prec > 9 && l < (int)blen; prec--, l++)
1042 (void)strcat(buf, "0");
1047 * Add on size and precision, if specified, to the format.
1050 (void)snprintf(tmp, sizeof(tmp), "%d", size);
1051 (void)strcat(lfmt, tmp);
1054 (void)snprintf(tmp, sizeof(tmp), ".%d", prec);
1055 (void)strcat(lfmt, tmp);
1059 * String output uses the temporary sdata.
1061 if (ofmt == FMTF_STRING) {
1063 errx(1, "%.*s: bad format", (int)flen, fmt);
1064 (void)strcat(lfmt, "s");
1065 return (snprintf(buf, blen, lfmt, sdata));
1069 * Ensure that sign extension does not cause bad looking output
1072 if (small && ofmt != FMTF_DECIMAL)
1073 data = (u_int32_t)data;
1076 * The four "numeric" output forms.
1078 (void)strcat(lfmt, "ll");
1080 case FMTF_DECIMAL: (void)strcat(lfmt, "d"); break;
1081 case FMTF_OCTAL: (void)strcat(lfmt, "o"); break;
1082 case FMTF_UNSIGNED: (void)strcat(lfmt, "u"); break;
1083 case FMTF_HEX: (void)strcat(lfmt, "x"); break;
1086 return (snprintf(buf, blen, lfmt, data));
1090 #define hex2nibble(c) (c <= '9' ? c - '0' : toupper(c) - 'A' + 10)
1092 hex2byte(const char c[2]) {
1093 if (!(ishexnumber(c[0]) && ishexnumber(c[1])))
1095 return (hex2nibble(c[0]) << 4) + hex2nibble(c[1]);