197952Sdougb/*
297952Sdougb * Copyright (c) 2002 The NetBSD Foundation, Inc.
397952Sdougb * All rights reserved.
497952Sdougb *
597952Sdougb * This code is derived from software contributed to The NetBSD Foundation
697952Sdougb * by Andrew Brown.
797952Sdougb *
897952Sdougb * Redistribution and use in source and binary forms, with or without
997952Sdougb * modification, are permitted provided that the following conditions
1097952Sdougb * are met:
1197952Sdougb * 1. Redistributions of source code must retain the above copyright
1297952Sdougb *    notice, this list of conditions and the following disclaimer.
1397952Sdougb * 2. Redistributions in binary form must reproduce the above copyright
1497952Sdougb *    notice, this list of conditions and the following disclaimer in the
1597952Sdougb *    documentation and/or other materials provided with the distribution.
1697952Sdougb *
1797952Sdougb * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1897952Sdougb * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
1997952Sdougb * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2097952Sdougb * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2197952Sdougb * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2297952Sdougb * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2397952Sdougb * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2497952Sdougb * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2597952Sdougb * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2697952Sdougb * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2797952Sdougb * POSSIBILITY OF SUCH DAMAGE.
2897952Sdougb */
2997952Sdougb
30114924Sdougb#include <sys/cdefs.h>
31101773Sdougb#if 0
3297952Sdougb#ifndef lint
33218535Sdougb__RCSID("$NetBSD: stat.c,v 1.33 2011/01/15 22:54:10 njoly Exp $"
34216343Sdougb"$OpenBSD: stat.c,v 1.14 2009/06/24 09:44:25 sobrado Exp $");
3597952Sdougb#endif
36101773Sdougb#endif
3797952Sdougb
38101773Sdougb__FBSDID("$FreeBSD$");
39101773Sdougb
40120836Sdougb#if HAVE_CONFIG_H
41120836Sdougb#include "config.h"
42120836Sdougb#else  /* HAVE_CONFIG_H */
43120836Sdougb#define HAVE_STRUCT_STAT_ST_FLAGS 1
44120836Sdougb#define HAVE_STRUCT_STAT_ST_GEN 1
45120836Sdougb#define HAVE_STRUCT_STAT_ST_BIRTHTIME 1
46120836Sdougb#define HAVE_STRUCT_STAT_ST_MTIMENSEC 1
47120836Sdougb#define HAVE_DEVNAME 1
48120836Sdougb#endif /* HAVE_CONFIG_H */
49120836Sdougb
50188498Sed#include <sys/param.h>
51101897Sdougb#include <sys/types.h>
5297952Sdougb#include <sys/stat.h>
53101773Sdougb
54101773Sdougb#include <ctype.h>
5597952Sdougb#include <err.h>
56216196Sdougb#include <errno.h>
57101773Sdougb#include <grp.h>
58101897Sdougb#include <limits.h>
59188498Sed#include <paths.h>
60101773Sdougb#include <pwd.h>
6197952Sdougb#include <stdio.h>
6297952Sdougb#include <stdlib.h>
63101773Sdougb#include <string.h>
64101773Sdougb#include <time.h>
65101773Sdougb#include <unistd.h>
6697952Sdougb
67120836Sdougb#if HAVE_STRUCT_STAT_ST_FLAGS
68120836Sdougb#define DEF_F "%#Xf "
69120836Sdougb#define RAW_F "%f "
70120836Sdougb#define SHELL_F " st_flags=%f"
71120836Sdougb#else /* HAVE_STRUCT_STAT_ST_FLAGS */
72120836Sdougb#define DEF_F
73120836Sdougb#define RAW_F
74120836Sdougb#define SHELL_F
75120836Sdougb#endif /* HAVE_STRUCT_STAT_ST_FLAGS */
76120836Sdougb
77120836Sdougb#if HAVE_STRUCT_STAT_ST_BIRTHTIME
78120836Sdougb#define DEF_B "\"%SB\" "
79120836Sdougb#define RAW_B "%B "
80120836Sdougb#define SHELL_B "st_birthtime=%B "
81120836Sdougb#else /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
82120836Sdougb#define DEF_B
83120836Sdougb#define RAW_B
84120836Sdougb#define SHELL_B
85120836Sdougb#endif /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
86120836Sdougb
87120836Sdougb#if HAVE_STRUCT_STAT_ST_ATIM
88120836Sdougb#define st_atimespec st_atim
89120836Sdougb#define st_ctimespec st_ctim
90120836Sdougb#define st_mtimespec st_mtim
91120836Sdougb#endif /* HAVE_STRUCT_STAT_ST_ATIM */
92120836Sdougb
9397952Sdougb#define DEF_FORMAT \
94120836Sdougb	"%d %i %Sp %l %Su %Sg %r %z \"%Sa\" \"%Sm\" \"%Sc\" " DEF_B \
95120836Sdougb	"%k %b " DEF_F "%N"
96120836Sdougb#define RAW_FORMAT	"%d %i %#p %l %u %g %r %z %a %m %c " RAW_B \
97120836Sdougb	"%k %b " RAW_F "%N"
9897952Sdougb#define LS_FORMAT	"%Sp %l %Su %Sg %Z %Sm %N%SY"
9997952Sdougb#define LSF_FORMAT	"%Sp %l %Su %Sg %Z %Sm %N%T%SY"
10097952Sdougb#define SHELL_FORMAT \
10197952Sdougb	"st_dev=%d st_ino=%i st_mode=%#p st_nlink=%l " \
10297952Sdougb	"st_uid=%u st_gid=%g st_rdev=%r st_size=%z " \
103120836Sdougb	"st_atime=%a st_mtime=%m st_ctime=%c " SHELL_B \
104120836Sdougb	"st_blksize=%k st_blocks=%b" SHELL_F
10597952Sdougb#define LINUX_FORMAT \
10697952Sdougb	"  File: \"%N\"%n" \
10797952Sdougb	"  Size: %-11z  FileType: %HT%n" \
108188481Smaxim	"  Mode: (%OMp%03OLp/%.10Sp)         Uid: (%5u/%8Su)  Gid: (%5g/%8Sg)%n" \
10997952Sdougb	"Device: %Hd,%Ld   Inode: %i    Links: %l%n" \
11097952Sdougb	"Access: %Sa%n" \
11197952Sdougb	"Modify: %Sm%n" \
11297952Sdougb	"Change: %Sc"
11397952Sdougb
11497952Sdougb#define TIME_FORMAT	"%b %e %T %Y"
11597952Sdougb
11697952Sdougb#define FLAG_POUND	0x01
11797952Sdougb#define FLAG_SPACE	0x02
11897952Sdougb#define FLAG_PLUS	0x04
11997952Sdougb#define FLAG_ZERO	0x08
12097952Sdougb#define FLAG_MINUS	0x10
12197952Sdougb
12297952Sdougb/*
12397952Sdougb * These format characters must all be unique, except the magic one.
12497952Sdougb */
12597952Sdougb#define FMT_MAGIC	'%'
12697952Sdougb#define FMT_DOT		'.'
12797952Sdougb
12897952Sdougb#define SIMPLE_NEWLINE	'n'
12997952Sdougb#define SIMPLE_TAB	't'
13097952Sdougb#define SIMPLE_PERCENT	'%'
13197952Sdougb#define SIMPLE_NUMBER	'@'
13297952Sdougb
13397952Sdougb#define FMT_POUND	'#'
13497952Sdougb#define FMT_SPACE	' '
13597952Sdougb#define FMT_PLUS	'+'
13697952Sdougb#define FMT_ZERO	'0'
13797952Sdougb#define FMT_MINUS	'-'
13897952Sdougb
13997952Sdougb#define FMT_DECIMAL 	'D'
14097952Sdougb#define FMT_OCTAL 	'O'
14197952Sdougb#define FMT_UNSIGNED 	'U'
14297952Sdougb#define FMT_HEX 	'X'
14397952Sdougb#define FMT_FLOAT 	'F'
14497952Sdougb#define FMT_STRING 	'S'
14597952Sdougb
146101773Sdougb#define FMTF_DECIMAL	0x01
147101773Sdougb#define FMTF_OCTAL	0x02
148101773Sdougb#define FMTF_UNSIGNED	0x04
149101773Sdougb#define FMTF_HEX	0x08
150101773Sdougb#define FMTF_FLOAT	0x10
151101773Sdougb#define FMTF_STRING	0x20
152101773Sdougb
15397952Sdougb#define HIGH_PIECE	'H'
15497952Sdougb#define MIDDLE_PIECE	'M'
15597952Sdougb#define LOW_PIECE	'L'
15697952Sdougb
157216203Sdougb#define	SHOW_realpath	'R'
15897952Sdougb#define SHOW_st_dev	'd'
15997952Sdougb#define SHOW_st_ino	'i'
16097952Sdougb#define SHOW_st_mode	'p'
16197952Sdougb#define SHOW_st_nlink	'l'
16297952Sdougb#define SHOW_st_uid	'u'
16397952Sdougb#define SHOW_st_gid	'g'
16497952Sdougb#define SHOW_st_rdev	'r'
16597952Sdougb#define SHOW_st_atime	'a'
16697952Sdougb#define SHOW_st_mtime	'm'
16797952Sdougb#define SHOW_st_ctime	'c'
168114924Sdougb#define SHOW_st_btime	'B'
16997952Sdougb#define SHOW_st_size	'z'
17097952Sdougb#define SHOW_st_blocks	'b'
17197952Sdougb#define SHOW_st_blksize	'k'
17297952Sdougb#define SHOW_st_flags	'f'
17397952Sdougb#define SHOW_st_gen	'v'
17497952Sdougb#define SHOW_symlink	'Y'
17597952Sdougb#define SHOW_filetype	'T'
17697952Sdougb#define SHOW_filename	'N'
17797952Sdougb#define SHOW_sizerdev	'Z'
17897952Sdougb
179101773Sdougbvoid	usage(const char *);
18097952Sdougbvoid	output(const struct stat *, const char *,
181216206Sdougb	    const char *, int, int);
18297952Sdougbint	format1(const struct stat *,	/* stat info */
18397952Sdougb	    const char *,		/* the file name */
18497952Sdougb	    const char *, int,		/* the format string itself */
18597952Sdougb	    char *, size_t,		/* a place to put the output */
18697952Sdougb	    int, int, int, int,		/* the parsed format */
18797952Sdougb	    int, int);
188207153Sjilles#if HAVE_STRUCT_STAT_ST_FLAGS
189207153Sjilleschar   *xfflagstostr(unsigned long);
190207153Sjilles#endif
19197952Sdougb
192241737Sedstatic const char *timefmt;
193241737Sedstatic int linkfail;
19497952Sdougb
195101773Sdougb#define addchar(s, c, nl) \
19697952Sdougb	do { \
197101773Sdougb		(void)fputc((c), (s)); \
198101773Sdougb		(*nl) = ((c) == '\n'); \
19997952Sdougb	} while (0/*CONSTCOND*/)
20097952Sdougb
20197952Sdougbint
20297952Sdougbmain(int argc, char *argv[])
20397952Sdougb{
20497952Sdougb	struct stat st;
205101773Sdougb	int ch, rc, errs, am_readlink;
206101773Sdougb	int lsF, fmtchar, usestat, fn, nonl, quiet;
207216205Sdougb	const char *statfmt, *options, *synopsis;
208188498Sed	char dname[sizeof _PATH_DEV + SPECNAMELEN] = _PATH_DEV;
209188498Sed	const char *file;
21097952Sdougb
211101773Sdougb	am_readlink = 0;
21297952Sdougb	lsF = 0;
21397952Sdougb	fmtchar = '\0';
21497952Sdougb	usestat = 0;
21597952Sdougb	nonl = 0;
216101773Sdougb	quiet = 0;
217101773Sdougb	linkfail = 0;
21897952Sdougb	statfmt = NULL;
21997952Sdougb	timefmt = NULL;
22097952Sdougb
221101773Sdougb	if (strcmp(getprogname(), "readlink") == 0) {
222101773Sdougb		am_readlink = 1;
223216203Sdougb		options = "fn";
224216203Sdougb		synopsis = "[-fn] [file ...]";
225101773Sdougb		statfmt = "%Y";
226101773Sdougb		fmtchar = 'f';
227101773Sdougb		quiet = 1;
228101773Sdougb	} else {
229101773Sdougb		options = "f:FlLnqrst:x";
230216343Sdougb		synopsis = "[-FLnq] [-f format | -l | -r | -s | -x] "
231216343Sdougb		    "[-t timefmt] [file ...]";
232101773Sdougb	}
233101773Sdougb
234101773Sdougb	while ((ch = getopt(argc, argv, options)) != -1)
23597952Sdougb		switch (ch) {
23697952Sdougb		case 'F':
23797952Sdougb			lsF = 1;
23897952Sdougb			break;
23997952Sdougb		case 'L':
24097952Sdougb			usestat = 1;
24197952Sdougb			break;
24297952Sdougb		case 'n':
24397952Sdougb			nonl = 1;
24497952Sdougb			break;
245101773Sdougb		case 'q':
246101773Sdougb			quiet = 1;
247101773Sdougb			break;
24897952Sdougb		case 'f':
249216203Sdougb			if (am_readlink) {
250216203Sdougb				statfmt = "%R";
251216203Sdougb				break;
252216203Sdougb			}
25397952Sdougb			statfmt = optarg;
25497952Sdougb			/* FALLTHROUGH */
25597952Sdougb		case 'l':
25697952Sdougb		case 'r':
25797952Sdougb		case 's':
25897952Sdougb		case 'x':
25997952Sdougb			if (fmtchar != 0)
26097952Sdougb				errx(1, "can't use format '%c' with '%c'",
26197952Sdougb				    fmtchar, ch);
26297952Sdougb			fmtchar = ch;
26397952Sdougb			break;
26497952Sdougb		case 't':
26597952Sdougb			timefmt = optarg;
26697952Sdougb			break;
26797952Sdougb		default:
268101773Sdougb			usage(synopsis);
26997952Sdougb		}
27097952Sdougb
27197952Sdougb	argc -= optind;
27297952Sdougb	argv += optind;
27397952Sdougb	fn = 1;
27497952Sdougb
27597952Sdougb	if (fmtchar == '\0') {
27697952Sdougb		if (lsF)
27797952Sdougb			fmtchar = 'l';
27897952Sdougb		else {
27997952Sdougb			fmtchar = 'f';
28097952Sdougb			statfmt = DEF_FORMAT;
28197952Sdougb		}
28297952Sdougb	}
28397952Sdougb
28497952Sdougb	if (lsF && fmtchar != 'l')
28597952Sdougb		errx(1, "can't use format '%c' with -F", fmtchar);
28697952Sdougb
28797952Sdougb	switch (fmtchar) {
28897952Sdougb	case 'f':
28997952Sdougb		/* statfmt already set */
29097952Sdougb		break;
29197952Sdougb	case 'l':
29297952Sdougb		statfmt = lsF ? LSF_FORMAT : LS_FORMAT;
29397952Sdougb		break;
29497952Sdougb	case 'r':
29597952Sdougb		statfmt = RAW_FORMAT;
29697952Sdougb		break;
29797952Sdougb	case 's':
29897952Sdougb		statfmt = SHELL_FORMAT;
29997952Sdougb		break;
30097952Sdougb	case 'x':
30197952Sdougb		statfmt = LINUX_FORMAT;
30297952Sdougb		if (timefmt == NULL)
30397952Sdougb			timefmt = "%c";
30497952Sdougb		break;
30597952Sdougb	default:
306101773Sdougb		usage(synopsis);
30797952Sdougb		/*NOTREACHED*/
30897952Sdougb	}
30997952Sdougb
31097952Sdougb	if (timefmt == NULL)
31197952Sdougb		timefmt = TIME_FORMAT;
31297952Sdougb
31397952Sdougb	errs = 0;
31497952Sdougb	do {
315188498Sed		if (argc == 0) {
316188498Sed			if (fdevname_r(STDIN_FILENO, dname +
317188498Sed			    sizeof _PATH_DEV - 1, SPECNAMELEN) != NULL)
318188498Sed				file = dname;
319188498Sed			else
320188498Sed				file = "(stdin)";
32197952Sdougb			rc = fstat(STDIN_FILENO, &st);
322188498Sed		} else {
323188498Sed			file = argv[0];
324216196Sdougb			if (usestat) {
325216196Sdougb				/*
326216196Sdougb				 * Try stat() and if it fails, fall back to
327216196Sdougb				 * lstat() just in case we're examining a
328216196Sdougb				 * broken symlink.
329216196Sdougb				 */
330216196Sdougb				if ((rc = stat(file, &st)) == -1 &&
331216196Sdougb				    errno == ENOENT &&
332216196Sdougb				    (rc = lstat(file, &st)) == -1)
333216196Sdougb					errno = ENOENT;
334216196Sdougb			}
335188498Sed			else
336188498Sed				rc = lstat(file, &st);
337188498Sed		}
33897952Sdougb
33997952Sdougb		if (rc == -1) {
34097952Sdougb			errs = 1;
341101773Sdougb			linkfail = 1;
342101773Sdougb			if (!quiet)
343188498Sed				warn("%s: stat", file);
34497952Sdougb		}
34597952Sdougb		else
346216206Sdougb			output(&st, file, statfmt, fn, nonl);
34797952Sdougb
34897952Sdougb		argv++;
34997952Sdougb		argc--;
35097952Sdougb		fn++;
35197952Sdougb	} while (argc > 0);
35297952Sdougb
353101773Sdougb	return (am_readlink ? linkfail : errs);
35497952Sdougb}
35597952Sdougb
356207153Sjilles#if HAVE_STRUCT_STAT_ST_FLAGS
357207153Sjilles/*
358207153Sjilles * fflagstostr() wrapper that leaks only once
359207153Sjilles */
360207153Sjilleschar *
361207153Sjillesxfflagstostr(unsigned long fflags)
362207153Sjilles{
363207153Sjilles	static char *str = NULL;
364207153Sjilles
365207153Sjilles	if (str != NULL)
366207153Sjilles		free(str);
367207153Sjilles
368207153Sjilles	str = fflagstostr(fflags);
369207153Sjilles	if (str == NULL)
370207153Sjilles		err(1, "fflagstostr");
371207153Sjilles	return (str);
372207153Sjilles}
373207153Sjilles#endif /* HAVE_STRUCT_STAT_ST_FLAGS */
374207153Sjilles
37597952Sdougbvoid
376101773Sdougbusage(const char *synopsis)
37797952Sdougb{
37897952Sdougb
379101773Sdougb	(void)fprintf(stderr, "usage: %s %s\n", getprogname(), synopsis);
38097952Sdougb	exit(1);
38197952Sdougb}
38297952Sdougb
38397952Sdougb/*
38497952Sdougb * Parses a format string.
38597952Sdougb */
38697952Sdougbvoid
38797952Sdougboutput(const struct stat *st, const char *file,
388216206Sdougb    const char *statfmt, int fn, int nonl)
38997952Sdougb{
39097952Sdougb	int flags, size, prec, ofmt, hilo, what;
391216202Sdougb	char buf[PATH_MAX + 4 + 1];
39297952Sdougb	const char *subfmt;
39397952Sdougb	int nl, t, i;
39497952Sdougb
395101773Sdougb	nl = 1;
39697952Sdougb	while (*statfmt != '\0') {
39797952Sdougb
39897952Sdougb		/*
39997952Sdougb		 * Non-format characters go straight out.
40097952Sdougb		 */
40197952Sdougb		if (*statfmt != FMT_MAGIC) {
402101773Sdougb			addchar(stdout, *statfmt, &nl);
40397952Sdougb			statfmt++;
40497952Sdougb			continue;
40597952Sdougb		}
40697952Sdougb
40797952Sdougb		/*
40897952Sdougb		 * The current format "substring" starts here,
40997952Sdougb		 * and then we skip the magic.
41097952Sdougb		 */
41197952Sdougb		subfmt = statfmt;
41297952Sdougb		statfmt++;
41397952Sdougb
41497952Sdougb		/*
41597952Sdougb		 * Some simple one-character "formats".
41697952Sdougb		 */
41797952Sdougb		switch (*statfmt) {
41897952Sdougb		case SIMPLE_NEWLINE:
419101773Sdougb			addchar(stdout, '\n', &nl);
42097952Sdougb			statfmt++;
42197952Sdougb			continue;
42297952Sdougb		case SIMPLE_TAB:
423101773Sdougb			addchar(stdout, '\t', &nl);
42497952Sdougb			statfmt++;
42597952Sdougb			continue;
42697952Sdougb		case SIMPLE_PERCENT:
427101773Sdougb			addchar(stdout, '%', &nl);
42897952Sdougb			statfmt++;
42997952Sdougb			continue;
43097952Sdougb		case SIMPLE_NUMBER: {
43197952Sdougb			char num[12], *p;
43297952Sdougb
43397952Sdougb			snprintf(num, sizeof(num), "%d", fn);
43497952Sdougb			for (p = &num[0]; *p; p++)
435101773Sdougb				addchar(stdout, *p, &nl);
43697952Sdougb			statfmt++;
43797952Sdougb			continue;
43897952Sdougb		}
43997952Sdougb		}
44097952Sdougb
44197952Sdougb		/*
44297952Sdougb		 * This must be an actual format string.  Format strings are
44397952Sdougb		 * similar to printf(3) formats up to a point, and are of
44497952Sdougb		 * the form:
44597952Sdougb		 *
44697952Sdougb		 *	%	required start of format
44797952Sdougb		 *	[-# +0]	opt. format characters
44897952Sdougb		 *	size	opt. field width
44997952Sdougb		 *	.	opt. decimal separator, followed by
45097952Sdougb		 *	prec	opt. precision
45197952Sdougb		 *	fmt	opt. output specifier (string, numeric, etc.)
45297952Sdougb		 *	sub	opt. sub field specifier (high, middle, low)
45397952Sdougb		 *	datum	required field specifier (size, mode, etc)
45497952Sdougb		 *
45597952Sdougb		 * Only the % and the datum selector are required.  All data
45697952Sdougb		 * have reasonable default output forms.  The "sub" specifier
45797952Sdougb		 * only applies to certain data (mode, dev, rdev, filetype).
45897952Sdougb		 * The symlink output defaults to STRING, yet will only emit
45997952Sdougb		 * the leading " -> " if STRING is explicitly specified.  The
46097952Sdougb		 * sizerdev datum will generate rdev output for character or
46197952Sdougb		 * block devices, and size output for all others.
46297952Sdougb		 */
46397952Sdougb		flags = 0;
46497952Sdougb		do {
46597952Sdougb			if      (*statfmt == FMT_POUND)
46697952Sdougb				flags |= FLAG_POUND;
46797952Sdougb			else if (*statfmt == FMT_SPACE)
46897952Sdougb				flags |= FLAG_SPACE;
46997952Sdougb			else if (*statfmt == FMT_PLUS)
47097952Sdougb				flags |= FLAG_PLUS;
47197952Sdougb			else if (*statfmt == FMT_ZERO)
47297952Sdougb				flags |= FLAG_ZERO;
47397952Sdougb			else if (*statfmt == FMT_MINUS)
47497952Sdougb				flags |= FLAG_MINUS;
47597952Sdougb			else
47697952Sdougb				break;
47797952Sdougb			statfmt++;
47897952Sdougb		} while (1/*CONSTCOND*/);
47997952Sdougb
48097952Sdougb		size = -1;
48197952Sdougb		if (isdigit((unsigned)*statfmt)) {
48297952Sdougb			size = 0;
48397952Sdougb			while (isdigit((unsigned)*statfmt)) {
48497952Sdougb				size = (size * 10) + (*statfmt - '0');
48597952Sdougb				statfmt++;
48697952Sdougb				if (size < 0)
48797952Sdougb					goto badfmt;
48897952Sdougb			}
48997952Sdougb		}
49097952Sdougb
49197952Sdougb		prec = -1;
49297952Sdougb		if (*statfmt == FMT_DOT) {
49397952Sdougb			statfmt++;
49497952Sdougb
49597952Sdougb			prec = 0;
49697952Sdougb			while (isdigit((unsigned)*statfmt)) {
49797952Sdougb				prec = (prec * 10) + (*statfmt - '0');
49897952Sdougb				statfmt++;
49997952Sdougb				if (prec < 0)
50097952Sdougb					goto badfmt;
50197952Sdougb			}
50297952Sdougb		}
50397952Sdougb
504101773Sdougb#define fmtcase(x, y)		case (y): (x) = (y); statfmt++; break
505101773Sdougb#define fmtcasef(x, y, z)	case (y): (x) = (z); statfmt++; break
50697952Sdougb		switch (*statfmt) {
507101773Sdougb			fmtcasef(ofmt, FMT_DECIMAL,	FMTF_DECIMAL);
508101773Sdougb			fmtcasef(ofmt, FMT_OCTAL,	FMTF_OCTAL);
509101773Sdougb			fmtcasef(ofmt, FMT_UNSIGNED,	FMTF_UNSIGNED);
510101773Sdougb			fmtcasef(ofmt, FMT_HEX,		FMTF_HEX);
511101773Sdougb			fmtcasef(ofmt, FMT_FLOAT,	FMTF_FLOAT);
512101773Sdougb			fmtcasef(ofmt, FMT_STRING,	FMTF_STRING);
51397952Sdougb		default:
51497952Sdougb			ofmt = 0;
51597952Sdougb			break;
51697952Sdougb		}
51797952Sdougb
51897952Sdougb		switch (*statfmt) {
51997952Sdougb			fmtcase(hilo, HIGH_PIECE);
52097952Sdougb			fmtcase(hilo, MIDDLE_PIECE);
52197952Sdougb			fmtcase(hilo, LOW_PIECE);
52297952Sdougb		default:
52397952Sdougb			hilo = 0;
52497952Sdougb			break;
52597952Sdougb		}
52697952Sdougb
52797952Sdougb		switch (*statfmt) {
528216203Sdougb			fmtcase(what, SHOW_realpath);
52997952Sdougb			fmtcase(what, SHOW_st_dev);
53097952Sdougb			fmtcase(what, SHOW_st_ino);
53197952Sdougb			fmtcase(what, SHOW_st_mode);
53297952Sdougb			fmtcase(what, SHOW_st_nlink);
53397952Sdougb			fmtcase(what, SHOW_st_uid);
53497952Sdougb			fmtcase(what, SHOW_st_gid);
53597952Sdougb			fmtcase(what, SHOW_st_rdev);
53697952Sdougb			fmtcase(what, SHOW_st_atime);
53797952Sdougb			fmtcase(what, SHOW_st_mtime);
53897952Sdougb			fmtcase(what, SHOW_st_ctime);
539114924Sdougb			fmtcase(what, SHOW_st_btime);
54097952Sdougb			fmtcase(what, SHOW_st_size);
54197952Sdougb			fmtcase(what, SHOW_st_blocks);
54297952Sdougb			fmtcase(what, SHOW_st_blksize);
54397952Sdougb			fmtcase(what, SHOW_st_flags);
54497952Sdougb			fmtcase(what, SHOW_st_gen);
54597952Sdougb			fmtcase(what, SHOW_symlink);
54697952Sdougb			fmtcase(what, SHOW_filetype);
54797952Sdougb			fmtcase(what, SHOW_filename);
54897952Sdougb			fmtcase(what, SHOW_sizerdev);
54997952Sdougb		default:
55097952Sdougb			goto badfmt;
55197952Sdougb		}
552101773Sdougb#undef fmtcasef
55397952Sdougb#undef fmtcase
55497952Sdougb
55597952Sdougb		t = format1(st,
55697952Sdougb		     file,
55797952Sdougb		     subfmt, statfmt - subfmt,
558101773Sdougb		     buf, sizeof(buf),
55997952Sdougb		     flags, size, prec, ofmt, hilo, what);
56097952Sdougb
561216205Sdougb		for (i = 0; i < t && i < (int)(sizeof(buf) - 1); i++)
562101773Sdougb			addchar(stdout, buf[i], &nl);
56397952Sdougb
56497952Sdougb		continue;
56597952Sdougb
56697952Sdougb	badfmt:
56797952Sdougb		errx(1, "%.*s: bad format",
56897952Sdougb		    (int)(statfmt - subfmt + 1), subfmt);
56997952Sdougb	}
57097952Sdougb
57197952Sdougb	if (!nl && !nonl)
572101773Sdougb		(void)fputc('\n', stdout);
573101773Sdougb	(void)fflush(stdout);
57497952Sdougb}
57597952Sdougb
57697952Sdougb/*
57797952Sdougb * Arranges output according to a single parsed format substring.
57897952Sdougb */
57997952Sdougbint
58097952Sdougbformat1(const struct stat *st,
58197952Sdougb    const char *file,
58297952Sdougb    const char *fmt, int flen,
58397952Sdougb    char *buf, size_t blen,
58497952Sdougb    int flags, int size, int prec, int ofmt,
58597952Sdougb    int hilo, int what)
58697952Sdougb{
58797952Sdougb	u_int64_t data;
588216205Sdougb	char *stmp, lfmt[24], tmp[20];
589216205Sdougb	const char *sdata;
590101773Sdougb	char smode[12], sid[12], path[PATH_MAX + 4];
59197952Sdougb	struct passwd *pw;
59297952Sdougb	struct group *gr;
59397952Sdougb	const struct timespec *tsp;
59497952Sdougb	struct timespec ts;
59597952Sdougb	struct tm *tm;
59697952Sdougb	int l, small, formats;
59797952Sdougb
59897952Sdougb	tsp = NULL;
59997952Sdougb	formats = 0;
60097952Sdougb	small = 0;
60197952Sdougb
60297952Sdougb	/*
60397952Sdougb	 * First, pick out the data and tweak it based on hilo or
60497952Sdougb	 * specified output format (symlink output only).
60597952Sdougb	 */
60697952Sdougb	switch (what) {
60797952Sdougb	case SHOW_st_dev:
60897952Sdougb	case SHOW_st_rdev:
60997952Sdougb		small = (sizeof(st->st_dev) == 4);
61097952Sdougb		data = (what == SHOW_st_dev) ? st->st_dev : st->st_rdev;
611120836Sdougb#if HAVE_DEVNAME
61297952Sdougb		sdata = (what == SHOW_st_dev) ?
61397952Sdougb		    devname(st->st_dev, S_IFBLK) :
61497952Sdougb		    devname(st->st_rdev,
61597952Sdougb		    S_ISCHR(st->st_mode) ? S_IFCHR :
61697952Sdougb		    S_ISBLK(st->st_mode) ? S_IFBLK :
61797952Sdougb		    0U);
61897952Sdougb		if (sdata == NULL)
61997952Sdougb			sdata = "???";
620120836Sdougb#endif /* HAVE_DEVNAME */
62197952Sdougb		if (hilo == HIGH_PIECE) {
62297952Sdougb			data = major(data);
62397952Sdougb			hilo = 0;
62497952Sdougb		}
62597952Sdougb		else if (hilo == LOW_PIECE) {
62697952Sdougb			data = minor((unsigned)data);
62797952Sdougb			hilo = 0;
62897952Sdougb		}
629101773Sdougb		formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
630120836Sdougb#if HAVE_DEVNAME
631101773Sdougb		    FMTF_STRING;
632120836Sdougb#else /* HAVE_DEVNAME */
633120836Sdougb		    0;
634120836Sdougb#endif /* HAVE_DEVNAME */
63597952Sdougb		if (ofmt == 0)
636101773Sdougb			ofmt = FMTF_UNSIGNED;
63797952Sdougb		break;
63897952Sdougb	case SHOW_st_ino:
63997952Sdougb		small = (sizeof(st->st_ino) == 4);
64097952Sdougb		data = st->st_ino;
64197952Sdougb		sdata = NULL;
642101773Sdougb		formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
64397952Sdougb		if (ofmt == 0)
644101773Sdougb			ofmt = FMTF_UNSIGNED;
64597952Sdougb		break;
64697952Sdougb	case SHOW_st_mode:
64797952Sdougb		small = (sizeof(st->st_mode) == 4);
64897952Sdougb		data = st->st_mode;
64997952Sdougb		strmode(st->st_mode, smode);
650216205Sdougb		stmp = smode;
651216205Sdougb		l = strlen(stmp);
652216205Sdougb		if (stmp[l - 1] == ' ')
653216205Sdougb			stmp[--l] = '\0';
65497952Sdougb		if (hilo == HIGH_PIECE) {
65597952Sdougb			data >>= 12;
656216205Sdougb			stmp += 1;
657216205Sdougb			stmp[3] = '\0';
65897952Sdougb			hilo = 0;
65997952Sdougb		}
66097952Sdougb		else if (hilo == MIDDLE_PIECE) {
66197952Sdougb			data = (data >> 9) & 07;
662216205Sdougb			stmp += 4;
663216205Sdougb			stmp[3] = '\0';
66497952Sdougb			hilo = 0;
66597952Sdougb		}
66697952Sdougb		else if (hilo == LOW_PIECE) {
66797952Sdougb			data &= 0777;
668216205Sdougb			stmp += 7;
669216205Sdougb			stmp[3] = '\0';
67097952Sdougb			hilo = 0;
67197952Sdougb		}
672216205Sdougb		sdata = stmp;
673101773Sdougb		formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
674101773Sdougb		    FMTF_STRING;
67597952Sdougb		if (ofmt == 0)
676101773Sdougb			ofmt = FMTF_OCTAL;
67797952Sdougb		break;
67897952Sdougb	case SHOW_st_nlink:
67997952Sdougb		small = (sizeof(st->st_dev) == 4);
68097952Sdougb		data = st->st_nlink;
68197952Sdougb		sdata = NULL;
682101773Sdougb		formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
68397952Sdougb		if (ofmt == 0)
684101773Sdougb			ofmt = FMTF_UNSIGNED;
68597952Sdougb		break;
68697952Sdougb	case SHOW_st_uid:
68797952Sdougb		small = (sizeof(st->st_uid) == 4);
68897952Sdougb		data = st->st_uid;
68997952Sdougb		if ((pw = getpwuid(st->st_uid)) != NULL)
69097952Sdougb			sdata = pw->pw_name;
69197952Sdougb		else {
69297952Sdougb			snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_uid);
69397952Sdougb			sdata = sid;
69497952Sdougb		}
695101773Sdougb		formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
696101773Sdougb		    FMTF_STRING;
69797952Sdougb		if (ofmt == 0)
698101773Sdougb			ofmt = FMTF_UNSIGNED;
69997952Sdougb		break;
70097952Sdougb	case SHOW_st_gid:
70197952Sdougb		small = (sizeof(st->st_gid) == 4);
70297952Sdougb		data = st->st_gid;
70397952Sdougb		if ((gr = getgrgid(st->st_gid)) != NULL)
70497952Sdougb			sdata = gr->gr_name;
70597952Sdougb		else {
70697952Sdougb			snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_gid);
70797952Sdougb			sdata = sid;
70897952Sdougb		}
709101773Sdougb		formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
710101773Sdougb		    FMTF_STRING;
71197952Sdougb		if (ofmt == 0)
712101773Sdougb			ofmt = FMTF_UNSIGNED;
71397952Sdougb		break;
71497952Sdougb	case SHOW_st_atime:
71597952Sdougb		tsp = &st->st_atimespec;
71697952Sdougb		/* FALLTHROUGH */
71797952Sdougb	case SHOW_st_mtime:
71897952Sdougb		if (tsp == NULL)
71997952Sdougb			tsp = &st->st_mtimespec;
72097952Sdougb		/* FALLTHROUGH */
72197952Sdougb	case SHOW_st_ctime:
72297952Sdougb		if (tsp == NULL)
72397952Sdougb			tsp = &st->st_ctimespec;
724114924Sdougb		/* FALLTHROUGH */
725120836Sdougb#if HAVE_STRUCT_STAT_ST_BIRTHTIME
726114924Sdougb	case SHOW_st_btime:
727114924Sdougb		if (tsp == NULL)
728114924Sdougb			tsp = &st->st_birthtimespec;
729120836Sdougb#endif /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
73097952Sdougb		ts = *tsp;		/* copy so we can muck with it */
73197952Sdougb		small = (sizeof(ts.tv_sec) == 4);
73297952Sdougb		data = ts.tv_sec;
73397952Sdougb		tm = localtime(&ts.tv_sec);
734218535Sdougb		if (tm == NULL) {
735218535Sdougb			ts.tv_sec = 0;
736218535Sdougb			tm = localtime(&ts.tv_sec);
737218535Sdougb		}
73897952Sdougb		(void)strftime(path, sizeof(path), timefmt, tm);
73997952Sdougb		sdata = path;
740101773Sdougb		formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
741101773Sdougb		    FMTF_FLOAT | FMTF_STRING;
74297952Sdougb		if (ofmt == 0)
743101773Sdougb			ofmt = FMTF_DECIMAL;
74497952Sdougb		break;
74597952Sdougb	case SHOW_st_size:
74697952Sdougb		small = (sizeof(st->st_size) == 4);
74797952Sdougb		data = st->st_size;
74897952Sdougb		sdata = NULL;
749101773Sdougb		formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
75097952Sdougb		if (ofmt == 0)
751101773Sdougb			ofmt = FMTF_UNSIGNED;
75297952Sdougb		break;
75397952Sdougb	case SHOW_st_blocks:
75497952Sdougb		small = (sizeof(st->st_blocks) == 4);
75597952Sdougb		data = st->st_blocks;
75697952Sdougb		sdata = NULL;
757101773Sdougb		formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
75897952Sdougb		if (ofmt == 0)
759101773Sdougb			ofmt = FMTF_UNSIGNED;
76097952Sdougb		break;
76197952Sdougb	case SHOW_st_blksize:
76297952Sdougb		small = (sizeof(st->st_blksize) == 4);
76397952Sdougb		data = st->st_blksize;
76497952Sdougb		sdata = NULL;
765101773Sdougb		formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
76697952Sdougb		if (ofmt == 0)
767101773Sdougb			ofmt = FMTF_UNSIGNED;
76897952Sdougb		break;
769120836Sdougb#if HAVE_STRUCT_STAT_ST_FLAGS
77097952Sdougb	case SHOW_st_flags:
77197952Sdougb		small = (sizeof(st->st_flags) == 4);
77297952Sdougb		data = st->st_flags;
773207153Sjilles		sdata = xfflagstostr(st->st_flags);
774207153Sjilles		if (*sdata == '\0')
775207153Sjilles			sdata = "-";
776207153Sjilles		formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
777207153Sjilles		    FMTF_STRING;
77897952Sdougb		if (ofmt == 0)
779101773Sdougb			ofmt = FMTF_UNSIGNED;
78097952Sdougb		break;
781120836Sdougb#endif /* HAVE_STRUCT_STAT_ST_FLAGS */
782120836Sdougb#if HAVE_STRUCT_STAT_ST_GEN
78397952Sdougb	case SHOW_st_gen:
78497952Sdougb		small = (sizeof(st->st_gen) == 4);
78597952Sdougb		data = st->st_gen;
78697952Sdougb		sdata = NULL;
787101773Sdougb		formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
78897952Sdougb		if (ofmt == 0)
789101773Sdougb			ofmt = FMTF_UNSIGNED;
79097952Sdougb		break;
791120836Sdougb#endif /* HAVE_STRUCT_STAT_ST_GEN */
792216203Sdougb	case SHOW_realpath:
793216203Sdougb		small = 0;
794216203Sdougb		data = 0;
795216203Sdougb		if (file == NULL) {
796216512Sdougb			(void)strlcpy(path, "(stdin)", sizeof(path));
797216203Sdougb			sdata = path;
798216203Sdougb		} else {
799216203Sdougb			snprintf(path, sizeof(path), " -> ");
800216203Sdougb			if (realpath(file, path + 4) == NULL) {
801216203Sdougb				linkfail = 1;
802216203Sdougb				l = 0;
803216203Sdougb				path[0] = '\0';
804216203Sdougb			}
805216203Sdougb			sdata = path + (ofmt == FMTF_STRING ? 0 : 4);
806216203Sdougb		}
807216203Sdougb
808216203Sdougb		formats = FMTF_STRING;
809216203Sdougb		if (ofmt == 0)
810216203Sdougb			ofmt = FMTF_STRING;
811216203Sdougb		break;
81297952Sdougb	case SHOW_symlink:
81397952Sdougb		small = 0;
81497952Sdougb		data = 0;
81597952Sdougb		if (S_ISLNK(st->st_mode)) {
81697952Sdougb			snprintf(path, sizeof(path), " -> ");
817105913Sdougb			l = readlink(file, path + 4, sizeof(path) - 4 - 1);
81897952Sdougb			if (l == -1) {
819101773Sdougb				linkfail = 1;
82097952Sdougb				l = 0;
82197952Sdougb				path[0] = '\0';
82297952Sdougb			}
82397952Sdougb			path[l + 4] = '\0';
824101773Sdougb			sdata = path + (ofmt == FMTF_STRING ? 0 : 4);
82597952Sdougb		}
826101773Sdougb		else {
827101773Sdougb			linkfail = 1;
82897952Sdougb			sdata = "";
829101773Sdougb		}
830101773Sdougb		formats = FMTF_STRING;
83197952Sdougb		if (ofmt == 0)
832101773Sdougb			ofmt = FMTF_STRING;
83397952Sdougb		break;
83497952Sdougb	case SHOW_filetype:
83597952Sdougb		small = 0;
83697952Sdougb		data = 0;
837216205Sdougb		sdata = "";
83897952Sdougb		if (hilo == 0 || hilo == LOW_PIECE) {
83997952Sdougb			switch (st->st_mode & S_IFMT) {
840216205Sdougb			case S_IFIFO:	sdata = "|";	break;
841216205Sdougb			case S_IFDIR:	sdata = "/";	break;
84297952Sdougb			case S_IFREG:
84397952Sdougb				if (st->st_mode &
84497952Sdougb				    (S_IXUSR | S_IXGRP | S_IXOTH))
845216205Sdougb					sdata = "*";
84697952Sdougb				break;
847216205Sdougb			case S_IFLNK:	sdata = "@";	break;
848216205Sdougb			case S_IFSOCK:	sdata = "=";	break;
849120836Sdougb#ifdef S_IFWHT
850216205Sdougb			case S_IFWHT:	sdata = "%";	break;
851120836Sdougb#endif /* S_IFWHT */
852120836Sdougb#ifdef S_IFDOOR
853216205Sdougb			case S_IFDOOR:	sdata = ">";	break;
854120836Sdougb#endif /* S_IFDOOR */
85597952Sdougb			}
85697952Sdougb			hilo = 0;
85797952Sdougb		}
85897952Sdougb		else if (hilo == HIGH_PIECE) {
85997952Sdougb			switch (st->st_mode & S_IFMT) {
86097952Sdougb			case S_IFIFO:	sdata = "Fifo File";		break;
86197952Sdougb			case S_IFCHR:	sdata = "Character Device";	break;
86297952Sdougb			case S_IFDIR:	sdata = "Directory";		break;
86397952Sdougb			case S_IFBLK:	sdata = "Block Device";		break;
86497952Sdougb			case S_IFREG:	sdata = "Regular File";		break;
86597952Sdougb			case S_IFLNK:	sdata = "Symbolic Link";	break;
86697952Sdougb			case S_IFSOCK:	sdata = "Socket";		break;
867120836Sdougb#ifdef S_IFWHT
86897952Sdougb			case S_IFWHT:	sdata = "Whiteout File";	break;
869120836Sdougb#endif /* S_IFWHT */
870120836Sdougb#ifdef S_IFDOOR
871120836Sdougb			case S_IFDOOR:	sdata = "Door";			break;
872120836Sdougb#endif /* S_IFDOOR */
87397952Sdougb			default:	sdata = "???";			break;
87497952Sdougb			}
87597952Sdougb			hilo = 0;
87697952Sdougb		}
877101773Sdougb		formats = FMTF_STRING;
87897952Sdougb		if (ofmt == 0)
879101773Sdougb			ofmt = FMTF_STRING;
88097952Sdougb		break;
88197952Sdougb	case SHOW_filename:
88297952Sdougb		small = 0;
88397952Sdougb		data = 0;
884216512Sdougb		(void)strlcpy(path, file, sizeof(path));
88597952Sdougb		sdata = path;
886101773Sdougb		formats = FMTF_STRING;
88797952Sdougb		if (ofmt == 0)
888101773Sdougb			ofmt = FMTF_STRING;
88997952Sdougb		break;
89097952Sdougb	case SHOW_sizerdev:
89197952Sdougb		if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
89297952Sdougb			char majdev[20], mindev[20];
89397952Sdougb			int l1, l2;
89497952Sdougb
89597952Sdougb			l1 = format1(st,
89697952Sdougb			    file,
89797952Sdougb			    fmt, flen,
89897952Sdougb			    majdev, sizeof(majdev),
89997952Sdougb			    flags, size, prec,
90097952Sdougb			    ofmt, HIGH_PIECE, SHOW_st_rdev);
90197952Sdougb			l2 = format1(st,
90297952Sdougb			    file,
90397952Sdougb			    fmt, flen,
90497952Sdougb			    mindev, sizeof(mindev),
90597952Sdougb			    flags, size, prec,
90697952Sdougb			    ofmt, LOW_PIECE, SHOW_st_rdev);
90797952Sdougb			return (snprintf(buf, blen, "%.*s,%.*s",
90897952Sdougb			    l1, majdev, l2, mindev));
90997952Sdougb		}
91097952Sdougb		else {
91197952Sdougb			return (format1(st,
91297952Sdougb			    file,
91397952Sdougb			    fmt, flen,
91497952Sdougb			    buf, blen,
91597952Sdougb			    flags, size, prec,
91697952Sdougb			    ofmt, 0, SHOW_st_size));
91797952Sdougb		}
91897952Sdougb		/*NOTREACHED*/
91997952Sdougb	default:
92097952Sdougb		errx(1, "%.*s: bad format", (int)flen, fmt);
92197952Sdougb	}
92297952Sdougb
92397952Sdougb	/*
92497952Sdougb	 * If a subdatum was specified but not supported, or an output
92597952Sdougb	 * format was selected that is not supported, that's an error.
92697952Sdougb	 */
92797952Sdougb	if (hilo != 0 || (ofmt & formats) == 0)
92897952Sdougb		errx(1, "%.*s: bad format", (int)flen, fmt);
92997952Sdougb
93097952Sdougb	/*
93197952Sdougb	 * Assemble the format string for passing to printf(3).
93297952Sdougb	 */
93397952Sdougb	lfmt[0] = '\0';
93497952Sdougb	(void)strcat(lfmt, "%");
93597952Sdougb	if (flags & FLAG_POUND)
93697952Sdougb		(void)strcat(lfmt, "#");
93797952Sdougb	if (flags & FLAG_SPACE)
93897952Sdougb		(void)strcat(lfmt, " ");
93997952Sdougb	if (flags & FLAG_PLUS)
94097952Sdougb		(void)strcat(lfmt, "+");
94197952Sdougb	if (flags & FLAG_MINUS)
94297952Sdougb		(void)strcat(lfmt, "-");
94397952Sdougb	if (flags & FLAG_ZERO)
94497952Sdougb		(void)strcat(lfmt, "0");
94597952Sdougb
94697952Sdougb	/*
94797952Sdougb	 * Only the timespecs support the FLOAT output format, and that
94897952Sdougb	 * requires work that differs from the other formats.
94997952Sdougb	 */
950101773Sdougb	if (ofmt == FMTF_FLOAT) {
95197952Sdougb		/*
95297952Sdougb		 * Nothing after the decimal point, so just print seconds.
95397952Sdougb		 */
95497952Sdougb		if (prec == 0) {
95597952Sdougb			if (size != -1) {
95697952Sdougb				(void)snprintf(tmp, sizeof(tmp), "%d", size);
95797952Sdougb				(void)strcat(lfmt, tmp);
95897952Sdougb			}
959216207Sdougb			(void)strcat(lfmt, "lld");
960216207Sdougb			return (snprintf(buf, blen, lfmt,
961216207Sdougb			    (long long)ts.tv_sec));
96297952Sdougb		}
96397952Sdougb
96497952Sdougb		/*
96597952Sdougb		 * Unspecified precision gets all the precision we have:
96697952Sdougb		 * 9 digits.
96797952Sdougb		 */
96897952Sdougb		if (prec == -1)
96997952Sdougb			prec = 9;
97097952Sdougb
97197952Sdougb		/*
97297952Sdougb		 * Adjust the size for the decimal point and the digits
97397952Sdougb		 * that will follow.
97497952Sdougb		 */
97597952Sdougb		size -= prec + 1;
97697952Sdougb
97797952Sdougb		/*
97897952Sdougb		 * Any leftover size that's legitimate will be used.
97997952Sdougb		 */
98097952Sdougb		if (size > 0) {
98197952Sdougb			(void)snprintf(tmp, sizeof(tmp), "%d", size);
98297952Sdougb			(void)strcat(lfmt, tmp);
98397952Sdougb		}
984216207Sdougb		/* Seconds: time_t cast to long long. */
985216207Sdougb		(void)strcat(lfmt, "lld");
98697952Sdougb
98797952Sdougb		/*
98897952Sdougb		 * The stuff after the decimal point always needs zero
98997952Sdougb		 * filling.
99097952Sdougb		 */
99197952Sdougb		(void)strcat(lfmt, ".%0");
99297952Sdougb
99397952Sdougb		/*
99497952Sdougb		 * We can "print" at most nine digits of precision.  The
99597952Sdougb		 * rest we will pad on at the end.
996216207Sdougb		 *
997216207Sdougb		 * Nanoseconds: long.
99897952Sdougb		 */
999216207Sdougb		(void)snprintf(tmp, sizeof(tmp), "%dld", prec > 9 ? 9 : prec);
100097952Sdougb		(void)strcat(lfmt, tmp);
100197952Sdougb
100297952Sdougb		/*
100397952Sdougb		 * For precision of less that nine digits, trim off the
100497952Sdougb		 * less significant figures.
100597952Sdougb		 */
100697952Sdougb		for (; prec < 9; prec++)
100797952Sdougb			ts.tv_nsec /= 10;
100897952Sdougb
100997952Sdougb		/*
101097952Sdougb		 * Use the format, and then tack on any zeroes that
101197952Sdougb		 * might be required to make up the requested precision.
101297952Sdougb		 */
1013216207Sdougb		l = snprintf(buf, blen, lfmt, (long long)ts.tv_sec, ts.tv_nsec);
1014216205Sdougb		for (; prec > 9 && l < (int)blen; prec--, l++)
101597952Sdougb			(void)strcat(buf, "0");
101697952Sdougb		return (l);
101797952Sdougb	}
101897952Sdougb
101997952Sdougb	/*
102097952Sdougb	 * Add on size and precision, if specified, to the format.
102197952Sdougb	 */
102297952Sdougb	if (size != -1) {
102397952Sdougb		(void)snprintf(tmp, sizeof(tmp), "%d", size);
102497952Sdougb		(void)strcat(lfmt, tmp);
102597952Sdougb	}
102697952Sdougb	if (prec != -1) {
102797952Sdougb		(void)snprintf(tmp, sizeof(tmp), ".%d", prec);
102897952Sdougb		(void)strcat(lfmt, tmp);
102997952Sdougb	}
103097952Sdougb
103197952Sdougb	/*
103297952Sdougb	 * String output uses the temporary sdata.
103397952Sdougb	 */
1034101773Sdougb	if (ofmt == FMTF_STRING) {
103597952Sdougb		if (sdata == NULL)
103697952Sdougb			errx(1, "%.*s: bad format", (int)flen, fmt);
103797952Sdougb		(void)strcat(lfmt, "s");
103897952Sdougb		return (snprintf(buf, blen, lfmt, sdata));
103997952Sdougb	}
104097952Sdougb
104197952Sdougb	/*
104297952Sdougb	 * Ensure that sign extension does not cause bad looking output
104397952Sdougb	 * for some forms.
104497952Sdougb	 */
1045101773Sdougb	if (small && ofmt != FMTF_DECIMAL)
104697952Sdougb		data = (u_int32_t)data;
104797952Sdougb
104897952Sdougb	/*
104997952Sdougb	 * The four "numeric" output forms.
105097952Sdougb	 */
105197952Sdougb	(void)strcat(lfmt, "ll");
105297952Sdougb	switch (ofmt) {
1053101773Sdougb	case FMTF_DECIMAL:	(void)strcat(lfmt, "d");	break;
1054101773Sdougb	case FMTF_OCTAL:		(void)strcat(lfmt, "o");	break;
1055101773Sdougb	case FMTF_UNSIGNED:	(void)strcat(lfmt, "u");	break;
1056101773Sdougb	case FMTF_HEX:		(void)strcat(lfmt, "x");	break;
105797952Sdougb	}
105897952Sdougb
105997952Sdougb	return (snprintf(buf, blen, lfmt, data));
106097952Sdougb}
1061