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