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