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>
67 #if HAVE_STRUCT_STAT_ST_FLAGS
70 #define SHELL_F " st_flags=%f"
71 #else /* HAVE_STRUCT_STAT_ST_FLAGS */
75 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
77 #if HAVE_STRUCT_STAT_ST_BIRTHTIME
78 #define DEF_B "\"%SB\" "
80 #define SHELL_B "st_birthtime=%B "
81 #else /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
85 #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
87 #if HAVE_STRUCT_STAT_ST_ATIM
88 #define st_atimespec st_atim
89 #define st_ctimespec st_ctim
90 #define st_mtimespec st_mtim
91 #endif /* HAVE_STRUCT_STAT_ST_ATIM */
94 "%d %i %Sp %l %Su %Sg %r %z \"%Sa\" \"%Sm\" \"%Sc\" " DEF_B \
96 #define RAW_FORMAT "%d %i %#p %l %u %g %r %z %a %m %c " RAW_B \
98 #define LS_FORMAT "%Sp %l %Su %Sg %Z %Sm %N%SY"
99 #define LSF_FORMAT "%Sp %l %Su %Sg %Z %Sm %N%T%SY"
100 #define SHELL_FORMAT \
101 "st_dev=%d st_ino=%i st_mode=%#p st_nlink=%l " \
102 "st_uid=%u st_gid=%g st_rdev=%r st_size=%z " \
103 "st_atime=%a st_mtime=%m st_ctime=%c " SHELL_B \
104 "st_blksize=%k st_blocks=%b" SHELL_F
105 #define LINUX_FORMAT \
107 " Size: %-11z FileType: %HT%n" \
108 " Mode: (%OMp%03OLp/%.10Sp) Uid: (%5u/%8Su) Gid: (%5g/%8Sg)%n" \
109 "Device: %Hd,%Ld Inode: %i Links: %l%n" \
114 #define TIME_FORMAT "%b %e %T %Y"
116 #define FLAG_POUND 0x01
117 #define FLAG_SPACE 0x02
118 #define FLAG_PLUS 0x04
119 #define FLAG_ZERO 0x08
120 #define FLAG_MINUS 0x10
123 * These format characters must all be unique, except the magic one.
125 #define FMT_MAGIC '%'
128 #define SIMPLE_NEWLINE 'n'
129 #define SIMPLE_TAB 't'
130 #define SIMPLE_PERCENT '%'
131 #define SIMPLE_NUMBER '@'
133 #define FMT_POUND '#'
134 #define FMT_SPACE ' '
137 #define FMT_MINUS '-'
139 #define FMT_DECIMAL 'D'
140 #define FMT_OCTAL 'O'
141 #define FMT_UNSIGNED 'U'
143 #define FMT_FLOAT 'F'
144 #define FMT_STRING 'S'
146 #define FMTF_DECIMAL 0x01
147 #define FMTF_OCTAL 0x02
148 #define FMTF_UNSIGNED 0x04
149 #define FMTF_HEX 0x08
150 #define FMTF_FLOAT 0x10
151 #define FMTF_STRING 0x20
153 #define HIGH_PIECE 'H'
154 #define MIDDLE_PIECE 'M'
155 #define LOW_PIECE 'L'
157 #define SHOW_realpath 'R'
158 #define SHOW_st_dev 'd'
159 #define SHOW_st_ino 'i'
160 #define SHOW_st_mode 'p'
161 #define SHOW_st_nlink 'l'
162 #define SHOW_st_uid 'u'
163 #define SHOW_st_gid 'g'
164 #define SHOW_st_rdev 'r'
165 #define SHOW_st_atime 'a'
166 #define SHOW_st_mtime 'm'
167 #define SHOW_st_ctime 'c'
168 #define SHOW_st_btime 'B'
169 #define SHOW_st_size 'z'
170 #define SHOW_st_blocks 'b'
171 #define SHOW_st_blksize 'k'
172 #define SHOW_st_flags 'f'
173 #define SHOW_st_gen 'v'
174 #define SHOW_symlink 'Y'
175 #define SHOW_filetype 'T'
176 #define SHOW_filename 'N'
177 #define SHOW_sizerdev 'Z'
179 void usage(const char *);
180 void output(const struct stat *, const char *,
181 const char *, int, int);
182 int format1(const struct stat *, /* stat info */
183 const char *, /* the file name */
184 const char *, int, /* the format string itself */
185 char *, size_t, /* a place to put the output */
186 int, int, int, int, /* the parsed format */
188 #if HAVE_STRUCT_STAT_ST_FLAGS
189 char *xfflagstostr(unsigned long);
195 #define addchar(s, c, nl) \
197 (void)fputc((c), (s)); \
198 (*nl) = ((c) == '\n'); \
199 } while (0/*CONSTCOND*/)
202 main(int argc, char *argv[])
205 int ch, rc, errs, am_readlink;
206 int lsF, fmtchar, usestat, fn, nonl, quiet;
207 const char *statfmt, *options, *synopsis;
208 char dname[sizeof _PATH_DEV + SPECNAMELEN] = _PATH_DEV;
221 if (strcmp(getprogname(), "readlink") == 0) {
224 synopsis = "[-fn] [file ...]";
229 options = "f:FlLnqrst:x";
230 synopsis = "[-FLnq] [-f format | -l | -r | -s | -x] "
231 "[-t timefmt] [file ...]";
234 while ((ch = getopt(argc, argv, options)) != -1)
260 errx(1, "can't use format '%c' with '%c'",
275 if (fmtchar == '\0') {
280 statfmt = DEF_FORMAT;
284 if (lsF && fmtchar != 'l')
285 errx(1, "can't use format '%c' with -F", fmtchar);
289 /* statfmt already set */
292 statfmt = lsF ? LSF_FORMAT : LS_FORMAT;
295 statfmt = RAW_FORMAT;
298 statfmt = SHELL_FORMAT;
301 statfmt = LINUX_FORMAT;
311 timefmt = TIME_FORMAT;
316 if (fdevname_r(STDIN_FILENO, dname +
317 sizeof _PATH_DEV - 1, SPECNAMELEN) != NULL)
321 rc = fstat(STDIN_FILENO, &st);
326 * Try stat() and if it fails, fall back to
327 * lstat() just in case we're examining a
330 if ((rc = stat(file, &st)) == -1 &&
332 (rc = lstat(file, &st)) == -1)
336 rc = lstat(file, &st);
343 warn("%s: stat", file);
346 output(&st, file, statfmt, fn, nonl);
353 return (am_readlink ? linkfail : errs);
356 #if HAVE_STRUCT_STAT_ST_FLAGS
358 * fflagstostr() wrapper that leaks only once
361 xfflagstostr(unsigned long fflags)
363 static char *str = NULL;
368 str = fflagstostr(fflags);
370 err(1, "fflagstostr");
373 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
376 usage(const char *synopsis)
379 (void)fprintf(stderr, "usage: %s %s\n", getprogname(), synopsis);
384 * Parses a format string.
387 output(const struct stat *st, const char *file,
388 const char *statfmt, int fn, int nonl)
390 int flags, size, prec, ofmt, hilo, what;
391 char buf[PATH_MAX + 4 + 1];
396 while (*statfmt != '\0') {
399 * Non-format characters go straight out.
401 if (*statfmt != FMT_MAGIC) {
402 addchar(stdout, *statfmt, &nl);
408 * The current format "substring" starts here,
409 * and then we skip the magic.
415 * Some simple one-character "formats".
419 addchar(stdout, '\n', &nl);
423 addchar(stdout, '\t', &nl);
427 addchar(stdout, '%', &nl);
430 case SIMPLE_NUMBER: {
433 snprintf(num, sizeof(num), "%d", fn);
434 for (p = &num[0]; *p; p++)
435 addchar(stdout, *p, &nl);
442 * This must be an actual format string. Format strings are
443 * similar to printf(3) formats up to a point, and are of
446 * % required start of format
447 * [-# +0] opt. format characters
448 * size opt. field width
449 * . opt. decimal separator, followed by
450 * prec opt. precision
451 * fmt opt. output specifier (string, numeric, etc.)
452 * sub opt. sub field specifier (high, middle, low)
453 * datum required field specifier (size, mode, etc)
455 * Only the % and the datum selector are required. All data
456 * have reasonable default output forms. The "sub" specifier
457 * only applies to certain data (mode, dev, rdev, filetype).
458 * The symlink output defaults to STRING, yet will only emit
459 * the leading " -> " if STRING is explicitly specified. The
460 * sizerdev datum will generate rdev output for character or
461 * block devices, and size output for all others.
465 if (*statfmt == FMT_POUND)
467 else if (*statfmt == FMT_SPACE)
469 else if (*statfmt == FMT_PLUS)
471 else if (*statfmt == FMT_ZERO)
473 else if (*statfmt == FMT_MINUS)
478 } while (1/*CONSTCOND*/);
481 if (isdigit((unsigned)*statfmt)) {
483 while (isdigit((unsigned)*statfmt)) {
484 size = (size * 10) + (*statfmt - '0');
492 if (*statfmt == FMT_DOT) {
496 while (isdigit((unsigned)*statfmt)) {
497 prec = (prec * 10) + (*statfmt - '0');
504 #define fmtcase(x, y) case (y): (x) = (y); statfmt++; break
505 #define fmtcasef(x, y, z) case (y): (x) = (z); statfmt++; break
507 fmtcasef(ofmt, FMT_DECIMAL, FMTF_DECIMAL);
508 fmtcasef(ofmt, FMT_OCTAL, FMTF_OCTAL);
509 fmtcasef(ofmt, FMT_UNSIGNED, FMTF_UNSIGNED);
510 fmtcasef(ofmt, FMT_HEX, FMTF_HEX);
511 fmtcasef(ofmt, FMT_FLOAT, FMTF_FLOAT);
512 fmtcasef(ofmt, FMT_STRING, FMTF_STRING);
519 fmtcase(hilo, HIGH_PIECE);
520 fmtcase(hilo, MIDDLE_PIECE);
521 fmtcase(hilo, LOW_PIECE);
528 fmtcase(what, SHOW_realpath);
529 fmtcase(what, SHOW_st_dev);
530 fmtcase(what, SHOW_st_ino);
531 fmtcase(what, SHOW_st_mode);
532 fmtcase(what, SHOW_st_nlink);
533 fmtcase(what, SHOW_st_uid);
534 fmtcase(what, SHOW_st_gid);
535 fmtcase(what, SHOW_st_rdev);
536 fmtcase(what, SHOW_st_atime);
537 fmtcase(what, SHOW_st_mtime);
538 fmtcase(what, SHOW_st_ctime);
539 fmtcase(what, SHOW_st_btime);
540 fmtcase(what, SHOW_st_size);
541 fmtcase(what, SHOW_st_blocks);
542 fmtcase(what, SHOW_st_blksize);
543 fmtcase(what, SHOW_st_flags);
544 fmtcase(what, SHOW_st_gen);
545 fmtcase(what, SHOW_symlink);
546 fmtcase(what, SHOW_filetype);
547 fmtcase(what, SHOW_filename);
548 fmtcase(what, SHOW_sizerdev);
557 subfmt, statfmt - subfmt,
559 flags, size, prec, ofmt, hilo, what);
561 for (i = 0; i < t && i < (int)(sizeof(buf) - 1); i++)
562 addchar(stdout, buf[i], &nl);
567 errx(1, "%.*s: bad format",
568 (int)(statfmt - subfmt + 1), subfmt);
572 (void)fputc('\n', stdout);
573 (void)fflush(stdout);
577 * Arranges output according to a single parsed format substring.
580 format1(const struct stat *st,
582 const char *fmt, int flen,
583 char *buf, size_t blen,
584 int flags, int size, int prec, int ofmt,
588 char *stmp, lfmt[24], tmp[20];
590 char smode[12], sid[12], path[PATH_MAX + 4];
593 const struct timespec *tsp;
596 int l, small, formats;
603 * First, pick out the data and tweak it based on hilo or
604 * specified output format (symlink output only).
609 small = (sizeof(st->st_dev) == 4);
610 data = (what == SHOW_st_dev) ? st->st_dev : st->st_rdev;
612 sdata = (what == SHOW_st_dev) ?
613 devname(st->st_dev, S_IFBLK) :
615 S_ISCHR(st->st_mode) ? S_IFCHR :
616 S_ISBLK(st->st_mode) ? S_IFBLK :
620 #endif /* HAVE_DEVNAME */
621 if (hilo == HIGH_PIECE) {
625 else if (hilo == LOW_PIECE) {
626 data = minor((unsigned)data);
629 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
632 #else /* HAVE_DEVNAME */
634 #endif /* HAVE_DEVNAME */
636 ofmt = FMTF_UNSIGNED;
639 small = (sizeof(st->st_ino) == 4);
642 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
644 ofmt = FMTF_UNSIGNED;
647 small = (sizeof(st->st_mode) == 4);
649 strmode(st->st_mode, smode);
652 if (stmp[l - 1] == ' ')
654 if (hilo == HIGH_PIECE) {
660 else if (hilo == MIDDLE_PIECE) {
661 data = (data >> 9) & 07;
666 else if (hilo == LOW_PIECE) {
673 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
679 small = (sizeof(st->st_dev) == 4);
682 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
684 ofmt = FMTF_UNSIGNED;
687 small = (sizeof(st->st_uid) == 4);
689 if ((pw = getpwuid(st->st_uid)) != NULL)
692 snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_uid);
695 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
698 ofmt = FMTF_UNSIGNED;
701 small = (sizeof(st->st_gid) == 4);
703 if ((gr = getgrgid(st->st_gid)) != NULL)
706 snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_gid);
709 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
712 ofmt = FMTF_UNSIGNED;
715 tsp = &st->st_atimespec;
719 tsp = &st->st_mtimespec;
723 tsp = &st->st_ctimespec;
725 #if HAVE_STRUCT_STAT_ST_BIRTHTIME
728 tsp = &st->st_birthtimespec;
729 #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
730 ts = *tsp; /* copy so we can muck with it */
731 small = (sizeof(ts.tv_sec) == 4);
733 tm = localtime(&ts.tv_sec);
736 tm = localtime(&ts.tv_sec);
738 (void)strftime(path, sizeof(path), timefmt, tm);
740 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
741 FMTF_FLOAT | FMTF_STRING;
746 small = (sizeof(st->st_size) == 4);
749 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
751 ofmt = FMTF_UNSIGNED;
754 small = (sizeof(st->st_blocks) == 4);
755 data = st->st_blocks;
757 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
759 ofmt = FMTF_UNSIGNED;
761 case SHOW_st_blksize:
762 small = (sizeof(st->st_blksize) == 4);
763 data = st->st_blksize;
765 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
767 ofmt = FMTF_UNSIGNED;
769 #if HAVE_STRUCT_STAT_ST_FLAGS
771 small = (sizeof(st->st_flags) == 4);
773 sdata = xfflagstostr(st->st_flags);
776 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
779 ofmt = FMTF_UNSIGNED;
781 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
782 #if HAVE_STRUCT_STAT_ST_GEN
784 small = (sizeof(st->st_gen) == 4);
787 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
789 ofmt = FMTF_UNSIGNED;
791 #endif /* HAVE_STRUCT_STAT_ST_GEN */
796 (void)strlcpy(path, "(stdin)", sizeof(path));
799 snprintf(path, sizeof(path), " -> ");
800 if (realpath(file, path + 4) == NULL) {
805 sdata = path + (ofmt == FMTF_STRING ? 0 : 4);
808 formats = FMTF_STRING;
815 if (S_ISLNK(st->st_mode)) {
816 snprintf(path, sizeof(path), " -> ");
817 l = readlink(file, path + 4, sizeof(path) - 4 - 1);
824 sdata = path + (ofmt == FMTF_STRING ? 0 : 4);
830 formats = FMTF_STRING;
838 if (hilo == 0 || hilo == LOW_PIECE) {
839 switch (st->st_mode & S_IFMT) {
840 case S_IFIFO: sdata = "|"; break;
841 case S_IFDIR: sdata = "/"; break;
844 (S_IXUSR | S_IXGRP | S_IXOTH))
847 case S_IFLNK: sdata = "@"; break;
848 case S_IFSOCK: sdata = "="; break;
850 case S_IFWHT: sdata = "%"; break;
853 case S_IFDOOR: sdata = ">"; break;
854 #endif /* S_IFDOOR */
858 else if (hilo == HIGH_PIECE) {
859 switch (st->st_mode & S_IFMT) {
860 case S_IFIFO: sdata = "Fifo File"; break;
861 case S_IFCHR: sdata = "Character Device"; break;
862 case S_IFDIR: sdata = "Directory"; break;
863 case S_IFBLK: sdata = "Block Device"; break;
864 case S_IFREG: sdata = "Regular File"; break;
865 case S_IFLNK: sdata = "Symbolic Link"; break;
866 case S_IFSOCK: sdata = "Socket"; break;
868 case S_IFWHT: sdata = "Whiteout File"; break;
871 case S_IFDOOR: sdata = "Door"; break;
872 #endif /* S_IFDOOR */
873 default: sdata = "???"; break;
877 formats = FMTF_STRING;
884 (void)strlcpy(path, file, sizeof(path));
886 formats = FMTF_STRING;
891 if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
892 char majdev[20], mindev[20];
898 majdev, sizeof(majdev),
900 ofmt, HIGH_PIECE, SHOW_st_rdev);
904 mindev, sizeof(mindev),
906 ofmt, LOW_PIECE, SHOW_st_rdev);
907 return (snprintf(buf, blen, "%.*s,%.*s",
908 l1, majdev, l2, mindev));
916 ofmt, 0, SHOW_st_size));
920 errx(1, "%.*s: bad format", (int)flen, fmt);
924 * If a subdatum was specified but not supported, or an output
925 * format was selected that is not supported, that's an error.
927 if (hilo != 0 || (ofmt & formats) == 0)
928 errx(1, "%.*s: bad format", (int)flen, fmt);
931 * Assemble the format string for passing to printf(3).
934 (void)strcat(lfmt, "%");
935 if (flags & FLAG_POUND)
936 (void)strcat(lfmt, "#");
937 if (flags & FLAG_SPACE)
938 (void)strcat(lfmt, " ");
939 if (flags & FLAG_PLUS)
940 (void)strcat(lfmt, "+");
941 if (flags & FLAG_MINUS)
942 (void)strcat(lfmt, "-");
943 if (flags & FLAG_ZERO)
944 (void)strcat(lfmt, "0");
947 * Only the timespecs support the FLOAT output format, and that
948 * requires work that differs from the other formats.
950 if (ofmt == FMTF_FLOAT) {
952 * Nothing after the decimal point, so just print seconds.
956 (void)snprintf(tmp, sizeof(tmp), "%d", size);
957 (void)strcat(lfmt, tmp);
959 (void)strcat(lfmt, "lld");
960 return (snprintf(buf, blen, lfmt,
961 (long long)ts.tv_sec));
965 * Unspecified precision gets all the precision we have:
972 * Adjust the size for the decimal point and the digits
978 * Any leftover size that's legitimate will be used.
981 (void)snprintf(tmp, sizeof(tmp), "%d", size);
982 (void)strcat(lfmt, tmp);
984 /* Seconds: time_t cast to long long. */
985 (void)strcat(lfmt, "lld");
988 * The stuff after the decimal point always needs zero
991 (void)strcat(lfmt, ".%0");
994 * We can "print" at most nine digits of precision. The
995 * rest we will pad on at the end.
999 (void)snprintf(tmp, sizeof(tmp), "%dld", prec > 9 ? 9 : prec);
1000 (void)strcat(lfmt, tmp);
1003 * For precision of less that nine digits, trim off the
1004 * less significant figures.
1006 for (; prec < 9; prec++)
1010 * Use the format, and then tack on any zeroes that
1011 * might be required to make up the requested precision.
1013 l = snprintf(buf, blen, lfmt, (long long)ts.tv_sec, ts.tv_nsec);
1014 for (; prec > 9 && l < (int)blen; prec--, l++)
1015 (void)strcat(buf, "0");
1020 * Add on size and precision, if specified, to the format.
1023 (void)snprintf(tmp, sizeof(tmp), "%d", size);
1024 (void)strcat(lfmt, tmp);
1027 (void)snprintf(tmp, sizeof(tmp), ".%d", prec);
1028 (void)strcat(lfmt, tmp);
1032 * String output uses the temporary sdata.
1034 if (ofmt == FMTF_STRING) {
1036 errx(1, "%.*s: bad format", (int)flen, fmt);
1037 (void)strcat(lfmt, "s");
1038 return (snprintf(buf, blen, lfmt, sdata));
1042 * Ensure that sign extension does not cause bad looking output
1045 if (small && ofmt != FMTF_DECIMAL)
1046 data = (u_int32_t)data;
1049 * The four "numeric" output forms.
1051 (void)strcat(lfmt, "ll");
1053 case FMTF_DECIMAL: (void)strcat(lfmt, "d"); break;
1054 case FMTF_OCTAL: (void)strcat(lfmt, "o"); break;
1055 case FMTF_UNSIGNED: (void)strcat(lfmt, "u"); break;
1056 case FMTF_HEX: (void)strcat(lfmt, "x"); break;
1059 return (snprintf(buf, blen, lfmt, data));