2 * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 * This code is derived from software contributed to The NetBSD Foundation
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/cdefs.h>
33 __RCSID("$NetBSD: stat.c,v 1.33 2011/01/15 22:54:10 njoly Exp $"
34 "$OpenBSD: stat.c,v 1.14 2009/06/24 09:44:25 sobrado Exp $");
38 __FBSDID("$FreeBSD$");
42 #else /* HAVE_CONFIG_H */
43 #define HAVE_STRUCT_STAT_ST_FLAGS 1
44 #define HAVE_STRUCT_STAT_ST_GEN 1
45 #define HAVE_STRUCT_STAT_ST_BIRTHTIME 1
46 #define HAVE_STRUCT_STAT_ST_MTIMENSEC 1
47 #define HAVE_DEVNAME 1
48 #endif /* HAVE_CONFIG_H */
50 #include <sys/param.h>
51 #include <sys/types.h>
53 #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" \
115 #define TIME_FORMAT "%b %e %T %Y"
117 #define FLAG_POUND 0x01
118 #define FLAG_SPACE 0x02
119 #define FLAG_PLUS 0x04
120 #define FLAG_ZERO 0x08
121 #define FLAG_MINUS 0x10
124 * These format characters must all be unique, except the magic one.
126 #define FMT_MAGIC '%'
129 #define SIMPLE_NEWLINE 'n'
130 #define SIMPLE_TAB 't'
131 #define SIMPLE_PERCENT '%'
132 #define SIMPLE_NUMBER '@'
134 #define FMT_POUND '#'
135 #define FMT_SPACE ' '
138 #define FMT_MINUS '-'
140 #define FMT_DECIMAL 'D'
141 #define FMT_OCTAL 'O'
142 #define FMT_UNSIGNED 'U'
144 #define FMT_FLOAT 'F'
145 #define FMT_STRING 'S'
147 #define FMTF_DECIMAL 0x01
148 #define FMTF_OCTAL 0x02
149 #define FMTF_UNSIGNED 0x04
150 #define FMTF_HEX 0x08
151 #define FMTF_FLOAT 0x10
152 #define FMTF_STRING 0x20
154 #define HIGH_PIECE 'H'
155 #define MIDDLE_PIECE 'M'
156 #define LOW_PIECE 'L'
158 #define SHOW_realpath 'R'
159 #define SHOW_st_dev 'd'
160 #define SHOW_st_ino 'i'
161 #define SHOW_st_mode 'p'
162 #define SHOW_st_nlink 'l'
163 #define SHOW_st_uid 'u'
164 #define SHOW_st_gid 'g'
165 #define SHOW_st_rdev 'r'
166 #define SHOW_st_atime 'a'
167 #define SHOW_st_mtime 'm'
168 #define SHOW_st_ctime 'c'
169 #define SHOW_st_btime 'B'
170 #define SHOW_st_size 'z'
171 #define SHOW_st_blocks 'b'
172 #define SHOW_st_blksize 'k'
173 #define SHOW_st_flags 'f'
174 #define SHOW_st_gen 'v'
175 #define SHOW_symlink 'Y'
176 #define SHOW_filetype 'T'
177 #define SHOW_filename 'N'
178 #define SHOW_sizerdev 'Z'
180 void usage(const char *);
181 void output(const struct stat *, const char *,
182 const char *, int, int);
183 int format1(const struct stat *, /* stat info */
184 const char *, /* the file name */
185 const char *, int, /* the format string itself */
186 char *, size_t, /* a place to put the output */
187 int, int, int, int, /* the parsed format */
189 int hex2byte(const char [2]);
190 #if HAVE_STRUCT_STAT_ST_FLAGS
191 char *xfflagstostr(unsigned long);
194 static const char *timefmt;
197 #define addchar(s, c, nl) \
199 (void)fputc((c), (s)); \
200 (*nl) = ((c) == '\n'); \
201 } while (0/*CONSTCOND*/)
204 main(int argc, char *argv[])
207 int ch, rc, errs, am_readlink;
208 int lsF, fmtchar, usestat, nfs_handle, fn, nonl, quiet;
209 const char *statfmt, *options, *synopsis;
210 char dname[sizeof _PATH_DEV + SPECNAMELEN] = _PATH_DEV;
225 if (strcmp(getprogname(), "readlink") == 0) {
228 synopsis = "[-fn] [file ...]";
233 options = "f:FHlLnqrst:x";
234 synopsis = "[-FLnq] [-f format | -l | -r | -s | -x] "
235 "[-t timefmt] [file|handle ...]";
238 while ((ch = getopt(argc, argv, options)) != -1)
267 errx(1, "can't use format '%c' with '%c'",
282 if (fmtchar == '\0') {
287 statfmt = DEF_FORMAT;
291 if (lsF && fmtchar != 'l')
292 errx(1, "can't use format '%c' with -F", fmtchar);
296 /* statfmt already set */
299 statfmt = lsF ? LSF_FORMAT : LS_FORMAT;
302 statfmt = RAW_FORMAT;
305 statfmt = SHELL_FORMAT;
308 statfmt = LINUX_FORMAT;
318 timefmt = TIME_FORMAT;
323 if (fdevname_r(STDIN_FILENO, dname +
324 sizeof _PATH_DEV - 1, SPECNAMELEN) != NULL)
328 rc = fstat(STDIN_FILENO, &st);
335 bzero(&fhnd, sizeof(fhnd));
336 j = MIN(2 * sizeof(fhnd), strlen(file));
341 rc = hex2byte(&file[j - 2]);
344 ((char*) &fhnd)[j / 2 - 1] = rc;
351 rc = fhstat(&fhnd, &st);
353 } else if (usestat) {
355 * Try stat() and if it fails, fall back to
356 * lstat() just in case we're examining a
359 if ((rc = stat(file, &st)) == -1 &&
361 (rc = lstat(file, &st)) == -1)
365 rc = lstat(file, &st);
372 warn("%s: stat", file);
375 output(&st, file, statfmt, fn, nonl);
382 return (am_readlink ? linkfail : errs);
385 #if HAVE_STRUCT_STAT_ST_FLAGS
387 * fflagstostr() wrapper that leaks only once
390 xfflagstostr(unsigned long fflags)
392 static char *str = NULL;
397 str = fflagstostr(fflags);
399 err(1, "fflagstostr");
402 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
405 usage(const char *synopsis)
408 (void)fprintf(stderr, "usage: %s %s\n", getprogname(), synopsis);
413 * Parses a format string.
416 output(const struct stat *st, const char *file,
417 const char *statfmt, int fn, int nonl)
419 int flags, size, prec, ofmt, hilo, what;
420 char buf[PATH_MAX + 4 + 1];
425 while (*statfmt != '\0') {
428 * Non-format characters go straight out.
430 if (*statfmt != FMT_MAGIC) {
431 addchar(stdout, *statfmt, &nl);
437 * The current format "substring" starts here,
438 * and then we skip the magic.
444 * Some simple one-character "formats".
448 addchar(stdout, '\n', &nl);
452 addchar(stdout, '\t', &nl);
456 addchar(stdout, '%', &nl);
459 case SIMPLE_NUMBER: {
462 snprintf(num, sizeof(num), "%d", fn);
463 for (p = &num[0]; *p; p++)
464 addchar(stdout, *p, &nl);
471 * This must be an actual format string. Format strings are
472 * similar to printf(3) formats up to a point, and are of
475 * % required start of format
476 * [-# +0] opt. format characters
477 * size opt. field width
478 * . opt. decimal separator, followed by
479 * prec opt. precision
480 * fmt opt. output specifier (string, numeric, etc.)
481 * sub opt. sub field specifier (high, middle, low)
482 * datum required field specifier (size, mode, etc)
484 * Only the % and the datum selector are required. All data
485 * have reasonable default output forms. The "sub" specifier
486 * only applies to certain data (mode, dev, rdev, filetype).
487 * The symlink output defaults to STRING, yet will only emit
488 * the leading " -> " if STRING is explicitly specified. The
489 * sizerdev datum will generate rdev output for character or
490 * block devices, and size output for all others.
494 if (*statfmt == FMT_POUND)
496 else if (*statfmt == FMT_SPACE)
498 else if (*statfmt == FMT_PLUS)
500 else if (*statfmt == FMT_ZERO)
502 else if (*statfmt == FMT_MINUS)
507 } while (1/*CONSTCOND*/);
510 if (isdigit((unsigned)*statfmt)) {
512 while (isdigit((unsigned)*statfmt)) {
513 size = (size * 10) + (*statfmt - '0');
521 if (*statfmt == FMT_DOT) {
525 while (isdigit((unsigned)*statfmt)) {
526 prec = (prec * 10) + (*statfmt - '0');
533 #define fmtcase(x, y) case (y): (x) = (y); statfmt++; break
534 #define fmtcasef(x, y, z) case (y): (x) = (z); statfmt++; break
536 fmtcasef(ofmt, FMT_DECIMAL, FMTF_DECIMAL);
537 fmtcasef(ofmt, FMT_OCTAL, FMTF_OCTAL);
538 fmtcasef(ofmt, FMT_UNSIGNED, FMTF_UNSIGNED);
539 fmtcasef(ofmt, FMT_HEX, FMTF_HEX);
540 fmtcasef(ofmt, FMT_FLOAT, FMTF_FLOAT);
541 fmtcasef(ofmt, FMT_STRING, FMTF_STRING);
548 fmtcase(hilo, HIGH_PIECE);
549 fmtcase(hilo, MIDDLE_PIECE);
550 fmtcase(hilo, LOW_PIECE);
557 fmtcase(what, SHOW_realpath);
558 fmtcase(what, SHOW_st_dev);
559 fmtcase(what, SHOW_st_ino);
560 fmtcase(what, SHOW_st_mode);
561 fmtcase(what, SHOW_st_nlink);
562 fmtcase(what, SHOW_st_uid);
563 fmtcase(what, SHOW_st_gid);
564 fmtcase(what, SHOW_st_rdev);
565 fmtcase(what, SHOW_st_atime);
566 fmtcase(what, SHOW_st_mtime);
567 fmtcase(what, SHOW_st_ctime);
568 fmtcase(what, SHOW_st_btime);
569 fmtcase(what, SHOW_st_size);
570 fmtcase(what, SHOW_st_blocks);
571 fmtcase(what, SHOW_st_blksize);
572 fmtcase(what, SHOW_st_flags);
573 fmtcase(what, SHOW_st_gen);
574 fmtcase(what, SHOW_symlink);
575 fmtcase(what, SHOW_filetype);
576 fmtcase(what, SHOW_filename);
577 fmtcase(what, SHOW_sizerdev);
586 subfmt, statfmt - subfmt,
588 flags, size, prec, ofmt, hilo, what);
590 for (i = 0; i < t && i < (int)(sizeof(buf) - 1); i++)
591 addchar(stdout, buf[i], &nl);
596 errx(1, "%.*s: bad format",
597 (int)(statfmt - subfmt + 1), subfmt);
601 (void)fputc('\n', stdout);
602 (void)fflush(stdout);
606 * Arranges output according to a single parsed format substring.
609 format1(const struct stat *st,
611 const char *fmt, int flen,
612 char *buf, size_t blen,
613 int flags, int size, int prec, int ofmt,
617 char *stmp, lfmt[24], tmp[20];
619 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 if ((pw = getpwuid(st->st_uid)) != NULL)
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 if ((gr = getgrgid(st->st_gid)) != NULL)
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)strftime(path, sizeof(path), timefmt, tm);
769 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
770 FMTF_FLOAT | FMTF_STRING;
775 small = (sizeof(st->st_size) == 4);
778 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
780 ofmt = FMTF_UNSIGNED;
783 small = (sizeof(st->st_blocks) == 4);
784 data = st->st_blocks;
786 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
788 ofmt = FMTF_UNSIGNED;
790 case SHOW_st_blksize:
791 small = (sizeof(st->st_blksize) == 4);
792 data = st->st_blksize;
794 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
796 ofmt = FMTF_UNSIGNED;
798 #if HAVE_STRUCT_STAT_ST_FLAGS
800 small = (sizeof(st->st_flags) == 4);
802 sdata = xfflagstostr(st->st_flags);
805 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
808 ofmt = FMTF_UNSIGNED;
810 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
811 #if HAVE_STRUCT_STAT_ST_GEN
813 small = (sizeof(st->st_gen) == 4);
816 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
818 ofmt = FMTF_UNSIGNED;
820 #endif /* HAVE_STRUCT_STAT_ST_GEN */
825 (void)strlcpy(path, "(stdin)", sizeof(path));
828 snprintf(path, sizeof(path), " -> ");
829 if (realpath(file, path + 4) == NULL) {
834 sdata = path + (ofmt == FMTF_STRING ? 0 : 4);
837 formats = FMTF_STRING;
844 if (S_ISLNK(st->st_mode)) {
845 snprintf(path, sizeof(path), " -> ");
846 l = readlink(file, path + 4, sizeof(path) - 4 - 1);
853 sdata = path + (ofmt == FMTF_STRING ? 0 : 4);
859 formats = FMTF_STRING;
867 if (hilo == 0 || hilo == LOW_PIECE) {
868 switch (st->st_mode & S_IFMT) {
869 case S_IFIFO: sdata = "|"; break;
870 case S_IFDIR: sdata = "/"; break;
873 (S_IXUSR | S_IXGRP | S_IXOTH))
876 case S_IFLNK: sdata = "@"; break;
877 case S_IFSOCK: sdata = "="; break;
879 case S_IFWHT: sdata = "%"; break;
882 case S_IFDOOR: sdata = ">"; break;
883 #endif /* S_IFDOOR */
887 else if (hilo == HIGH_PIECE) {
888 switch (st->st_mode & S_IFMT) {
889 case S_IFIFO: sdata = "Fifo File"; break;
890 case S_IFCHR: sdata = "Character Device"; break;
891 case S_IFDIR: sdata = "Directory"; break;
892 case S_IFBLK: sdata = "Block Device"; break;
893 case S_IFREG: sdata = "Regular File"; break;
894 case S_IFLNK: sdata = "Symbolic Link"; break;
895 case S_IFSOCK: sdata = "Socket"; break;
897 case S_IFWHT: sdata = "Whiteout File"; break;
900 case S_IFDOOR: sdata = "Door"; break;
901 #endif /* S_IFDOOR */
902 default: sdata = "???"; break;
906 formats = FMTF_STRING;
913 (void)strlcpy(path, file, sizeof(path));
915 formats = FMTF_STRING;
920 if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
921 char majdev[20], mindev[20];
927 majdev, sizeof(majdev),
929 ofmt, HIGH_PIECE, SHOW_st_rdev);
933 mindev, sizeof(mindev),
935 ofmt, LOW_PIECE, SHOW_st_rdev);
936 return (snprintf(buf, blen, "%.*s,%.*s",
937 l1, majdev, l2, mindev));
945 ofmt, 0, SHOW_st_size));
949 errx(1, "%.*s: bad format", (int)flen, fmt);
953 * If a subdatum was specified but not supported, or an output
954 * format was selected that is not supported, that's an error.
956 if (hilo != 0 || (ofmt & formats) == 0)
957 errx(1, "%.*s: bad format", (int)flen, fmt);
960 * Assemble the format string for passing to printf(3).
963 (void)strcat(lfmt, "%");
964 if (flags & FLAG_POUND)
965 (void)strcat(lfmt, "#");
966 if (flags & FLAG_SPACE)
967 (void)strcat(lfmt, " ");
968 if (flags & FLAG_PLUS)
969 (void)strcat(lfmt, "+");
970 if (flags & FLAG_MINUS)
971 (void)strcat(lfmt, "-");
972 if (flags & FLAG_ZERO)
973 (void)strcat(lfmt, "0");
976 * Only the timespecs support the FLOAT output format, and that
977 * requires work that differs from the other formats.
979 if (ofmt == FMTF_FLOAT) {
981 * Nothing after the decimal point, so just print seconds.
985 (void)snprintf(tmp, sizeof(tmp), "%d", size);
986 (void)strcat(lfmt, tmp);
988 (void)strcat(lfmt, "lld");
989 return (snprintf(buf, blen, lfmt,
990 (long long)ts.tv_sec));
994 * Unspecified precision gets all the precision we have:
1001 * Adjust the size for the decimal point and the digits
1007 * Any leftover size that's legitimate will be used.
1010 (void)snprintf(tmp, sizeof(tmp), "%d", size);
1011 (void)strcat(lfmt, tmp);
1013 /* Seconds: time_t cast to long long. */
1014 (void)strcat(lfmt, "lld");
1017 * The stuff after the decimal point always needs zero
1020 (void)strcat(lfmt, ".%0");
1023 * We can "print" at most nine digits of precision. The
1024 * rest we will pad on at the end.
1026 * Nanoseconds: long.
1028 (void)snprintf(tmp, sizeof(tmp), "%dld", prec > 9 ? 9 : prec);
1029 (void)strcat(lfmt, tmp);
1032 * For precision of less that nine digits, trim off the
1033 * less significant figures.
1035 for (; prec < 9; prec++)
1039 * Use the format, and then tack on any zeroes that
1040 * might be required to make up the requested precision.
1042 l = snprintf(buf, blen, lfmt, (long long)ts.tv_sec, ts.tv_nsec);
1043 for (; prec > 9 && l < (int)blen; prec--, l++)
1044 (void)strcat(buf, "0");
1049 * Add on size and precision, if specified, to the format.
1052 (void)snprintf(tmp, sizeof(tmp), "%d", size);
1053 (void)strcat(lfmt, tmp);
1056 (void)snprintf(tmp, sizeof(tmp), ".%d", prec);
1057 (void)strcat(lfmt, tmp);
1061 * String output uses the temporary sdata.
1063 if (ofmt == FMTF_STRING) {
1065 errx(1, "%.*s: bad format", (int)flen, fmt);
1066 (void)strcat(lfmt, "s");
1067 return (snprintf(buf, blen, lfmt, sdata));
1071 * Ensure that sign extension does not cause bad looking output
1074 if (small && ofmt != FMTF_DECIMAL)
1075 data = (u_int32_t)data;
1078 * The four "numeric" output forms.
1080 (void)strcat(lfmt, "ll");
1082 case FMTF_DECIMAL: (void)strcat(lfmt, "d"); break;
1083 case FMTF_OCTAL: (void)strcat(lfmt, "o"); break;
1084 case FMTF_UNSIGNED: (void)strcat(lfmt, "u"); break;
1085 case FMTF_HEX: (void)strcat(lfmt, "x"); break;
1088 return (snprintf(buf, blen, lfmt, data));
1092 #define hex2nibble(c) (c <= '9' ? c - '0' : toupper(c) - 'A' + 10)
1094 hex2byte(const char c[2]) {
1095 if (!(ishexnumber(c[0]) && ishexnumber(c[1])))
1097 return (hex2nibble(c[0]) << 4) + hex2nibble(c[1]);