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>
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);
376 warn("%s: stat", file);
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 = (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 sdata = user_from_uid(st->st_uid, 1);
722 snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_uid);
725 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
728 ofmt = FMTF_UNSIGNED;
731 small = (sizeof(st->st_gid) == 4);
733 sdata = group_from_gid(st->st_gid, 1);
735 snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_gid);
738 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
741 ofmt = FMTF_UNSIGNED;
744 tsp = &st->st_atimespec;
748 tsp = &st->st_mtimespec;
752 tsp = &st->st_ctimespec;
754 #if HAVE_STRUCT_STAT_ST_BIRTHTIME
757 tsp = &st->st_birthtimespec;
758 #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
759 ts = *tsp; /* copy so we can muck with it */
760 small = (sizeof(ts.tv_sec) == 4);
762 tm = localtime(&ts.tv_sec);
765 tm = localtime(&ts.tv_sec);
767 (void)setlocale(LC_TIME, "");
768 (void)strftime(path, sizeof(path), timefmt, tm);
770 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
771 FMTF_FLOAT | FMTF_STRING;
776 small = (sizeof(st->st_size) == 4);
779 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
781 ofmt = FMTF_UNSIGNED;
784 small = (sizeof(st->st_blocks) == 4);
785 data = st->st_blocks;
787 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
789 ofmt = FMTF_UNSIGNED;
791 case SHOW_st_blksize:
792 small = (sizeof(st->st_blksize) == 4);
793 data = st->st_blksize;
795 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
797 ofmt = FMTF_UNSIGNED;
799 #if HAVE_STRUCT_STAT_ST_FLAGS
801 small = (sizeof(st->st_flags) == 4);
803 sdata = xfflagstostr(st->st_flags);
806 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
809 ofmt = FMTF_UNSIGNED;
811 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
812 #if HAVE_STRUCT_STAT_ST_GEN
814 small = (sizeof(st->st_gen) == 4);
817 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
819 ofmt = FMTF_UNSIGNED;
821 #endif /* HAVE_STRUCT_STAT_ST_GEN */
826 (void)strlcpy(path, "(stdin)", sizeof(path));
829 snprintf(path, sizeof(path), " -> ");
830 if (realpath(file, path + 4) == NULL) {
835 sdata = path + (ofmt == FMTF_STRING ? 0 : 4);
838 formats = FMTF_STRING;
845 if (S_ISLNK(st->st_mode)) {
846 snprintf(path, sizeof(path), " -> ");
847 l = readlink(file, path + 4, sizeof(path) - 4 - 1);
854 sdata = path + (ofmt == FMTF_STRING ? 0 : 4);
860 formats = FMTF_STRING;
868 if (hilo == 0 || hilo == LOW_PIECE) {
869 switch (st->st_mode & S_IFMT) {
870 case S_IFIFO: sdata = "|"; break;
871 case S_IFDIR: sdata = "/"; break;
874 (S_IXUSR | S_IXGRP | S_IXOTH))
877 case S_IFLNK: sdata = "@"; break;
878 case S_IFSOCK: sdata = "="; break;
880 case S_IFWHT: sdata = "%"; break;
883 case S_IFDOOR: sdata = ">"; break;
884 #endif /* S_IFDOOR */
888 else if (hilo == HIGH_PIECE) {
889 switch (st->st_mode & S_IFMT) {
890 case S_IFIFO: sdata = "Fifo File"; break;
891 case S_IFCHR: sdata = "Character Device"; break;
892 case S_IFDIR: sdata = "Directory"; break;
893 case S_IFBLK: sdata = "Block Device"; break;
894 case S_IFREG: sdata = "Regular File"; break;
895 case S_IFLNK: sdata = "Symbolic Link"; break;
896 case S_IFSOCK: sdata = "Socket"; break;
898 case S_IFWHT: sdata = "Whiteout File"; break;
901 case S_IFDOOR: sdata = "Door"; break;
902 #endif /* S_IFDOOR */
903 default: sdata = "???"; break;
907 formats = FMTF_STRING;
914 (void)strlcpy(path, file, sizeof(path));
916 formats = FMTF_STRING;
921 if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
922 char majdev[20], mindev[20];
928 majdev, sizeof(majdev),
930 ofmt, HIGH_PIECE, SHOW_st_rdev);
934 mindev, sizeof(mindev),
936 ofmt, LOW_PIECE, SHOW_st_rdev);
937 return (snprintf(buf, blen, "%.*s,%.*s",
938 l1, majdev, l2, mindev));
946 ofmt, 0, SHOW_st_size));
950 errx(1, "%.*s: bad format", (int)flen, fmt);
954 * If a subdatum was specified but not supported, or an output
955 * format was selected that is not supported, that's an error.
957 if (hilo != 0 || (ofmt & formats) == 0)
958 errx(1, "%.*s: bad format", (int)flen, fmt);
961 * Assemble the format string for passing to printf(3).
964 (void)strcat(lfmt, "%");
965 if (flags & FLAG_POUND)
966 (void)strcat(lfmt, "#");
967 if (flags & FLAG_SPACE)
968 (void)strcat(lfmt, " ");
969 if (flags & FLAG_PLUS)
970 (void)strcat(lfmt, "+");
971 if (flags & FLAG_MINUS)
972 (void)strcat(lfmt, "-");
973 if (flags & FLAG_ZERO)
974 (void)strcat(lfmt, "0");
977 * Only the timespecs support the FLOAT output format, and that
978 * requires work that differs from the other formats.
980 if (ofmt == FMTF_FLOAT) {
982 * Nothing after the decimal point, so just print seconds.
986 (void)snprintf(tmp, sizeof(tmp), "%d", size);
987 (void)strcat(lfmt, tmp);
989 (void)strcat(lfmt, "lld");
990 return (snprintf(buf, blen, lfmt,
991 (long long)ts.tv_sec));
995 * Unspecified precision gets all the precision we have:
1002 * Adjust the size for the decimal point and the digits
1008 * Any leftover size that's legitimate will be used.
1011 (void)snprintf(tmp, sizeof(tmp), "%d", size);
1012 (void)strcat(lfmt, tmp);
1014 /* Seconds: time_t cast to long long. */
1015 (void)strcat(lfmt, "lld");
1018 * The stuff after the decimal point always needs zero
1021 (void)strcat(lfmt, ".%0");
1024 * We can "print" at most nine digits of precision. The
1025 * rest we will pad on at the end.
1027 * Nanoseconds: long.
1029 (void)snprintf(tmp, sizeof(tmp), "%dld", MIN(prec, 9));
1030 (void)strcat(lfmt, tmp);
1033 * For precision of less that nine digits, trim off the
1034 * less significant figures.
1036 for (; prec < 9; prec++)
1040 * Use the format, and then tack on any zeroes that
1041 * might be required to make up the requested precision.
1043 l = snprintf(buf, blen, lfmt, (long long)ts.tv_sec, ts.tv_nsec);
1044 for (; prec > 9 && l < (int)blen; prec--, l++)
1045 (void)strcat(buf, "0");
1050 * Add on size and precision, if specified, to the format.
1053 (void)snprintf(tmp, sizeof(tmp), "%d", size);
1054 (void)strcat(lfmt, tmp);
1057 (void)snprintf(tmp, sizeof(tmp), ".%d", prec);
1058 (void)strcat(lfmt, tmp);
1062 * String output uses the temporary sdata.
1064 if (ofmt == FMTF_STRING) {
1066 errx(1, "%.*s: bad format", (int)flen, fmt);
1067 (void)strcat(lfmt, "s");
1068 return (snprintf(buf, blen, lfmt, sdata));
1072 * Ensure that sign extension does not cause bad looking output
1075 if (small && ofmt != FMTF_DECIMAL)
1076 data = (u_int32_t)data;
1079 * The four "numeric" output forms.
1081 (void)strcat(lfmt, "ll");
1083 case FMTF_DECIMAL: (void)strcat(lfmt, "d"); break;
1084 case FMTF_OCTAL: (void)strcat(lfmt, "o"); break;
1085 case FMTF_UNSIGNED: (void)strcat(lfmt, "u"); break;
1086 case FMTF_HEX: (void)strcat(lfmt, "x"); break;
1089 return (snprintf(buf, blen, lfmt, data));
1093 #define hex2nibble(c) (c <= '9' ? c - '0' : toupper(c) - 'A' + 10)
1095 hex2byte(const char c[2]) {
1096 if (!(ishexnumber(c[0]) && ishexnumber(c[1])))
1098 return (hex2nibble(c[0]) << 4) + hex2nibble(c[1]);