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