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 $");
41 #else /* HAVE_CONFIG_H */
42 #define HAVE_STRUCT_STAT_ST_FLAGS 1
43 #define HAVE_STRUCT_STAT_ST_GEN 1
44 #define HAVE_STRUCT_STAT_ST_BIRTHTIME 1
45 #define HAVE_STRUCT_STAT_ST_MTIMENSEC 1
46 #define HAVE_DEVNAME 1
47 #endif /* HAVE_CONFIG_H */
49 #include <sys/param.h>
50 #include <sys/types.h>
52 #include <sys/mount.h>
68 #if HAVE_STRUCT_STAT_ST_FLAGS
71 #define SHELL_F " st_flags=%f"
72 #else /* HAVE_STRUCT_STAT_ST_FLAGS */
76 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
78 #if HAVE_STRUCT_STAT_ST_BIRTHTIME
79 #define DEF_B "\"%SB\" "
81 #define SHELL_B "st_birthtime=%B "
82 #else /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
86 #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
88 #if HAVE_STRUCT_STAT_ST_ATIM
89 #define st_atimespec st_atim
90 #define st_ctimespec st_ctim
91 #define st_mtimespec st_mtim
92 #endif /* HAVE_STRUCT_STAT_ST_ATIM */
95 "%d %i %Sp %l %Su %Sg %r %z \"%Sa\" \"%Sm\" \"%Sc\" " DEF_B \
97 #define RAW_FORMAT "%d %i %#p %l %u %g %r %z %a %m %c " RAW_B \
99 #define LS_FORMAT "%Sp %l %Su %Sg %Z %Sm %N%SY"
100 #define LSF_FORMAT "%Sp %l %Su %Sg %Z %Sm %N%T%SY"
101 #define SHELL_FORMAT \
102 "st_dev=%d st_ino=%i st_mode=%#p st_nlink=%l " \
103 "st_uid=%u st_gid=%g st_rdev=%r st_size=%z " \
104 "st_atime=%a st_mtime=%m st_ctime=%c " SHELL_B \
105 "st_blksize=%k st_blocks=%b" SHELL_F
106 #define LINUX_FORMAT \
108 " Size: %-11z FileType: %HT%n" \
109 " Mode: (%OMp%03OLp/%.10Sp) Uid: (%5u/%8Su) Gid: (%5g/%8Sg)%n" \
110 "Device: %Hd,%Ld Inode: %i Links: %l%n" \
116 #define TIME_FORMAT "%b %e %T %Y"
118 #define FLAG_POUND 0x01
119 #define FLAG_SPACE 0x02
120 #define FLAG_PLUS 0x04
121 #define FLAG_ZERO 0x08
122 #define FLAG_MINUS 0x10
125 * These format characters must all be unique, except the magic one.
127 #define FMT_MAGIC '%'
130 #define SIMPLE_NEWLINE 'n'
131 #define SIMPLE_TAB 't'
132 #define SIMPLE_PERCENT '%'
133 #define SIMPLE_NUMBER '@'
135 #define FMT_POUND '#'
136 #define FMT_SPACE ' '
139 #define FMT_MINUS '-'
141 #define FMT_DECIMAL 'D'
142 #define FMT_OCTAL 'O'
143 #define FMT_UNSIGNED 'U'
145 #define FMT_FLOAT 'F'
146 #define FMT_STRING 'S'
148 #define FMTF_DECIMAL 0x01
149 #define FMTF_OCTAL 0x02
150 #define FMTF_UNSIGNED 0x04
151 #define FMTF_HEX 0x08
152 #define FMTF_FLOAT 0x10
153 #define FMTF_STRING 0x20
155 #define HIGH_PIECE 'H'
156 #define MIDDLE_PIECE 'M'
157 #define LOW_PIECE 'L'
159 #define SHOW_realpath 'R'
160 #define SHOW_st_dev 'd'
161 #define SHOW_st_ino 'i'
162 #define SHOW_st_mode 'p'
163 #define SHOW_st_nlink 'l'
164 #define SHOW_st_uid 'u'
165 #define SHOW_st_gid 'g'
166 #define SHOW_st_rdev 'r'
167 #define SHOW_st_atime 'a'
168 #define SHOW_st_mtime 'm'
169 #define SHOW_st_ctime 'c'
170 #define SHOW_st_btime 'B'
171 #define SHOW_st_size 'z'
172 #define SHOW_st_blocks 'b'
173 #define SHOW_st_blksize 'k'
174 #define SHOW_st_flags 'f'
175 #define SHOW_st_gen 'v'
176 #define SHOW_symlink 'Y'
177 #define SHOW_filetype 'T'
178 #define SHOW_filename 'N'
179 #define SHOW_sizerdev 'Z'
181 void usage(const char *);
182 void output(const struct stat *, const char *,
183 const char *, int, int);
184 int format1(const struct stat *, /* stat info */
185 const char *, /* the file name */
186 const char *, int, /* the format string itself */
187 char *, size_t, /* a place to put the output */
188 int, int, int, int, /* the parsed format */
190 int hex2byte(const char [2]);
191 #if HAVE_STRUCT_STAT_ST_FLAGS
192 char *xfflagstostr(unsigned long);
195 static const char *timefmt;
198 #define addchar(s, c, nl) \
200 (void)fputc((c), (s)); \
201 (*nl) = ((c) == '\n'); \
202 } while (0/*CONSTCOND*/)
205 main(int argc, char *argv[])
208 int ch, rc, errs, am_readlink;
209 int lsF, fmtchar, usestat, nfs_handle, fn, nonl, quiet;
210 const char *statfmt, *options, *synopsis;
211 char dname[sizeof _PATH_DEV + SPECNAMELEN] = _PATH_DEV;
226 if (strcmp(getprogname(), "readlink") == 0) {
229 synopsis = "[-fn] [file ...]";
234 options = "f:FHlLnqrst:x";
235 synopsis = "[-FLnq] [-f format | -l | -r | -s | -x] "
236 "[-t timefmt] [file|handle ...]";
239 while ((ch = getopt(argc, argv, options)) != -1)
268 errx(1, "can't use format '%c' with '%c'",
283 if (fmtchar == '\0') {
288 statfmt = DEF_FORMAT;
292 if (lsF && fmtchar != 'l')
293 errx(1, "can't use format '%c' with -F", fmtchar);
297 /* statfmt already set */
300 statfmt = lsF ? LSF_FORMAT : LS_FORMAT;
303 statfmt = RAW_FORMAT;
306 statfmt = SHELL_FORMAT;
309 statfmt = LINUX_FORMAT;
319 timefmt = TIME_FORMAT;
324 if (fdevname_r(STDIN_FILENO, dname +
325 sizeof _PATH_DEV - 1, SPECNAMELEN) != NULL)
329 rc = fstat(STDIN_FILENO, &st);
336 bzero(&fhnd, sizeof(fhnd));
337 j = MIN(2 * sizeof(fhnd), strlen(file));
342 rc = hex2byte(&file[j - 2]);
345 ((char*) &fhnd)[j / 2 - 1] = rc;
352 rc = fhstat(&fhnd, &st);
354 } else if (usestat) {
356 * Try stat() and if it fails, fall back to
357 * lstat() just in case we're examining a
360 if ((rc = stat(file, &st)) == -1 &&
362 (rc = lstat(file, &st)) == -1)
366 rc = lstat(file, &st);
376 output(&st, file, statfmt, fn, nonl);
383 return (am_readlink ? linkfail : errs);
386 #if HAVE_STRUCT_STAT_ST_FLAGS
388 * fflagstostr() wrapper that leaks only once
391 xfflagstostr(unsigned long fflags)
393 static char *str = NULL;
398 str = fflagstostr(fflags);
400 err(1, "fflagstostr");
403 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
406 usage(const char *synopsis)
409 (void)fprintf(stderr, "usage: %s %s\n", getprogname(), synopsis);
414 * Parses a format string.
417 output(const struct stat *st, const char *file,
418 const char *statfmt, int fn, int nonl)
420 int flags, size, prec, ofmt, hilo, what;
421 char buf[PATH_MAX + 4 + 1];
426 while (*statfmt != '\0') {
429 * Non-format characters go straight out.
431 if (*statfmt != FMT_MAGIC) {
432 addchar(stdout, *statfmt, &nl);
438 * The current format "substring" starts here,
439 * and then we skip the magic.
445 * Some simple one-character "formats".
449 addchar(stdout, '\n', &nl);
453 addchar(stdout, '\t', &nl);
457 addchar(stdout, '%', &nl);
460 case SIMPLE_NUMBER: {
463 snprintf(num, sizeof(num), "%d", fn);
464 for (p = &num[0]; *p; p++)
465 addchar(stdout, *p, &nl);
472 * This must be an actual format string. Format strings are
473 * similar to printf(3) formats up to a point, and are of
476 * % required start of format
477 * [-# +0] opt. format characters
478 * size opt. field width
479 * . opt. decimal separator, followed by
480 * prec opt. precision
481 * fmt opt. output specifier (string, numeric, etc.)
482 * sub opt. sub field specifier (high, middle, low)
483 * datum required field specifier (size, mode, etc)
485 * Only the % and the datum selector are required. All data
486 * have reasonable default output forms. The "sub" specifier
487 * only applies to certain data (mode, dev, rdev, filetype).
488 * The symlink output defaults to STRING, yet will only emit
489 * the leading " -> " if STRING is explicitly specified. The
490 * sizerdev datum will generate rdev output for character or
491 * block devices, and size output for all others.
495 if (*statfmt == FMT_POUND)
497 else if (*statfmt == FMT_SPACE)
499 else if (*statfmt == FMT_PLUS)
501 else if (*statfmt == FMT_ZERO)
503 else if (*statfmt == FMT_MINUS)
508 } while (1/*CONSTCOND*/);
511 if (isdigit((unsigned)*statfmt)) {
513 while (isdigit((unsigned)*statfmt)) {
514 size = (size * 10) + (*statfmt - '0');
522 if (*statfmt == FMT_DOT) {
526 while (isdigit((unsigned)*statfmt)) {
527 prec = (prec * 10) + (*statfmt - '0');
534 #define fmtcase(x, y) case (y): (x) = (y); statfmt++; break
535 #define fmtcasef(x, y, z) case (y): (x) = (z); statfmt++; break
537 fmtcasef(ofmt, FMT_DECIMAL, FMTF_DECIMAL);
538 fmtcasef(ofmt, FMT_OCTAL, FMTF_OCTAL);
539 fmtcasef(ofmt, FMT_UNSIGNED, FMTF_UNSIGNED);
540 fmtcasef(ofmt, FMT_HEX, FMTF_HEX);
541 fmtcasef(ofmt, FMT_FLOAT, FMTF_FLOAT);
542 fmtcasef(ofmt, FMT_STRING, FMTF_STRING);
549 fmtcase(hilo, HIGH_PIECE);
550 fmtcase(hilo, MIDDLE_PIECE);
551 fmtcase(hilo, LOW_PIECE);
558 fmtcase(what, SHOW_realpath);
559 fmtcase(what, SHOW_st_dev);
560 fmtcase(what, SHOW_st_ino);
561 fmtcase(what, SHOW_st_mode);
562 fmtcase(what, SHOW_st_nlink);
563 fmtcase(what, SHOW_st_uid);
564 fmtcase(what, SHOW_st_gid);
565 fmtcase(what, SHOW_st_rdev);
566 fmtcase(what, SHOW_st_atime);
567 fmtcase(what, SHOW_st_mtime);
568 fmtcase(what, SHOW_st_ctime);
569 fmtcase(what, SHOW_st_btime);
570 fmtcase(what, SHOW_st_size);
571 fmtcase(what, SHOW_st_blocks);
572 fmtcase(what, SHOW_st_blksize);
573 fmtcase(what, SHOW_st_flags);
574 fmtcase(what, SHOW_st_gen);
575 fmtcase(what, SHOW_symlink);
576 fmtcase(what, SHOW_filetype);
577 fmtcase(what, SHOW_filename);
578 fmtcase(what, SHOW_sizerdev);
587 subfmt, statfmt - subfmt,
589 flags, size, prec, ofmt, hilo, what);
591 for (i = 0; i < t && i < (int)(sizeof(buf) - 1); i++)
592 addchar(stdout, buf[i], &nl);
597 errx(1, "%.*s: bad format",
598 (int)(statfmt - subfmt + 1), subfmt);
602 (void)fputc('\n', stdout);
603 (void)fflush(stdout);
607 * Arranges output according to a single parsed format substring.
610 format1(const struct stat *st,
612 const char *fmt, int flen,
613 char *buf, size_t blen,
614 int flags, int size, int prec, int ofmt,
618 char *stmp, lfmt[24], tmp[20];
620 char smode[12], sid[12], path[PATH_MAX + 4];
621 const struct timespec *tsp;
624 int l, small, formats;
631 * First, pick out the data and tweak it based on hilo or
632 * specified output format (symlink output only).
637 small = (sizeof(st->st_dev) == 4);
638 data = (what == SHOW_st_dev) ? st->st_dev : st->st_rdev;
640 sdata = devname(what == SHOW_st_dev ? st->st_dev :
641 st->st_rdev, S_ISCHR(st->st_mode) ? S_IFCHR :
642 (S_ISBLK(st->st_mode) ? S_IFBLK : 0));
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 sdata = user_from_uid(st->st_uid, 1);
714 snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_uid);
717 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
720 ofmt = FMTF_UNSIGNED;
723 small = (sizeof(st->st_gid) == 4);
725 sdata = group_from_gid(st->st_gid, 1);
727 snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_gid);
730 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
733 ofmt = FMTF_UNSIGNED;
736 tsp = &st->st_atimespec;
740 tsp = &st->st_mtimespec;
744 tsp = &st->st_ctimespec;
746 #if HAVE_STRUCT_STAT_ST_BIRTHTIME
749 tsp = &st->st_birthtimespec;
750 #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
751 ts = *tsp; /* copy so we can muck with it */
752 small = (sizeof(ts.tv_sec) == 4);
754 tm = localtime(&ts.tv_sec);
757 tm = localtime(&ts.tv_sec);
759 (void)setlocale(LC_TIME, "");
760 (void)strftime(path, sizeof(path), timefmt, tm);
762 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
763 FMTF_FLOAT | FMTF_STRING;
768 small = (sizeof(st->st_size) == 4);
771 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
773 ofmt = FMTF_UNSIGNED;
776 small = (sizeof(st->st_blocks) == 4);
777 data = st->st_blocks;
779 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
781 ofmt = FMTF_UNSIGNED;
783 case SHOW_st_blksize:
784 small = (sizeof(st->st_blksize) == 4);
785 data = st->st_blksize;
787 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
789 ofmt = FMTF_UNSIGNED;
791 #if HAVE_STRUCT_STAT_ST_FLAGS
793 small = (sizeof(st->st_flags) == 4);
795 sdata = xfflagstostr(st->st_flags);
798 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
801 ofmt = FMTF_UNSIGNED;
803 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
804 #if HAVE_STRUCT_STAT_ST_GEN
806 small = (sizeof(st->st_gen) == 4);
809 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
811 ofmt = FMTF_UNSIGNED;
813 #endif /* HAVE_STRUCT_STAT_ST_GEN */
818 (void)strlcpy(path, "(stdin)", sizeof(path));
821 snprintf(path, sizeof(path), " -> ");
822 if (realpath(file, path + 4) == NULL) {
827 sdata = path + (ofmt == FMTF_STRING ? 0 : 4);
830 formats = FMTF_STRING;
837 if (S_ISLNK(st->st_mode)) {
838 snprintf(path, sizeof(path), " -> ");
839 l = readlink(file, path + 4, sizeof(path) - 4 - 1);
846 sdata = path + (ofmt == FMTF_STRING ? 0 : 4);
852 formats = FMTF_STRING;
860 if (hilo == 0 || hilo == LOW_PIECE) {
861 switch (st->st_mode & S_IFMT) {
862 case S_IFIFO: sdata = "|"; break;
863 case S_IFDIR: sdata = "/"; break;
866 (S_IXUSR | S_IXGRP | S_IXOTH))
869 case S_IFLNK: sdata = "@"; break;
870 case S_IFSOCK: sdata = "="; break;
872 case S_IFWHT: sdata = "%"; break;
875 case S_IFDOOR: sdata = ">"; break;
876 #endif /* S_IFDOOR */
880 else if (hilo == HIGH_PIECE) {
881 switch (st->st_mode & S_IFMT) {
882 case S_IFIFO: sdata = "Fifo File"; break;
883 case S_IFCHR: sdata = "Character Device"; break;
884 case S_IFDIR: sdata = "Directory"; break;
885 case S_IFBLK: sdata = "Block Device"; break;
886 case S_IFREG: sdata = "Regular File"; break;
887 case S_IFLNK: sdata = "Symbolic Link"; break;
888 case S_IFSOCK: sdata = "Socket"; break;
890 case S_IFWHT: sdata = "Whiteout File"; break;
893 case S_IFDOOR: sdata = "Door"; break;
894 #endif /* S_IFDOOR */
895 default: sdata = "???"; break;
899 formats = FMTF_STRING;
906 (void)strlcpy(path, file, sizeof(path));
908 formats = FMTF_STRING;
913 if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
914 char majdev[20], mindev[20];
920 majdev, sizeof(majdev),
922 ofmt, HIGH_PIECE, SHOW_st_rdev);
926 mindev, sizeof(mindev),
928 ofmt, LOW_PIECE, SHOW_st_rdev);
929 return (snprintf(buf, blen, "%.*s,%.*s",
930 l1, majdev, l2, mindev));
938 ofmt, 0, SHOW_st_size));
942 errx(1, "%.*s: bad format", (int)flen, fmt);
946 * If a subdatum was specified but not supported, or an output
947 * format was selected that is not supported, that's an error.
949 if (hilo != 0 || (ofmt & formats) == 0)
950 errx(1, "%.*s: bad format", (int)flen, fmt);
953 * Assemble the format string for passing to printf(3).
956 (void)strcat(lfmt, "%");
957 if (flags & FLAG_POUND)
958 (void)strcat(lfmt, "#");
959 if (flags & FLAG_SPACE)
960 (void)strcat(lfmt, " ");
961 if (flags & FLAG_PLUS)
962 (void)strcat(lfmt, "+");
963 if (flags & FLAG_MINUS)
964 (void)strcat(lfmt, "-");
965 if (flags & FLAG_ZERO)
966 (void)strcat(lfmt, "0");
969 * Only the timespecs support the FLOAT output format, and that
970 * requires work that differs from the other formats.
972 if (ofmt == FMTF_FLOAT) {
974 * Nothing after the decimal point, so just print seconds.
978 (void)snprintf(tmp, sizeof(tmp), "%d", size);
979 (void)strcat(lfmt, tmp);
981 (void)strcat(lfmt, "lld");
982 return (snprintf(buf, blen, lfmt,
983 (long long)ts.tv_sec));
987 * Unspecified precision gets all the precision we have:
994 * Adjust the size for the decimal point and the digits
1000 * Any leftover size that's legitimate will be used.
1003 (void)snprintf(tmp, sizeof(tmp), "%d", size);
1004 (void)strcat(lfmt, tmp);
1006 /* Seconds: time_t cast to long long. */
1007 (void)strcat(lfmt, "lld");
1010 * The stuff after the decimal point always needs zero
1013 (void)strcat(lfmt, ".%0");
1016 * We can "print" at most nine digits of precision. The
1017 * rest we will pad on at the end.
1019 * Nanoseconds: long.
1021 (void)snprintf(tmp, sizeof(tmp), "%dld", MIN(prec, 9));
1022 (void)strcat(lfmt, tmp);
1025 * For precision of less than nine digits, trim off the
1026 * less significant figures.
1028 for (; prec < 9; prec++)
1032 * Use the format, and then tack on any zeroes that
1033 * might be required to make up the requested precision.
1035 l = snprintf(buf, blen, lfmt, (long long)ts.tv_sec, ts.tv_nsec);
1036 for (; prec > 9 && l < (int)blen; prec--, l++)
1037 (void)strcat(buf, "0");
1042 * Add on size and precision, if specified, to the format.
1045 (void)snprintf(tmp, sizeof(tmp), "%d", size);
1046 (void)strcat(lfmt, tmp);
1049 (void)snprintf(tmp, sizeof(tmp), ".%d", prec);
1050 (void)strcat(lfmt, tmp);
1054 * String output uses the temporary sdata.
1056 if (ofmt == FMTF_STRING) {
1058 errx(1, "%.*s: bad format", (int)flen, fmt);
1059 (void)strcat(lfmt, "s");
1060 return (snprintf(buf, blen, lfmt, sdata));
1064 * Ensure that sign extension does not cause bad looking output
1067 if (small && ofmt != FMTF_DECIMAL)
1068 data = (u_int32_t)data;
1071 * The four "numeric" output forms.
1073 (void)strcat(lfmt, "ll");
1075 case FMTF_DECIMAL: (void)strcat(lfmt, "d"); break;
1076 case FMTF_OCTAL: (void)strcat(lfmt, "o"); break;
1077 case FMTF_UNSIGNED: (void)strcat(lfmt, "u"); break;
1078 case FMTF_HEX: (void)strcat(lfmt, "x"); break;
1081 return (snprintf(buf, blen, lfmt, data));
1085 #define hex2nibble(c) (c <= '9' ? c - '0' : toupper(c) - 'A' + 10)
1087 hex2byte(const char c[2]) {
1088 if (!(ishexnumber(c[0]) && ishexnumber(c[1])))
1090 return (hex2nibble(c[0]) << 4) + hex2nibble(c[1]);