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