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