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