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