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