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