1 /* $NetBSD: stat.c,v 1.38 2013/01/03 13:28:41 dsl Exp $ */
4 * Copyright (c) 2002-2011 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 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
34 /* config checked libc, we need the prototype as well */
38 #include <sys/cdefs.h>
40 __RCSID("$NetBSD: stat.c,v 1.38 2013/01/03 13:28:41 dsl Exp $");
43 #if ! HAVE_NBTOOL_CONFIG_H
44 #define HAVE_STRUCT_STAT_ST_FLAGS 1
45 #define HAVE_STRUCT_STAT_ST_GEN 1
46 #define HAVE_STRUCT_STAT_ST_BIRTHTIME 1
47 #define HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC 1
48 #define HAVE_STRUCT_STAT_ST_MTIMENSEC 1
49 #define HAVE_DEVNAME 1
50 #endif /* HAVE_NBTOOL_CONFIG_H */
52 #include <sys/types.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=%SB "
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=%Sa st_mtime=%Sm st_ctime=%Sc " SHELL_B \
105 "st_blksize=%k st_blocks=%b" SHELL_F
106 #define LINUX_FORMAT \
108 " Size: %-11z FileType: %HT%n" \
109 " Mode: (%04OLp/%.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 static void usage(const char *) __dead
;
181 static void output(const struct stat
*, const char *,
182 const char *, int, int, int);
183 static 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 */
190 static const char *timefmt
;
193 #define addchar(s, c, nl) \
195 (void)fputc((c), (s)); \
196 (*nl) = ((c) == '\n'); \
197 } while (0/*CONSTCOND*/)
200 main(int argc
, char *argv
[])
203 int ch
, rc
, errs
, am_readlink
;
204 int lsF
, fmtchar
, usestat
, fn
, nonl
, quiet
;
205 const char *statfmt
, *options
, *synopsis
;
217 setprogname(argv
[0]);
219 if (strcmp(getprogname(), "readlink") == 0) {
222 synopsis
= "[-fnqsv] [file ...]";
227 options
= "f:FlLnqrst:x";
228 synopsis
= "[-FlLnqrsx] [-f format] [-t timefmt] [file ...]";
231 while ((ch
= getopt(argc
, argv
, options
)) != -1)
262 errx(1, "can't use format '%c' with '%c'",
280 if (fmtchar
== '\0') {
285 statfmt
= DEF_FORMAT
;
289 if (lsF
&& fmtchar
!= 'l')
290 errx(1, "can't use format '%c' with -F", fmtchar
);
294 /* statfmt already set */
297 statfmt
= lsF
? LSF_FORMAT
: LS_FORMAT
;
300 statfmt
= RAW_FORMAT
;
303 statfmt
= SHELL_FORMAT
;
308 statfmt
= LINUX_FORMAT
;
318 timefmt
= TIME_FORMAT
;
323 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(argv
[0], &st
)) == -1 &&
332 (rc
= lstat(argv
[0], &st
)) == -1)
336 rc
= lstat(argv
[0], &st
);
343 argc
== 0 ? "(stdin)" : argv
[0],
344 usestat
? "stat" : "lstat");
347 output(&st
, argv
[0], statfmt
, fn
, nonl
, quiet
);
354 return (am_readlink
? linkfail
: errs
);
358 usage(const char *synopsis
)
361 (void)fprintf(stderr
, "usage: %s %s\n", getprogname(), synopsis
);
366 * Parses a format string.
369 output(const struct stat
*st
, const char *file
,
370 const char *statfmt
, int fn
, int nonl
, int quiet
)
372 int flags
, size
, prec
, ofmt
, hilo
, what
;
374 * buf size is enough for an item of length PATH_MAX,
375 * multiplied by 4 for vis encoding, plus 4 for symlink
376 * " -> " prefix, plus 1 for \0 terminator.
378 char buf
[PATH_MAX
* 4 + 4 + 1];
383 while (*statfmt
!= '\0') {
386 * Non-format characters go straight out.
388 if (*statfmt
!= FMT_MAGIC
) {
389 addchar(stdout
, *statfmt
, &nl
);
395 * The current format "substring" starts here,
396 * and then we skip the magic.
402 * Some simple one-character "formats".
406 addchar(stdout
, '\n', &nl
);
410 addchar(stdout
, '\t', &nl
);
414 addchar(stdout
, '%', &nl
);
417 case SIMPLE_NUMBER
: {
420 snprintf(num
, sizeof(num
), "%d", fn
);
421 for (p
= &num
[0]; *p
; p
++)
422 addchar(stdout
, *p
, &nl
);
429 * This must be an actual format string. Format strings are
430 * similar to printf(3) formats up to a point, and are of
433 * % required start of format
434 * [-# +0] opt. format characters
435 * size opt. field width
436 * . opt. decimal separator, followed by
437 * prec opt. precision
438 * fmt opt. output specifier (string, numeric, etc.)
439 * sub opt. sub field specifier (high, middle, low)
440 * datum required field specifier (size, mode, etc)
442 * Only the % and the datum selector are required. All data
443 * have reasonable default output forms. The "sub" specifier
444 * only applies to certain data (mode, dev, rdev, filetype).
445 * The symlink output defaults to STRING, yet will only emit
446 * the leading " -> " if STRING is explicitly specified. The
447 * sizerdev datum will generate rdev output for character or
448 * block devices, and size output for all others.
449 * For STRING output, the # format requests vis encoding.
453 if (*statfmt
== FMT_POUND
)
455 else if (*statfmt
== FMT_SPACE
)
457 else if (*statfmt
== FMT_PLUS
)
459 else if (*statfmt
== FMT_ZERO
)
461 else if (*statfmt
== FMT_MINUS
)
466 } while (1/*CONSTCOND*/);
469 if (isdigit((unsigned)*statfmt
)) {
471 while (isdigit((unsigned)*statfmt
)) {
472 size
= (size
* 10) + (*statfmt
- '0');
480 if (*statfmt
== FMT_DOT
) {
484 while (isdigit((unsigned)*statfmt
)) {
485 prec
= (prec
* 10) + (*statfmt
- '0');
492 #define fmtcase(x, y) case (y): (x) = (y); statfmt++; break
493 #define fmtcasef(x, y, z) case (y): (x) = (z); statfmt++; break
495 fmtcasef(ofmt
, FMT_DECIMAL
, FMTF_DECIMAL
);
496 fmtcasef(ofmt
, FMT_OCTAL
, FMTF_OCTAL
);
497 fmtcasef(ofmt
, FMT_UNSIGNED
, FMTF_UNSIGNED
);
498 fmtcasef(ofmt
, FMT_HEX
, FMTF_HEX
);
499 fmtcasef(ofmt
, FMT_FLOAT
, FMTF_FLOAT
);
500 fmtcasef(ofmt
, FMT_STRING
, FMTF_STRING
);
507 fmtcase(hilo
, HIGH_PIECE
);
508 fmtcase(hilo
, MIDDLE_PIECE
);
509 fmtcase(hilo
, LOW_PIECE
);
516 fmtcase(what
, SHOW_realpath
);
517 fmtcase(what
, SHOW_st_dev
);
518 fmtcase(what
, SHOW_st_ino
);
519 fmtcase(what
, SHOW_st_mode
);
520 fmtcase(what
, SHOW_st_nlink
);
521 fmtcase(what
, SHOW_st_uid
);
522 fmtcase(what
, SHOW_st_gid
);
523 fmtcase(what
, SHOW_st_rdev
);
524 fmtcase(what
, SHOW_st_atime
);
525 fmtcase(what
, SHOW_st_mtime
);
526 fmtcase(what
, SHOW_st_ctime
);
527 fmtcase(what
, SHOW_st_btime
);
528 fmtcase(what
, SHOW_st_size
);
529 fmtcase(what
, SHOW_st_blocks
);
530 fmtcase(what
, SHOW_st_blksize
);
531 fmtcase(what
, SHOW_st_flags
);
532 fmtcase(what
, SHOW_st_gen
);
533 fmtcase(what
, SHOW_symlink
);
534 fmtcase(what
, SHOW_filetype
);
535 fmtcase(what
, SHOW_filename
);
536 fmtcase(what
, SHOW_sizerdev
);
545 subfmt
, statfmt
- subfmt
,
547 flags
, size
, prec
, ofmt
, hilo
, what
, quiet
);
549 for (i
= 0; i
< t
&& i
< (int)(sizeof(buf
) - 1); i
++)
550 addchar(stdout
, buf
[i
], &nl
);
555 errx(1, "%.*s: bad format",
556 (int)(statfmt
- subfmt
+ 1), subfmt
);
560 (void)fputc('\n', stdout
);
561 (void)fflush(stdout
);
565 * Arranges output according to a single parsed format substring.
568 format1(const struct stat
*st
,
570 const char *fmt
, int flen
,
571 char *buf
, size_t blen
,
572 int flags
, int size
, int prec
, int ofmt
,
573 int hilo
, int what
, int quiet
)
576 char *stmp
, lfmt
[24], tmp
[20];
578 char smode
[12], sid
[12], path
[PATH_MAX
+ 4], visbuf
[PATH_MAX
* 4 + 4];
585 int formats
; /* bitmap of allowed formats for this datum */
586 int small
; /* true if datum is a small integer */
587 int gottime
; /* true if secs and nsecs are valid */
588 int shift
; /* powers of 2 to scale numbers before printing */
589 size_t prefixlen
; /* length of constant prefix for string data */
600 * First, pick out the data and tweak it based on hilo or
601 * specified output format (symlink output only).
606 small
= (sizeof(st
->st_dev
) == 4);
607 data
= (what
== SHOW_st_dev
) ? st
->st_dev
: st
->st_rdev
;
609 sdata
= (what
== SHOW_st_dev
) ?
610 devname(st
->st_dev
, S_IFBLK
) :
612 S_ISCHR(st
->st_mode
) ? S_IFCHR
:
613 S_ISBLK(st
->st_mode
) ? S_IFBLK
:
617 #endif /* HAVE_DEVNAME */
618 if (hilo
== HIGH_PIECE
) {
622 else if (hilo
== LOW_PIECE
) {
623 data
= minor((unsigned)data
);
626 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
|
629 #else /* HAVE_DEVNAME */
631 #endif /* HAVE_DEVNAME */
633 ofmt
= FMTF_UNSIGNED
;
636 small
= (sizeof(st
->st_ino
) == 4);
639 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
641 ofmt
= FMTF_UNSIGNED
;
644 small
= (sizeof(st
->st_mode
) == 4);
646 strmode(st
->st_mode
, smode
);
649 if (stmp
[l
- 1] == ' ')
651 if (hilo
== HIGH_PIECE
) {
657 else if (hilo
== MIDDLE_PIECE
) {
658 data
= (data
>> 9) & 07;
663 else if (hilo
== LOW_PIECE
) {
670 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
|
676 small
= (sizeof(st
->st_dev
) == 4);
679 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
681 ofmt
= FMTF_UNSIGNED
;
684 small
= (sizeof(st
->st_uid
) == 4);
686 if ((pw
= getpwuid(st
->st_uid
)) != NULL
)
689 snprintf(sid
, sizeof(sid
), "(%ld)", (long)st
->st_uid
);
692 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
|
695 ofmt
= FMTF_UNSIGNED
;
698 small
= (sizeof(st
->st_gid
) == 4);
700 if ((gr
= getgrgid(st
->st_gid
)) != NULL
)
703 snprintf(sid
, sizeof(sid
), "(%ld)", (long)st
->st_gid
);
706 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
|
709 ofmt
= FMTF_UNSIGNED
;
714 #if HAVE_STRUCT_STAT_ST_MTIMENSEC
715 nsecs
= st
->st_atimensec
;
722 #if HAVE_STRUCT_STAT_ST_MTIMENSEC
723 nsecs
= st
->st_mtimensec
;
731 #if HAVE_STRUCT_STAT_ST_MTIMENSEC
732 nsecs
= st
->st_ctimensec
;
736 #if HAVE_STRUCT_STAT_ST_BIRTHTIME
740 secs
= st
->st_birthtime
;
741 #if HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC
742 nsecs
= st
->st_birthtimensec
;
743 #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC */
745 #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
746 small
= (sizeof(secs
) == 4);
748 tm
= localtime(&secs
);
751 tm
= localtime(&secs
);
753 (void)strftime(path
, sizeof(path
), timefmt
, tm
);
755 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
|
756 FMTF_FLOAT
| FMTF_STRING
;
761 small
= (sizeof(st
->st_size
) == 4);
764 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
766 ofmt
= FMTF_UNSIGNED
;
769 shift
= 30; /* gigabytes */
773 shift
= 20; /* megabytes */
777 shift
= 10; /* kilobytes */
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);
803 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
805 ofmt
= FMTF_UNSIGNED
;
807 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
808 #if HAVE_STRUCT_STAT_ST_GEN
810 small
= (sizeof(st
->st_gen
) == 4);
813 formats
= FMTF_DECIMAL
| FMTF_OCTAL
| FMTF_UNSIGNED
| FMTF_HEX
;
815 ofmt
= FMTF_UNSIGNED
;
817 #endif /* HAVE_STRUCT_STAT_ST_GEN */
822 (void)strlcpy(path
, "(stdin)", sizeof(path
));
825 snprintf(path
, sizeof(path
), " -> ");
826 if (realpath(file
, path
+ 4) == NULL
) {
828 warn("realpath `%s'", file
);
833 sdata
= path
+ (ofmt
== FMTF_STRING
? 0 : 4);
834 prefixlen
= (ofmt
== FMTF_STRING
? 4 : 0);
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);
849 warn("readlink `%s'", file
);
855 sdata
= path
+ (ofmt
== FMTF_STRING
? 0 : 4);
856 prefixlen
= (ofmt
== FMTF_STRING
? 4 : 0);
862 formats
= FMTF_STRING
;
870 if (hilo
== 0 || hilo
== LOW_PIECE
) {
871 switch (st
->st_mode
& S_IFMT
) {
872 case S_IFIFO
: sdata
= "|"; break;
873 case S_IFDIR
: sdata
= "/"; break;
876 (S_IXUSR
| S_IXGRP
| S_IXOTH
))
879 case S_IFLNK
: sdata
= "@"; break;
881 case S_IFSOCK
: sdata
= "="; break;
884 case S_IFWHT
: sdata
= "%"; break;
887 case S_IFDOOR
: sdata
= ">"; break;
888 #endif /* S_IFDOOR */
892 else if (hilo
== HIGH_PIECE
) {
893 switch (st
->st_mode
& S_IFMT
) {
894 case S_IFIFO
: sdata
= "Fifo File"; break;
895 case S_IFCHR
: sdata
= "Character Device"; break;
896 case S_IFDIR
: sdata
= "Directory"; break;
897 case S_IFBLK
: sdata
= "Block Device"; break;
898 case S_IFREG
: sdata
= "Regular File"; break;
899 case S_IFLNK
: sdata
= "Symbolic Link"; break;
901 case S_IFSOCK
: sdata
= "Socket"; break;
904 case S_IFWHT
: sdata
= "Whiteout File"; break;
907 case S_IFDOOR
: sdata
= "Door"; break;
908 #endif /* S_IFDOOR */
909 default: sdata
= "???"; break;
913 formats
= FMTF_STRING
;
921 (void)strlcpy(path
, "(stdin)", sizeof(path
));
922 if (hilo
== HIGH_PIECE
|| hilo
== LOW_PIECE
)
926 (void)strlcpy(path
, file
, sizeof(path
));
929 (void)strlcpy(path
, file
, sizeof(path
));
930 s
= strrchr(path
, '/');
932 /* trim off trailing /'s */
934 s
[0] == '/' && s
[1] == '\0')
936 s
= strrchr(path
, '/');
938 if (hilo
== HIGH_PIECE
) {
940 (void)strlcpy(path
, ".", sizeof(path
));
942 while (s
!= path
&& s
[0] == '/')
947 else if (hilo
== LOW_PIECE
) {
948 if (s
!= NULL
&& s
[1] != '\0')
949 (void)strlcpy(path
, s
+ 1,
955 formats
= FMTF_STRING
;
960 if (S_ISCHR(st
->st_mode
) || S_ISBLK(st
->st_mode
)) {
961 char majdev
[20], mindev
[20];
967 majdev
, sizeof(majdev
),
969 ofmt
, HIGH_PIECE
, SHOW_st_rdev
, quiet
);
973 mindev
, sizeof(mindev
),
975 ofmt
, LOW_PIECE
, SHOW_st_rdev
, quiet
);
976 return (snprintf(buf
, blen
, "%.*s,%.*s",
977 l1
, majdev
, l2
, mindev
));
985 ofmt
, 0, SHOW_st_size
, quiet
));
989 errx(1, "%.*s: bad format", (int)flen
, fmt
);
993 * If a subdatum was specified but not supported, or an output
994 * format was selected that is not supported, that's an error.
996 if (hilo
!= 0 || (ofmt
& formats
) == 0)
997 errx(1, "%.*s: bad format", (int)flen
, fmt
);
1000 * FLAG_POUND with FMTF_STRING means use vis(3) encoding.
1001 * First prefixlen chars are not encoded.
1003 if ((flags
& FLAG_POUND
) != 0 && ofmt
== FMTF_STRING
) {
1004 flags
&= !FLAG_POUND
;
1005 strncpy(visbuf
, sdata
, prefixlen
);
1006 strnvis(visbuf
+ prefixlen
, sizeof(visbuf
) - prefixlen
,
1007 sdata
+ prefixlen
, VIS_WHITE
| VIS_OCTAL
| VIS_CSTYLE
);
1012 * Assemble the format string for passing to printf(3).
1015 (void)strcat(lfmt
, "%");
1016 if (flags
& FLAG_POUND
)
1017 (void)strcat(lfmt
, "#");
1018 if (flags
& FLAG_SPACE
)
1019 (void)strcat(lfmt
, " ");
1020 if (flags
& FLAG_PLUS
)
1021 (void)strcat(lfmt
, "+");
1022 if (flags
& FLAG_MINUS
)
1023 (void)strcat(lfmt
, "-");
1024 if (flags
& FLAG_ZERO
)
1025 (void)strcat(lfmt
, "0");
1028 * Only the timespecs support the FLOAT output format, and that
1029 * requires work that differs from the other formats.
1031 if (ofmt
== FMTF_FLOAT
) {
1033 * Nothing after the decimal point, so just print seconds.
1037 (void)snprintf(tmp
, sizeof(tmp
), "%d", size
);
1038 (void)strcat(lfmt
, tmp
);
1040 (void)strcat(lfmt
, "lld");
1041 return (snprintf(buf
, blen
, lfmt
,
1046 * Unspecified precision gets all the precision we have:
1053 * Adjust the size for the decimal point and the digits
1059 * Any leftover size that's legitimate will be used.
1062 (void)snprintf(tmp
, sizeof(tmp
), "%d", size
);
1063 (void)strcat(lfmt
, tmp
);
1065 /* Seconds: time_t cast to long long. */
1066 (void)strcat(lfmt
, "lld");
1069 * The stuff after the decimal point always needs zero
1072 (void)strcat(lfmt
, ".%0");
1075 * We can "print" at most nine digits of precision. The
1076 * rest we will pad on at the end.
1078 * Nanoseconds: long.
1080 (void)snprintf(tmp
, sizeof(tmp
), "%dld", prec
> 9 ? 9 : prec
);
1081 (void)strcat(lfmt
, tmp
);
1084 * For precision of less that nine digits, trim off the
1085 * less significant figures.
1087 for (; prec
< 9; prec
++)
1091 * Use the format, and then tack on any zeroes that
1092 * might be required to make up the requested precision.
1094 l
= snprintf(buf
, blen
, lfmt
, (long long)secs
, nsecs
);
1095 for (; prec
> 9 && l
< (int)blen
; prec
--, l
++)
1096 (void)strcat(buf
, "0");
1101 * Add on size and precision, if specified, to the format.
1104 (void)snprintf(tmp
, sizeof(tmp
), "%d", size
);
1105 (void)strcat(lfmt
, tmp
);
1108 (void)snprintf(tmp
, sizeof(tmp
), ".%d", prec
);
1109 (void)strcat(lfmt
, tmp
);
1113 * String output uses the temporary sdata.
1115 if (ofmt
== FMTF_STRING
) {
1117 errx(1, "%.*s: bad format", (int)flen
, fmt
);
1118 (void)strcat(lfmt
, "s");
1119 return (snprintf(buf
, blen
, lfmt
, sdata
));
1123 * Ensure that sign extension does not cause bad looking output
1126 if (small
&& ofmt
!= FMTF_DECIMAL
)
1127 data
= (u_int32_t
)data
;
1130 * The four "numeric" output forms.
1132 (void)strcat(lfmt
, "ll");
1134 case FMTF_DECIMAL
: (void)strcat(lfmt
, "d"); break;
1135 case FMTF_OCTAL
: (void)strcat(lfmt
, "o"); break;
1136 case FMTF_UNSIGNED
: (void)strcat(lfmt
, "u"); break;
1137 case FMTF_HEX
: (void)strcat(lfmt
, "x"); break;
1141 * shift and round to nearest for kilobytes, megabytes,
1145 data
>>= (shift
- 1);
1150 return (snprintf(buf
, blen
, lfmt
, data
));