Home | History | Annotate | Line # | Download | only in stat
stat.c revision 1.5
      1  1.5  atatat /*	$NetBSD: stat.c,v 1.5 2002/07/09 17:22:26 atatat Exp $ */
      2  1.1  atatat 
      3  1.1  atatat /*
      4  1.1  atatat  * Copyright (c) 2002 The NetBSD Foundation, Inc.
      5  1.1  atatat  * All rights reserved.
      6  1.1  atatat  *
      7  1.1  atatat  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  atatat  * by Andrew Brown.
      9  1.1  atatat  *
     10  1.1  atatat  * Redistribution and use in source and binary forms, with or without
     11  1.1  atatat  * modification, are permitted provided that the following conditions
     12  1.1  atatat  * are met:
     13  1.1  atatat  * 1. Redistributions of source code must retain the above copyright
     14  1.1  atatat  *    notice, this list of conditions and the following disclaimer.
     15  1.1  atatat  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  atatat  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  atatat  *    documentation and/or other materials provided with the distribution.
     18  1.1  atatat  * 3. All advertising materials mentioning features or use of this software
     19  1.1  atatat  *    must display the following acknowledgement:
     20  1.1  atatat  *      This product includes software developed by the NetBSD
     21  1.1  atatat  *      Foundation, Inc. and its contributors.
     22  1.1  atatat  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1  atatat  *    contributors may be used to endorse or promote products derived
     24  1.1  atatat  *    from this software without specific prior written permission.
     25  1.1  atatat  *
     26  1.1  atatat  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1  atatat  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1  atatat  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1  atatat  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1  atatat  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1  atatat  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1  atatat  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1  atatat  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1  atatat  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1  atatat  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1  atatat  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1  atatat  */
     38  1.1  atatat 
     39  1.1  atatat #include <sys/cdefs.h>
     40  1.1  atatat #ifndef lint
     41  1.5  atatat __RCSID("$NetBSD: stat.c,v 1.5 2002/07/09 17:22:26 atatat Exp $");
     42  1.1  atatat #endif
     43  1.1  atatat 
     44  1.1  atatat #include <sys/types.h>
     45  1.1  atatat #include <sys/param.h>
     46  1.1  atatat #include <sys/stat.h>
     47  1.1  atatat #include <unistd.h>
     48  1.4  atatat #include <fcntl.h>
     49  1.1  atatat #include <err.h>
     50  1.1  atatat #include <string.h>
     51  1.1  atatat #include <stdio.h>
     52  1.1  atatat #include <ctype.h>
     53  1.1  atatat #include <stddef.h>
     54  1.1  atatat #include <stdlib.h>
     55  1.1  atatat #include <pwd.h>
     56  1.1  atatat #include <grp.h>
     57  1.1  atatat 
     58  1.1  atatat #define DEF_FORMAT \
     59  1.1  atatat 	"%d %i %Sp %l %Su %Sg %r %z \"%Sa\" \"%Sm\" \"%Sc\" %k %b %N"
     60  1.1  atatat #define RAW_FORMAT	"%d %i %#p %l %u %g %r %z %a %m %c %k %b %N"
     61  1.1  atatat #define LS_FORMAT	"%Sp %l %Su %Sg %Z %Sm %N%SY"
     62  1.1  atatat #define LSF_FORMAT	"%Sp %l %Su %Sg %Z %Sm %N%T%SY"
     63  1.1  atatat #define SHELL_FORMAT \
     64  1.1  atatat 	"st_dev=%d st_ino=%i st_mode=%#p st_nlink=%l " \
     65  1.1  atatat 	"st_uid=%u st_gid=%g st_rdev=%r st_size=%z " \
     66  1.1  atatat 	"st_atimespec=%a st_mtimespec=%m st_ctimespec=%c " \
     67  1.1  atatat 	"st_blksize=%k st_blocks=%b"
     68  1.1  atatat #define LINUX_FORMAT \
     69  1.1  atatat 	"  File: \"%N\"%n" \
     70  1.1  atatat 	"  Size: %-11z  FileType: %HT%n" \
     71  1.1  atatat 	"  Mode: (%04OLp/%.10Sp)         Uid: (%5u/%8Su)  Gid: (%5g/%8Sg)%n" \
     72  1.1  atatat 	"Device: %Hd,%Ld   Inode: %i    Links: %l%n" \
     73  1.1  atatat 	"Access: %Sa%n" \
     74  1.1  atatat 	"Modify: %Sm%n" \
     75  1.1  atatat 	"Change: %Sc"
     76  1.1  atatat 
     77  1.1  atatat #define TIME_FORMAT	"%b %e %T %Y"
     78  1.1  atatat 
     79  1.2  atatat #define FLAG_POUND	0x01
     80  1.2  atatat #define FLAG_SPACE	0x02
     81  1.2  atatat #define FLAG_PLUS	0x04
     82  1.2  atatat #define FLAG_ZERO	0x08
     83  1.2  atatat #define FLAG_MINUS	0x10
     84  1.1  atatat 
     85  1.1  atatat /*
     86  1.2  atatat  * These format characters must all be unique, except the magic one.
     87  1.1  atatat  */
     88  1.2  atatat #define FMT_MAGIC	'%'
     89  1.2  atatat #define FMT_DOT		'.'
     90  1.2  atatat 
     91  1.1  atatat #define SIMPLE_NEWLINE	'n'
     92  1.1  atatat #define SIMPLE_TAB	't'
     93  1.1  atatat #define SIMPLE_PERCENT	'%'
     94  1.2  atatat #define SIMPLE_NUMBER	'@'
     95  1.2  atatat 
     96  1.2  atatat #define FMT_POUND	'#'
     97  1.2  atatat #define FMT_SPACE	' '
     98  1.2  atatat #define FMT_PLUS	'+'
     99  1.2  atatat #define FMT_ZERO	'0'
    100  1.2  atatat #define FMT_MINUS	'-'
    101  1.1  atatat 
    102  1.1  atatat #define FMT_DECIMAL 	'D'
    103  1.1  atatat #define FMT_OCTAL 	'O'
    104  1.1  atatat #define FMT_UNSIGNED 	'U'
    105  1.1  atatat #define FMT_HEX 	'X'
    106  1.1  atatat #define FMT_FLOAT 	'F'
    107  1.1  atatat #define FMT_STRING 	'S'
    108  1.1  atatat 
    109  1.5  atatat #define FMTF_DECIMAL	0x01
    110  1.5  atatat #define FMTF_OCTAL	0x02
    111  1.5  atatat #define FMTF_UNSIGNED	0x04
    112  1.5  atatat #define FMTF_HEX	0x08
    113  1.5  atatat #define FMTF_FLOAT	0x10
    114  1.5  atatat #define FMTF_STRING	0x20
    115  1.5  atatat 
    116  1.1  atatat #define HIGH_PIECE	'H'
    117  1.1  atatat #define MIDDLE_PIECE	'M'
    118  1.1  atatat #define LOW_PIECE	'L'
    119  1.1  atatat 
    120  1.1  atatat #define SHOW_st_dev	'd'
    121  1.1  atatat #define SHOW_st_ino	'i'
    122  1.1  atatat #define SHOW_st_mode	'p'
    123  1.1  atatat #define SHOW_st_nlink	'l'
    124  1.1  atatat #define SHOW_st_uid	'u'
    125  1.1  atatat #define SHOW_st_gid	'g'
    126  1.1  atatat #define SHOW_st_rdev	'r'
    127  1.1  atatat #define SHOW_st_atime	'a'
    128  1.1  atatat #define SHOW_st_mtime	'm'
    129  1.1  atatat #define SHOW_st_ctime	'c'
    130  1.1  atatat #define SHOW_st_size	'z'
    131  1.1  atatat #define SHOW_st_blocks	'b'
    132  1.1  atatat #define SHOW_st_blksize	'k'
    133  1.1  atatat #define SHOW_st_flags	'f'
    134  1.1  atatat #define SHOW_st_gen	'v'
    135  1.1  atatat #define SHOW_symlink	'Y'
    136  1.1  atatat #define SHOW_filetype	'T'
    137  1.1  atatat #define SHOW_filename	'N'
    138  1.1  atatat #define SHOW_sizerdev	'Z'
    139  1.1  atatat 
    140  1.4  atatat void	usage(const char *);
    141  1.1  atatat void	output(const struct stat *, const char *,
    142  1.4  atatat 	    const char *, int, int, int);
    143  1.1  atatat int	format1(const struct stat *,	/* stat info */
    144  1.1  atatat 	    const char *,		/* the file name */
    145  1.1  atatat 	    const char *, int,		/* the format string itself */
    146  1.1  atatat 	    char *, size_t,		/* a place to put the output */
    147  1.1  atatat 	    int, int, int, int,		/* the parsed format */
    148  1.1  atatat 	    int, int);
    149  1.1  atatat 
    150  1.1  atatat char *timefmt;
    151  1.4  atatat int linkfail;
    152  1.1  atatat 
    153  1.4  atatat #define addchar(s, c, nl) \
    154  1.1  atatat 	do { \
    155  1.4  atatat 		(void)fputc((c), (s)); \
    156  1.4  atatat 		(*nl) = ((c) == '\n'); \
    157  1.1  atatat 	} while (0/*CONSTCOND*/)
    158  1.1  atatat 
    159  1.1  atatat int
    160  1.1  atatat main(int argc, char *argv[])
    161  1.1  atatat {
    162  1.1  atatat 	struct stat st;
    163  1.4  atatat 	int ch, rc, errs, am_readlink;
    164  1.4  atatat 	int lsF, fmtchar, usestat, fn, nonl, quiet;
    165  1.4  atatat 	char *statfmt, *options, *synopsis;
    166  1.1  atatat 
    167  1.4  atatat 	am_readlink = 0;
    168  1.1  atatat 	lsF = 0;
    169  1.1  atatat 	fmtchar = '\0';
    170  1.1  atatat 	usestat = 0;
    171  1.1  atatat 	nonl = 0;
    172  1.4  atatat 	quiet = 0;
    173  1.4  atatat 	linkfail = 0;
    174  1.1  atatat 	statfmt = NULL;
    175  1.1  atatat 	timefmt = NULL;
    176  1.1  atatat 
    177  1.4  atatat 	if (strcmp(getprogname(), "readlink") == 0) {
    178  1.4  atatat 		am_readlink = 1;
    179  1.4  atatat 		options = "n";
    180  1.4  atatat 		synopsis = "[-n] [file ...]";
    181  1.4  atatat 		statfmt = "%Y";
    182  1.4  atatat 		fmtchar = 'f';
    183  1.4  atatat 		quiet = 1;
    184  1.4  atatat 	} else {
    185  1.4  atatat 		options = "f:FlLnqrst:x";
    186  1.4  atatat 		synopsis = "[-FlLnqrsx] [-f format] [-t timefmt] [file ...]";
    187  1.4  atatat 	}
    188  1.4  atatat 
    189  1.4  atatat 	while ((ch = getopt(argc, argv, options)) != -1)
    190  1.1  atatat 		switch (ch) {
    191  1.1  atatat 		case 'F':
    192  1.1  atatat 			lsF = 1;
    193  1.1  atatat 			break;
    194  1.1  atatat 		case 'L':
    195  1.1  atatat 			usestat = 1;
    196  1.1  atatat 			break;
    197  1.1  atatat 		case 'n':
    198  1.1  atatat 			nonl = 1;
    199  1.1  atatat 			break;
    200  1.4  atatat 		case 'q':
    201  1.4  atatat 			quiet = 1;
    202  1.4  atatat 			break;
    203  1.1  atatat 		case 'f':
    204  1.1  atatat 			statfmt = optarg;
    205  1.1  atatat 			/* FALLTHROUGH */
    206  1.1  atatat 		case 'l':
    207  1.1  atatat 		case 'r':
    208  1.1  atatat 		case 's':
    209  1.1  atatat 		case 'x':
    210  1.1  atatat 			if (fmtchar != 0)
    211  1.1  atatat 				errx(1, "can't use format '%c' with '%c'",
    212  1.1  atatat 				    fmtchar, ch);
    213  1.1  atatat 			fmtchar = ch;
    214  1.1  atatat 			break;
    215  1.1  atatat 		case 't':
    216  1.1  atatat 			timefmt = optarg;
    217  1.1  atatat 			break;
    218  1.1  atatat 		default:
    219  1.4  atatat 			usage(synopsis);
    220  1.1  atatat 		}
    221  1.1  atatat 
    222  1.1  atatat 	argc -= optind;
    223  1.1  atatat 	argv += optind;
    224  1.2  atatat 	fn = 1;
    225  1.1  atatat 
    226  1.1  atatat 	if (fmtchar == '\0') {
    227  1.1  atatat 		if (lsF)
    228  1.1  atatat 			fmtchar = 'l';
    229  1.1  atatat 		else {
    230  1.1  atatat 			fmtchar = 'f';
    231  1.1  atatat 			statfmt = DEF_FORMAT;
    232  1.1  atatat 		}
    233  1.1  atatat 	}
    234  1.1  atatat 
    235  1.1  atatat 	if (lsF && fmtchar != 'l')
    236  1.1  atatat 		errx(1, "can't use format '%c' with -F", fmtchar);
    237  1.1  atatat 
    238  1.1  atatat 	switch (fmtchar) {
    239  1.1  atatat 	case 'f':
    240  1.1  atatat 		/* statfmt already set */
    241  1.1  atatat 		break;
    242  1.1  atatat 	case 'l':
    243  1.1  atatat 		statfmt = lsF ? LSF_FORMAT : LS_FORMAT;
    244  1.1  atatat 		break;
    245  1.1  atatat 	case 'r':
    246  1.1  atatat 		statfmt = RAW_FORMAT;
    247  1.1  atatat 		break;
    248  1.1  atatat 	case 's':
    249  1.1  atatat 		statfmt = SHELL_FORMAT;
    250  1.1  atatat 		break;
    251  1.1  atatat 	case 'x':
    252  1.1  atatat 		statfmt = LINUX_FORMAT;
    253  1.1  atatat 		if (timefmt == NULL)
    254  1.1  atatat 			timefmt = "%c";
    255  1.1  atatat 		break;
    256  1.1  atatat 	default:
    257  1.4  atatat 		usage(synopsis);
    258  1.1  atatat 		/*NOTREACHED*/
    259  1.1  atatat 	}
    260  1.1  atatat 
    261  1.1  atatat 	if (timefmt == NULL)
    262  1.1  atatat 		timefmt = TIME_FORMAT;
    263  1.1  atatat 
    264  1.1  atatat 	errs = 0;
    265  1.1  atatat 	do {
    266  1.1  atatat 		if (argc == 0)
    267  1.1  atatat 			rc = fstat(STDIN_FILENO, &st);
    268  1.1  atatat 		else if (usestat)
    269  1.1  atatat 			rc = stat(argv[0], &st);
    270  1.1  atatat 		else
    271  1.1  atatat 			rc = lstat(argv[0], &st);
    272  1.1  atatat 
    273  1.1  atatat 		if (rc == -1) {
    274  1.1  atatat 			errs = 1;
    275  1.4  atatat 			linkfail = 1;
    276  1.4  atatat 			if (!quiet)
    277  1.4  atatat 				warn("%s: stat",
    278  1.4  atatat 				    argc == 0 ? "(stdin)" : argv[0]);
    279  1.1  atatat 		}
    280  1.1  atatat 		else
    281  1.4  atatat 			output(&st, argv[0], statfmt, fn, nonl, quiet);
    282  1.1  atatat 
    283  1.1  atatat 		argv++;
    284  1.1  atatat 		argc--;
    285  1.2  atatat 		fn++;
    286  1.1  atatat 	} while (argc > 0);
    287  1.1  atatat 
    288  1.4  atatat 	return (am_readlink ? linkfail : errs);
    289  1.1  atatat }
    290  1.1  atatat 
    291  1.1  atatat void
    292  1.4  atatat usage(const char *synopsis)
    293  1.1  atatat {
    294  1.1  atatat 
    295  1.4  atatat 	(void)fprintf(stderr, "usage: %s %s\n", getprogname(), synopsis);
    296  1.1  atatat 	exit(1);
    297  1.1  atatat }
    298  1.1  atatat 
    299  1.1  atatat /*
    300  1.1  atatat  * Parses a format string.
    301  1.1  atatat  */
    302  1.1  atatat void
    303  1.1  atatat output(const struct stat *st, const char *file,
    304  1.4  atatat     const char *statfmt, int fn, int nonl, int quiet)
    305  1.1  atatat {
    306  1.1  atatat 	int flags, size, prec, ofmt, hilo, what;
    307  1.4  atatat 	char buf[MAXPATHLEN];
    308  1.1  atatat 	const char *subfmt;
    309  1.1  atatat 	int nl, t, i;
    310  1.1  atatat 
    311  1.4  atatat 	nl = 1;
    312  1.1  atatat 	while (*statfmt != '\0') {
    313  1.1  atatat 
    314  1.1  atatat 		/*
    315  1.1  atatat 		 * Non-format characters go straight out.
    316  1.1  atatat 		 */
    317  1.2  atatat 		if (*statfmt != FMT_MAGIC) {
    318  1.4  atatat 			addchar(stdout, *statfmt, &nl);
    319  1.1  atatat 			statfmt++;
    320  1.1  atatat 			continue;
    321  1.1  atatat 		}
    322  1.1  atatat 
    323  1.1  atatat 		/*
    324  1.1  atatat 		 * The current format "substring" starts here,
    325  1.2  atatat 		 * and then we skip the magic.
    326  1.1  atatat 		 */
    327  1.1  atatat 		subfmt = statfmt;
    328  1.1  atatat 		statfmt++;
    329  1.1  atatat 
    330  1.1  atatat 		/*
    331  1.1  atatat 		 * Some simple one-character "formats".
    332  1.1  atatat 		 */
    333  1.1  atatat 		switch (*statfmt) {
    334  1.1  atatat 		case SIMPLE_NEWLINE:
    335  1.4  atatat 			addchar(stdout, '\n', &nl);
    336  1.1  atatat 			statfmt++;
    337  1.1  atatat 			continue;
    338  1.1  atatat 		case SIMPLE_TAB:
    339  1.4  atatat 			addchar(stdout, '\t', &nl);
    340  1.1  atatat 			statfmt++;
    341  1.1  atatat 			continue;
    342  1.1  atatat 		case SIMPLE_PERCENT:
    343  1.4  atatat 			addchar(stdout, '%', &nl);
    344  1.1  atatat 			statfmt++;
    345  1.1  atatat 			continue;
    346  1.2  atatat 		case SIMPLE_NUMBER: {
    347  1.2  atatat 			char num[12], *p;
    348  1.2  atatat 
    349  1.2  atatat 			snprintf(num, sizeof(num), "%d", fn);
    350  1.2  atatat 			for (p = &num[0]; *p; p++)
    351  1.4  atatat 				addchar(stdout, *p, &nl);
    352  1.2  atatat 			statfmt++;
    353  1.2  atatat 			continue;
    354  1.2  atatat 		}
    355  1.1  atatat 		}
    356  1.1  atatat 
    357  1.1  atatat 		/*
    358  1.1  atatat 		 * This must be an actual format string.  Format strings are
    359  1.1  atatat 		 * similar to printf(3) formats up to a point, and are of
    360  1.1  atatat 		 * the form:
    361  1.1  atatat 		 *
    362  1.1  atatat 		 *	%	required start of format
    363  1.1  atatat 		 *	[-# +0]	opt. format characters
    364  1.1  atatat 		 *	size	opt. field width
    365  1.1  atatat 		 *	.	opt. decimal separator, followed by
    366  1.1  atatat 		 *	prec	opt. precision
    367  1.1  atatat 		 *	fmt	opt. output specifier (string, numeric, etc.)
    368  1.1  atatat 		 *	sub	opt. sub field specifier (high, middle, low)
    369  1.1  atatat 		 *	datum	required field specifier (size, mode, etc)
    370  1.1  atatat 		 *
    371  1.1  atatat 		 * Only the % and the datum selector are required.  All data
    372  1.1  atatat 		 * have reasonable default output forms.  The "sub" specifier
    373  1.1  atatat 		 * only applies to certain data (mode, dev, rdev, filetype).
    374  1.1  atatat 		 * The symlink output defaults to STRING, yet will only emit
    375  1.1  atatat 		 * the leading " -> " if STRING is explicitly specified.  The
    376  1.1  atatat 		 * sizerdev datum will generate rdev output for character or
    377  1.1  atatat 		 * block devices, and size output for all others.
    378  1.1  atatat 		 */
    379  1.1  atatat 		flags = 0;
    380  1.1  atatat 		do {
    381  1.2  atatat 			if      (*statfmt == FMT_POUND)
    382  1.2  atatat 				flags |= FLAG_POUND;
    383  1.2  atatat 			else if (*statfmt == FMT_SPACE)
    384  1.2  atatat 				flags |= FLAG_SPACE;
    385  1.2  atatat 			else if (*statfmt == FMT_PLUS)
    386  1.2  atatat 				flags |= FLAG_PLUS;
    387  1.2  atatat 			else if (*statfmt == FMT_ZERO)
    388  1.2  atatat 				flags |= FLAG_ZERO;
    389  1.2  atatat 			else if (*statfmt == FMT_MINUS)
    390  1.2  atatat 				flags |= FLAG_MINUS;
    391  1.1  atatat 			else
    392  1.1  atatat 				break;
    393  1.1  atatat 			statfmt++;
    394  1.1  atatat 		} while (1/*CONSTCOND*/);
    395  1.1  atatat 
    396  1.1  atatat 		size = -1;
    397  1.1  atatat 		if (isdigit((unsigned)*statfmt)) {
    398  1.1  atatat 			size = 0;
    399  1.1  atatat 			while (isdigit((unsigned)*statfmt)) {
    400  1.1  atatat 				size = (size * 10) + (*statfmt - '0');
    401  1.1  atatat 				statfmt++;
    402  1.1  atatat 				if (size < 0)
    403  1.1  atatat 					goto badfmt;
    404  1.1  atatat 			}
    405  1.1  atatat 		}
    406  1.1  atatat 
    407  1.1  atatat 		prec = -1;
    408  1.2  atatat 		if (*statfmt == FMT_DOT) {
    409  1.1  atatat 			statfmt++;
    410  1.1  atatat 
    411  1.1  atatat 			prec = 0;
    412  1.1  atatat 			while (isdigit((unsigned)*statfmt)) {
    413  1.1  atatat 				prec = (prec * 10) + (*statfmt - '0');
    414  1.1  atatat 				statfmt++;
    415  1.1  atatat 				if (prec < 0)
    416  1.1  atatat 					goto badfmt;
    417  1.1  atatat 			}
    418  1.1  atatat 		}
    419  1.1  atatat 
    420  1.5  atatat #define fmtcase(x, y)		case (y): (x) = (y); statfmt++; break
    421  1.5  atatat #define fmtcasef(x, y, z)	case (y): (x) = (z); statfmt++; break
    422  1.1  atatat 		switch (*statfmt) {
    423  1.5  atatat 			fmtcasef(ofmt, FMT_DECIMAL,	FMTF_DECIMAL);
    424  1.5  atatat 			fmtcasef(ofmt, FMT_OCTAL,	FMTF_OCTAL);
    425  1.5  atatat 			fmtcasef(ofmt, FMT_UNSIGNED,	FMTF_UNSIGNED);
    426  1.5  atatat 			fmtcasef(ofmt, FMT_HEX,		FMTF_HEX);
    427  1.5  atatat 			fmtcasef(ofmt, FMT_FLOAT,	FMTF_FLOAT);
    428  1.5  atatat 			fmtcasef(ofmt, FMT_STRING,	FMTF_STRING);
    429  1.1  atatat 		default:
    430  1.1  atatat 			ofmt = 0;
    431  1.1  atatat 			break;
    432  1.1  atatat 		}
    433  1.1  atatat 
    434  1.1  atatat 		switch (*statfmt) {
    435  1.1  atatat 			fmtcase(hilo, HIGH_PIECE);
    436  1.1  atatat 			fmtcase(hilo, MIDDLE_PIECE);
    437  1.1  atatat 			fmtcase(hilo, LOW_PIECE);
    438  1.1  atatat 		default:
    439  1.1  atatat 			hilo = 0;
    440  1.1  atatat 			break;
    441  1.1  atatat 		}
    442  1.1  atatat 
    443  1.1  atatat 		switch (*statfmt) {
    444  1.1  atatat 			fmtcase(what, SHOW_st_dev);
    445  1.1  atatat 			fmtcase(what, SHOW_st_ino);
    446  1.1  atatat 			fmtcase(what, SHOW_st_mode);
    447  1.1  atatat 			fmtcase(what, SHOW_st_nlink);
    448  1.1  atatat 			fmtcase(what, SHOW_st_uid);
    449  1.1  atatat 			fmtcase(what, SHOW_st_gid);
    450  1.1  atatat 			fmtcase(what, SHOW_st_rdev);
    451  1.1  atatat 			fmtcase(what, SHOW_st_atime);
    452  1.1  atatat 			fmtcase(what, SHOW_st_mtime);
    453  1.1  atatat 			fmtcase(what, SHOW_st_ctime);
    454  1.1  atatat 			fmtcase(what, SHOW_st_size);
    455  1.1  atatat 			fmtcase(what, SHOW_st_blocks);
    456  1.1  atatat 			fmtcase(what, SHOW_st_blksize);
    457  1.1  atatat 			fmtcase(what, SHOW_st_flags);
    458  1.1  atatat 			fmtcase(what, SHOW_st_gen);
    459  1.1  atatat 			fmtcase(what, SHOW_symlink);
    460  1.1  atatat 			fmtcase(what, SHOW_filetype);
    461  1.1  atatat 			fmtcase(what, SHOW_filename);
    462  1.1  atatat 			fmtcase(what, SHOW_sizerdev);
    463  1.1  atatat 		default:
    464  1.1  atatat 			goto badfmt;
    465  1.1  atatat 		}
    466  1.5  atatat #undef fmtcasef
    467  1.1  atatat #undef fmtcase
    468  1.1  atatat 
    469  1.1  atatat 		t = format1(st,
    470  1.1  atatat 		     file,
    471  1.1  atatat 		     subfmt, statfmt - subfmt,
    472  1.4  atatat 		     buf, sizeof(buf),
    473  1.1  atatat 		     flags, size, prec, ofmt, hilo, what);
    474  1.1  atatat 
    475  1.4  atatat 		for (i = 0; i < t && i < sizeof(buf); i++)
    476  1.4  atatat 			addchar(stdout, buf[i], &nl);
    477  1.1  atatat 
    478  1.1  atatat 		continue;
    479  1.1  atatat 
    480  1.1  atatat 	badfmt:
    481  1.1  atatat 		errx(1, "%.*s: bad format",
    482  1.1  atatat 		    (int)(statfmt - subfmt + 1), subfmt);
    483  1.1  atatat 	}
    484  1.1  atatat 
    485  1.1  atatat 	if (!nl && !nonl)
    486  1.4  atatat 		(void)fputc('\n', stdout);
    487  1.4  atatat 	(void)fflush(stdout);
    488  1.1  atatat }
    489  1.1  atatat 
    490  1.1  atatat /*
    491  1.1  atatat  * Arranges output according to a single parsed format substring.
    492  1.1  atatat  */
    493  1.1  atatat int
    494  1.1  atatat format1(const struct stat *st,
    495  1.1  atatat     const char *file,
    496  1.1  atatat     const char *fmt, int flen,
    497  1.1  atatat     char *buf, size_t blen,
    498  1.1  atatat     int flags, int size, int prec, int ofmt,
    499  1.1  atatat     int hilo, int what)
    500  1.1  atatat {
    501  1.1  atatat 	u_int64_t data;
    502  1.1  atatat 	char *sdata, lfmt[24], tmp[20];
    503  1.1  atatat 	char smode[12], sid[12], path[MAXPATHLEN + 4];
    504  1.1  atatat 	struct passwd *pw;
    505  1.1  atatat 	struct group *gr;
    506  1.1  atatat 	const struct timespec *tsp;
    507  1.1  atatat 	struct timespec ts;
    508  1.1  atatat 	struct tm *tm;
    509  1.1  atatat 	int l, small, formats;
    510  1.1  atatat 
    511  1.1  atatat 	tsp = NULL;
    512  1.1  atatat 	formats = 0;
    513  1.1  atatat 	small = 0;
    514  1.1  atatat 
    515  1.1  atatat 	/*
    516  1.1  atatat 	 * First, pick out the data and tweak it based on hilo or
    517  1.1  atatat 	 * specified output format (symlink output only).
    518  1.1  atatat 	 */
    519  1.1  atatat 	switch (what) {
    520  1.1  atatat 	case SHOW_st_dev:
    521  1.1  atatat 	case SHOW_st_rdev:
    522  1.1  atatat 		small = (sizeof(st->st_dev) == 4);
    523  1.1  atatat 		data = (what == SHOW_st_dev) ? st->st_dev : st->st_rdev;
    524  1.1  atatat 		sdata = (what == SHOW_st_dev) ?
    525  1.1  atatat 		    devname(st->st_dev, S_IFBLK) :
    526  1.1  atatat 		    devname(st->st_rdev,
    527  1.1  atatat 		    S_ISCHR(st->st_mode) ? S_IFCHR :
    528  1.1  atatat 		    S_ISBLK(st->st_mode) ? S_IFBLK :
    529  1.1  atatat 		    0U);
    530  1.3  atatat 		if (sdata == NULL)
    531  1.3  atatat 			sdata = "???";
    532  1.1  atatat 		if (hilo == HIGH_PIECE) {
    533  1.1  atatat 			data = major(data);
    534  1.1  atatat 			hilo = 0;
    535  1.1  atatat 		}
    536  1.1  atatat 		else if (hilo == LOW_PIECE) {
    537  1.1  atatat 			data = minor((unsigned)data);
    538  1.1  atatat 			hilo = 0;
    539  1.1  atatat 		}
    540  1.5  atatat 		formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
    541  1.5  atatat 		    FMTF_STRING;
    542  1.1  atatat 		if (ofmt == 0)
    543  1.5  atatat 			ofmt = FMTF_UNSIGNED;
    544  1.1  atatat 		break;
    545  1.1  atatat 	case SHOW_st_ino:
    546  1.1  atatat 		small = (sizeof(st->st_ino) == 4);
    547  1.1  atatat 		data = st->st_ino;
    548  1.1  atatat 		sdata = NULL;
    549  1.5  atatat 		formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
    550  1.1  atatat 		if (ofmt == 0)
    551  1.5  atatat 			ofmt = FMTF_UNSIGNED;
    552  1.1  atatat 		break;
    553  1.1  atatat 	case SHOW_st_mode:
    554  1.1  atatat 		small = (sizeof(st->st_mode) == 4);
    555  1.1  atatat 		data = st->st_mode;
    556  1.1  atatat 		strmode(st->st_mode, smode);
    557  1.1  atatat 		sdata = smode;
    558  1.1  atatat 		l = strlen(sdata);
    559  1.1  atatat 		if (sdata[l - 1] == ' ')
    560  1.1  atatat 			sdata[--l] = '\0';
    561  1.1  atatat 		if (hilo == HIGH_PIECE) {
    562  1.1  atatat 			data >>= 12;
    563  1.1  atatat 			sdata += 1;
    564  1.1  atatat 			sdata[3] = '\0';
    565  1.1  atatat 			hilo = 0;
    566  1.1  atatat 		}
    567  1.1  atatat 		else if (hilo == MIDDLE_PIECE) {
    568  1.2  atatat 			data = (data >> 9) & 07;
    569  1.1  atatat 			sdata += 4;
    570  1.1  atatat 			sdata[3] = '\0';
    571  1.1  atatat 			hilo = 0;
    572  1.1  atatat 		}
    573  1.1  atatat 		else if (hilo == LOW_PIECE) {
    574  1.2  atatat 			data &= 0777;
    575  1.1  atatat 			sdata += 7;
    576  1.1  atatat 			sdata[3] = '\0';
    577  1.1  atatat 			hilo = 0;
    578  1.1  atatat 		}
    579  1.5  atatat 		formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
    580  1.5  atatat 		    FMTF_STRING;
    581  1.1  atatat 		if (ofmt == 0)
    582  1.5  atatat 			ofmt = FMTF_OCTAL;
    583  1.1  atatat 		break;
    584  1.1  atatat 	case SHOW_st_nlink:
    585  1.1  atatat 		small = (sizeof(st->st_dev) == 4);
    586  1.1  atatat 		data = st->st_nlink;
    587  1.1  atatat 		sdata = NULL;
    588  1.5  atatat 		formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
    589  1.1  atatat 		if (ofmt == 0)
    590  1.5  atatat 			ofmt = FMTF_UNSIGNED;
    591  1.1  atatat 		break;
    592  1.1  atatat 	case SHOW_st_uid:
    593  1.1  atatat 		small = (sizeof(st->st_uid) == 4);
    594  1.1  atatat 		data = st->st_uid;
    595  1.1  atatat 		if ((pw = getpwuid(st->st_uid)) != NULL)
    596  1.1  atatat 			sdata = pw->pw_name;
    597  1.1  atatat 		else {
    598  1.1  atatat 			snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_uid);
    599  1.1  atatat 			sdata = sid;
    600  1.1  atatat 		}
    601  1.5  atatat 		formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
    602  1.5  atatat 		    FMTF_STRING;
    603  1.1  atatat 		if (ofmt == 0)
    604  1.5  atatat 			ofmt = FMTF_UNSIGNED;
    605  1.1  atatat 		break;
    606  1.1  atatat 	case SHOW_st_gid:
    607  1.1  atatat 		small = (sizeof(st->st_gid) == 4);
    608  1.1  atatat 		data = st->st_gid;
    609  1.1  atatat 		if ((gr = getgrgid(st->st_gid)) != NULL)
    610  1.1  atatat 			sdata = gr->gr_name;
    611  1.1  atatat 		else {
    612  1.1  atatat 			snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_gid);
    613  1.1  atatat 			sdata = sid;
    614  1.1  atatat 		}
    615  1.5  atatat 		formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
    616  1.5  atatat 		    FMTF_STRING;
    617  1.1  atatat 		if (ofmt == 0)
    618  1.5  atatat 			ofmt = FMTF_UNSIGNED;
    619  1.1  atatat 		break;
    620  1.1  atatat 	case SHOW_st_atime:
    621  1.1  atatat 		tsp = &st->st_atimespec;
    622  1.1  atatat 		/* FALLTHROUGH */
    623  1.1  atatat 	case SHOW_st_mtime:
    624  1.1  atatat 		if (tsp == NULL)
    625  1.1  atatat 			tsp = &st->st_mtimespec;
    626  1.1  atatat 		/* FALLTHROUGH */
    627  1.1  atatat 	case SHOW_st_ctime:
    628  1.1  atatat 		if (tsp == NULL)
    629  1.1  atatat 			tsp = &st->st_ctimespec;
    630  1.1  atatat 		ts = *tsp;		/* copy so we can muck with it */
    631  1.1  atatat 		small = (sizeof(ts.tv_sec) == 4);
    632  1.1  atatat 		data = ts.tv_sec;
    633  1.1  atatat 		small = 1;
    634  1.1  atatat 		tm = localtime(&ts.tv_sec);
    635  1.1  atatat 		(void)strftime(path, sizeof(path), timefmt, tm);
    636  1.1  atatat 		sdata = path;
    637  1.5  atatat 		formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
    638  1.5  atatat 		    FMTF_FLOAT | FMTF_STRING;
    639  1.1  atatat 		if (ofmt == 0)
    640  1.5  atatat 			ofmt = FMTF_DECIMAL;
    641  1.1  atatat 		break;
    642  1.1  atatat 	case SHOW_st_size:
    643  1.1  atatat 		small = (sizeof(st->st_size) == 4);
    644  1.1  atatat 		data = st->st_size;
    645  1.1  atatat 		sdata = NULL;
    646  1.5  atatat 		formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
    647  1.1  atatat 		if (ofmt == 0)
    648  1.5  atatat 			ofmt = FMTF_UNSIGNED;
    649  1.1  atatat 		break;
    650  1.1  atatat 	case SHOW_st_blocks:
    651  1.1  atatat 		small = (sizeof(st->st_blocks) == 4);
    652  1.1  atatat 		data = st->st_blocks;
    653  1.1  atatat 		sdata = NULL;
    654  1.5  atatat 		formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
    655  1.1  atatat 		if (ofmt == 0)
    656  1.5  atatat 			ofmt = FMTF_UNSIGNED;
    657  1.1  atatat 		break;
    658  1.1  atatat 	case SHOW_st_blksize:
    659  1.1  atatat 		small = (sizeof(st->st_blksize) == 4);
    660  1.1  atatat 		data = st->st_blksize;
    661  1.1  atatat 		sdata = NULL;
    662  1.5  atatat 		formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
    663  1.1  atatat 		if (ofmt == 0)
    664  1.5  atatat 			ofmt = FMTF_UNSIGNED;
    665  1.1  atatat 		break;
    666  1.1  atatat 	case SHOW_st_flags:
    667  1.1  atatat 		small = (sizeof(st->st_flags) == 4);
    668  1.1  atatat 		data = st->st_flags;
    669  1.1  atatat 		sdata = NULL;
    670  1.5  atatat 		formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
    671  1.1  atatat 		if (ofmt == 0)
    672  1.5  atatat 			ofmt = FMTF_UNSIGNED;
    673  1.1  atatat 		break;
    674  1.1  atatat 	case SHOW_st_gen:
    675  1.1  atatat 		small = (sizeof(st->st_gen) == 4);
    676  1.1  atatat 		data = st->st_gen;
    677  1.1  atatat 		sdata = NULL;
    678  1.5  atatat 		formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
    679  1.1  atatat 		if (ofmt == 0)
    680  1.5  atatat 			ofmt = FMTF_UNSIGNED;
    681  1.1  atatat 		break;
    682  1.1  atatat 	case SHOW_symlink:
    683  1.1  atatat 		small = 0;
    684  1.1  atatat 		data = 0;
    685  1.1  atatat 		if (S_ISLNK(st->st_mode)) {
    686  1.1  atatat 			snprintf(path, sizeof(path), " -> ");
    687  1.1  atatat 			l = readlink(file, path + 4, sizeof(path) - 4);
    688  1.1  atatat 			if (l == -1) {
    689  1.4  atatat 				linkfail = 1;
    690  1.1  atatat 				l = 0;
    691  1.1  atatat 				path[0] = '\0';
    692  1.1  atatat 			}
    693  1.1  atatat 			path[l + 4] = '\0';
    694  1.5  atatat 			sdata = path + (ofmt == FMTF_STRING ? 0 : 4);
    695  1.1  atatat 		}
    696  1.4  atatat 		else {
    697  1.4  atatat 			linkfail = 1;
    698  1.1  atatat 			sdata = "";
    699  1.4  atatat 		}
    700  1.5  atatat 		formats = FMTF_STRING;
    701  1.1  atatat 		if (ofmt == 0)
    702  1.5  atatat 			ofmt = FMTF_STRING;
    703  1.1  atatat 		break;
    704  1.1  atatat 	case SHOW_filetype:
    705  1.1  atatat 		small = 0;
    706  1.1  atatat 		data = 0;
    707  1.1  atatat 		sdata = smode;
    708  1.1  atatat 		sdata[0] = '\0';
    709  1.1  atatat 		if (hilo == 0 || hilo == LOW_PIECE) {
    710  1.1  atatat 			switch (st->st_mode & S_IFMT) {
    711  1.1  atatat 			case S_IFIFO:	(void)strcat(sdata, "|");	break;
    712  1.1  atatat 			case S_IFDIR:	(void)strcat(sdata, "/");	break;
    713  1.1  atatat 			case S_IFREG:
    714  1.1  atatat 				if (st->st_mode &
    715  1.1  atatat 				    (S_IXUSR | S_IXGRP | S_IXOTH))
    716  1.1  atatat 					(void)strcat(sdata, "*");
    717  1.1  atatat 				break;
    718  1.1  atatat 			case S_IFLNK:	(void)strcat(sdata, "@");	break;
    719  1.1  atatat 			case S_IFSOCK:	(void)strcat(sdata, "=");	break;
    720  1.1  atatat 			case S_IFWHT:	(void)strcat(sdata, "%");	break;
    721  1.1  atatat 			}
    722  1.1  atatat 			hilo = 0;
    723  1.1  atatat 		}
    724  1.1  atatat 		else if (hilo == HIGH_PIECE) {
    725  1.1  atatat 			switch (st->st_mode & S_IFMT) {
    726  1.1  atatat 			case S_IFIFO:	sdata = "Fifo File";		break;
    727  1.1  atatat 			case S_IFCHR:	sdata = "Character Device";	break;
    728  1.1  atatat 			case S_IFDIR:	sdata = "Directory";		break;
    729  1.1  atatat 			case S_IFBLK:	sdata = "Block Device";		break;
    730  1.1  atatat 			case S_IFREG:	sdata = "Regular File";		break;
    731  1.1  atatat 			case S_IFLNK:	sdata = "Symbolic Link";	break;
    732  1.1  atatat 			case S_IFSOCK:	sdata = "Socket";		break;
    733  1.1  atatat 			case S_IFWHT:	sdata = "Whiteout File";	break;
    734  1.1  atatat 			default:	sdata = "???";			break;
    735  1.1  atatat 			}
    736  1.1  atatat 			hilo = 0;
    737  1.1  atatat 		}
    738  1.5  atatat 		formats = FMTF_STRING;
    739  1.1  atatat 		if (ofmt == 0)
    740  1.5  atatat 			ofmt = FMTF_STRING;
    741  1.1  atatat 		break;
    742  1.1  atatat 	case SHOW_filename:
    743  1.1  atatat 		small = 0;
    744  1.1  atatat 		data = 0;
    745  1.1  atatat 		if (file == NULL)
    746  1.1  atatat 			(void)strncpy(path, "(stdin)", sizeof(path));
    747  1.1  atatat 		else
    748  1.1  atatat 			(void)strncpy(path, file, sizeof(path));
    749  1.1  atatat 		sdata = path;
    750  1.5  atatat 		formats = FMTF_STRING;
    751  1.1  atatat 		if (ofmt == 0)
    752  1.5  atatat 			ofmt = FMTF_STRING;
    753  1.1  atatat 		break;
    754  1.1  atatat 	case SHOW_sizerdev:
    755  1.1  atatat 		if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
    756  1.1  atatat 			char majdev[20], mindev[20];
    757  1.1  atatat 			int l1, l2;
    758  1.1  atatat 
    759  1.1  atatat 			l1 = format1(st,
    760  1.1  atatat 			    file,
    761  1.1  atatat 			    fmt, flen,
    762  1.1  atatat 			    majdev, sizeof(majdev),
    763  1.1  atatat 			    flags, size, prec,
    764  1.1  atatat 			    ofmt, HIGH_PIECE, SHOW_st_rdev);
    765  1.1  atatat 			l2 = format1(st,
    766  1.1  atatat 			    file,
    767  1.1  atatat 			    fmt, flen,
    768  1.1  atatat 			    mindev, sizeof(mindev),
    769  1.1  atatat 			    flags, size, prec,
    770  1.1  atatat 			    ofmt, LOW_PIECE, SHOW_st_rdev);
    771  1.1  atatat 			return (snprintf(buf, blen, "%.*s,%.*s",
    772  1.1  atatat 			    l1, majdev, l2, mindev));
    773  1.1  atatat 		}
    774  1.1  atatat 		else {
    775  1.1  atatat 			return (format1(st,
    776  1.1  atatat 			    file,
    777  1.1  atatat 			    fmt, flen,
    778  1.1  atatat 			    buf, blen,
    779  1.1  atatat 			    flags, size, prec,
    780  1.1  atatat 			    ofmt, 0, SHOW_st_size));
    781  1.1  atatat 		}
    782  1.1  atatat 		/*NOTREACHED*/
    783  1.1  atatat 	default:
    784  1.1  atatat 		errx(1, "%.*s: bad format", (int)flen, fmt);
    785  1.1  atatat 	}
    786  1.1  atatat 
    787  1.1  atatat 	/*
    788  1.1  atatat 	 * If a subdatum was specified but not supported, or an output
    789  1.1  atatat 	 * format was selected that is not supported, that's an error.
    790  1.1  atatat 	 */
    791  1.1  atatat 	if (hilo != 0 || (ofmt & formats) == 0)
    792  1.1  atatat 		errx(1, "%.*s: bad format", (int)flen, fmt);
    793  1.1  atatat 
    794  1.1  atatat 	/*
    795  1.1  atatat 	 * Assemble the format string for passing to printf(3).
    796  1.1  atatat 	 */
    797  1.1  atatat 	lfmt[0] = '\0';
    798  1.1  atatat 	(void)strcat(lfmt, "%");
    799  1.2  atatat 	if (flags & FLAG_POUND)
    800  1.1  atatat 		(void)strcat(lfmt, "#");
    801  1.2  atatat 	if (flags & FLAG_SPACE)
    802  1.1  atatat 		(void)strcat(lfmt, " ");
    803  1.2  atatat 	if (flags & FLAG_PLUS)
    804  1.1  atatat 		(void)strcat(lfmt, "+");
    805  1.2  atatat 	if (flags & FLAG_MINUS)
    806  1.1  atatat 		(void)strcat(lfmt, "-");
    807  1.2  atatat 	if (flags & FLAG_ZERO)
    808  1.1  atatat 		(void)strcat(lfmt, "0");
    809  1.1  atatat 
    810  1.1  atatat 	/*
    811  1.1  atatat 	 * Only the timespecs support the FLOAT output format, and that
    812  1.1  atatat 	 * requires work that differs from the other formats.
    813  1.1  atatat 	 */
    814  1.5  atatat 	if (ofmt == FMTF_FLOAT) {
    815  1.1  atatat 		/*
    816  1.1  atatat 		 * Nothing after the decimal point, so just print seconds.
    817  1.1  atatat 		 */
    818  1.1  atatat 		if (prec == 0) {
    819  1.1  atatat 			if (size != -1) {
    820  1.1  atatat 				(void)snprintf(tmp, sizeof(tmp), "%d", size);
    821  1.1  atatat 				(void)strcat(lfmt, tmp);
    822  1.1  atatat 			}
    823  1.1  atatat 			(void)strcat(lfmt, "d");
    824  1.1  atatat 			return (snprintf(buf, blen, lfmt, ts.tv_sec));
    825  1.1  atatat 		}
    826  1.1  atatat 
    827  1.1  atatat 		/*
    828  1.1  atatat 		 * Unspecified precision gets all the precision we have:
    829  1.1  atatat 		 * 9 digits.
    830  1.1  atatat 		 */
    831  1.1  atatat 		if (prec == -1)
    832  1.1  atatat 			prec = 9;
    833  1.1  atatat 
    834  1.1  atatat 		/*
    835  1.1  atatat 		 * Adjust the size for the decimal point and the digits
    836  1.1  atatat 		 * that will follow.
    837  1.1  atatat 		 */
    838  1.1  atatat 		size -= prec + 1;
    839  1.1  atatat 
    840  1.1  atatat 		/*
    841  1.1  atatat 		 * Any leftover size that's legitimate will be used.
    842  1.1  atatat 		 */
    843  1.1  atatat 		if (size > 0) {
    844  1.1  atatat 			(void)snprintf(tmp, sizeof(tmp), "%d", size);
    845  1.1  atatat 			(void)strcat(lfmt, tmp);
    846  1.1  atatat 		}
    847  1.1  atatat 		(void)strcat(lfmt, "d");
    848  1.1  atatat 
    849  1.1  atatat 		/*
    850  1.1  atatat 		 * The stuff after the decimal point always needs zero
    851  1.1  atatat 		 * filling.
    852  1.1  atatat 		 */
    853  1.1  atatat 		(void)strcat(lfmt, ".%0");
    854  1.1  atatat 
    855  1.1  atatat 		/*
    856  1.1  atatat 		 * We can "print" at most nine digits of precision.  The
    857  1.1  atatat 		 * rest we will pad on at the end.
    858  1.1  atatat 		 */
    859  1.1  atatat 		(void)snprintf(tmp, sizeof(tmp), "%dd", prec > 9 ? 9 : prec);
    860  1.1  atatat 		(void)strcat(lfmt, tmp);
    861  1.1  atatat 
    862  1.1  atatat 		/*
    863  1.1  atatat 		 * For precision of less that nine digits, trim off the
    864  1.1  atatat 		 * less significant figures.
    865  1.1  atatat 		 */
    866  1.1  atatat 		for (; prec < 9; prec++)
    867  1.1  atatat 			ts.tv_nsec /= 10;
    868  1.1  atatat 
    869  1.1  atatat 		/*
    870  1.1  atatat 		 * Use the format, and then tack on any zeroes that
    871  1.1  atatat 		 * might be required to make up the requested precision.
    872  1.1  atatat 		 */
    873  1.1  atatat 		l = snprintf(buf, blen, lfmt, ts.tv_sec, ts.tv_nsec);
    874  1.1  atatat 		for (; prec > 9 && l < blen; prec--, l++)
    875  1.1  atatat 			(void)strcat(buf, "0");
    876  1.1  atatat 		return (l);
    877  1.1  atatat 	}
    878  1.1  atatat 
    879  1.1  atatat 	/*
    880  1.1  atatat 	 * Add on size and precision, if specified, to the format.
    881  1.1  atatat 	 */
    882  1.1  atatat 	if (size != -1) {
    883  1.1  atatat 		(void)snprintf(tmp, sizeof(tmp), "%d", size);
    884  1.1  atatat 		(void)strcat(lfmt, tmp);
    885  1.1  atatat 	}
    886  1.1  atatat 	if (prec != -1) {
    887  1.1  atatat 		(void)snprintf(tmp, sizeof(tmp), ".%d", prec);
    888  1.1  atatat 		(void)strcat(lfmt, tmp);
    889  1.1  atatat 	}
    890  1.1  atatat 
    891  1.1  atatat 	/*
    892  1.1  atatat 	 * String output uses the temporary sdata.
    893  1.1  atatat 	 */
    894  1.5  atatat 	if (ofmt == FMTF_STRING) {
    895  1.1  atatat 		if (sdata == NULL)
    896  1.1  atatat 			errx(1, "%.*s: bad format", (int)flen, fmt);
    897  1.1  atatat 		(void)strcat(lfmt, "s");
    898  1.1  atatat 		return (snprintf(buf, blen, lfmt, sdata));
    899  1.1  atatat 	}
    900  1.1  atatat 
    901  1.1  atatat 	/*
    902  1.1  atatat 	 * Ensure that sign extension does not cause bad looking output
    903  1.1  atatat 	 * for some forms.
    904  1.1  atatat 	 */
    905  1.5  atatat 	if (small && ofmt != FMTF_DECIMAL)
    906  1.1  atatat 		data = (u_int32_t)data;
    907  1.1  atatat 
    908  1.1  atatat 	/*
    909  1.1  atatat 	 * The four "numeric" output forms.
    910  1.1  atatat 	 */
    911  1.1  atatat 	(void)strcat(lfmt, "ll");
    912  1.1  atatat 	switch (ofmt) {
    913  1.5  atatat 	case FMTF_DECIMAL:	(void)strcat(lfmt, "d");	break;
    914  1.5  atatat 	case FMTF_OCTAL:		(void)strcat(lfmt, "o");	break;
    915  1.5  atatat 	case FMTF_UNSIGNED:	(void)strcat(lfmt, "u");	break;
    916  1.5  atatat 	case FMTF_HEX:		(void)strcat(lfmt, "x");	break;
    917  1.1  atatat 	}
    918  1.1  atatat 
    919  1.1  atatat 	return (snprintf(buf, blen, lfmt, data));
    920  1.1  atatat }
    921