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