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