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