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" \
118 #define TIME_FORMAT "%b %e %T %Y"
120 #define FLAG_POUND 0x01
121 #define FLAG_SPACE 0x02
122 #define FLAG_PLUS 0x04
123 #define FLAG_ZERO 0x08
124 #define FLAG_MINUS 0x10
127 * These format characters must all be unique, except the magic one.
129 #define FMT_MAGIC '%'
132 #define SIMPLE_NEWLINE 'n'
133 #define SIMPLE_TAB 't'
134 #define SIMPLE_PERCENT '%'
135 #define SIMPLE_NUMBER '@'
137 #define FMT_POUND '#'
138 #define FMT_SPACE ' '
141 #define FMT_MINUS '-'
143 #define FMT_DECIMAL 'D'
144 #define FMT_OCTAL 'O'
145 #define FMT_UNSIGNED 'U'
147 #define FMT_FLOAT 'F'
148 #define FMT_STRING 'S'
150 #define FMTF_DECIMAL 0x01
151 #define FMTF_OCTAL 0x02
152 #define FMTF_UNSIGNED 0x04
153 #define FMTF_HEX 0x08
154 #define FMTF_FLOAT 0x10
155 #define FMTF_STRING 0x20
157 #define HIGH_PIECE 'H'
158 #define MIDDLE_PIECE 'M'
159 #define LOW_PIECE 'L'
161 #define SHOW_realpath 'R'
162 #define SHOW_st_dev 'd'
163 #define SHOW_st_ino 'i'
164 #define SHOW_st_mode 'p'
165 #define SHOW_st_nlink 'l'
166 #define SHOW_st_uid 'u'
167 #define SHOW_st_gid 'g'
168 #define SHOW_st_rdev 'r'
169 #define SHOW_st_atime 'a'
170 #define SHOW_st_mtime 'm'
171 #define SHOW_st_ctime 'c'
172 #define SHOW_st_btime 'B'
173 #define SHOW_st_size 'z'
174 #define SHOW_st_blocks 'b'
175 #define SHOW_st_blksize 'k'
176 #define SHOW_st_flags 'f'
177 #define SHOW_st_gen 'v'
178 #define SHOW_symlink 'Y'
179 #define SHOW_filetype 'T'
180 #define SHOW_filename 'N'
181 #define SHOW_sizerdev 'Z'
183 void usage(const char *);
184 void output(const struct stat *, const char *,
185 const char *, int, int);
186 int format1(const struct stat *, /* stat info */
187 const char *, /* the file name */
188 const char *, int, /* the format string itself */
189 char *, size_t, /* a place to put the output */
190 int, int, int, int, /* the parsed format */
192 int hex2byte(const char [2]);
193 #if HAVE_STRUCT_STAT_ST_FLAGS
194 char *xfflagstostr(unsigned long);
197 static const char *timefmt;
200 #define addchar(s, c, nl) \
202 (void)fputc((c), (s)); \
203 (*nl) = ((c) == '\n'); \
204 } while (0/*CONSTCOND*/)
207 main(int argc, char *argv[])
210 int ch, rc, errs, am_readlink;
211 int lsF, fmtchar, usestat, nfs_handle, fn, nonl, quiet;
212 const char *statfmt, *options, *synopsis;
213 char dname[sizeof _PATH_DEV + SPECNAMELEN] = _PATH_DEV;
228 if (strcmp(getprogname(), "readlink") == 0) {
231 synopsis = "[-fn] [file ...]";
236 options = "f:FHlLnqrst:x";
237 synopsis = "[-FLnq] [-f format | -l | -r | -s | -x] "
238 "[-t timefmt] [file|handle ...]";
241 while ((ch = getopt(argc, argv, options)) != -1)
270 errx(1, "can't use format '%c' with '%c'",
285 if (fmtchar == '\0') {
290 statfmt = DEF_FORMAT;
294 if (lsF && fmtchar != 'l')
295 errx(1, "can't use format '%c' with -F", fmtchar);
299 /* statfmt already set */
302 statfmt = lsF ? LSF_FORMAT : LS_FORMAT;
305 statfmt = RAW_FORMAT;
308 statfmt = SHELL_FORMAT;
311 statfmt = LINUX_FORMAT;
321 timefmt = TIME_FORMAT;
326 if (fdevname_r(STDIN_FILENO, dname +
327 sizeof _PATH_DEV - 1, SPECNAMELEN) != NULL)
331 rc = fstat(STDIN_FILENO, &st);
338 bzero(&fhnd, sizeof(fhnd));
339 j = MIN(2 * sizeof(fhnd), strlen(file));
344 rc = hex2byte(&file[j - 2]);
347 ((char*) &fhnd)[j / 2 - 1] = rc;
354 rc = fhstat(&fhnd, &st);
356 } else if (usestat) {
358 * Try stat() and if it fails, fall back to
359 * lstat() just in case we're examining a
362 if ((rc = stat(file, &st)) == -1 &&
364 (rc = lstat(file, &st)) == -1)
368 rc = lstat(file, &st);
375 warn("%s: stat", file);
378 output(&st, file, statfmt, fn, nonl);
385 return (am_readlink ? linkfail : errs);
388 #if HAVE_STRUCT_STAT_ST_FLAGS
390 * fflagstostr() wrapper that leaks only once
393 xfflagstostr(unsigned long fflags)
395 static char *str = NULL;
400 str = fflagstostr(fflags);
402 err(1, "fflagstostr");
405 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
408 usage(const char *synopsis)
411 (void)fprintf(stderr, "usage: %s %s\n", getprogname(), synopsis);
416 * Parses a format string.
419 output(const struct stat *st, const char *file,
420 const char *statfmt, int fn, int nonl)
422 int flags, size, prec, ofmt, hilo, what;
423 char buf[PATH_MAX + 4 + 1];
428 while (*statfmt != '\0') {
431 * Non-format characters go straight out.
433 if (*statfmt != FMT_MAGIC) {
434 addchar(stdout, *statfmt, &nl);
440 * The current format "substring" starts here,
441 * and then we skip the magic.
447 * Some simple one-character "formats".
451 addchar(stdout, '\n', &nl);
455 addchar(stdout, '\t', &nl);
459 addchar(stdout, '%', &nl);
462 case SIMPLE_NUMBER: {
465 snprintf(num, sizeof(num), "%d", fn);
466 for (p = &num[0]; *p; p++)
467 addchar(stdout, *p, &nl);
474 * This must be an actual format string. Format strings are
475 * similar to printf(3) formats up to a point, and are of
478 * % required start of format
479 * [-# +0] opt. format characters
480 * size opt. field width
481 * . opt. decimal separator, followed by
482 * prec opt. precision
483 * fmt opt. output specifier (string, numeric, etc.)
484 * sub opt. sub field specifier (high, middle, low)
485 * datum required field specifier (size, mode, etc)
487 * Only the % and the datum selector are required. All data
488 * have reasonable default output forms. The "sub" specifier
489 * only applies to certain data (mode, dev, rdev, filetype).
490 * The symlink output defaults to STRING, yet will only emit
491 * the leading " -> " if STRING is explicitly specified. The
492 * sizerdev datum will generate rdev output for character or
493 * block devices, and size output for all others.
497 if (*statfmt == FMT_POUND)
499 else if (*statfmt == FMT_SPACE)
501 else if (*statfmt == FMT_PLUS)
503 else if (*statfmt == FMT_ZERO)
505 else if (*statfmt == FMT_MINUS)
510 } while (1/*CONSTCOND*/);
513 if (isdigit((unsigned)*statfmt)) {
515 while (isdigit((unsigned)*statfmt)) {
516 size = (size * 10) + (*statfmt - '0');
524 if (*statfmt == FMT_DOT) {
528 while (isdigit((unsigned)*statfmt)) {
529 prec = (prec * 10) + (*statfmt - '0');
536 #define fmtcase(x, y) case (y): (x) = (y); statfmt++; break
537 #define fmtcasef(x, y, z) case (y): (x) = (z); statfmt++; break
539 fmtcasef(ofmt, FMT_DECIMAL, FMTF_DECIMAL);
540 fmtcasef(ofmt, FMT_OCTAL, FMTF_OCTAL);
541 fmtcasef(ofmt, FMT_UNSIGNED, FMTF_UNSIGNED);
542 fmtcasef(ofmt, FMT_HEX, FMTF_HEX);
543 fmtcasef(ofmt, FMT_FLOAT, FMTF_FLOAT);
544 fmtcasef(ofmt, FMT_STRING, FMTF_STRING);
551 fmtcase(hilo, HIGH_PIECE);
552 fmtcase(hilo, MIDDLE_PIECE);
553 fmtcase(hilo, LOW_PIECE);
560 fmtcase(what, SHOW_realpath);
561 fmtcase(what, SHOW_st_dev);
562 fmtcase(what, SHOW_st_ino);
563 fmtcase(what, SHOW_st_mode);
564 fmtcase(what, SHOW_st_nlink);
565 fmtcase(what, SHOW_st_uid);
566 fmtcase(what, SHOW_st_gid);
567 fmtcase(what, SHOW_st_rdev);
568 fmtcase(what, SHOW_st_atime);
569 fmtcase(what, SHOW_st_mtime);
570 fmtcase(what, SHOW_st_ctime);
571 fmtcase(what, SHOW_st_btime);
572 fmtcase(what, SHOW_st_size);
573 fmtcase(what, SHOW_st_blocks);
574 fmtcase(what, SHOW_st_blksize);
575 fmtcase(what, SHOW_st_flags);
576 fmtcase(what, SHOW_st_gen);
577 fmtcase(what, SHOW_symlink);
578 fmtcase(what, SHOW_filetype);
579 fmtcase(what, SHOW_filename);
580 fmtcase(what, SHOW_sizerdev);
589 subfmt, statfmt - subfmt,
591 flags, size, prec, ofmt, hilo, what);
593 for (i = 0; i < t && i < (int)(sizeof(buf) - 1); i++)
594 addchar(stdout, buf[i], &nl);
599 errx(1, "%.*s: bad format",
600 (int)(statfmt - subfmt + 1), subfmt);
604 (void)fputc('\n', stdout);
605 (void)fflush(stdout);
609 * Arranges output according to a single parsed format substring.
612 format1(const struct stat *st,
614 const char *fmt, int flen,
615 char *buf, size_t blen,
616 int flags, int size, int prec, int ofmt,
620 char *stmp, lfmt[24], tmp[20];
622 char smode[12], sid[12], path[PATH_MAX + 4];
623 const struct timespec *tsp;
626 int l, small, formats;
633 * First, pick out the data and tweak it based on hilo or
634 * specified output format (symlink output only).
639 small = (sizeof(st->st_dev) == 4);
640 data = (what == SHOW_st_dev) ? st->st_dev : st->st_rdev;
642 sdata = (what == SHOW_st_dev) ?
643 devname(st->st_dev, S_IFBLK) :
645 S_ISCHR(st->st_mode) ? S_IFCHR :
646 S_ISBLK(st->st_mode) ? S_IFBLK :
650 #endif /* HAVE_DEVNAME */
651 if (hilo == HIGH_PIECE) {
655 else if (hilo == LOW_PIECE) {
656 data = minor((unsigned)data);
659 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
662 #else /* HAVE_DEVNAME */
664 #endif /* HAVE_DEVNAME */
666 ofmt = FMTF_UNSIGNED;
669 small = (sizeof(st->st_ino) == 4);
672 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
674 ofmt = FMTF_UNSIGNED;
677 small = (sizeof(st->st_mode) == 4);
679 strmode(st->st_mode, smode);
682 if (stmp[l - 1] == ' ')
684 if (hilo == HIGH_PIECE) {
690 else if (hilo == MIDDLE_PIECE) {
691 data = (data >> 9) & 07;
696 else if (hilo == LOW_PIECE) {
703 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
709 small = (sizeof(st->st_dev) == 4);
712 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
714 ofmt = FMTF_UNSIGNED;
717 small = (sizeof(st->st_uid) == 4);
719 sdata = user_from_uid(st->st_uid, 1);
721 snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_uid);
724 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
727 ofmt = FMTF_UNSIGNED;
730 small = (sizeof(st->st_gid) == 4);
732 sdata = group_from_gid(st->st_gid, 1);
734 snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_gid);
737 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
740 ofmt = FMTF_UNSIGNED;
743 tsp = &st->st_atimespec;
747 tsp = &st->st_mtimespec;
751 tsp = &st->st_ctimespec;
753 #if HAVE_STRUCT_STAT_ST_BIRTHTIME
756 tsp = &st->st_birthtimespec;
757 #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
758 ts = *tsp; /* copy so we can muck with it */
759 small = (sizeof(ts.tv_sec) == 4);
761 tm = localtime(&ts.tv_sec);
764 tm = localtime(&ts.tv_sec);
766 (void)strftime(path, sizeof(path), timefmt, tm);
768 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
769 FMTF_FLOAT | FMTF_STRING;
774 small = (sizeof(st->st_size) == 4);
777 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
779 ofmt = FMTF_UNSIGNED;
782 small = (sizeof(st->st_blocks) == 4);
783 data = st->st_blocks;
785 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
787 ofmt = FMTF_UNSIGNED;
789 case SHOW_st_blksize:
790 small = (sizeof(st->st_blksize) == 4);
791 data = st->st_blksize;
793 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
795 ofmt = FMTF_UNSIGNED;
797 #if HAVE_STRUCT_STAT_ST_FLAGS
799 small = (sizeof(st->st_flags) == 4);
801 sdata = xfflagstostr(st->st_flags);
804 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
807 ofmt = FMTF_UNSIGNED;
809 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
810 #if HAVE_STRUCT_STAT_ST_GEN
812 small = (sizeof(st->st_gen) == 4);
815 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
817 ofmt = FMTF_UNSIGNED;
819 #endif /* HAVE_STRUCT_STAT_ST_GEN */
824 (void)strlcpy(path, "(stdin)", sizeof(path));
827 snprintf(path, sizeof(path), " -> ");
828 if (realpath(file, path + 4) == NULL) {
833 sdata = path + (ofmt == FMTF_STRING ? 0 : 4);
836 formats = FMTF_STRING;
843 if (S_ISLNK(st->st_mode)) {
844 snprintf(path, sizeof(path), " -> ");
845 l = readlink(file, path + 4, sizeof(path) - 4 - 1);
852 sdata = path + (ofmt == FMTF_STRING ? 0 : 4);
858 formats = FMTF_STRING;
866 if (hilo == 0 || hilo == LOW_PIECE) {
867 switch (st->st_mode & S_IFMT) {
868 case S_IFIFO: sdata = "|"; break;
869 case S_IFDIR: sdata = "/"; break;
872 (S_IXUSR | S_IXGRP | S_IXOTH))
875 case S_IFLNK: sdata = "@"; break;
876 case S_IFSOCK: sdata = "="; break;
878 case S_IFWHT: sdata = "%"; break;
881 case S_IFDOOR: sdata = ">"; break;
882 #endif /* S_IFDOOR */
886 else if (hilo == HIGH_PIECE) {
887 switch (st->st_mode & S_IFMT) {
888 case S_IFIFO: sdata = "Fifo File"; break;
889 case S_IFCHR: sdata = "Character Device"; break;
890 case S_IFDIR: sdata = "Directory"; break;
891 case S_IFBLK: sdata = "Block Device"; break;
892 case S_IFREG: sdata = "Regular File"; break;
893 case S_IFLNK: sdata = "Symbolic Link"; break;
894 case S_IFSOCK: sdata = "Socket"; break;
896 case S_IFWHT: sdata = "Whiteout File"; break;
899 case S_IFDOOR: sdata = "Door"; break;
900 #endif /* S_IFDOOR */
901 default: sdata = "???"; break;
905 formats = FMTF_STRING;
912 (void)strlcpy(path, file, sizeof(path));
914 formats = FMTF_STRING;
919 if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
920 char majdev[20], mindev[20];
926 majdev, sizeof(majdev),
928 ofmt, HIGH_PIECE, SHOW_st_rdev);
932 mindev, sizeof(mindev),
934 ofmt, LOW_PIECE, SHOW_st_rdev);
935 return (snprintf(buf, blen, "%.*s,%.*s",
936 l1, majdev, l2, mindev));
944 ofmt, 0, SHOW_st_size));
948 errx(1, "%.*s: bad format", (int)flen, fmt);
952 * If a subdatum was specified but not supported, or an output
953 * format was selected that is not supported, that's an error.
955 if (hilo != 0 || (ofmt & formats) == 0)
956 errx(1, "%.*s: bad format", (int)flen, fmt);
959 * Assemble the format string for passing to printf(3).
962 (void)strcat(lfmt, "%");
963 if (flags & FLAG_POUND)
964 (void)strcat(lfmt, "#");
965 if (flags & FLAG_SPACE)
966 (void)strcat(lfmt, " ");
967 if (flags & FLAG_PLUS)
968 (void)strcat(lfmt, "+");
969 if (flags & FLAG_MINUS)
970 (void)strcat(lfmt, "-");
971 if (flags & FLAG_ZERO)
972 (void)strcat(lfmt, "0");
975 * Only the timespecs support the FLOAT output format, and that
976 * requires work that differs from the other formats.
978 if (ofmt == FMTF_FLOAT) {
980 * Nothing after the decimal point, so just print seconds.
984 (void)snprintf(tmp, sizeof(tmp), "%d", size);
985 (void)strcat(lfmt, tmp);
987 (void)strcat(lfmt, "lld");
988 return (snprintf(buf, blen, lfmt,
989 (long long)ts.tv_sec));
993 * Unspecified precision gets all the precision we have:
1000 * Adjust the size for the decimal point and the digits
1006 * Any leftover size that's legitimate will be used.
1009 (void)snprintf(tmp, sizeof(tmp), "%d", size);
1010 (void)strcat(lfmt, tmp);
1012 /* Seconds: time_t cast to long long. */
1013 (void)strcat(lfmt, "lld");
1016 * The stuff after the decimal point always needs zero
1019 (void)strcat(lfmt, ".%0");
1022 * We can "print" at most nine digits of precision. The
1023 * rest we will pad on at the end.
1025 * Nanoseconds: long.
1027 (void)snprintf(tmp, sizeof(tmp), "%dld", MIN(prec, 9));
1028 (void)strcat(lfmt, tmp);
1031 * For precision of less that nine digits, trim off the
1032 * less significant figures.
1034 for (; prec < 9; prec++)
1038 * Use the format, and then tack on any zeroes that
1039 * might be required to make up the requested precision.
1041 l = snprintf(buf, blen, lfmt, (long long)ts.tv_sec, ts.tv_nsec);
1042 for (; prec > 9 && l < (int)blen; prec--, l++)
1043 (void)strcat(buf, "0");
1048 * Add on size and precision, if specified, to the format.
1051 (void)snprintf(tmp, sizeof(tmp), "%d", size);
1052 (void)strcat(lfmt, tmp);
1055 (void)snprintf(tmp, sizeof(tmp), ".%d", prec);
1056 (void)strcat(lfmt, tmp);
1060 * String output uses the temporary sdata.
1062 if (ofmt == FMTF_STRING) {
1064 errx(1, "%.*s: bad format", (int)flen, fmt);
1065 (void)strcat(lfmt, "s");
1066 return (snprintf(buf, blen, lfmt, sdata));
1070 * Ensure that sign extension does not cause bad looking output
1073 if (small && ofmt != FMTF_DECIMAL)
1074 data = (u_int32_t)data;
1077 * The four "numeric" output forms.
1079 (void)strcat(lfmt, "ll");
1081 case FMTF_DECIMAL: (void)strcat(lfmt, "d"); break;
1082 case FMTF_OCTAL: (void)strcat(lfmt, "o"); break;
1083 case FMTF_UNSIGNED: (void)strcat(lfmt, "u"); break;
1084 case FMTF_HEX: (void)strcat(lfmt, "x"); break;
1087 return (snprintf(buf, blen, lfmt, data));
1091 #define hex2nibble(c) (c <= '9' ? c - '0' : toupper(c) - 'A' + 10)
1093 hex2byte(const char c[2]) {
1094 if (!(ishexnumber(c[0]) && ishexnumber(c[1])))
1096 return (hex2nibble(c[0]) << 4) + hex2nibble(c[1]);