xref: /minix3/usr.bin/stat/stat.c (revision 84d9c625bfea59e274550651111ae9edfdc40fbd)
1*84d9c625SLionel Sambuc /*	$NetBSD: stat.c,v 1.38 2013/01/03 13:28:41 dsl Exp $ */
249cab1c7SVivek Prakash 
349cab1c7SVivek Prakash /*
4474b24f9SLionel Sambuc  * Copyright (c) 2002-2011 The NetBSD Foundation, Inc.
549cab1c7SVivek Prakash  * All rights reserved.
649cab1c7SVivek Prakash  *
749cab1c7SVivek Prakash  * This code is derived from software contributed to The NetBSD Foundation
849cab1c7SVivek Prakash  * by Andrew Brown.
949cab1c7SVivek Prakash  *
1049cab1c7SVivek Prakash  * Redistribution and use in source and binary forms, with or without
1149cab1c7SVivek Prakash  * modification, are permitted provided that the following conditions
1249cab1c7SVivek Prakash  * are met:
1349cab1c7SVivek Prakash  * 1. Redistributions of source code must retain the above copyright
1449cab1c7SVivek Prakash  *    notice, this list of conditions and the following disclaimer.
1549cab1c7SVivek Prakash  * 2. Redistributions in binary form must reproduce the above copyright
1649cab1c7SVivek Prakash  *    notice, this list of conditions and the following disclaimer in the
1749cab1c7SVivek Prakash  *    documentation and/or other materials provided with the distribution.
1849cab1c7SVivek Prakash  *
1949cab1c7SVivek Prakash  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2049cab1c7SVivek Prakash  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2149cab1c7SVivek Prakash  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2249cab1c7SVivek Prakash  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2349cab1c7SVivek Prakash  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2449cab1c7SVivek Prakash  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2549cab1c7SVivek Prakash  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2649cab1c7SVivek Prakash  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2749cab1c7SVivek Prakash  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2849cab1c7SVivek Prakash  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2949cab1c7SVivek Prakash  * POSSIBILITY OF SUCH DAMAGE.
3049cab1c7SVivek Prakash  */
3149cab1c7SVivek Prakash 
3249cab1c7SVivek Prakash #if HAVE_NBTOOL_CONFIG_H
3349cab1c7SVivek Prakash #include "nbtool_config.h"
34*84d9c625SLionel Sambuc /* config checked libc, we need the prototype as well */
35*84d9c625SLionel Sambuc #undef HAVE_DEVNAME
3649cab1c7SVivek Prakash #endif
3749cab1c7SVivek Prakash 
3849cab1c7SVivek Prakash #include <sys/cdefs.h>
3949cab1c7SVivek Prakash #if !defined(lint)
40*84d9c625SLionel Sambuc __RCSID("$NetBSD: stat.c,v 1.38 2013/01/03 13:28:41 dsl Exp $");
4149cab1c7SVivek Prakash #endif
4249cab1c7SVivek Prakash 
4349cab1c7SVivek Prakash #if ! HAVE_NBTOOL_CONFIG_H
4449cab1c7SVivek Prakash #define HAVE_STRUCT_STAT_ST_FLAGS 1
4549cab1c7SVivek Prakash #define HAVE_STRUCT_STAT_ST_GEN 1
4649cab1c7SVivek Prakash #define HAVE_STRUCT_STAT_ST_BIRTHTIME 1
4749cab1c7SVivek Prakash #define HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC 1
4849cab1c7SVivek Prakash #define HAVE_STRUCT_STAT_ST_MTIMENSEC 1
4949cab1c7SVivek Prakash #define HAVE_DEVNAME 1
5049cab1c7SVivek Prakash #endif /* HAVE_NBTOOL_CONFIG_H */
5149cab1c7SVivek Prakash 
5249cab1c7SVivek Prakash #include <sys/types.h>
5349cab1c7SVivek Prakash #include <sys/stat.h>
5449cab1c7SVivek Prakash 
5549cab1c7SVivek Prakash #include <ctype.h>
5649cab1c7SVivek Prakash #include <err.h>
5749cab1c7SVivek Prakash #include <errno.h>
5849cab1c7SVivek Prakash #include <grp.h>
5949cab1c7SVivek Prakash #include <limits.h>
6049cab1c7SVivek Prakash #include <pwd.h>
6149cab1c7SVivek Prakash #include <stdio.h>
6249cab1c7SVivek Prakash #include <stdlib.h>
6349cab1c7SVivek Prakash #include <string.h>
6449cab1c7SVivek Prakash #include <time.h>
6549cab1c7SVivek Prakash #include <unistd.h>
66474b24f9SLionel Sambuc #include <vis.h>
6749cab1c7SVivek Prakash 
6849cab1c7SVivek Prakash #if HAVE_STRUCT_STAT_ST_FLAGS
6949cab1c7SVivek Prakash #define DEF_F "%#Xf "
7049cab1c7SVivek Prakash #define RAW_F "%f "
7149cab1c7SVivek Prakash #define SHELL_F " st_flags=%f"
7249cab1c7SVivek Prakash #else /* HAVE_STRUCT_STAT_ST_FLAGS */
7349cab1c7SVivek Prakash #define DEF_F
7449cab1c7SVivek Prakash #define RAW_F
7549cab1c7SVivek Prakash #define SHELL_F
7649cab1c7SVivek Prakash #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
7749cab1c7SVivek Prakash 
7849cab1c7SVivek Prakash #if HAVE_STRUCT_STAT_ST_BIRTHTIME
7949cab1c7SVivek Prakash #define DEF_B "\"%SB\" "
8049cab1c7SVivek Prakash #define RAW_B "%B "
81*84d9c625SLionel Sambuc #define SHELL_B "st_birthtime=%SB "
8249cab1c7SVivek Prakash #else /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
8349cab1c7SVivek Prakash #define DEF_B
8449cab1c7SVivek Prakash #define RAW_B
8549cab1c7SVivek Prakash #define SHELL_B
8649cab1c7SVivek Prakash #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
8749cab1c7SVivek Prakash 
8849cab1c7SVivek Prakash #if HAVE_STRUCT_STAT_ST_ATIM
8949cab1c7SVivek Prakash #define st_atimespec st_atim
9049cab1c7SVivek Prakash #define st_ctimespec st_ctim
9149cab1c7SVivek Prakash #define st_mtimespec st_mtim
9249cab1c7SVivek Prakash #endif /* HAVE_STRUCT_STAT_ST_ATIM */
9349cab1c7SVivek Prakash 
9449cab1c7SVivek Prakash #define DEF_FORMAT \
9549cab1c7SVivek Prakash 	"%d %i %Sp %l %Su %Sg %r %z \"%Sa\" \"%Sm\" \"%Sc\" " DEF_B \
9649cab1c7SVivek Prakash 	"%k %b " DEF_F "%N"
9749cab1c7SVivek Prakash #define RAW_FORMAT	"%d %i %#p %l %u %g %r %z %a %m %c " RAW_B \
9849cab1c7SVivek Prakash 	"%k %b " RAW_F "%N"
9949cab1c7SVivek Prakash #define LS_FORMAT	"%Sp %l %Su %Sg %Z %Sm %N%SY"
10049cab1c7SVivek Prakash #define LSF_FORMAT	"%Sp %l %Su %Sg %Z %Sm %N%T%SY"
10149cab1c7SVivek Prakash #define SHELL_FORMAT \
10249cab1c7SVivek Prakash 	"st_dev=%d st_ino=%i st_mode=%#p st_nlink=%l " \
10349cab1c7SVivek Prakash 	"st_uid=%u st_gid=%g st_rdev=%r st_size=%z " \
104*84d9c625SLionel Sambuc 	"st_atime=%Sa st_mtime=%Sm st_ctime=%Sc " SHELL_B \
10549cab1c7SVivek Prakash 	"st_blksize=%k st_blocks=%b" SHELL_F
10649cab1c7SVivek Prakash #define LINUX_FORMAT \
10749cab1c7SVivek Prakash 	"  File: \"%N\"%n" \
10849cab1c7SVivek Prakash 	"  Size: %-11z  FileType: %HT%n" \
10949cab1c7SVivek Prakash 	"  Mode: (%04OLp/%.10Sp)         Uid: (%5u/%8Su)  Gid: (%5g/%8Sg)%n" \
11049cab1c7SVivek Prakash 	"Device: %Hd,%Ld   Inode: %i    Links: %l%n" \
11149cab1c7SVivek Prakash 	"Access: %Sa%n" \
11249cab1c7SVivek Prakash 	"Modify: %Sm%n" \
11349cab1c7SVivek Prakash 	"Change: %Sc"
11449cab1c7SVivek Prakash 
11549cab1c7SVivek Prakash #define TIME_FORMAT	"%b %e %T %Y"
11649cab1c7SVivek Prakash 
11749cab1c7SVivek Prakash #define FLAG_POUND	0x01
11849cab1c7SVivek Prakash #define FLAG_SPACE	0x02
11949cab1c7SVivek Prakash #define FLAG_PLUS	0x04
12049cab1c7SVivek Prakash #define FLAG_ZERO	0x08
12149cab1c7SVivek Prakash #define FLAG_MINUS	0x10
12249cab1c7SVivek Prakash 
12349cab1c7SVivek Prakash /*
12449cab1c7SVivek Prakash  * These format characters must all be unique, except the magic one.
12549cab1c7SVivek Prakash  */
12649cab1c7SVivek Prakash #define FMT_MAGIC	'%'
12749cab1c7SVivek Prakash #define FMT_DOT		'.'
12849cab1c7SVivek Prakash 
12949cab1c7SVivek Prakash #define SIMPLE_NEWLINE	'n'
13049cab1c7SVivek Prakash #define SIMPLE_TAB	't'
13149cab1c7SVivek Prakash #define SIMPLE_PERCENT	'%'
13249cab1c7SVivek Prakash #define SIMPLE_NUMBER	'@'
13349cab1c7SVivek Prakash 
13449cab1c7SVivek Prakash #define FMT_POUND	'#'
13549cab1c7SVivek Prakash #define FMT_SPACE	' '
13649cab1c7SVivek Prakash #define FMT_PLUS	'+'
13749cab1c7SVivek Prakash #define FMT_ZERO	'0'
13849cab1c7SVivek Prakash #define FMT_MINUS	'-'
13949cab1c7SVivek Prakash 
14049cab1c7SVivek Prakash #define FMT_DECIMAL 	'D'
14149cab1c7SVivek Prakash #define FMT_OCTAL 	'O'
14249cab1c7SVivek Prakash #define FMT_UNSIGNED 	'U'
14349cab1c7SVivek Prakash #define FMT_HEX 	'X'
14449cab1c7SVivek Prakash #define FMT_FLOAT 	'F'
14549cab1c7SVivek Prakash #define FMT_STRING 	'S'
14649cab1c7SVivek Prakash 
14749cab1c7SVivek Prakash #define FMTF_DECIMAL	0x01
14849cab1c7SVivek Prakash #define FMTF_OCTAL	0x02
14949cab1c7SVivek Prakash #define FMTF_UNSIGNED	0x04
15049cab1c7SVivek Prakash #define FMTF_HEX	0x08
15149cab1c7SVivek Prakash #define FMTF_FLOAT	0x10
15249cab1c7SVivek Prakash #define FMTF_STRING	0x20
15349cab1c7SVivek Prakash 
15449cab1c7SVivek Prakash #define HIGH_PIECE	'H'
15549cab1c7SVivek Prakash #define MIDDLE_PIECE	'M'
15649cab1c7SVivek Prakash #define LOW_PIECE	'L'
15749cab1c7SVivek Prakash 
15849cab1c7SVivek Prakash #define	SHOW_realpath	'R'
15949cab1c7SVivek Prakash #define SHOW_st_dev	'd'
16049cab1c7SVivek Prakash #define SHOW_st_ino	'i'
16149cab1c7SVivek Prakash #define SHOW_st_mode	'p'
16249cab1c7SVivek Prakash #define SHOW_st_nlink	'l'
16349cab1c7SVivek Prakash #define SHOW_st_uid	'u'
16449cab1c7SVivek Prakash #define SHOW_st_gid	'g'
16549cab1c7SVivek Prakash #define SHOW_st_rdev	'r'
16649cab1c7SVivek Prakash #define SHOW_st_atime	'a'
16749cab1c7SVivek Prakash #define SHOW_st_mtime	'm'
16849cab1c7SVivek Prakash #define SHOW_st_ctime	'c'
16949cab1c7SVivek Prakash #define SHOW_st_btime	'B'
17049cab1c7SVivek Prakash #define SHOW_st_size	'z'
17149cab1c7SVivek Prakash #define SHOW_st_blocks	'b'
17249cab1c7SVivek Prakash #define SHOW_st_blksize	'k'
17349cab1c7SVivek Prakash #define SHOW_st_flags	'f'
17449cab1c7SVivek Prakash #define SHOW_st_gen	'v'
17549cab1c7SVivek Prakash #define SHOW_symlink	'Y'
17649cab1c7SVivek Prakash #define SHOW_filetype	'T'
17749cab1c7SVivek Prakash #define SHOW_filename	'N'
17849cab1c7SVivek Prakash #define SHOW_sizerdev	'Z'
17949cab1c7SVivek Prakash 
180474b24f9SLionel Sambuc static void	usage(const char *) __dead;
181474b24f9SLionel Sambuc static void	output(const struct stat *, const char *,
182474b24f9SLionel Sambuc 	    const char *, int, int, int);
183474b24f9SLionel Sambuc static int	format1(const struct stat *,	/* stat info */
18449cab1c7SVivek Prakash 	    const char *,		/* the file name */
18549cab1c7SVivek Prakash 	    const char *, int,		/* the format string itself */
18649cab1c7SVivek Prakash 	    char *, size_t,		/* a place to put the output */
18749cab1c7SVivek Prakash 	    int, int, int, int,		/* the parsed format */
188474b24f9SLionel Sambuc 	    int, int, int);
18949cab1c7SVivek Prakash 
190474b24f9SLionel Sambuc static const char *timefmt;
191474b24f9SLionel Sambuc static int linkfail;
19249cab1c7SVivek Prakash 
19349cab1c7SVivek Prakash #define addchar(s, c, nl) \
19449cab1c7SVivek Prakash 	do { \
19549cab1c7SVivek Prakash 		(void)fputc((c), (s)); \
19649cab1c7SVivek Prakash 		(*nl) = ((c) == '\n'); \
19749cab1c7SVivek Prakash 	} while (0/*CONSTCOND*/)
19849cab1c7SVivek Prakash 
19949cab1c7SVivek Prakash int
main(int argc,char * argv[])20049cab1c7SVivek Prakash main(int argc, char *argv[])
20149cab1c7SVivek Prakash {
20249cab1c7SVivek Prakash 	struct stat st;
20349cab1c7SVivek Prakash 	int ch, rc, errs, am_readlink;
20449cab1c7SVivek Prakash 	int lsF, fmtchar, usestat, fn, nonl, quiet;
20549cab1c7SVivek Prakash 	const char *statfmt, *options, *synopsis;
20649cab1c7SVivek Prakash 
20749cab1c7SVivek Prakash 	am_readlink = 0;
20849cab1c7SVivek Prakash 	lsF = 0;
20949cab1c7SVivek Prakash 	fmtchar = '\0';
21049cab1c7SVivek Prakash 	usestat = 0;
21149cab1c7SVivek Prakash 	nonl = 0;
21249cab1c7SVivek Prakash 	quiet = 0;
21349cab1c7SVivek Prakash 	linkfail = 0;
21449cab1c7SVivek Prakash 	statfmt = NULL;
21549cab1c7SVivek Prakash 	timefmt = NULL;
21649cab1c7SVivek Prakash 
217474b24f9SLionel Sambuc 	setprogname(argv[0]);
218474b24f9SLionel Sambuc 
21949cab1c7SVivek Prakash 	if (strcmp(getprogname(), "readlink") == 0) {
22049cab1c7SVivek Prakash 		am_readlink = 1;
221474b24f9SLionel Sambuc 		options = "fnqsv";
222474b24f9SLionel Sambuc 		synopsis = "[-fnqsv] [file ...]";
22349cab1c7SVivek Prakash 		statfmt = "%Y";
22449cab1c7SVivek Prakash 		fmtchar = 'f';
22549cab1c7SVivek Prakash 		quiet = 1;
22649cab1c7SVivek Prakash 	} else {
22749cab1c7SVivek Prakash 		options = "f:FlLnqrst:x";
22849cab1c7SVivek Prakash 		synopsis = "[-FlLnqrsx] [-f format] [-t timefmt] [file ...]";
22949cab1c7SVivek Prakash 	}
23049cab1c7SVivek Prakash 
23149cab1c7SVivek Prakash 	while ((ch = getopt(argc, argv, options)) != -1)
23249cab1c7SVivek Prakash 		switch (ch) {
23349cab1c7SVivek Prakash 		case 'F':
23449cab1c7SVivek Prakash 			lsF = 1;
23549cab1c7SVivek Prakash 			break;
23649cab1c7SVivek Prakash 		case 'L':
23749cab1c7SVivek Prakash 			usestat = 1;
23849cab1c7SVivek Prakash 			break;
23949cab1c7SVivek Prakash 		case 'n':
24049cab1c7SVivek Prakash 			nonl = 1;
24149cab1c7SVivek Prakash 			break;
24249cab1c7SVivek Prakash 		case 'q':
24349cab1c7SVivek Prakash 			quiet = 1;
24449cab1c7SVivek Prakash 			break;
24549cab1c7SVivek Prakash 		case 'f':
24649cab1c7SVivek Prakash 			if (am_readlink) {
24749cab1c7SVivek Prakash 				statfmt = "%R";
24849cab1c7SVivek Prakash 				break;
24949cab1c7SVivek Prakash 			}
25049cab1c7SVivek Prakash 			statfmt = optarg;
25149cab1c7SVivek Prakash 			/* FALLTHROUGH */
25249cab1c7SVivek Prakash 		case 'l':
25349cab1c7SVivek Prakash 		case 'r':
25449cab1c7SVivek Prakash 		case 's':
255474b24f9SLionel Sambuc 			if (am_readlink) {
256474b24f9SLionel Sambuc 				quiet = 1;
257474b24f9SLionel Sambuc 				break;
258474b24f9SLionel Sambuc 			}
259474b24f9SLionel Sambuc 			/*FALLTHROUGH*/
26049cab1c7SVivek Prakash 		case 'x':
26149cab1c7SVivek Prakash 			if (fmtchar != 0)
26249cab1c7SVivek Prakash 				errx(1, "can't use format '%c' with '%c'",
26349cab1c7SVivek Prakash 				    fmtchar, ch);
26449cab1c7SVivek Prakash 			fmtchar = ch;
26549cab1c7SVivek Prakash 			break;
26649cab1c7SVivek Prakash 		case 't':
26749cab1c7SVivek Prakash 			timefmt = optarg;
26849cab1c7SVivek Prakash 			break;
269474b24f9SLionel Sambuc 		case 'v':
270474b24f9SLionel Sambuc 			quiet = 0;
271474b24f9SLionel Sambuc 			break;
27249cab1c7SVivek Prakash 		default:
27349cab1c7SVivek Prakash 			usage(synopsis);
27449cab1c7SVivek Prakash 		}
27549cab1c7SVivek Prakash 
27649cab1c7SVivek Prakash 	argc -= optind;
27749cab1c7SVivek Prakash 	argv += optind;
27849cab1c7SVivek Prakash 	fn = 1;
27949cab1c7SVivek Prakash 
28049cab1c7SVivek Prakash 	if (fmtchar == '\0') {
28149cab1c7SVivek Prakash 		if (lsF)
28249cab1c7SVivek Prakash 			fmtchar = 'l';
28349cab1c7SVivek Prakash 		else {
28449cab1c7SVivek Prakash 			fmtchar = 'f';
28549cab1c7SVivek Prakash 			statfmt = DEF_FORMAT;
28649cab1c7SVivek Prakash 		}
28749cab1c7SVivek Prakash 	}
28849cab1c7SVivek Prakash 
28949cab1c7SVivek Prakash 	if (lsF && fmtchar != 'l')
29049cab1c7SVivek Prakash 		errx(1, "can't use format '%c' with -F", fmtchar);
29149cab1c7SVivek Prakash 
29249cab1c7SVivek Prakash 	switch (fmtchar) {
29349cab1c7SVivek Prakash 	case 'f':
29449cab1c7SVivek Prakash 		/* statfmt already set */
29549cab1c7SVivek Prakash 		break;
29649cab1c7SVivek Prakash 	case 'l':
29749cab1c7SVivek Prakash 		statfmt = lsF ? LSF_FORMAT : LS_FORMAT;
29849cab1c7SVivek Prakash 		break;
29949cab1c7SVivek Prakash 	case 'r':
30049cab1c7SVivek Prakash 		statfmt = RAW_FORMAT;
30149cab1c7SVivek Prakash 		break;
30249cab1c7SVivek Prakash 	case 's':
30349cab1c7SVivek Prakash 		statfmt = SHELL_FORMAT;
304*84d9c625SLionel Sambuc 		if (timefmt == NULL)
305*84d9c625SLionel Sambuc 			timefmt = "%s";
30649cab1c7SVivek Prakash 		break;
30749cab1c7SVivek Prakash 	case 'x':
30849cab1c7SVivek Prakash 		statfmt = LINUX_FORMAT;
30949cab1c7SVivek Prakash 		if (timefmt == NULL)
31049cab1c7SVivek Prakash 			timefmt = "%c";
31149cab1c7SVivek Prakash 		break;
31249cab1c7SVivek Prakash 	default:
31349cab1c7SVivek Prakash 		usage(synopsis);
31449cab1c7SVivek Prakash 		/*NOTREACHED*/
31549cab1c7SVivek Prakash 	}
31649cab1c7SVivek Prakash 
31749cab1c7SVivek Prakash 	if (timefmt == NULL)
31849cab1c7SVivek Prakash 		timefmt = TIME_FORMAT;
31949cab1c7SVivek Prakash 
32049cab1c7SVivek Prakash 	errs = 0;
32149cab1c7SVivek Prakash 	do {
32249cab1c7SVivek Prakash 		if (argc == 0)
32349cab1c7SVivek Prakash 			rc = fstat(STDIN_FILENO, &st);
32449cab1c7SVivek Prakash 		else if (usestat) {
32549cab1c7SVivek Prakash 			/*
32649cab1c7SVivek Prakash 			 * Try stat() and if it fails, fall back to
32749cab1c7SVivek Prakash 			 * lstat() just in case we're examining a
32849cab1c7SVivek Prakash 			 * broken symlink.
32949cab1c7SVivek Prakash 			 */
33049cab1c7SVivek Prakash 			if ((rc = stat(argv[0], &st)) == -1 &&
33149cab1c7SVivek Prakash 			    errno == ENOENT &&
33249cab1c7SVivek Prakash 			    (rc = lstat(argv[0], &st)) == -1)
33349cab1c7SVivek Prakash 				errno = ENOENT;
33449cab1c7SVivek Prakash 		}
33549cab1c7SVivek Prakash 		else
33649cab1c7SVivek Prakash 			rc = lstat(argv[0], &st);
33749cab1c7SVivek Prakash 
33849cab1c7SVivek Prakash 		if (rc == -1) {
33949cab1c7SVivek Prakash 			errs = 1;
34049cab1c7SVivek Prakash 			linkfail = 1;
34149cab1c7SVivek Prakash 			if (!quiet)
34249cab1c7SVivek Prakash 				warn("%s: %s",
34349cab1c7SVivek Prakash 				    argc == 0 ? "(stdin)" : argv[0],
34449cab1c7SVivek Prakash 				    usestat ? "stat" : "lstat");
34549cab1c7SVivek Prakash 		}
34649cab1c7SVivek Prakash 		else
347474b24f9SLionel Sambuc 			output(&st, argv[0], statfmt, fn, nonl, quiet);
34849cab1c7SVivek Prakash 
34949cab1c7SVivek Prakash 		argv++;
35049cab1c7SVivek Prakash 		argc--;
35149cab1c7SVivek Prakash 		fn++;
35249cab1c7SVivek Prakash 	} while (argc > 0);
35349cab1c7SVivek Prakash 
35449cab1c7SVivek Prakash 	return (am_readlink ? linkfail : errs);
35549cab1c7SVivek Prakash }
35649cab1c7SVivek Prakash 
357474b24f9SLionel Sambuc static void
usage(const char * synopsis)35849cab1c7SVivek Prakash usage(const char *synopsis)
35949cab1c7SVivek Prakash {
36049cab1c7SVivek Prakash 
36149cab1c7SVivek Prakash 	(void)fprintf(stderr, "usage: %s %s\n", getprogname(), synopsis);
36249cab1c7SVivek Prakash 	exit(1);
36349cab1c7SVivek Prakash }
36449cab1c7SVivek Prakash 
36549cab1c7SVivek Prakash /*
36649cab1c7SVivek Prakash  * Parses a format string.
36749cab1c7SVivek Prakash  */
368474b24f9SLionel Sambuc static void
output(const struct stat * st,const char * file,const char * statfmt,int fn,int nonl,int quiet)36949cab1c7SVivek Prakash output(const struct stat *st, const char *file,
370474b24f9SLionel Sambuc     const char *statfmt, int fn, int nonl, int quiet)
37149cab1c7SVivek Prakash {
37249cab1c7SVivek Prakash 	int flags, size, prec, ofmt, hilo, what;
373474b24f9SLionel Sambuc 	/*
374474b24f9SLionel Sambuc 	 * buf size is enough for an item of length PATH_MAX,
375474b24f9SLionel Sambuc 	 * multiplied by 4 for vis encoding, plus 4 for symlink
376474b24f9SLionel Sambuc 	 * " -> " prefix, plus 1 for \0 terminator.
377474b24f9SLionel Sambuc 	 */
378474b24f9SLionel Sambuc 	char buf[PATH_MAX * 4 + 4 + 1];
37949cab1c7SVivek Prakash 	const char *subfmt;
38049cab1c7SVivek Prakash 	int nl, t, i;
38149cab1c7SVivek Prakash 
38249cab1c7SVivek Prakash 	nl = 1;
38349cab1c7SVivek Prakash 	while (*statfmt != '\0') {
38449cab1c7SVivek Prakash 
38549cab1c7SVivek Prakash 		/*
38649cab1c7SVivek Prakash 		 * Non-format characters go straight out.
38749cab1c7SVivek Prakash 		 */
38849cab1c7SVivek Prakash 		if (*statfmt != FMT_MAGIC) {
38949cab1c7SVivek Prakash 			addchar(stdout, *statfmt, &nl);
39049cab1c7SVivek Prakash 			statfmt++;
39149cab1c7SVivek Prakash 			continue;
39249cab1c7SVivek Prakash 		}
39349cab1c7SVivek Prakash 
39449cab1c7SVivek Prakash 		/*
39549cab1c7SVivek Prakash 		 * The current format "substring" starts here,
39649cab1c7SVivek Prakash 		 * and then we skip the magic.
39749cab1c7SVivek Prakash 		 */
39849cab1c7SVivek Prakash 		subfmt = statfmt;
39949cab1c7SVivek Prakash 		statfmt++;
40049cab1c7SVivek Prakash 
40149cab1c7SVivek Prakash 		/*
40249cab1c7SVivek Prakash 		 * Some simple one-character "formats".
40349cab1c7SVivek Prakash 		 */
40449cab1c7SVivek Prakash 		switch (*statfmt) {
40549cab1c7SVivek Prakash 		case SIMPLE_NEWLINE:
40649cab1c7SVivek Prakash 			addchar(stdout, '\n', &nl);
40749cab1c7SVivek Prakash 			statfmt++;
40849cab1c7SVivek Prakash 			continue;
40949cab1c7SVivek Prakash 		case SIMPLE_TAB:
41049cab1c7SVivek Prakash 			addchar(stdout, '\t', &nl);
41149cab1c7SVivek Prakash 			statfmt++;
41249cab1c7SVivek Prakash 			continue;
41349cab1c7SVivek Prakash 		case SIMPLE_PERCENT:
41449cab1c7SVivek Prakash 			addchar(stdout, '%', &nl);
41549cab1c7SVivek Prakash 			statfmt++;
41649cab1c7SVivek Prakash 			continue;
41749cab1c7SVivek Prakash 		case SIMPLE_NUMBER: {
41849cab1c7SVivek Prakash 			char num[12], *p;
41949cab1c7SVivek Prakash 
42049cab1c7SVivek Prakash 			snprintf(num, sizeof(num), "%d", fn);
42149cab1c7SVivek Prakash 			for (p = &num[0]; *p; p++)
42249cab1c7SVivek Prakash 				addchar(stdout, *p, &nl);
42349cab1c7SVivek Prakash 			statfmt++;
42449cab1c7SVivek Prakash 			continue;
42549cab1c7SVivek Prakash 		}
42649cab1c7SVivek Prakash 		}
42749cab1c7SVivek Prakash 
42849cab1c7SVivek Prakash 		/*
42949cab1c7SVivek Prakash 		 * This must be an actual format string.  Format strings are
43049cab1c7SVivek Prakash 		 * similar to printf(3) formats up to a point, and are of
43149cab1c7SVivek Prakash 		 * the form:
43249cab1c7SVivek Prakash 		 *
43349cab1c7SVivek Prakash 		 *	%	required start of format
43449cab1c7SVivek Prakash 		 *	[-# +0]	opt. format characters
43549cab1c7SVivek Prakash 		 *	size	opt. field width
43649cab1c7SVivek Prakash 		 *	.	opt. decimal separator, followed by
43749cab1c7SVivek Prakash 		 *	prec	opt. precision
43849cab1c7SVivek Prakash 		 *	fmt	opt. output specifier (string, numeric, etc.)
43949cab1c7SVivek Prakash 		 *	sub	opt. sub field specifier (high, middle, low)
44049cab1c7SVivek Prakash 		 *	datum	required field specifier (size, mode, etc)
44149cab1c7SVivek Prakash 		 *
44249cab1c7SVivek Prakash 		 * Only the % and the datum selector are required.  All data
44349cab1c7SVivek Prakash 		 * have reasonable default output forms.  The "sub" specifier
44449cab1c7SVivek Prakash 		 * only applies to certain data (mode, dev, rdev, filetype).
44549cab1c7SVivek Prakash 		 * The symlink output defaults to STRING, yet will only emit
44649cab1c7SVivek Prakash 		 * the leading " -> " if STRING is explicitly specified.  The
44749cab1c7SVivek Prakash 		 * sizerdev datum will generate rdev output for character or
44849cab1c7SVivek Prakash 		 * block devices, and size output for all others.
449474b24f9SLionel Sambuc 		 * For STRING output, the # format requests vis encoding.
45049cab1c7SVivek Prakash 		 */
45149cab1c7SVivek Prakash 		flags = 0;
45249cab1c7SVivek Prakash 		do {
45349cab1c7SVivek Prakash 			if      (*statfmt == FMT_POUND)
45449cab1c7SVivek Prakash 				flags |= FLAG_POUND;
45549cab1c7SVivek Prakash 			else if (*statfmt == FMT_SPACE)
45649cab1c7SVivek Prakash 				flags |= FLAG_SPACE;
45749cab1c7SVivek Prakash 			else if (*statfmt == FMT_PLUS)
45849cab1c7SVivek Prakash 				flags |= FLAG_PLUS;
45949cab1c7SVivek Prakash 			else if (*statfmt == FMT_ZERO)
46049cab1c7SVivek Prakash 				flags |= FLAG_ZERO;
46149cab1c7SVivek Prakash 			else if (*statfmt == FMT_MINUS)
46249cab1c7SVivek Prakash 				flags |= FLAG_MINUS;
46349cab1c7SVivek Prakash 			else
46449cab1c7SVivek Prakash 				break;
46549cab1c7SVivek Prakash 			statfmt++;
46649cab1c7SVivek Prakash 		} while (1/*CONSTCOND*/);
46749cab1c7SVivek Prakash 
46849cab1c7SVivek Prakash 		size = -1;
46949cab1c7SVivek Prakash 		if (isdigit((unsigned)*statfmt)) {
47049cab1c7SVivek Prakash 			size = 0;
47149cab1c7SVivek Prakash 			while (isdigit((unsigned)*statfmt)) {
47249cab1c7SVivek Prakash 				size = (size * 10) + (*statfmt - '0');
47349cab1c7SVivek Prakash 				statfmt++;
47449cab1c7SVivek Prakash 				if (size < 0)
47549cab1c7SVivek Prakash 					goto badfmt;
47649cab1c7SVivek Prakash 			}
47749cab1c7SVivek Prakash 		}
47849cab1c7SVivek Prakash 
47949cab1c7SVivek Prakash 		prec = -1;
48049cab1c7SVivek Prakash 		if (*statfmt == FMT_DOT) {
48149cab1c7SVivek Prakash 			statfmt++;
48249cab1c7SVivek Prakash 
48349cab1c7SVivek Prakash 			prec = 0;
48449cab1c7SVivek Prakash 			while (isdigit((unsigned)*statfmt)) {
48549cab1c7SVivek Prakash 				prec = (prec * 10) + (*statfmt - '0');
48649cab1c7SVivek Prakash 				statfmt++;
48749cab1c7SVivek Prakash 				if (prec < 0)
48849cab1c7SVivek Prakash 					goto badfmt;
48949cab1c7SVivek Prakash 			}
49049cab1c7SVivek Prakash 		}
49149cab1c7SVivek Prakash 
49249cab1c7SVivek Prakash #define fmtcase(x, y)		case (y): (x) = (y); statfmt++; break
49349cab1c7SVivek Prakash #define fmtcasef(x, y, z)	case (y): (x) = (z); statfmt++; break
49449cab1c7SVivek Prakash 		switch (*statfmt) {
49549cab1c7SVivek Prakash 			fmtcasef(ofmt, FMT_DECIMAL,	FMTF_DECIMAL);
49649cab1c7SVivek Prakash 			fmtcasef(ofmt, FMT_OCTAL,	FMTF_OCTAL);
49749cab1c7SVivek Prakash 			fmtcasef(ofmt, FMT_UNSIGNED,	FMTF_UNSIGNED);
49849cab1c7SVivek Prakash 			fmtcasef(ofmt, FMT_HEX,		FMTF_HEX);
49949cab1c7SVivek Prakash 			fmtcasef(ofmt, FMT_FLOAT,	FMTF_FLOAT);
50049cab1c7SVivek Prakash 			fmtcasef(ofmt, FMT_STRING,	FMTF_STRING);
50149cab1c7SVivek Prakash 		default:
50249cab1c7SVivek Prakash 			ofmt = 0;
50349cab1c7SVivek Prakash 			break;
50449cab1c7SVivek Prakash 		}
50549cab1c7SVivek Prakash 
50649cab1c7SVivek Prakash 		switch (*statfmt) {
50749cab1c7SVivek Prakash 			fmtcase(hilo, HIGH_PIECE);
50849cab1c7SVivek Prakash 			fmtcase(hilo, MIDDLE_PIECE);
50949cab1c7SVivek Prakash 			fmtcase(hilo, LOW_PIECE);
51049cab1c7SVivek Prakash 		default:
51149cab1c7SVivek Prakash 			hilo = 0;
51249cab1c7SVivek Prakash 			break;
51349cab1c7SVivek Prakash 		}
51449cab1c7SVivek Prakash 
51549cab1c7SVivek Prakash 		switch (*statfmt) {
51649cab1c7SVivek Prakash 			fmtcase(what, SHOW_realpath);
51749cab1c7SVivek Prakash 			fmtcase(what, SHOW_st_dev);
51849cab1c7SVivek Prakash 			fmtcase(what, SHOW_st_ino);
51949cab1c7SVivek Prakash 			fmtcase(what, SHOW_st_mode);
52049cab1c7SVivek Prakash 			fmtcase(what, SHOW_st_nlink);
52149cab1c7SVivek Prakash 			fmtcase(what, SHOW_st_uid);
52249cab1c7SVivek Prakash 			fmtcase(what, SHOW_st_gid);
52349cab1c7SVivek Prakash 			fmtcase(what, SHOW_st_rdev);
52449cab1c7SVivek Prakash 			fmtcase(what, SHOW_st_atime);
52549cab1c7SVivek Prakash 			fmtcase(what, SHOW_st_mtime);
52649cab1c7SVivek Prakash 			fmtcase(what, SHOW_st_ctime);
52749cab1c7SVivek Prakash 			fmtcase(what, SHOW_st_btime);
52849cab1c7SVivek Prakash 			fmtcase(what, SHOW_st_size);
52949cab1c7SVivek Prakash 			fmtcase(what, SHOW_st_blocks);
53049cab1c7SVivek Prakash 			fmtcase(what, SHOW_st_blksize);
53149cab1c7SVivek Prakash 			fmtcase(what, SHOW_st_flags);
53249cab1c7SVivek Prakash 			fmtcase(what, SHOW_st_gen);
53349cab1c7SVivek Prakash 			fmtcase(what, SHOW_symlink);
53449cab1c7SVivek Prakash 			fmtcase(what, SHOW_filetype);
53549cab1c7SVivek Prakash 			fmtcase(what, SHOW_filename);
53649cab1c7SVivek Prakash 			fmtcase(what, SHOW_sizerdev);
53749cab1c7SVivek Prakash 		default:
53849cab1c7SVivek Prakash 			goto badfmt;
53949cab1c7SVivek Prakash 		}
54049cab1c7SVivek Prakash #undef fmtcasef
54149cab1c7SVivek Prakash #undef fmtcase
54249cab1c7SVivek Prakash 
54349cab1c7SVivek Prakash 		t = format1(st,
54449cab1c7SVivek Prakash 		     file,
54549cab1c7SVivek Prakash 		     subfmt, statfmt - subfmt,
54649cab1c7SVivek Prakash 		     buf, sizeof(buf),
547474b24f9SLionel Sambuc 		     flags, size, prec, ofmt, hilo, what, quiet);
54849cab1c7SVivek Prakash 
54949cab1c7SVivek Prakash 		for (i = 0; i < t && i < (int)(sizeof(buf) - 1); i++)
55049cab1c7SVivek Prakash 			addchar(stdout, buf[i], &nl);
55149cab1c7SVivek Prakash 
55249cab1c7SVivek Prakash 		continue;
55349cab1c7SVivek Prakash 
55449cab1c7SVivek Prakash 	badfmt:
55549cab1c7SVivek Prakash 		errx(1, "%.*s: bad format",
55649cab1c7SVivek Prakash 		    (int)(statfmt - subfmt + 1), subfmt);
55749cab1c7SVivek Prakash 	}
55849cab1c7SVivek Prakash 
55949cab1c7SVivek Prakash 	if (!nl && !nonl)
56049cab1c7SVivek Prakash 		(void)fputc('\n', stdout);
56149cab1c7SVivek Prakash 	(void)fflush(stdout);
56249cab1c7SVivek Prakash }
56349cab1c7SVivek Prakash 
56449cab1c7SVivek Prakash /*
56549cab1c7SVivek Prakash  * Arranges output according to a single parsed format substring.
56649cab1c7SVivek Prakash  */
567474b24f9SLionel Sambuc static int
format1(const struct stat * st,const char * file,const char * fmt,int flen,char * buf,size_t blen,int flags,int size,int prec,int ofmt,int hilo,int what,int quiet)56849cab1c7SVivek Prakash format1(const struct stat *st,
56949cab1c7SVivek Prakash     const char *file,
57049cab1c7SVivek Prakash     const char *fmt, int flen,
57149cab1c7SVivek Prakash     char *buf, size_t blen,
57249cab1c7SVivek Prakash     int flags, int size, int prec, int ofmt,
573474b24f9SLionel Sambuc     int hilo, int what, int quiet)
57449cab1c7SVivek Prakash {
57549cab1c7SVivek Prakash 	u_int64_t data;
57649cab1c7SVivek Prakash 	char *stmp, lfmt[24], tmp[20];
57749cab1c7SVivek Prakash 	const char *sdata;
578474b24f9SLionel Sambuc 	char smode[12], sid[12], path[PATH_MAX + 4], visbuf[PATH_MAX * 4 + 4];
57949cab1c7SVivek Prakash 	struct passwd *pw;
58049cab1c7SVivek Prakash 	struct group *gr;
58149cab1c7SVivek Prakash 	struct tm *tm;
58249cab1c7SVivek Prakash 	time_t secs;
58349cab1c7SVivek Prakash 	long nsecs;
584474b24f9SLionel Sambuc 	int l;
585474b24f9SLionel Sambuc 	int formats;	/* bitmap of allowed formats for this datum */
586474b24f9SLionel Sambuc 	int small;	/* true if datum is a small integer */
587474b24f9SLionel Sambuc 	int gottime;	/* true if secs and nsecs are valid */
588474b24f9SLionel Sambuc 	int shift;	/* powers of 2 to scale numbers before printing */
589474b24f9SLionel Sambuc 	size_t prefixlen; /* length of constant prefix for string data */
59049cab1c7SVivek Prakash 
59149cab1c7SVivek Prakash 	formats = 0;
59249cab1c7SVivek Prakash 	small = 0;
59349cab1c7SVivek Prakash 	gottime = 0;
59449cab1c7SVivek Prakash 	secs = 0;
59549cab1c7SVivek Prakash 	nsecs = 0;
59649cab1c7SVivek Prakash 	shift = 0;
597474b24f9SLionel Sambuc 	prefixlen = 0;
59849cab1c7SVivek Prakash 
59949cab1c7SVivek Prakash 	/*
60049cab1c7SVivek Prakash 	 * First, pick out the data and tweak it based on hilo or
60149cab1c7SVivek Prakash 	 * specified output format (symlink output only).
60249cab1c7SVivek Prakash 	 */
60349cab1c7SVivek Prakash 	switch (what) {
60449cab1c7SVivek Prakash 	case SHOW_st_dev:
60549cab1c7SVivek Prakash 	case SHOW_st_rdev:
60649cab1c7SVivek Prakash 		small = (sizeof(st->st_dev) == 4);
60749cab1c7SVivek Prakash 		data = (what == SHOW_st_dev) ? st->st_dev : st->st_rdev;
60849cab1c7SVivek Prakash #if HAVE_DEVNAME
60949cab1c7SVivek Prakash 		sdata = (what == SHOW_st_dev) ?
61049cab1c7SVivek Prakash 		    devname(st->st_dev, S_IFBLK) :
61149cab1c7SVivek Prakash 		    devname(st->st_rdev,
61249cab1c7SVivek Prakash 			S_ISCHR(st->st_mode) ? S_IFCHR :
61349cab1c7SVivek Prakash 			S_ISBLK(st->st_mode) ? S_IFBLK :
61449cab1c7SVivek Prakash 			0U);
61549cab1c7SVivek Prakash 		if (sdata == NULL)
61649cab1c7SVivek Prakash 			sdata = "???";
61749cab1c7SVivek Prakash #endif /* HAVE_DEVNAME */
61849cab1c7SVivek Prakash 		if (hilo == HIGH_PIECE) {
61949cab1c7SVivek Prakash 			data = major(data);
62049cab1c7SVivek Prakash 			hilo = 0;
62149cab1c7SVivek Prakash 		}
62249cab1c7SVivek Prakash 		else if (hilo == LOW_PIECE) {
62349cab1c7SVivek Prakash 			data = minor((unsigned)data);
62449cab1c7SVivek Prakash 			hilo = 0;
62549cab1c7SVivek Prakash 		}
62649cab1c7SVivek Prakash 		formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
62749cab1c7SVivek Prakash #if HAVE_DEVNAME
62849cab1c7SVivek Prakash 		    FMTF_STRING;
62949cab1c7SVivek Prakash #else /* HAVE_DEVNAME */
63049cab1c7SVivek Prakash 		    0;
63149cab1c7SVivek Prakash #endif /* HAVE_DEVNAME */
63249cab1c7SVivek Prakash 		if (ofmt == 0)
63349cab1c7SVivek Prakash 			ofmt = FMTF_UNSIGNED;
63449cab1c7SVivek Prakash 		break;
63549cab1c7SVivek Prakash 	case SHOW_st_ino:
63649cab1c7SVivek Prakash 		small = (sizeof(st->st_ino) == 4);
63749cab1c7SVivek Prakash 		data = st->st_ino;
63849cab1c7SVivek Prakash 		sdata = NULL;
63949cab1c7SVivek Prakash 		formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
64049cab1c7SVivek Prakash 		if (ofmt == 0)
64149cab1c7SVivek Prakash 			ofmt = FMTF_UNSIGNED;
64249cab1c7SVivek Prakash 		break;
64349cab1c7SVivek Prakash 	case SHOW_st_mode:
64449cab1c7SVivek Prakash 		small = (sizeof(st->st_mode) == 4);
64549cab1c7SVivek Prakash 		data = st->st_mode;
64649cab1c7SVivek Prakash 		strmode(st->st_mode, smode);
64749cab1c7SVivek Prakash 		stmp = smode;
64849cab1c7SVivek Prakash 		l = strlen(stmp);
64949cab1c7SVivek Prakash 		if (stmp[l - 1] == ' ')
65049cab1c7SVivek Prakash 			stmp[--l] = '\0';
65149cab1c7SVivek Prakash 		if (hilo == HIGH_PIECE) {
65249cab1c7SVivek Prakash 			data >>= 12;
65349cab1c7SVivek Prakash 			stmp += 1;
65449cab1c7SVivek Prakash 			stmp[3] = '\0';
65549cab1c7SVivek Prakash 			hilo = 0;
65649cab1c7SVivek Prakash 		}
65749cab1c7SVivek Prakash 		else if (hilo == MIDDLE_PIECE) {
65849cab1c7SVivek Prakash 			data = (data >> 9) & 07;
65949cab1c7SVivek Prakash 			stmp += 4;
66049cab1c7SVivek Prakash 			stmp[3] = '\0';
66149cab1c7SVivek Prakash 			hilo = 0;
66249cab1c7SVivek Prakash 		}
66349cab1c7SVivek Prakash 		else if (hilo == LOW_PIECE) {
66449cab1c7SVivek Prakash 			data &= 0777;
66549cab1c7SVivek Prakash 			stmp += 7;
66649cab1c7SVivek Prakash 			stmp[3] = '\0';
66749cab1c7SVivek Prakash 			hilo = 0;
66849cab1c7SVivek Prakash 		}
66949cab1c7SVivek Prakash 		sdata = stmp;
67049cab1c7SVivek Prakash 		formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
67149cab1c7SVivek Prakash 		    FMTF_STRING;
67249cab1c7SVivek Prakash 		if (ofmt == 0)
67349cab1c7SVivek Prakash 			ofmt = FMTF_OCTAL;
67449cab1c7SVivek Prakash 		break;
67549cab1c7SVivek Prakash 	case SHOW_st_nlink:
67649cab1c7SVivek Prakash 		small = (sizeof(st->st_dev) == 4);
67749cab1c7SVivek Prakash 		data = st->st_nlink;
67849cab1c7SVivek Prakash 		sdata = NULL;
67949cab1c7SVivek Prakash 		formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
68049cab1c7SVivek Prakash 		if (ofmt == 0)
68149cab1c7SVivek Prakash 			ofmt = FMTF_UNSIGNED;
68249cab1c7SVivek Prakash 		break;
68349cab1c7SVivek Prakash 	case SHOW_st_uid:
68449cab1c7SVivek Prakash 		small = (sizeof(st->st_uid) == 4);
68549cab1c7SVivek Prakash 		data = st->st_uid;
68649cab1c7SVivek Prakash 		if ((pw = getpwuid(st->st_uid)) != NULL)
68749cab1c7SVivek Prakash 			sdata = pw->pw_name;
68849cab1c7SVivek Prakash 		else {
68949cab1c7SVivek Prakash 			snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_uid);
69049cab1c7SVivek Prakash 			sdata = sid;
69149cab1c7SVivek Prakash 		}
69249cab1c7SVivek Prakash 		formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
69349cab1c7SVivek Prakash 		    FMTF_STRING;
69449cab1c7SVivek Prakash 		if (ofmt == 0)
69549cab1c7SVivek Prakash 			ofmt = FMTF_UNSIGNED;
69649cab1c7SVivek Prakash 		break;
69749cab1c7SVivek Prakash 	case SHOW_st_gid:
69849cab1c7SVivek Prakash 		small = (sizeof(st->st_gid) == 4);
69949cab1c7SVivek Prakash 		data = st->st_gid;
70049cab1c7SVivek Prakash 		if ((gr = getgrgid(st->st_gid)) != NULL)
70149cab1c7SVivek Prakash 			sdata = gr->gr_name;
70249cab1c7SVivek Prakash 		else {
70349cab1c7SVivek Prakash 			snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_gid);
70449cab1c7SVivek Prakash 			sdata = sid;
70549cab1c7SVivek Prakash 		}
70649cab1c7SVivek Prakash 		formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
70749cab1c7SVivek Prakash 		    FMTF_STRING;
70849cab1c7SVivek Prakash 		if (ofmt == 0)
70949cab1c7SVivek Prakash 			ofmt = FMTF_UNSIGNED;
71049cab1c7SVivek Prakash 		break;
71149cab1c7SVivek Prakash 	case SHOW_st_atime:
71249cab1c7SVivek Prakash 		gottime = 1;
71349cab1c7SVivek Prakash 		secs = st->st_atime;
71449cab1c7SVivek Prakash #if HAVE_STRUCT_STAT_ST_MTIMENSEC
71549cab1c7SVivek Prakash 		nsecs = st->st_atimensec;
71649cab1c7SVivek Prakash #endif
71749cab1c7SVivek Prakash 		/* FALLTHROUGH */
71849cab1c7SVivek Prakash 	case SHOW_st_mtime:
71949cab1c7SVivek Prakash 		if (!gottime) {
72049cab1c7SVivek Prakash 			gottime = 1;
72149cab1c7SVivek Prakash 			secs = st->st_mtime;
72249cab1c7SVivek Prakash #if HAVE_STRUCT_STAT_ST_MTIMENSEC
72349cab1c7SVivek Prakash 			nsecs = st->st_mtimensec;
72449cab1c7SVivek Prakash #endif
72549cab1c7SVivek Prakash 		}
72649cab1c7SVivek Prakash 		/* FALLTHROUGH */
72749cab1c7SVivek Prakash 	case SHOW_st_ctime:
72849cab1c7SVivek Prakash 		if (!gottime) {
72949cab1c7SVivek Prakash 			gottime = 1;
73049cab1c7SVivek Prakash 			secs = st->st_ctime;
73149cab1c7SVivek Prakash #if HAVE_STRUCT_STAT_ST_MTIMENSEC
73249cab1c7SVivek Prakash 			nsecs = st->st_ctimensec;
73349cab1c7SVivek Prakash #endif
73449cab1c7SVivek Prakash 		}
73549cab1c7SVivek Prakash 		/* FALLTHROUGH */
73649cab1c7SVivek Prakash #if HAVE_STRUCT_STAT_ST_BIRTHTIME
73749cab1c7SVivek Prakash 	case SHOW_st_btime:
73849cab1c7SVivek Prakash 		if (!gottime) {
73949cab1c7SVivek Prakash 			gottime = 1;
74049cab1c7SVivek Prakash 			secs = st->st_birthtime;
74149cab1c7SVivek Prakash #if HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC
74249cab1c7SVivek Prakash 			nsecs = st->st_birthtimensec;
74349cab1c7SVivek Prakash #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC */
74449cab1c7SVivek Prakash 		}
74549cab1c7SVivek Prakash #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
74649cab1c7SVivek Prakash 		small = (sizeof(secs) == 4);
74749cab1c7SVivek Prakash 		data = secs;
74849cab1c7SVivek Prakash 		tm = localtime(&secs);
74949cab1c7SVivek Prakash 		if (tm == NULL) {
75049cab1c7SVivek Prakash 			secs = 0;
75149cab1c7SVivek Prakash 			tm = localtime(&secs);
75249cab1c7SVivek Prakash 		}
75349cab1c7SVivek Prakash 		(void)strftime(path, sizeof(path), timefmt, tm);
75449cab1c7SVivek Prakash 		sdata = path;
75549cab1c7SVivek Prakash 		formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
75649cab1c7SVivek Prakash 		    FMTF_FLOAT | FMTF_STRING;
75749cab1c7SVivek Prakash 		if (ofmt == 0)
75849cab1c7SVivek Prakash 			ofmt = FMTF_DECIMAL;
75949cab1c7SVivek Prakash 		break;
76049cab1c7SVivek Prakash 	case SHOW_st_size:
76149cab1c7SVivek Prakash 		small = (sizeof(st->st_size) == 4);
76249cab1c7SVivek Prakash 		data = st->st_size;
76349cab1c7SVivek Prakash 		sdata = NULL;
76449cab1c7SVivek Prakash 		formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
76549cab1c7SVivek Prakash 		if (ofmt == 0)
76649cab1c7SVivek Prakash 			ofmt = FMTF_UNSIGNED;
76749cab1c7SVivek Prakash 		switch (hilo) {
76849cab1c7SVivek Prakash 		case HIGH_PIECE:
76949cab1c7SVivek Prakash 			shift = 30;	/* gigabytes */
77049cab1c7SVivek Prakash 			hilo = 0;
77149cab1c7SVivek Prakash 			break;
77249cab1c7SVivek Prakash 		case MIDDLE_PIECE:
77349cab1c7SVivek Prakash 			shift = 20;	/* megabytes */
77449cab1c7SVivek Prakash 			hilo = 0;
77549cab1c7SVivek Prakash 			break;
77649cab1c7SVivek Prakash 		case LOW_PIECE:
77749cab1c7SVivek Prakash 			shift = 10;	/* kilobytes */
77849cab1c7SVivek Prakash 			hilo = 0;
77949cab1c7SVivek Prakash 			break;
78049cab1c7SVivek Prakash 		}
78149cab1c7SVivek Prakash 		break;
78249cab1c7SVivek Prakash 	case SHOW_st_blocks:
78349cab1c7SVivek Prakash 		small = (sizeof(st->st_blocks) == 4);
78449cab1c7SVivek Prakash 		data = st->st_blocks;
78549cab1c7SVivek Prakash 		sdata = NULL;
78649cab1c7SVivek Prakash 		formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
78749cab1c7SVivek Prakash 		if (ofmt == 0)
78849cab1c7SVivek Prakash 			ofmt = FMTF_UNSIGNED;
78949cab1c7SVivek Prakash 		break;
79049cab1c7SVivek Prakash 	case SHOW_st_blksize:
79149cab1c7SVivek Prakash 		small = (sizeof(st->st_blksize) == 4);
79249cab1c7SVivek Prakash 		data = st->st_blksize;
79349cab1c7SVivek Prakash 		sdata = NULL;
79449cab1c7SVivek Prakash 		formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
79549cab1c7SVivek Prakash 		if (ofmt == 0)
79649cab1c7SVivek Prakash 			ofmt = FMTF_UNSIGNED;
79749cab1c7SVivek Prakash 		break;
79849cab1c7SVivek Prakash #if HAVE_STRUCT_STAT_ST_FLAGS
79949cab1c7SVivek Prakash 	case SHOW_st_flags:
80049cab1c7SVivek Prakash 		small = (sizeof(st->st_flags) == 4);
80149cab1c7SVivek Prakash 		data = st->st_flags;
80249cab1c7SVivek Prakash 		sdata = NULL;
80349cab1c7SVivek Prakash 		formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
80449cab1c7SVivek Prakash 		if (ofmt == 0)
80549cab1c7SVivek Prakash 			ofmt = FMTF_UNSIGNED;
80649cab1c7SVivek Prakash 		break;
80749cab1c7SVivek Prakash #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
80849cab1c7SVivek Prakash #if HAVE_STRUCT_STAT_ST_GEN
80949cab1c7SVivek Prakash 	case SHOW_st_gen:
81049cab1c7SVivek Prakash 		small = (sizeof(st->st_gen) == 4);
81149cab1c7SVivek Prakash 		data = st->st_gen;
81249cab1c7SVivek Prakash 		sdata = NULL;
81349cab1c7SVivek Prakash 		formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
81449cab1c7SVivek Prakash 		if (ofmt == 0)
81549cab1c7SVivek Prakash 			ofmt = FMTF_UNSIGNED;
81649cab1c7SVivek Prakash 		break;
81749cab1c7SVivek Prakash #endif /* HAVE_STRUCT_STAT_ST_GEN */
81849cab1c7SVivek Prakash 	case SHOW_realpath:
81949cab1c7SVivek Prakash 		small = 0;
82049cab1c7SVivek Prakash 		data = 0;
82149cab1c7SVivek Prakash 		if (file == NULL) {
82249cab1c7SVivek Prakash 			(void)strlcpy(path, "(stdin)", sizeof(path));
82349cab1c7SVivek Prakash 			sdata = path;
82449cab1c7SVivek Prakash 		} else {
82549cab1c7SVivek Prakash 			snprintf(path, sizeof(path), " -> ");
82649cab1c7SVivek Prakash 			if (realpath(file, path + 4) == NULL) {
827474b24f9SLionel Sambuc 				if (!quiet)
828474b24f9SLionel Sambuc 					warn("realpath `%s'", file);
82949cab1c7SVivek Prakash 				linkfail = 1;
83049cab1c7SVivek Prakash 				l = 0;
83149cab1c7SVivek Prakash 				path[0] = '\0';
83249cab1c7SVivek Prakash 			}
83349cab1c7SVivek Prakash 			sdata = path + (ofmt == FMTF_STRING ? 0 : 4);
834474b24f9SLionel Sambuc 			prefixlen = (ofmt == FMTF_STRING ? 4 : 0);
83549cab1c7SVivek Prakash 		}
83649cab1c7SVivek Prakash 
83749cab1c7SVivek Prakash 		formats = FMTF_STRING;
83849cab1c7SVivek Prakash 		if (ofmt == 0)
83949cab1c7SVivek Prakash 			ofmt = FMTF_STRING;
84049cab1c7SVivek Prakash 		break;
84149cab1c7SVivek Prakash 	case SHOW_symlink:
84249cab1c7SVivek Prakash 		small = 0;
84349cab1c7SVivek Prakash 		data = 0;
84449cab1c7SVivek Prakash 		if (S_ISLNK(st->st_mode)) {
84549cab1c7SVivek Prakash 			snprintf(path, sizeof(path), " -> ");
84649cab1c7SVivek Prakash 			l = readlink(file, path + 4, sizeof(path) - 4 - 1);
84749cab1c7SVivek Prakash 			if (l == -1) {
848474b24f9SLionel Sambuc 				if (!quiet)
849474b24f9SLionel Sambuc 					warn("readlink `%s'", file);
85049cab1c7SVivek Prakash 				linkfail = 1;
85149cab1c7SVivek Prakash 				l = 0;
85249cab1c7SVivek Prakash 				path[0] = '\0';
85349cab1c7SVivek Prakash 			}
85449cab1c7SVivek Prakash 			path[l + 4] = '\0';
85549cab1c7SVivek Prakash 			sdata = path + (ofmt == FMTF_STRING ? 0 : 4);
856474b24f9SLionel Sambuc 			prefixlen = (ofmt == FMTF_STRING ? 4 : 0);
85749cab1c7SVivek Prakash 		}
85849cab1c7SVivek Prakash 		else {
85949cab1c7SVivek Prakash 			linkfail = 1;
86049cab1c7SVivek Prakash 			sdata = "";
86149cab1c7SVivek Prakash 		}
86249cab1c7SVivek Prakash 		formats = FMTF_STRING;
86349cab1c7SVivek Prakash 		if (ofmt == 0)
86449cab1c7SVivek Prakash 			ofmt = FMTF_STRING;
86549cab1c7SVivek Prakash 		break;
86649cab1c7SVivek Prakash 	case SHOW_filetype:
86749cab1c7SVivek Prakash 		small = 0;
86849cab1c7SVivek Prakash 		data = 0;
86949cab1c7SVivek Prakash 		sdata = "";
87049cab1c7SVivek Prakash 		if (hilo == 0 || hilo == LOW_PIECE) {
87149cab1c7SVivek Prakash 			switch (st->st_mode & S_IFMT) {
87249cab1c7SVivek Prakash 			case S_IFIFO:	sdata = "|";			break;
87349cab1c7SVivek Prakash 			case S_IFDIR:	sdata = "/";			break;
87449cab1c7SVivek Prakash 			case S_IFREG:
87549cab1c7SVivek Prakash 				if (st->st_mode &
87649cab1c7SVivek Prakash 				    (S_IXUSR | S_IXGRP | S_IXOTH))
87749cab1c7SVivek Prakash 					sdata = "*";
87849cab1c7SVivek Prakash 				break;
87949cab1c7SVivek Prakash 			case S_IFLNK:	sdata = "@";			break;
88049cab1c7SVivek Prakash #ifdef S_IFSOCK
88149cab1c7SVivek Prakash 			case S_IFSOCK:	sdata = "=";			break;
88249cab1c7SVivek Prakash #endif
88349cab1c7SVivek Prakash #ifdef S_IFWHT
88449cab1c7SVivek Prakash 			case S_IFWHT:	sdata = "%";			break;
88549cab1c7SVivek Prakash #endif /* S_IFWHT */
88649cab1c7SVivek Prakash #ifdef S_IFDOOR
88749cab1c7SVivek Prakash 			case S_IFDOOR:	sdata = ">";			break;
88849cab1c7SVivek Prakash #endif /* S_IFDOOR */
88949cab1c7SVivek Prakash 			}
89049cab1c7SVivek Prakash 			hilo = 0;
89149cab1c7SVivek Prakash 		}
89249cab1c7SVivek Prakash 		else if (hilo == HIGH_PIECE) {
89349cab1c7SVivek Prakash 			switch (st->st_mode & S_IFMT) {
89449cab1c7SVivek Prakash 			case S_IFIFO:	sdata = "Fifo File";		break;
89549cab1c7SVivek Prakash 			case S_IFCHR:	sdata = "Character Device";	break;
89649cab1c7SVivek Prakash 			case S_IFDIR:	sdata = "Directory";		break;
89749cab1c7SVivek Prakash 			case S_IFBLK:	sdata = "Block Device";		break;
89849cab1c7SVivek Prakash 			case S_IFREG:	sdata = "Regular File";		break;
89949cab1c7SVivek Prakash 			case S_IFLNK:	sdata = "Symbolic Link";	break;
90049cab1c7SVivek Prakash #ifdef S_IFSOCK
90149cab1c7SVivek Prakash 			case S_IFSOCK:	sdata = "Socket";		break;
90249cab1c7SVivek Prakash #endif
90349cab1c7SVivek Prakash #ifdef S_IFWHT
90449cab1c7SVivek Prakash 			case S_IFWHT:	sdata = "Whiteout File";	break;
90549cab1c7SVivek Prakash #endif /* S_IFWHT */
90649cab1c7SVivek Prakash #ifdef S_IFDOOR
90749cab1c7SVivek Prakash 			case S_IFDOOR:	sdata = "Door";			break;
90849cab1c7SVivek Prakash #endif /* S_IFDOOR */
90949cab1c7SVivek Prakash 			default:	sdata = "???";			break;
91049cab1c7SVivek Prakash 			}
91149cab1c7SVivek Prakash 			hilo = 0;
91249cab1c7SVivek Prakash 		}
91349cab1c7SVivek Prakash 		formats = FMTF_STRING;
91449cab1c7SVivek Prakash 		if (ofmt == 0)
91549cab1c7SVivek Prakash 			ofmt = FMTF_STRING;
91649cab1c7SVivek Prakash 		break;
91749cab1c7SVivek Prakash 	case SHOW_filename:
91849cab1c7SVivek Prakash 		small = 0;
91949cab1c7SVivek Prakash 		data = 0;
92049cab1c7SVivek Prakash 		if (file == NULL) {
92149cab1c7SVivek Prakash 			(void)strlcpy(path, "(stdin)", sizeof(path));
92249cab1c7SVivek Prakash 			if (hilo == HIGH_PIECE || hilo == LOW_PIECE)
92349cab1c7SVivek Prakash 				hilo = 0;
92449cab1c7SVivek Prakash 		}
92549cab1c7SVivek Prakash 		else if (hilo == 0)
92649cab1c7SVivek Prakash 			(void)strlcpy(path, file, sizeof(path));
92749cab1c7SVivek Prakash 		else {
92849cab1c7SVivek Prakash 			char *s;
92949cab1c7SVivek Prakash 			(void)strlcpy(path, file, sizeof(path));
93049cab1c7SVivek Prakash 			s = strrchr(path, '/');
93149cab1c7SVivek Prakash 			if (s != NULL) {
93249cab1c7SVivek Prakash 				/* trim off trailing /'s */
93349cab1c7SVivek Prakash 				while (s != path &&
93449cab1c7SVivek Prakash 				    s[0] == '/' && s[1] == '\0')
93549cab1c7SVivek Prakash 					*s-- = '\0';
93649cab1c7SVivek Prakash 				s = strrchr(path, '/');
93749cab1c7SVivek Prakash 			}
93849cab1c7SVivek Prakash 			if (hilo == HIGH_PIECE) {
93949cab1c7SVivek Prakash 				if (s == NULL)
94049cab1c7SVivek Prakash 					(void)strlcpy(path, ".", sizeof(path));
94149cab1c7SVivek Prakash 				else {
94249cab1c7SVivek Prakash 					while (s != path && s[0] == '/')
94349cab1c7SVivek Prakash 						*s-- = '\0';
94449cab1c7SVivek Prakash 				}
94549cab1c7SVivek Prakash 				hilo = 0;
94649cab1c7SVivek Prakash 			}
94749cab1c7SVivek Prakash 			else if (hilo == LOW_PIECE) {
94849cab1c7SVivek Prakash 				if (s != NULL && s[1] != '\0')
94949cab1c7SVivek Prakash 					(void)strlcpy(path, s + 1,
95049cab1c7SVivek Prakash 						      sizeof(path));
95149cab1c7SVivek Prakash 				hilo = 0;
95249cab1c7SVivek Prakash 			}
95349cab1c7SVivek Prakash 		}
95449cab1c7SVivek Prakash 		sdata = path;
95549cab1c7SVivek Prakash 		formats = FMTF_STRING;
95649cab1c7SVivek Prakash 		if (ofmt == 0)
95749cab1c7SVivek Prakash 			ofmt = FMTF_STRING;
95849cab1c7SVivek Prakash 		break;
95949cab1c7SVivek Prakash 	case SHOW_sizerdev:
96049cab1c7SVivek Prakash 		if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
96149cab1c7SVivek Prakash 			char majdev[20], mindev[20];
96249cab1c7SVivek Prakash 			int l1, l2;
96349cab1c7SVivek Prakash 
96449cab1c7SVivek Prakash 			l1 = format1(st,
96549cab1c7SVivek Prakash 			    file,
96649cab1c7SVivek Prakash 			    fmt, flen,
96749cab1c7SVivek Prakash 			    majdev, sizeof(majdev),
96849cab1c7SVivek Prakash 			    flags, size, prec,
969474b24f9SLionel Sambuc 			    ofmt, HIGH_PIECE, SHOW_st_rdev, quiet);
97049cab1c7SVivek Prakash 			l2 = format1(st,
97149cab1c7SVivek Prakash 			    file,
97249cab1c7SVivek Prakash 			    fmt, flen,
97349cab1c7SVivek Prakash 			    mindev, sizeof(mindev),
97449cab1c7SVivek Prakash 			    flags, size, prec,
975474b24f9SLionel Sambuc 			    ofmt, LOW_PIECE, SHOW_st_rdev, quiet);
97649cab1c7SVivek Prakash 			return (snprintf(buf, blen, "%.*s,%.*s",
97749cab1c7SVivek Prakash 			    l1, majdev, l2, mindev));
97849cab1c7SVivek Prakash 		}
97949cab1c7SVivek Prakash 		else {
98049cab1c7SVivek Prakash 			return (format1(st,
98149cab1c7SVivek Prakash 			    file,
98249cab1c7SVivek Prakash 			    fmt, flen,
98349cab1c7SVivek Prakash 			    buf, blen,
98449cab1c7SVivek Prakash 			    flags, size, prec,
985474b24f9SLionel Sambuc 			    ofmt, 0, SHOW_st_size, quiet));
98649cab1c7SVivek Prakash 		}
98749cab1c7SVivek Prakash 		/*NOTREACHED*/
98849cab1c7SVivek Prakash 	default:
98949cab1c7SVivek Prakash 		errx(1, "%.*s: bad format", (int)flen, fmt);
99049cab1c7SVivek Prakash 	}
99149cab1c7SVivek Prakash 
99249cab1c7SVivek Prakash 	/*
99349cab1c7SVivek Prakash 	 * If a subdatum was specified but not supported, or an output
99449cab1c7SVivek Prakash 	 * format was selected that is not supported, that's an error.
99549cab1c7SVivek Prakash 	 */
99649cab1c7SVivek Prakash 	if (hilo != 0 || (ofmt & formats) == 0)
99749cab1c7SVivek Prakash 		errx(1, "%.*s: bad format", (int)flen, fmt);
99849cab1c7SVivek Prakash 
99949cab1c7SVivek Prakash 	/*
1000474b24f9SLionel Sambuc 	 * FLAG_POUND with FMTF_STRING means use vis(3) encoding.
1001474b24f9SLionel Sambuc 	 * First prefixlen chars are not encoded.
1002474b24f9SLionel Sambuc 	 */
1003474b24f9SLionel Sambuc 	if ((flags & FLAG_POUND) != 0 && ofmt == FMTF_STRING) {
1004474b24f9SLionel Sambuc 		flags &= !FLAG_POUND;
1005474b24f9SLionel Sambuc 		strncpy(visbuf, sdata, prefixlen);
1006474b24f9SLionel Sambuc 		strnvis(visbuf + prefixlen, sizeof(visbuf) - prefixlen,
1007474b24f9SLionel Sambuc 		    sdata + prefixlen, VIS_WHITE | VIS_OCTAL | VIS_CSTYLE);
1008474b24f9SLionel Sambuc 		sdata = visbuf;
1009474b24f9SLionel Sambuc 	}
1010474b24f9SLionel Sambuc 
1011474b24f9SLionel Sambuc 	/*
101249cab1c7SVivek Prakash 	 * Assemble the format string for passing to printf(3).
101349cab1c7SVivek Prakash 	 */
101449cab1c7SVivek Prakash 	lfmt[0] = '\0';
101549cab1c7SVivek Prakash 	(void)strcat(lfmt, "%");
101649cab1c7SVivek Prakash 	if (flags & FLAG_POUND)
101749cab1c7SVivek Prakash 		(void)strcat(lfmt, "#");
101849cab1c7SVivek Prakash 	if (flags & FLAG_SPACE)
101949cab1c7SVivek Prakash 		(void)strcat(lfmt, " ");
102049cab1c7SVivek Prakash 	if (flags & FLAG_PLUS)
102149cab1c7SVivek Prakash 		(void)strcat(lfmt, "+");
102249cab1c7SVivek Prakash 	if (flags & FLAG_MINUS)
102349cab1c7SVivek Prakash 		(void)strcat(lfmt, "-");
102449cab1c7SVivek Prakash 	if (flags & FLAG_ZERO)
102549cab1c7SVivek Prakash 		(void)strcat(lfmt, "0");
102649cab1c7SVivek Prakash 
102749cab1c7SVivek Prakash 	/*
102849cab1c7SVivek Prakash 	 * Only the timespecs support the FLOAT output format, and that
102949cab1c7SVivek Prakash 	 * requires work that differs from the other formats.
103049cab1c7SVivek Prakash 	 */
103149cab1c7SVivek Prakash 	if (ofmt == FMTF_FLOAT) {
103249cab1c7SVivek Prakash 		/*
103349cab1c7SVivek Prakash 		 * Nothing after the decimal point, so just print seconds.
103449cab1c7SVivek Prakash 		 */
103549cab1c7SVivek Prakash 		if (prec == 0) {
103649cab1c7SVivek Prakash 			if (size != -1) {
103749cab1c7SVivek Prakash 				(void)snprintf(tmp, sizeof(tmp), "%d", size);
103849cab1c7SVivek Prakash 				(void)strcat(lfmt, tmp);
103949cab1c7SVivek Prakash 			}
104049cab1c7SVivek Prakash 			(void)strcat(lfmt, "lld");
104149cab1c7SVivek Prakash 			return (snprintf(buf, blen, lfmt,
104249cab1c7SVivek Prakash 			    (long long)secs));
104349cab1c7SVivek Prakash 		}
104449cab1c7SVivek Prakash 
104549cab1c7SVivek Prakash 		/*
104649cab1c7SVivek Prakash 		 * Unspecified precision gets all the precision we have:
104749cab1c7SVivek Prakash 		 * 9 digits.
104849cab1c7SVivek Prakash 		 */
104949cab1c7SVivek Prakash 		if (prec == -1)
105049cab1c7SVivek Prakash 			prec = 9;
105149cab1c7SVivek Prakash 
105249cab1c7SVivek Prakash 		/*
105349cab1c7SVivek Prakash 		 * Adjust the size for the decimal point and the digits
105449cab1c7SVivek Prakash 		 * that will follow.
105549cab1c7SVivek Prakash 		 */
105649cab1c7SVivek Prakash 		size -= prec + 1;
105749cab1c7SVivek Prakash 
105849cab1c7SVivek Prakash 		/*
105949cab1c7SVivek Prakash 		 * Any leftover size that's legitimate will be used.
106049cab1c7SVivek Prakash 		 */
106149cab1c7SVivek Prakash 		if (size > 0) {
106249cab1c7SVivek Prakash 			(void)snprintf(tmp, sizeof(tmp), "%d", size);
106349cab1c7SVivek Prakash 			(void)strcat(lfmt, tmp);
106449cab1c7SVivek Prakash 		}
106549cab1c7SVivek Prakash 		/* Seconds: time_t cast to long long. */
106649cab1c7SVivek Prakash 		(void)strcat(lfmt, "lld");
106749cab1c7SVivek Prakash 
106849cab1c7SVivek Prakash 		/*
106949cab1c7SVivek Prakash 		 * The stuff after the decimal point always needs zero
107049cab1c7SVivek Prakash 		 * filling.
107149cab1c7SVivek Prakash 		 */
107249cab1c7SVivek Prakash 		(void)strcat(lfmt, ".%0");
107349cab1c7SVivek Prakash 
107449cab1c7SVivek Prakash 		/*
107549cab1c7SVivek Prakash 		 * We can "print" at most nine digits of precision.  The
107649cab1c7SVivek Prakash 		 * rest we will pad on at the end.
107749cab1c7SVivek Prakash 		 *
107849cab1c7SVivek Prakash 		 * Nanoseconds: long.
107949cab1c7SVivek Prakash 		 */
108049cab1c7SVivek Prakash 		(void)snprintf(tmp, sizeof(tmp), "%dld", prec > 9 ? 9 : prec);
108149cab1c7SVivek Prakash 		(void)strcat(lfmt, tmp);
108249cab1c7SVivek Prakash 
108349cab1c7SVivek Prakash 		/*
108449cab1c7SVivek Prakash 		 * For precision of less that nine digits, trim off the
108549cab1c7SVivek Prakash 		 * less significant figures.
108649cab1c7SVivek Prakash 		 */
108749cab1c7SVivek Prakash 		for (; prec < 9; prec++)
108849cab1c7SVivek Prakash 			nsecs /= 10;
108949cab1c7SVivek Prakash 
109049cab1c7SVivek Prakash 		/*
109149cab1c7SVivek Prakash 		 * Use the format, and then tack on any zeroes that
109249cab1c7SVivek Prakash 		 * might be required to make up the requested precision.
109349cab1c7SVivek Prakash 		 */
109449cab1c7SVivek Prakash 		l = snprintf(buf, blen, lfmt, (long long)secs, nsecs);
109549cab1c7SVivek Prakash 		for (; prec > 9 && l < (int)blen; prec--, l++)
109649cab1c7SVivek Prakash 			(void)strcat(buf, "0");
109749cab1c7SVivek Prakash 		return (l);
109849cab1c7SVivek Prakash 	}
109949cab1c7SVivek Prakash 
110049cab1c7SVivek Prakash 	/*
110149cab1c7SVivek Prakash 	 * Add on size and precision, if specified, to the format.
110249cab1c7SVivek Prakash 	 */
110349cab1c7SVivek Prakash 	if (size != -1) {
110449cab1c7SVivek Prakash 		(void)snprintf(tmp, sizeof(tmp), "%d", size);
110549cab1c7SVivek Prakash 		(void)strcat(lfmt, tmp);
110649cab1c7SVivek Prakash 	}
110749cab1c7SVivek Prakash 	if (prec != -1) {
110849cab1c7SVivek Prakash 		(void)snprintf(tmp, sizeof(tmp), ".%d", prec);
110949cab1c7SVivek Prakash 		(void)strcat(lfmt, tmp);
111049cab1c7SVivek Prakash 	}
111149cab1c7SVivek Prakash 
111249cab1c7SVivek Prakash 	/*
111349cab1c7SVivek Prakash 	 * String output uses the temporary sdata.
111449cab1c7SVivek Prakash 	 */
111549cab1c7SVivek Prakash 	if (ofmt == FMTF_STRING) {
111649cab1c7SVivek Prakash 		if (sdata == NULL)
111749cab1c7SVivek Prakash 			errx(1, "%.*s: bad format", (int)flen, fmt);
111849cab1c7SVivek Prakash 		(void)strcat(lfmt, "s");
111949cab1c7SVivek Prakash 		return (snprintf(buf, blen, lfmt, sdata));
112049cab1c7SVivek Prakash 	}
112149cab1c7SVivek Prakash 
112249cab1c7SVivek Prakash 	/*
112349cab1c7SVivek Prakash 	 * Ensure that sign extension does not cause bad looking output
112449cab1c7SVivek Prakash 	 * for some forms.
112549cab1c7SVivek Prakash 	 */
112649cab1c7SVivek Prakash 	if (small && ofmt != FMTF_DECIMAL)
112749cab1c7SVivek Prakash 		data = (u_int32_t)data;
112849cab1c7SVivek Prakash 
112949cab1c7SVivek Prakash 	/*
113049cab1c7SVivek Prakash 	 * The four "numeric" output forms.
113149cab1c7SVivek Prakash 	 */
113249cab1c7SVivek Prakash 	(void)strcat(lfmt, "ll");
113349cab1c7SVivek Prakash 	switch (ofmt) {
113449cab1c7SVivek Prakash 	case FMTF_DECIMAL:	(void)strcat(lfmt, "d");	break;
113549cab1c7SVivek Prakash 	case FMTF_OCTAL:	(void)strcat(lfmt, "o");	break;
113649cab1c7SVivek Prakash 	case FMTF_UNSIGNED:	(void)strcat(lfmt, "u");	break;
113749cab1c7SVivek Prakash 	case FMTF_HEX:		(void)strcat(lfmt, "x");	break;
113849cab1c7SVivek Prakash 	}
113949cab1c7SVivek Prakash 
114049cab1c7SVivek Prakash 	/*
114149cab1c7SVivek Prakash 	 * shift and round to nearest for kilobytes, megabytes,
114249cab1c7SVivek Prakash 	 * gigabytes.
114349cab1c7SVivek Prakash 	 */
114449cab1c7SVivek Prakash 	if (shift > 0) {
114549cab1c7SVivek Prakash 		data >>= (shift - 1);
114649cab1c7SVivek Prakash 		data++;
114749cab1c7SVivek Prakash 		data >>= 1;
114849cab1c7SVivek Prakash 	}
114949cab1c7SVivek Prakash 
115049cab1c7SVivek Prakash 	return (snprintf(buf, blen, lfmt, data));
115149cab1c7SVivek Prakash }
1152