Home | History | Annotate | Line # | Download | only in stat
stat.c revision 1.24
      1  1.24    elad /*	$NetBSD: stat.c,v 1.24 2006/10/07 10:41:50 elad 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.15   lukem #if HAVE_NBTOOL_CONFIG_H
     40  1.15   lukem #include "nbtool_config.h"
     41  1.15   lukem #endif
     42  1.15   lukem 
     43   1.1  atatat #include <sys/cdefs.h>
     44  1.15   lukem #if !defined(lint)
     45  1.24    elad __RCSID("$NetBSD: stat.c,v 1.24 2006/10/07 10:41:50 elad Exp $");
     46  1.11   lukem #endif
     47  1.11   lukem 
     48  1.15   lukem #if ! HAVE_NBTOOL_CONFIG_H
     49  1.11   lukem #define HAVE_STRUCT_STAT_ST_FLAGS 1
     50  1.13  atatat #define HAVE_STRUCT_STAT_ST_GEN 1
     51  1.13  atatat #define HAVE_STRUCT_STAT_ST_BIRTHTIME 1
     52  1.21     jmc #define HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC 1
     53  1.13  atatat #define HAVE_STRUCT_STAT_ST_MTIMENSEC 1
     54  1.13  atatat #define HAVE_DEVNAME 1
     55  1.15   lukem #endif /* HAVE_NBTOOL_CONFIG_H */
     56   1.1  atatat 
     57  1.10  atatat #include <sys/types.h>
     58   1.1  atatat #include <sys/stat.h>
     59   1.6  atatat 
     60   1.6  atatat #include <ctype.h>
     61   1.7  atatat #include <err.h>
     62  1.18  atatat #include <errno.h>
     63   1.6  atatat #include <grp.h>
     64   1.8  atatat #include <limits.h>
     65   1.6  atatat #include <pwd.h>
     66   1.6  atatat #include <stdio.h>
     67   1.7  atatat #include <stdlib.h>
     68   1.1  atatat #include <string.h>
     69   1.6  atatat #include <time.h>
     70   1.6  atatat #include <unistd.h>
     71   1.1  atatat 
     72  1.13  atatat #if HAVE_STRUCT_STAT_ST_FLAGS
     73  1.13  atatat #define DEF_F "%#Xf "
     74  1.13  atatat #define RAW_F "%f "
     75  1.13  atatat #define SHELL_F " st_flags=%f"
     76  1.13  atatat #else /* HAVE_STRUCT_STAT_ST_FLAGS */
     77  1.13  atatat #define DEF_F
     78  1.13  atatat #define RAW_F
     79  1.13  atatat #define SHELL_F
     80  1.13  atatat #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
     81  1.13  atatat 
     82  1.13  atatat #if HAVE_STRUCT_STAT_ST_BIRTHTIME
     83  1.13  atatat #define DEF_B "\"%SB\" "
     84  1.13  atatat #define RAW_B "%B "
     85  1.13  atatat #define SHELL_B "st_birthtime=%B "
     86  1.13  atatat #else /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
     87  1.13  atatat #define DEF_B
     88  1.13  atatat #define RAW_B
     89  1.13  atatat #define SHELL_B
     90  1.13  atatat #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
     91  1.13  atatat 
     92  1.13  atatat #if HAVE_STRUCT_STAT_ST_ATIM
     93  1.13  atatat #define st_atimespec st_atim
     94  1.13  atatat #define st_ctimespec st_ctim
     95  1.13  atatat #define st_mtimespec st_mtim
     96  1.13  atatat #endif /* HAVE_STRUCT_STAT_ST_ATIM */
     97  1.13  atatat 
     98   1.1  atatat #define DEF_FORMAT \
     99  1.13  atatat 	"%d %i %Sp %l %Su %Sg %r %z \"%Sa\" \"%Sm\" \"%Sc\" " DEF_B \
    100  1.13  atatat 	"%k %b " DEF_F "%N"
    101  1.13  atatat #define RAW_FORMAT	"%d %i %#p %l %u %g %r %z %a %m %c " RAW_B \
    102  1.13  atatat 	"%k %b " RAW_F "%N"
    103   1.1  atatat #define LS_FORMAT	"%Sp %l %Su %Sg %Z %Sm %N%SY"
    104   1.1  atatat #define LSF_FORMAT	"%Sp %l %Su %Sg %Z %Sm %N%T%SY"
    105   1.1  atatat #define SHELL_FORMAT \
    106   1.1  atatat 	"st_dev=%d st_ino=%i st_mode=%#p st_nlink=%l " \
    107   1.1  atatat 	"st_uid=%u st_gid=%g st_rdev=%r st_size=%z " \
    108  1.13  atatat 	"st_atime=%a st_mtime=%m st_ctime=%c " SHELL_B \
    109  1.13  atatat 	"st_blksize=%k st_blocks=%b" SHELL_F
    110   1.1  atatat #define LINUX_FORMAT \
    111   1.1  atatat 	"  File: \"%N\"%n" \
    112   1.1  atatat 	"  Size: %-11z  FileType: %HT%n" \
    113   1.1  atatat 	"  Mode: (%04OLp/%.10Sp)         Uid: (%5u/%8Su)  Gid: (%5g/%8Sg)%n" \
    114   1.1  atatat 	"Device: %Hd,%Ld   Inode: %i    Links: %l%n" \
    115   1.1  atatat 	"Access: %Sa%n" \
    116   1.1  atatat 	"Modify: %Sm%n" \
    117   1.1  atatat 	"Change: %Sc"
    118   1.1  atatat 
    119   1.1  atatat #define TIME_FORMAT	"%b %e %T %Y"
    120   1.1  atatat 
    121   1.2  atatat #define FLAG_POUND	0x01
    122   1.2  atatat #define FLAG_SPACE	0x02
    123   1.2  atatat #define FLAG_PLUS	0x04
    124   1.2  atatat #define FLAG_ZERO	0x08
    125   1.2  atatat #define FLAG_MINUS	0x10
    126   1.1  atatat 
    127   1.1  atatat /*
    128   1.2  atatat  * These format characters must all be unique, except the magic one.
    129   1.1  atatat  */
    130   1.2  atatat #define FMT_MAGIC	'%'
    131   1.2  atatat #define FMT_DOT		'.'
    132   1.2  atatat 
    133   1.1  atatat #define SIMPLE_NEWLINE	'n'
    134   1.1  atatat #define SIMPLE_TAB	't'
    135   1.1  atatat #define SIMPLE_PERCENT	'%'
    136   1.2  atatat #define SIMPLE_NUMBER	'@'
    137   1.2  atatat 
    138   1.2  atatat #define FMT_POUND	'#'
    139   1.2  atatat #define FMT_SPACE	' '
    140   1.2  atatat #define FMT_PLUS	'+'
    141   1.2  atatat #define FMT_ZERO	'0'
    142   1.2  atatat #define FMT_MINUS	'-'
    143   1.1  atatat 
    144   1.1  atatat #define FMT_DECIMAL 	'D'
    145   1.1  atatat #define FMT_OCTAL 	'O'
    146   1.1  atatat #define FMT_UNSIGNED 	'U'
    147   1.1  atatat #define FMT_HEX 	'X'
    148   1.1  atatat #define FMT_FLOAT 	'F'
    149   1.1  atatat #define FMT_STRING 	'S'
    150   1.1  atatat 
    151   1.5  atatat #define FMTF_DECIMAL	0x01
    152   1.5  atatat #define FMTF_OCTAL	0x02
    153   1.5  atatat #define FMTF_UNSIGNED	0x04
    154   1.5  atatat #define FMTF_HEX	0x08
    155   1.5  atatat #define FMTF_FLOAT	0x10
    156   1.5  atatat #define FMTF_STRING	0x20
    157   1.5  atatat 
    158   1.1  atatat #define HIGH_PIECE	'H'
    159   1.1  atatat #define MIDDLE_PIECE	'M'
    160   1.1  atatat #define LOW_PIECE	'L'
    161   1.1  atatat 
    162  1.24    elad #define	SHOW_realpath	'R'
    163   1.1  atatat #define SHOW_st_dev	'd'
    164   1.1  atatat #define SHOW_st_ino	'i'
    165   1.1  atatat #define SHOW_st_mode	'p'
    166   1.1  atatat #define SHOW_st_nlink	'l'
    167   1.1  atatat #define SHOW_st_uid	'u'
    168   1.1  atatat #define SHOW_st_gid	'g'
    169   1.1  atatat #define SHOW_st_rdev	'r'
    170   1.1  atatat #define SHOW_st_atime	'a'
    171   1.1  atatat #define SHOW_st_mtime	'm'
    172   1.1  atatat #define SHOW_st_ctime	'c'
    173  1.10  atatat #define SHOW_st_btime	'B'
    174   1.1  atatat #define SHOW_st_size	'z'
    175   1.1  atatat #define SHOW_st_blocks	'b'
    176   1.1  atatat #define SHOW_st_blksize	'k'
    177   1.1  atatat #define SHOW_st_flags	'f'
    178   1.1  atatat #define SHOW_st_gen	'v'
    179   1.1  atatat #define SHOW_symlink	'Y'
    180   1.1  atatat #define SHOW_filetype	'T'
    181   1.1  atatat #define SHOW_filename	'N'
    182   1.1  atatat #define SHOW_sizerdev	'Z'
    183   1.1  atatat 
    184   1.4  atatat void	usage(const char *);
    185   1.1  atatat void	output(const struct stat *, const char *,
    186   1.4  atatat 	    const char *, int, int, int);
    187   1.1  atatat int	format1(const struct stat *,	/* stat info */
    188   1.1  atatat 	    const char *,		/* the file name */
    189   1.1  atatat 	    const char *, int,		/* the format string itself */
    190   1.1  atatat 	    char *, size_t,		/* a place to put the output */
    191   1.1  atatat 	    int, int, int, int,		/* the parsed format */
    192   1.1  atatat 	    int, int);
    193   1.1  atatat 
    194   1.1  atatat char *timefmt;
    195   1.4  atatat int linkfail;
    196   1.1  atatat 
    197   1.4  atatat #define addchar(s, c, nl) \
    198   1.1  atatat 	do { \
    199   1.4  atatat 		(void)fputc((c), (s)); \
    200   1.4  atatat 		(*nl) = ((c) == '\n'); \
    201   1.1  atatat 	} while (0/*CONSTCOND*/)
    202   1.1  atatat 
    203   1.1  atatat int
    204   1.1  atatat main(int argc, char *argv[])
    205   1.1  atatat {
    206   1.1  atatat 	struct stat st;
    207   1.4  atatat 	int ch, rc, errs, am_readlink;
    208   1.4  atatat 	int lsF, fmtchar, usestat, fn, nonl, quiet;
    209   1.4  atatat 	char *statfmt, *options, *synopsis;
    210   1.1  atatat 
    211   1.4  atatat 	am_readlink = 0;
    212   1.1  atatat 	lsF = 0;
    213   1.1  atatat 	fmtchar = '\0';
    214   1.1  atatat 	usestat = 0;
    215   1.1  atatat 	nonl = 0;
    216   1.4  atatat 	quiet = 0;
    217   1.4  atatat 	linkfail = 0;
    218   1.1  atatat 	statfmt = NULL;
    219   1.1  atatat 	timefmt = NULL;
    220   1.1  atatat 
    221   1.4  atatat 	if (strcmp(getprogname(), "readlink") == 0) {
    222   1.4  atatat 		am_readlink = 1;
    223  1.24    elad 		options = "fn";
    224  1.24    elad 		synopsis = "[-fn] [file ...]";
    225   1.4  atatat 		statfmt = "%Y";
    226   1.4  atatat 		fmtchar = 'f';
    227   1.4  atatat 		quiet = 1;
    228   1.4  atatat 	} else {
    229   1.4  atatat 		options = "f:FlLnqrst:x";
    230   1.4  atatat 		synopsis = "[-FlLnqrsx] [-f format] [-t timefmt] [file ...]";
    231   1.4  atatat 	}
    232   1.4  atatat 
    233   1.4  atatat 	while ((ch = getopt(argc, argv, options)) != -1)
    234   1.1  atatat 		switch (ch) {
    235   1.1  atatat 		case 'F':
    236   1.1  atatat 			lsF = 1;
    237   1.1  atatat 			break;
    238   1.1  atatat 		case 'L':
    239   1.1  atatat 			usestat = 1;
    240   1.1  atatat 			break;
    241   1.1  atatat 		case 'n':
    242   1.1  atatat 			nonl = 1;
    243   1.1  atatat 			break;
    244   1.4  atatat 		case 'q':
    245   1.4  atatat 			quiet = 1;
    246   1.4  atatat 			break;
    247   1.1  atatat 		case 'f':
    248  1.24    elad 			if (am_readlink) {
    249  1.24    elad 				statfmt = "%R";
    250  1.24    elad 				break;
    251  1.24    elad 			}
    252   1.1  atatat 			statfmt = optarg;
    253   1.1  atatat 			/* FALLTHROUGH */
    254   1.1  atatat 		case 'l':
    255   1.1  atatat 		case 'r':
    256   1.1  atatat 		case 's':
    257   1.1  atatat 		case 'x':
    258   1.1  atatat 			if (fmtchar != 0)
    259   1.1  atatat 				errx(1, "can't use format '%c' with '%c'",
    260   1.1  atatat 				    fmtchar, ch);
    261   1.1  atatat 			fmtchar = ch;
    262   1.1  atatat 			break;
    263   1.1  atatat 		case 't':
    264   1.1  atatat 			timefmt = optarg;
    265   1.1  atatat 			break;
    266   1.1  atatat 		default:
    267   1.4  atatat 			usage(synopsis);
    268   1.1  atatat 		}
    269   1.1  atatat 
    270   1.1  atatat 	argc -= optind;
    271   1.1  atatat 	argv += optind;
    272   1.2  atatat 	fn = 1;
    273   1.1  atatat 
    274   1.1  atatat 	if (fmtchar == '\0') {
    275   1.1  atatat 		if (lsF)
    276   1.1  atatat 			fmtchar = 'l';
    277   1.1  atatat 		else {
    278   1.1  atatat 			fmtchar = 'f';
    279   1.1  atatat 			statfmt = DEF_FORMAT;
    280   1.1  atatat 		}
    281   1.1  atatat 	}
    282   1.1  atatat 
    283   1.1  atatat 	if (lsF && fmtchar != 'l')
    284   1.1  atatat 		errx(1, "can't use format '%c' with -F", fmtchar);
    285   1.1  atatat 
    286   1.1  atatat 	switch (fmtchar) {
    287   1.1  atatat 	case 'f':
    288   1.1  atatat 		/* statfmt already set */
    289   1.1  atatat 		break;
    290   1.1  atatat 	case 'l':
    291   1.1  atatat 		statfmt = lsF ? LSF_FORMAT : LS_FORMAT;
    292   1.1  atatat 		break;
    293   1.1  atatat 	case 'r':
    294   1.1  atatat 		statfmt = RAW_FORMAT;
    295   1.1  atatat 		break;
    296   1.1  atatat 	case 's':
    297   1.1  atatat 		statfmt = SHELL_FORMAT;
    298   1.1  atatat 		break;
    299   1.1  atatat 	case 'x':
    300   1.1  atatat 		statfmt = LINUX_FORMAT;
    301   1.1  atatat 		if (timefmt == NULL)
    302   1.1  atatat 			timefmt = "%c";
    303   1.1  atatat 		break;
    304   1.1  atatat 	default:
    305   1.4  atatat 		usage(synopsis);
    306   1.1  atatat 		/*NOTREACHED*/
    307   1.1  atatat 	}
    308   1.1  atatat 
    309   1.1  atatat 	if (timefmt == NULL)
    310   1.1  atatat 		timefmt = TIME_FORMAT;
    311   1.1  atatat 
    312   1.1  atatat 	errs = 0;
    313   1.1  atatat 	do {
    314   1.1  atatat 		if (argc == 0)
    315   1.1  atatat 			rc = fstat(STDIN_FILENO, &st);
    316  1.18  atatat 		else if (usestat) {
    317  1.18  atatat 			/*
    318  1.18  atatat 			 * Try stat() and if it fails, fall back to
    319  1.18  atatat 			 * lstat() just in case we're examining a
    320  1.18  atatat 			 * broken symlink.
    321  1.18  atatat 			 */
    322  1.18  atatat 			if ((rc = stat(argv[0], &st)) == -1 &&
    323  1.18  atatat 			    errno == ENOENT &&
    324  1.18  atatat 			    (rc = lstat(argv[0], &st)) == -1)
    325  1.18  atatat 				errno = ENOENT;
    326  1.18  atatat 		}
    327   1.1  atatat 		else
    328   1.1  atatat 			rc = lstat(argv[0], &st);
    329   1.1  atatat 
    330   1.1  atatat 		if (rc == -1) {
    331   1.1  atatat 			errs = 1;
    332   1.4  atatat 			linkfail = 1;
    333   1.4  atatat 			if (!quiet)
    334  1.20  atatat 				warn("%s: %s",
    335  1.20  atatat 				    argc == 0 ? "(stdin)" : argv[0],
    336  1.20  atatat 				    usestat ? "stat" : "lstat");
    337   1.1  atatat 		}
    338   1.1  atatat 		else
    339   1.4  atatat 			output(&st, argv[0], statfmt, fn, nonl, quiet);
    340   1.1  atatat 
    341   1.1  atatat 		argv++;
    342   1.1  atatat 		argc--;
    343   1.2  atatat 		fn++;
    344   1.1  atatat 	} while (argc > 0);
    345   1.1  atatat 
    346   1.4  atatat 	return (am_readlink ? linkfail : errs);
    347   1.1  atatat }
    348   1.1  atatat 
    349   1.1  atatat void
    350   1.4  atatat usage(const char *synopsis)
    351   1.1  atatat {
    352   1.1  atatat 
    353   1.4  atatat 	(void)fprintf(stderr, "usage: %s %s\n", getprogname(), synopsis);
    354   1.1  atatat 	exit(1);
    355   1.1  atatat }
    356   1.1  atatat 
    357   1.1  atatat /*
    358   1.1  atatat  * Parses a format string.
    359   1.1  atatat  */
    360   1.1  atatat void
    361   1.1  atatat output(const struct stat *st, const char *file,
    362   1.4  atatat     const char *statfmt, int fn, int nonl, int quiet)
    363   1.1  atatat {
    364   1.1  atatat 	int flags, size, prec, ofmt, hilo, what;
    365  1.22  atatat 	char buf[PATH_MAX + 4 + 1];
    366   1.1  atatat 	const char *subfmt;
    367   1.1  atatat 	int nl, t, i;
    368   1.1  atatat 
    369   1.4  atatat 	nl = 1;
    370   1.1  atatat 	while (*statfmt != '\0') {
    371   1.1  atatat 
    372   1.1  atatat 		/*
    373   1.1  atatat 		 * Non-format characters go straight out.
    374   1.1  atatat 		 */
    375   1.2  atatat 		if (*statfmt != FMT_MAGIC) {
    376   1.4  atatat 			addchar(stdout, *statfmt, &nl);
    377   1.1  atatat 			statfmt++;
    378   1.1  atatat 			continue;
    379   1.1  atatat 		}
    380   1.1  atatat 
    381   1.1  atatat 		/*
    382   1.1  atatat 		 * The current format "substring" starts here,
    383   1.2  atatat 		 * and then we skip the magic.
    384   1.1  atatat 		 */
    385   1.1  atatat 		subfmt = statfmt;
    386   1.1  atatat 		statfmt++;
    387   1.1  atatat 
    388   1.1  atatat 		/*
    389   1.1  atatat 		 * Some simple one-character "formats".
    390   1.1  atatat 		 */
    391   1.1  atatat 		switch (*statfmt) {
    392   1.1  atatat 		case SIMPLE_NEWLINE:
    393   1.4  atatat 			addchar(stdout, '\n', &nl);
    394   1.1  atatat 			statfmt++;
    395   1.1  atatat 			continue;
    396   1.1  atatat 		case SIMPLE_TAB:
    397   1.4  atatat 			addchar(stdout, '\t', &nl);
    398   1.1  atatat 			statfmt++;
    399   1.1  atatat 			continue;
    400   1.1  atatat 		case SIMPLE_PERCENT:
    401   1.4  atatat 			addchar(stdout, '%', &nl);
    402   1.1  atatat 			statfmt++;
    403   1.1  atatat 			continue;
    404   1.2  atatat 		case SIMPLE_NUMBER: {
    405   1.2  atatat 			char num[12], *p;
    406   1.2  atatat 
    407   1.2  atatat 			snprintf(num, sizeof(num), "%d", fn);
    408   1.2  atatat 			for (p = &num[0]; *p; p++)
    409   1.4  atatat 				addchar(stdout, *p, &nl);
    410   1.2  atatat 			statfmt++;
    411   1.2  atatat 			continue;
    412   1.2  atatat 		}
    413   1.1  atatat 		}
    414   1.1  atatat 
    415   1.1  atatat 		/*
    416   1.1  atatat 		 * This must be an actual format string.  Format strings are
    417   1.1  atatat 		 * similar to printf(3) formats up to a point, and are of
    418   1.1  atatat 		 * the form:
    419   1.1  atatat 		 *
    420   1.1  atatat 		 *	%	required start of format
    421   1.1  atatat 		 *	[-# +0]	opt. format characters
    422   1.1  atatat 		 *	size	opt. field width
    423   1.1  atatat 		 *	.	opt. decimal separator, followed by
    424   1.1  atatat 		 *	prec	opt. precision
    425   1.1  atatat 		 *	fmt	opt. output specifier (string, numeric, etc.)
    426   1.1  atatat 		 *	sub	opt. sub field specifier (high, middle, low)
    427   1.1  atatat 		 *	datum	required field specifier (size, mode, etc)
    428   1.1  atatat 		 *
    429   1.1  atatat 		 * Only the % and the datum selector are required.  All data
    430   1.1  atatat 		 * have reasonable default output forms.  The "sub" specifier
    431   1.1  atatat 		 * only applies to certain data (mode, dev, rdev, filetype).
    432   1.1  atatat 		 * The symlink output defaults to STRING, yet will only emit
    433   1.1  atatat 		 * the leading " -> " if STRING is explicitly specified.  The
    434   1.1  atatat 		 * sizerdev datum will generate rdev output for character or
    435   1.1  atatat 		 * block devices, and size output for all others.
    436   1.1  atatat 		 */
    437   1.1  atatat 		flags = 0;
    438   1.1  atatat 		do {
    439   1.2  atatat 			if      (*statfmt == FMT_POUND)
    440   1.2  atatat 				flags |= FLAG_POUND;
    441   1.2  atatat 			else if (*statfmt == FMT_SPACE)
    442   1.2  atatat 				flags |= FLAG_SPACE;
    443   1.2  atatat 			else if (*statfmt == FMT_PLUS)
    444   1.2  atatat 				flags |= FLAG_PLUS;
    445   1.2  atatat 			else if (*statfmt == FMT_ZERO)
    446   1.2  atatat 				flags |= FLAG_ZERO;
    447   1.2  atatat 			else if (*statfmt == FMT_MINUS)
    448   1.2  atatat 				flags |= FLAG_MINUS;
    449   1.1  atatat 			else
    450   1.1  atatat 				break;
    451   1.1  atatat 			statfmt++;
    452   1.1  atatat 		} while (1/*CONSTCOND*/);
    453   1.1  atatat 
    454   1.1  atatat 		size = -1;
    455   1.1  atatat 		if (isdigit((unsigned)*statfmt)) {
    456   1.1  atatat 			size = 0;
    457   1.1  atatat 			while (isdigit((unsigned)*statfmt)) {
    458   1.1  atatat 				size = (size * 10) + (*statfmt - '0');
    459   1.1  atatat 				statfmt++;
    460   1.1  atatat 				if (size < 0)
    461   1.1  atatat 					goto badfmt;
    462   1.1  atatat 			}
    463   1.1  atatat 		}
    464   1.1  atatat 
    465   1.1  atatat 		prec = -1;
    466   1.2  atatat 		if (*statfmt == FMT_DOT) {
    467   1.1  atatat 			statfmt++;
    468   1.1  atatat 
    469   1.1  atatat 			prec = 0;
    470   1.1  atatat 			while (isdigit((unsigned)*statfmt)) {
    471   1.1  atatat 				prec = (prec * 10) + (*statfmt - '0');
    472   1.1  atatat 				statfmt++;
    473   1.1  atatat 				if (prec < 0)
    474   1.1  atatat 					goto badfmt;
    475   1.1  atatat 			}
    476   1.1  atatat 		}
    477   1.1  atatat 
    478   1.5  atatat #define fmtcase(x, y)		case (y): (x) = (y); statfmt++; break
    479   1.5  atatat #define fmtcasef(x, y, z)	case (y): (x) = (z); statfmt++; break
    480   1.1  atatat 		switch (*statfmt) {
    481   1.5  atatat 			fmtcasef(ofmt, FMT_DECIMAL,	FMTF_DECIMAL);
    482   1.5  atatat 			fmtcasef(ofmt, FMT_OCTAL,	FMTF_OCTAL);
    483   1.5  atatat 			fmtcasef(ofmt, FMT_UNSIGNED,	FMTF_UNSIGNED);
    484   1.5  atatat 			fmtcasef(ofmt, FMT_HEX,		FMTF_HEX);
    485   1.5  atatat 			fmtcasef(ofmt, FMT_FLOAT,	FMTF_FLOAT);
    486   1.5  atatat 			fmtcasef(ofmt, FMT_STRING,	FMTF_STRING);
    487   1.1  atatat 		default:
    488   1.1  atatat 			ofmt = 0;
    489   1.1  atatat 			break;
    490   1.1  atatat 		}
    491   1.1  atatat 
    492   1.1  atatat 		switch (*statfmt) {
    493   1.1  atatat 			fmtcase(hilo, HIGH_PIECE);
    494   1.1  atatat 			fmtcase(hilo, MIDDLE_PIECE);
    495   1.1  atatat 			fmtcase(hilo, LOW_PIECE);
    496   1.1  atatat 		default:
    497   1.1  atatat 			hilo = 0;
    498   1.1  atatat 			break;
    499   1.1  atatat 		}
    500   1.1  atatat 
    501   1.1  atatat 		switch (*statfmt) {
    502  1.24    elad 			fmtcase(what, SHOW_realpath);
    503   1.1  atatat 			fmtcase(what, SHOW_st_dev);
    504   1.1  atatat 			fmtcase(what, SHOW_st_ino);
    505   1.1  atatat 			fmtcase(what, SHOW_st_mode);
    506   1.1  atatat 			fmtcase(what, SHOW_st_nlink);
    507   1.1  atatat 			fmtcase(what, SHOW_st_uid);
    508   1.1  atatat 			fmtcase(what, SHOW_st_gid);
    509   1.1  atatat 			fmtcase(what, SHOW_st_rdev);
    510   1.1  atatat 			fmtcase(what, SHOW_st_atime);
    511   1.1  atatat 			fmtcase(what, SHOW_st_mtime);
    512   1.1  atatat 			fmtcase(what, SHOW_st_ctime);
    513  1.10  atatat 			fmtcase(what, SHOW_st_btime);
    514   1.1  atatat 			fmtcase(what, SHOW_st_size);
    515   1.1  atatat 			fmtcase(what, SHOW_st_blocks);
    516   1.1  atatat 			fmtcase(what, SHOW_st_blksize);
    517   1.1  atatat 			fmtcase(what, SHOW_st_flags);
    518   1.1  atatat 			fmtcase(what, SHOW_st_gen);
    519   1.1  atatat 			fmtcase(what, SHOW_symlink);
    520   1.1  atatat 			fmtcase(what, SHOW_filetype);
    521   1.1  atatat 			fmtcase(what, SHOW_filename);
    522   1.1  atatat 			fmtcase(what, SHOW_sizerdev);
    523   1.1  atatat 		default:
    524   1.1  atatat 			goto badfmt;
    525   1.1  atatat 		}
    526   1.5  atatat #undef fmtcasef
    527   1.1  atatat #undef fmtcase
    528   1.1  atatat 
    529   1.1  atatat 		t = format1(st,
    530   1.1  atatat 		     file,
    531   1.1  atatat 		     subfmt, statfmt - subfmt,
    532   1.4  atatat 		     buf, sizeof(buf),
    533   1.1  atatat 		     flags, size, prec, ofmt, hilo, what);
    534   1.1  atatat 
    535  1.22  atatat 		for (i = 0; i < t && i < sizeof(buf) - 1; i++)
    536   1.4  atatat 			addchar(stdout, buf[i], &nl);
    537   1.1  atatat 
    538   1.1  atatat 		continue;
    539   1.1  atatat 
    540   1.1  atatat 	badfmt:
    541   1.1  atatat 		errx(1, "%.*s: bad format",
    542   1.1  atatat 		    (int)(statfmt - subfmt + 1), subfmt);
    543   1.1  atatat 	}
    544   1.1  atatat 
    545   1.1  atatat 	if (!nl && !nonl)
    546   1.4  atatat 		(void)fputc('\n', stdout);
    547   1.4  atatat 	(void)fflush(stdout);
    548   1.1  atatat }
    549   1.1  atatat 
    550   1.1  atatat /*
    551   1.1  atatat  * Arranges output according to a single parsed format substring.
    552   1.1  atatat  */
    553   1.1  atatat int
    554   1.1  atatat format1(const struct stat *st,
    555   1.1  atatat     const char *file,
    556   1.1  atatat     const char *fmt, int flen,
    557   1.1  atatat     char *buf, size_t blen,
    558   1.1  atatat     int flags, int size, int prec, int ofmt,
    559   1.1  atatat     int hilo, int what)
    560   1.1  atatat {
    561   1.1  atatat 	u_int64_t data;
    562   1.1  atatat 	char *sdata, lfmt[24], tmp[20];
    563   1.7  atatat 	char smode[12], sid[12], path[PATH_MAX + 4];
    564   1.1  atatat 	struct passwd *pw;
    565   1.1  atatat 	struct group *gr;
    566   1.1  atatat 	struct tm *tm;
    567  1.14     chs 	time_t secs;
    568  1.14     chs 	long nsecs;
    569  1.23  atatat 	int l, small, formats, gottime, shift;
    570   1.1  atatat 
    571   1.1  atatat 	formats = 0;
    572   1.1  atatat 	small = 0;
    573  1.14     chs 	gottime = 0;
    574  1.14     chs 	secs = 0;
    575  1.14     chs 	nsecs = 0;
    576  1.23  atatat 	shift = 0;
    577   1.1  atatat 
    578   1.1  atatat 	/*
    579   1.1  atatat 	 * First, pick out the data and tweak it based on hilo or
    580   1.1  atatat 	 * specified output format (symlink output only).
    581   1.1  atatat 	 */
    582   1.1  atatat 	switch (what) {
    583   1.1  atatat 	case SHOW_st_dev:
    584   1.1  atatat 	case SHOW_st_rdev:
    585   1.1  atatat 		small = (sizeof(st->st_dev) == 4);
    586   1.1  atatat 		data = (what == SHOW_st_dev) ? st->st_dev : st->st_rdev;
    587  1.13  atatat #if HAVE_DEVNAME
    588   1.1  atatat 		sdata = (what == SHOW_st_dev) ?
    589   1.1  atatat 		    devname(st->st_dev, S_IFBLK) :
    590   1.1  atatat 		    devname(st->st_rdev,
    591   1.1  atatat 		    S_ISCHR(st->st_mode) ? S_IFCHR :
    592   1.1  atatat 		    S_ISBLK(st->st_mode) ? S_IFBLK :
    593   1.1  atatat 		    0U);
    594   1.3  atatat 		if (sdata == NULL)
    595   1.3  atatat 			sdata = "???";
    596  1.13  atatat #endif /* HAVE_DEVNAME */
    597   1.1  atatat 		if (hilo == HIGH_PIECE) {
    598   1.1  atatat 			data = major(data);
    599   1.1  atatat 			hilo = 0;
    600   1.1  atatat 		}
    601   1.1  atatat 		else if (hilo == LOW_PIECE) {
    602   1.1  atatat 			data = minor((unsigned)data);
    603   1.1  atatat 			hilo = 0;
    604   1.1  atatat 		}
    605   1.5  atatat 		formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
    606  1.13  atatat #if HAVE_DEVNAME
    607   1.5  atatat 		    FMTF_STRING;
    608  1.13  atatat #else /* HAVE_DEVNAME */
    609  1.13  atatat 		    0;
    610  1.13  atatat #endif /* HAVE_DEVNAME */
    611   1.1  atatat 		if (ofmt == 0)
    612   1.5  atatat 			ofmt = FMTF_UNSIGNED;
    613   1.1  atatat 		break;
    614   1.1  atatat 	case SHOW_st_ino:
    615   1.1  atatat 		small = (sizeof(st->st_ino) == 4);
    616   1.1  atatat 		data = st->st_ino;
    617   1.1  atatat 		sdata = NULL;
    618   1.5  atatat 		formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
    619   1.1  atatat 		if (ofmt == 0)
    620   1.5  atatat 			ofmt = FMTF_UNSIGNED;
    621   1.1  atatat 		break;
    622   1.1  atatat 	case SHOW_st_mode:
    623   1.1  atatat 		small = (sizeof(st->st_mode) == 4);
    624   1.1  atatat 		data = st->st_mode;
    625   1.1  atatat 		strmode(st->st_mode, smode);
    626   1.1  atatat 		sdata = smode;
    627   1.1  atatat 		l = strlen(sdata);
    628   1.1  atatat 		if (sdata[l - 1] == ' ')
    629   1.1  atatat 			sdata[--l] = '\0';
    630   1.1  atatat 		if (hilo == HIGH_PIECE) {
    631   1.1  atatat 			data >>= 12;
    632   1.1  atatat 			sdata += 1;
    633   1.1  atatat 			sdata[3] = '\0';
    634   1.1  atatat 			hilo = 0;
    635   1.1  atatat 		}
    636   1.1  atatat 		else if (hilo == MIDDLE_PIECE) {
    637   1.2  atatat 			data = (data >> 9) & 07;
    638   1.1  atatat 			sdata += 4;
    639   1.1  atatat 			sdata[3] = '\0';
    640   1.1  atatat 			hilo = 0;
    641   1.1  atatat 		}
    642   1.1  atatat 		else if (hilo == LOW_PIECE) {
    643   1.2  atatat 			data &= 0777;
    644   1.1  atatat 			sdata += 7;
    645   1.1  atatat 			sdata[3] = '\0';
    646   1.1  atatat 			hilo = 0;
    647   1.1  atatat 		}
    648   1.5  atatat 		formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
    649   1.5  atatat 		    FMTF_STRING;
    650   1.1  atatat 		if (ofmt == 0)
    651   1.5  atatat 			ofmt = FMTF_OCTAL;
    652   1.1  atatat 		break;
    653   1.1  atatat 	case SHOW_st_nlink:
    654   1.1  atatat 		small = (sizeof(st->st_dev) == 4);
    655   1.1  atatat 		data = st->st_nlink;
    656   1.1  atatat 		sdata = NULL;
    657   1.5  atatat 		formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
    658   1.1  atatat 		if (ofmt == 0)
    659   1.5  atatat 			ofmt = FMTF_UNSIGNED;
    660   1.1  atatat 		break;
    661   1.1  atatat 	case SHOW_st_uid:
    662   1.1  atatat 		small = (sizeof(st->st_uid) == 4);
    663   1.1  atatat 		data = st->st_uid;
    664   1.1  atatat 		if ((pw = getpwuid(st->st_uid)) != NULL)
    665   1.1  atatat 			sdata = pw->pw_name;
    666   1.1  atatat 		else {
    667   1.1  atatat 			snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_uid);
    668   1.1  atatat 			sdata = sid;
    669   1.1  atatat 		}
    670   1.5  atatat 		formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
    671   1.5  atatat 		    FMTF_STRING;
    672   1.1  atatat 		if (ofmt == 0)
    673   1.5  atatat 			ofmt = FMTF_UNSIGNED;
    674   1.1  atatat 		break;
    675   1.1  atatat 	case SHOW_st_gid:
    676   1.1  atatat 		small = (sizeof(st->st_gid) == 4);
    677   1.1  atatat 		data = st->st_gid;
    678   1.1  atatat 		if ((gr = getgrgid(st->st_gid)) != NULL)
    679   1.1  atatat 			sdata = gr->gr_name;
    680   1.1  atatat 		else {
    681   1.1  atatat 			snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_gid);
    682   1.1  atatat 			sdata = sid;
    683   1.1  atatat 		}
    684   1.5  atatat 		formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
    685   1.5  atatat 		    FMTF_STRING;
    686   1.1  atatat 		if (ofmt == 0)
    687   1.5  atatat 			ofmt = FMTF_UNSIGNED;
    688   1.1  atatat 		break;
    689   1.1  atatat 	case SHOW_st_atime:
    690  1.14     chs 		gottime = 1;
    691  1.14     chs 		secs = st->st_atime;
    692  1.16   lukem #if HAVE_STRUCT_STAT_ST_MTIMENSEC
    693  1.14     chs 		nsecs = st->st_atimensec;
    694  1.14     chs #endif
    695   1.1  atatat 		/* FALLTHROUGH */
    696   1.1  atatat 	case SHOW_st_mtime:
    697  1.14     chs 		if (!gottime) {
    698  1.17  atatat 			gottime = 1;
    699  1.14     chs 			secs = st->st_mtime;
    700  1.16   lukem #if HAVE_STRUCT_STAT_ST_MTIMENSEC
    701  1.14     chs 			nsecs = st->st_mtimensec;
    702  1.14     chs #endif
    703  1.14     chs 		}
    704   1.1  atatat 		/* FALLTHROUGH */
    705   1.1  atatat 	case SHOW_st_ctime:
    706  1.14     chs 		if (!gottime) {
    707  1.17  atatat 			gottime = 1;
    708  1.14     chs 			secs = st->st_ctime;
    709  1.16   lukem #if HAVE_STRUCT_STAT_ST_MTIMENSEC
    710  1.14     chs 			nsecs = st->st_ctimensec;
    711  1.14     chs #endif
    712  1.14     chs 		}
    713  1.10  atatat 		/* FALLTHROUGH */
    714  1.13  atatat #if HAVE_STRUCT_STAT_ST_BIRTHTIME
    715  1.10  atatat 	case SHOW_st_btime:
    716  1.14     chs 		if (!gottime) {
    717  1.17  atatat 			gottime = 1;
    718  1.19     jmc 			secs = st->st_birthtime;
    719  1.21     jmc #if HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC
    720  1.19     jmc 			nsecs = st->st_birthtimensec;
    721  1.21     jmc #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC */
    722  1.14     chs 		}
    723  1.13  atatat #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
    724  1.14     chs 		small = (sizeof(secs) == 4);
    725  1.14     chs 		data = secs;
    726   1.1  atatat 		small = 1;
    727  1.14     chs 		tm = localtime(&secs);
    728   1.1  atatat 		(void)strftime(path, sizeof(path), timefmt, tm);
    729   1.1  atatat 		sdata = path;
    730   1.5  atatat 		formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
    731   1.5  atatat 		    FMTF_FLOAT | FMTF_STRING;
    732   1.1  atatat 		if (ofmt == 0)
    733   1.5  atatat 			ofmt = FMTF_DECIMAL;
    734   1.1  atatat 		break;
    735   1.1  atatat 	case SHOW_st_size:
    736   1.1  atatat 		small = (sizeof(st->st_size) == 4);
    737   1.1  atatat 		data = st->st_size;
    738   1.1  atatat 		sdata = NULL;
    739   1.5  atatat 		formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
    740   1.1  atatat 		if (ofmt == 0)
    741   1.5  atatat 			ofmt = FMTF_UNSIGNED;
    742  1.23  atatat 		switch (hilo) {
    743  1.23  atatat 		case HIGH_PIECE:
    744  1.23  atatat 			shift = 30;	/* gigabytes */
    745  1.23  atatat 			hilo = 0;
    746  1.23  atatat 			break;
    747  1.23  atatat 		case MIDDLE_PIECE:
    748  1.23  atatat 			shift = 20;	/* megabytes */
    749  1.23  atatat 			hilo = 0;
    750  1.23  atatat 			break;
    751  1.23  atatat 		case LOW_PIECE:
    752  1.23  atatat 			shift = 10;	/* kilobytes */
    753  1.23  atatat 			hilo = 0;
    754  1.23  atatat 			break;
    755  1.23  atatat 		}
    756   1.1  atatat 		break;
    757   1.1  atatat 	case SHOW_st_blocks:
    758   1.1  atatat 		small = (sizeof(st->st_blocks) == 4);
    759   1.1  atatat 		data = st->st_blocks;
    760   1.1  atatat 		sdata = NULL;
    761   1.5  atatat 		formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
    762   1.1  atatat 		if (ofmt == 0)
    763   1.5  atatat 			ofmt = FMTF_UNSIGNED;
    764   1.1  atatat 		break;
    765   1.1  atatat 	case SHOW_st_blksize:
    766   1.1  atatat 		small = (sizeof(st->st_blksize) == 4);
    767   1.1  atatat 		data = st->st_blksize;
    768   1.1  atatat 		sdata = NULL;
    769   1.5  atatat 		formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
    770   1.1  atatat 		if (ofmt == 0)
    771   1.5  atatat 			ofmt = FMTF_UNSIGNED;
    772   1.1  atatat 		break;
    773  1.11   lukem #if HAVE_STRUCT_STAT_ST_FLAGS
    774   1.1  atatat 	case SHOW_st_flags:
    775   1.1  atatat 		small = (sizeof(st->st_flags) == 4);
    776   1.1  atatat 		data = st->st_flags;
    777   1.1  atatat 		sdata = NULL;
    778   1.5  atatat 		formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
    779   1.1  atatat 		if (ofmt == 0)
    780   1.5  atatat 			ofmt = FMTF_UNSIGNED;
    781   1.1  atatat 		break;
    782  1.13  atatat #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
    783  1.13  atatat #if HAVE_STRUCT_STAT_ST_GEN
    784   1.1  atatat 	case SHOW_st_gen:
    785   1.1  atatat 		small = (sizeof(st->st_gen) == 4);
    786   1.1  atatat 		data = st->st_gen;
    787   1.1  atatat 		sdata = NULL;
    788   1.5  atatat 		formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
    789   1.1  atatat 		if (ofmt == 0)
    790   1.5  atatat 			ofmt = FMTF_UNSIGNED;
    791   1.1  atatat 		break;
    792  1.13  atatat #endif /* HAVE_STRUCT_STAT_ST_GEN */
    793  1.24    elad 	case SHOW_realpath:
    794  1.24    elad 		small = 0;
    795  1.24    elad 		data = 0;
    796  1.24    elad 		snprintf(path, sizeof(path), " -> ");
    797  1.24    elad 		if (realpath(file, path + 4) == NULL) {
    798  1.24    elad 			linkfail = 1;
    799  1.24    elad 			l = 0;
    800  1.24    elad 			path[0] = '\0';
    801  1.24    elad 		}
    802  1.24    elad 		sdata = path + (ofmt == FMT_STRING ? 0: 4);
    803  1.24    elad 
    804  1.24    elad 		formats = FMTF_STRING;
    805  1.24    elad 		if (ofmt == 0)
    806  1.24    elad 			ofmt = FMTF_STRING;
    807  1.24    elad 		break;
    808   1.1  atatat 	case SHOW_symlink:
    809   1.1  atatat 		small = 0;
    810   1.1  atatat 		data = 0;
    811   1.1  atatat 		if (S_ISLNK(st->st_mode)) {
    812   1.1  atatat 			snprintf(path, sizeof(path), " -> ");
    813   1.9  provos 			l = readlink(file, path + 4, sizeof(path) - 4 - 1);
    814   1.1  atatat 			if (l == -1) {
    815   1.4  atatat 				linkfail = 1;
    816   1.1  atatat 				l = 0;
    817   1.1  atatat 				path[0] = '\0';
    818   1.1  atatat 			}
    819   1.1  atatat 			path[l + 4] = '\0';
    820   1.5  atatat 			sdata = path + (ofmt == FMTF_STRING ? 0 : 4);
    821   1.1  atatat 		}
    822   1.4  atatat 		else {
    823   1.4  atatat 			linkfail = 1;
    824   1.1  atatat 			sdata = "";
    825   1.4  atatat 		}
    826   1.5  atatat 		formats = FMTF_STRING;
    827   1.1  atatat 		if (ofmt == 0)
    828   1.5  atatat 			ofmt = FMTF_STRING;
    829   1.1  atatat 		break;
    830   1.1  atatat 	case SHOW_filetype:
    831   1.1  atatat 		small = 0;
    832   1.1  atatat 		data = 0;
    833   1.1  atatat 		sdata = smode;
    834   1.1  atatat 		sdata[0] = '\0';
    835   1.1  atatat 		if (hilo == 0 || hilo == LOW_PIECE) {
    836   1.1  atatat 			switch (st->st_mode & S_IFMT) {
    837   1.1  atatat 			case S_IFIFO:	(void)strcat(sdata, "|");	break;
    838   1.1  atatat 			case S_IFDIR:	(void)strcat(sdata, "/");	break;
    839   1.1  atatat 			case S_IFREG:
    840   1.1  atatat 				if (st->st_mode &
    841   1.1  atatat 				    (S_IXUSR | S_IXGRP | S_IXOTH))
    842   1.1  atatat 					(void)strcat(sdata, "*");
    843   1.1  atatat 				break;
    844   1.1  atatat 			case S_IFLNK:	(void)strcat(sdata, "@");	break;
    845  1.19     jmc #ifdef S_IFSOCK
    846   1.1  atatat 			case S_IFSOCK:	(void)strcat(sdata, "=");	break;
    847  1.19     jmc #endif
    848  1.13  atatat #ifdef S_IFWHT
    849   1.1  atatat 			case S_IFWHT:	(void)strcat(sdata, "%");	break;
    850  1.13  atatat #endif /* S_IFWHT */
    851  1.13  atatat #ifdef S_IFDOOR
    852  1.13  atatat 			case S_IFDOOR:	(void)strcat(sdata, ">");	break;
    853  1.13  atatat #endif /* S_IFDOOR */
    854   1.1  atatat 			}
    855   1.1  atatat 			hilo = 0;
    856   1.1  atatat 		}
    857   1.1  atatat 		else if (hilo == HIGH_PIECE) {
    858   1.1  atatat 			switch (st->st_mode & S_IFMT) {
    859   1.1  atatat 			case S_IFIFO:	sdata = "Fifo File";		break;
    860   1.1  atatat 			case S_IFCHR:	sdata = "Character Device";	break;
    861   1.1  atatat 			case S_IFDIR:	sdata = "Directory";		break;
    862   1.1  atatat 			case S_IFBLK:	sdata = "Block Device";		break;
    863   1.1  atatat 			case S_IFREG:	sdata = "Regular File";		break;
    864   1.1  atatat 			case S_IFLNK:	sdata = "Symbolic Link";	break;
    865  1.19     jmc #ifdef S_IFSOCK
    866   1.1  atatat 			case S_IFSOCK:	sdata = "Socket";		break;
    867  1.19     jmc #endif
    868  1.13  atatat #ifdef S_IFWHT
    869   1.1  atatat 			case S_IFWHT:	sdata = "Whiteout File";	break;
    870  1.13  atatat #endif /* S_IFWHT */
    871  1.13  atatat #ifdef S_IFDOOR
    872  1.13  atatat 			case S_IFDOOR:	sdata = "Door";			break;
    873  1.13  atatat #endif /* S_IFDOOR */
    874   1.1  atatat 			default:	sdata = "???";			break;
    875   1.1  atatat 			}
    876   1.1  atatat 			hilo = 0;
    877   1.1  atatat 		}
    878   1.5  atatat 		formats = FMTF_STRING;
    879   1.1  atatat 		if (ofmt == 0)
    880   1.5  atatat 			ofmt = FMTF_STRING;
    881   1.1  atatat 		break;
    882   1.1  atatat 	case SHOW_filename:
    883   1.1  atatat 		small = 0;
    884   1.1  atatat 		data = 0;
    885  1.20  atatat 		if (file == NULL) {
    886   1.1  atatat 			(void)strncpy(path, "(stdin)", sizeof(path));
    887  1.20  atatat 			if (hilo == HIGH_PIECE || hilo == LOW_PIECE)
    888  1.20  atatat 				hilo = 0;
    889  1.20  atatat 		}
    890  1.20  atatat 		else if (hilo == 0)
    891  1.20  atatat 			(void)strncpy(path, file, sizeof(path));
    892  1.20  atatat 		else {
    893  1.20  atatat 			char *s;
    894   1.1  atatat 			(void)strncpy(path, file, sizeof(path));
    895  1.20  atatat 			s = strrchr(path, '/');
    896  1.20  atatat 			if (s != NULL) {
    897  1.20  atatat 				/* trim off trailing /'s */
    898  1.20  atatat 				while (s != path &&
    899  1.20  atatat 				    s[0] == '/' && s[1] == '\0')
    900  1.20  atatat 					*s-- = '\0';
    901  1.20  atatat 				s = strrchr(path, '/');
    902  1.20  atatat 			}
    903  1.20  atatat 			if (hilo == HIGH_PIECE) {
    904  1.20  atatat 				if (s == NULL)
    905  1.20  atatat 					(void)strncpy(path, ".", sizeof(path));
    906  1.20  atatat 				else {
    907  1.20  atatat 					while (s != path && s[0] == '/')
    908  1.20  atatat 						*s-- = '\0';
    909  1.20  atatat 				}
    910  1.20  atatat 				hilo = 0;
    911  1.20  atatat 			}
    912  1.20  atatat 			else if (hilo == LOW_PIECE) {
    913  1.20  atatat 				if (s != NULL && s[1] != '\0')
    914  1.20  atatat 					(void)strncpy(path, s + 1,
    915  1.20  atatat 						      sizeof(path));
    916  1.20  atatat 				hilo = 0;
    917  1.20  atatat 			}
    918  1.20  atatat 		}
    919   1.1  atatat 		sdata = path;
    920   1.5  atatat 		formats = FMTF_STRING;
    921   1.1  atatat 		if (ofmt == 0)
    922   1.5  atatat 			ofmt = FMTF_STRING;
    923   1.1  atatat 		break;
    924   1.1  atatat 	case SHOW_sizerdev:
    925   1.1  atatat 		if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
    926   1.1  atatat 			char majdev[20], mindev[20];
    927   1.1  atatat 			int l1, l2;
    928   1.1  atatat 
    929   1.1  atatat 			l1 = format1(st,
    930   1.1  atatat 			    file,
    931   1.1  atatat 			    fmt, flen,
    932   1.1  atatat 			    majdev, sizeof(majdev),
    933   1.1  atatat 			    flags, size, prec,
    934   1.1  atatat 			    ofmt, HIGH_PIECE, SHOW_st_rdev);
    935   1.1  atatat 			l2 = format1(st,
    936   1.1  atatat 			    file,
    937   1.1  atatat 			    fmt, flen,
    938   1.1  atatat 			    mindev, sizeof(mindev),
    939   1.1  atatat 			    flags, size, prec,
    940   1.1  atatat 			    ofmt, LOW_PIECE, SHOW_st_rdev);
    941   1.1  atatat 			return (snprintf(buf, blen, "%.*s,%.*s",
    942   1.1  atatat 			    l1, majdev, l2, mindev));
    943   1.1  atatat 		}
    944   1.1  atatat 		else {
    945   1.1  atatat 			return (format1(st,
    946   1.1  atatat 			    file,
    947   1.1  atatat 			    fmt, flen,
    948   1.1  atatat 			    buf, blen,
    949   1.1  atatat 			    flags, size, prec,
    950   1.1  atatat 			    ofmt, 0, SHOW_st_size));
    951   1.1  atatat 		}
    952   1.1  atatat 		/*NOTREACHED*/
    953   1.1  atatat 	default:
    954   1.1  atatat 		errx(1, "%.*s: bad format", (int)flen, fmt);
    955   1.1  atatat 	}
    956   1.1  atatat 
    957   1.1  atatat 	/*
    958   1.1  atatat 	 * If a subdatum was specified but not supported, or an output
    959   1.1  atatat 	 * format was selected that is not supported, that's an error.
    960   1.1  atatat 	 */
    961   1.1  atatat 	if (hilo != 0 || (ofmt & formats) == 0)
    962   1.1  atatat 		errx(1, "%.*s: bad format", (int)flen, fmt);
    963   1.1  atatat 
    964   1.1  atatat 	/*
    965   1.1  atatat 	 * Assemble the format string for passing to printf(3).
    966   1.1  atatat 	 */
    967   1.1  atatat 	lfmt[0] = '\0';
    968   1.1  atatat 	(void)strcat(lfmt, "%");
    969   1.2  atatat 	if (flags & FLAG_POUND)
    970   1.1  atatat 		(void)strcat(lfmt, "#");
    971   1.2  atatat 	if (flags & FLAG_SPACE)
    972   1.1  atatat 		(void)strcat(lfmt, " ");
    973   1.2  atatat 	if (flags & FLAG_PLUS)
    974   1.1  atatat 		(void)strcat(lfmt, "+");
    975   1.2  atatat 	if (flags & FLAG_MINUS)
    976   1.1  atatat 		(void)strcat(lfmt, "-");
    977   1.2  atatat 	if (flags & FLAG_ZERO)
    978   1.1  atatat 		(void)strcat(lfmt, "0");
    979   1.1  atatat 
    980   1.1  atatat 	/*
    981   1.1  atatat 	 * Only the timespecs support the FLOAT output format, and that
    982   1.1  atatat 	 * requires work that differs from the other formats.
    983   1.1  atatat 	 */
    984   1.5  atatat 	if (ofmt == FMTF_FLOAT) {
    985   1.1  atatat 		/*
    986   1.1  atatat 		 * Nothing after the decimal point, so just print seconds.
    987   1.1  atatat 		 */
    988   1.1  atatat 		if (prec == 0) {
    989   1.1  atatat 			if (size != -1) {
    990   1.1  atatat 				(void)snprintf(tmp, sizeof(tmp), "%d", size);
    991   1.1  atatat 				(void)strcat(lfmt, tmp);
    992   1.1  atatat 			}
    993   1.1  atatat 			(void)strcat(lfmt, "d");
    994  1.14     chs 			return (snprintf(buf, blen, lfmt, secs));
    995   1.1  atatat 		}
    996   1.1  atatat 
    997   1.1  atatat 		/*
    998   1.1  atatat 		 * Unspecified precision gets all the precision we have:
    999   1.1  atatat 		 * 9 digits.
   1000   1.1  atatat 		 */
   1001   1.1  atatat 		if (prec == -1)
   1002   1.1  atatat 			prec = 9;
   1003   1.1  atatat 
   1004   1.1  atatat 		/*
   1005   1.1  atatat 		 * Adjust the size for the decimal point and the digits
   1006   1.1  atatat 		 * that will follow.
   1007   1.1  atatat 		 */
   1008   1.1  atatat 		size -= prec + 1;
   1009   1.1  atatat 
   1010   1.1  atatat 		/*
   1011   1.1  atatat 		 * Any leftover size that's legitimate will be used.
   1012   1.1  atatat 		 */
   1013   1.1  atatat 		if (size > 0) {
   1014   1.1  atatat 			(void)snprintf(tmp, sizeof(tmp), "%d", size);
   1015   1.1  atatat 			(void)strcat(lfmt, tmp);
   1016   1.1  atatat 		}
   1017   1.1  atatat 		(void)strcat(lfmt, "d");
   1018   1.1  atatat 
   1019   1.1  atatat 		/*
   1020   1.1  atatat 		 * The stuff after the decimal point always needs zero
   1021   1.1  atatat 		 * filling.
   1022   1.1  atatat 		 */
   1023   1.1  atatat 		(void)strcat(lfmt, ".%0");
   1024   1.1  atatat 
   1025   1.1  atatat 		/*
   1026   1.1  atatat 		 * We can "print" at most nine digits of precision.  The
   1027   1.1  atatat 		 * rest we will pad on at the end.
   1028   1.1  atatat 		 */
   1029   1.1  atatat 		(void)snprintf(tmp, sizeof(tmp), "%dd", prec > 9 ? 9 : prec);
   1030   1.1  atatat 		(void)strcat(lfmt, tmp);
   1031   1.1  atatat 
   1032   1.1  atatat 		/*
   1033   1.1  atatat 		 * For precision of less that nine digits, trim off the
   1034   1.1  atatat 		 * less significant figures.
   1035   1.1  atatat 		 */
   1036   1.1  atatat 		for (; prec < 9; prec++)
   1037  1.14     chs 			nsecs /= 10;
   1038   1.1  atatat 
   1039   1.1  atatat 		/*
   1040   1.1  atatat 		 * Use the format, and then tack on any zeroes that
   1041   1.1  atatat 		 * might be required to make up the requested precision.
   1042   1.1  atatat 		 */
   1043  1.14     chs 		l = snprintf(buf, blen, lfmt, secs, nsecs);
   1044   1.1  atatat 		for (; prec > 9 && l < blen; prec--, l++)
   1045   1.1  atatat 			(void)strcat(buf, "0");
   1046   1.1  atatat 		return (l);
   1047   1.1  atatat 	}
   1048   1.1  atatat 
   1049   1.1  atatat 	/*
   1050   1.1  atatat 	 * Add on size and precision, if specified, to the format.
   1051   1.1  atatat 	 */
   1052   1.1  atatat 	if (size != -1) {
   1053   1.1  atatat 		(void)snprintf(tmp, sizeof(tmp), "%d", size);
   1054   1.1  atatat 		(void)strcat(lfmt, tmp);
   1055   1.1  atatat 	}
   1056   1.1  atatat 	if (prec != -1) {
   1057   1.1  atatat 		(void)snprintf(tmp, sizeof(tmp), ".%d", prec);
   1058   1.1  atatat 		(void)strcat(lfmt, tmp);
   1059   1.1  atatat 	}
   1060   1.1  atatat 
   1061   1.1  atatat 	/*
   1062   1.1  atatat 	 * String output uses the temporary sdata.
   1063   1.1  atatat 	 */
   1064   1.5  atatat 	if (ofmt == FMTF_STRING) {
   1065   1.1  atatat 		if (sdata == NULL)
   1066   1.1  atatat 			errx(1, "%.*s: bad format", (int)flen, fmt);
   1067   1.1  atatat 		(void)strcat(lfmt, "s");
   1068   1.1  atatat 		return (snprintf(buf, blen, lfmt, sdata));
   1069   1.1  atatat 	}
   1070   1.1  atatat 
   1071   1.1  atatat 	/*
   1072   1.1  atatat 	 * Ensure that sign extension does not cause bad looking output
   1073   1.1  atatat 	 * for some forms.
   1074   1.1  atatat 	 */
   1075   1.5  atatat 	if (small && ofmt != FMTF_DECIMAL)
   1076   1.1  atatat 		data = (u_int32_t)data;
   1077   1.1  atatat 
   1078   1.1  atatat 	/*
   1079   1.1  atatat 	 * The four "numeric" output forms.
   1080   1.1  atatat 	 */
   1081   1.1  atatat 	(void)strcat(lfmt, "ll");
   1082   1.1  atatat 	switch (ofmt) {
   1083   1.5  atatat 	case FMTF_DECIMAL:	(void)strcat(lfmt, "d");	break;
   1084  1.23  atatat 	case FMTF_OCTAL:	(void)strcat(lfmt, "o");	break;
   1085   1.5  atatat 	case FMTF_UNSIGNED:	(void)strcat(lfmt, "u");	break;
   1086   1.5  atatat 	case FMTF_HEX:		(void)strcat(lfmt, "x");	break;
   1087   1.1  atatat 	}
   1088   1.1  atatat 
   1089  1.23  atatat 	/*
   1090  1.23  atatat 	 * shift and round to nearest for kilobytes, megabytes,
   1091  1.23  atatat 	 * gigabytes.
   1092  1.23  atatat 	 */
   1093  1.23  atatat 	if (shift > 0) {
   1094  1.23  atatat 		data >>= (shift - 1);
   1095  1.23  atatat 		data++;
   1096  1.23  atatat 		data >>= 1;
   1097  1.23  atatat 	}
   1098  1.23  atatat 
   1099   1.1  atatat 	return (snprintf(buf, blen, lfmt, data));
   1100   1.1  atatat }
   1101