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