stat.c revision 188481
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#include <sys/cdefs.h>
38#if 0
39#ifndef lint
40__RCSID("$NetBSD: stat.c,v 1.13 2003/07/25 03:21:17 atatat Exp $");
41#endif
42#endif
43
44__FBSDID("$FreeBSD: head/usr.bin/stat/stat.c 188481 2009-02-11 10:50:26Z maxim $");
45
46#if HAVE_CONFIG_H
47#include "config.h"
48#else  /* HAVE_CONFIG_H */
49#define HAVE_STRUCT_STAT_ST_FLAGS 1
50#define HAVE_STRUCT_STAT_ST_GEN 1
51#define HAVE_STRUCT_STAT_ST_BIRTHTIME 1
52#define HAVE_STRUCT_STAT_ST_MTIMENSEC 1
53#define HAVE_DEVNAME 1
54#endif /* HAVE_CONFIG_H */
55
56#include <sys/types.h>
57#include <sys/stat.h>
58
59#include <ctype.h>
60#include <err.h>
61#include <grp.h>
62#include <limits.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"
116
117#define TIME_FORMAT	"%b %e %T %Y"
118
119#define FLAG_POUND	0x01
120#define FLAG_SPACE	0x02
121#define FLAG_PLUS	0x04
122#define FLAG_ZERO	0x08
123#define FLAG_MINUS	0x10
124
125/*
126 * These format characters must all be unique, except the magic one.
127 */
128#define FMT_MAGIC	'%'
129#define FMT_DOT		'.'
130
131#define SIMPLE_NEWLINE	'n'
132#define SIMPLE_TAB	't'
133#define SIMPLE_PERCENT	'%'
134#define SIMPLE_NUMBER	'@'
135
136#define FMT_POUND	'#'
137#define FMT_SPACE	' '
138#define FMT_PLUS	'+'
139#define FMT_ZERO	'0'
140#define FMT_MINUS	'-'
141
142#define FMT_DECIMAL 	'D'
143#define FMT_OCTAL 	'O'
144#define FMT_UNSIGNED 	'U'
145#define FMT_HEX 	'X'
146#define FMT_FLOAT 	'F'
147#define FMT_STRING 	'S'
148
149#define FMTF_DECIMAL	0x01
150#define FMTF_OCTAL	0x02
151#define FMTF_UNSIGNED	0x04
152#define FMTF_HEX	0x08
153#define FMTF_FLOAT	0x10
154#define FMTF_STRING	0x20
155
156#define HIGH_PIECE	'H'
157#define MIDDLE_PIECE	'M'
158#define LOW_PIECE	'L'
159
160#define SHOW_st_dev	'd'
161#define SHOW_st_ino	'i'
162#define SHOW_st_mode	'p'
163#define SHOW_st_nlink	'l'
164#define SHOW_st_uid	'u'
165#define SHOW_st_gid	'g'
166#define SHOW_st_rdev	'r'
167#define SHOW_st_atime	'a'
168#define SHOW_st_mtime	'm'
169#define SHOW_st_ctime	'c'
170#define SHOW_st_btime	'B'
171#define SHOW_st_size	'z'
172#define SHOW_st_blocks	'b'
173#define SHOW_st_blksize	'k'
174#define SHOW_st_flags	'f'
175#define SHOW_st_gen	'v'
176#define SHOW_symlink	'Y'
177#define SHOW_filetype	'T'
178#define SHOW_filename	'N'
179#define SHOW_sizerdev	'Z'
180
181void	usage(const char *);
182void	output(const struct stat *, const char *,
183	    const char *, int, int, int);
184int	format1(const struct stat *,	/* stat info */
185	    const char *,		/* the file name */
186	    const char *, int,		/* the format string itself */
187	    char *, size_t,		/* a place to put the output */
188	    int, int, int, int,		/* the parsed format */
189	    int, int);
190
191char *timefmt;
192int linkfail;
193
194#define addchar(s, c, nl) \
195	do { \
196		(void)fputc((c), (s)); \
197		(*nl) = ((c) == '\n'); \
198	} while (0/*CONSTCOND*/)
199
200int
201main(int argc, char *argv[])
202{
203	struct stat st;
204	int ch, rc, errs, am_readlink;
205	int lsF, fmtchar, usestat, fn, nonl, quiet;
206	char *statfmt, *options, *synopsis;
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			rc = fstat(STDIN_FILENO, &st);
309		else if (usestat)
310			rc = stat(argv[0], &st);
311		else
312			rc = lstat(argv[0], &st);
313
314		if (rc == -1) {
315			errs = 1;
316			linkfail = 1;
317			if (!quiet)
318				warn("%s: stat",
319				    argc == 0 ? "(stdin)" : argv[0]);
320		}
321		else
322			output(&st, argv[0], statfmt, fn, nonl, quiet);
323
324		argv++;
325		argc--;
326		fn++;
327	} while (argc > 0);
328
329	return (am_readlink ? linkfail : errs);
330}
331
332void
333usage(const char *synopsis)
334{
335
336	(void)fprintf(stderr, "usage: %s %s\n", getprogname(), synopsis);
337	exit(1);
338}
339
340/*
341 * Parses a format string.
342 */
343void
344output(const struct stat *st, const char *file,
345    const char *statfmt, int fn, int nonl, int quiet)
346{
347	int flags, size, prec, ofmt, hilo, what;
348	char buf[PATH_MAX];
349	const char *subfmt;
350	int nl, t, i;
351
352	nl = 1;
353	while (*statfmt != '\0') {
354
355		/*
356		 * Non-format characters go straight out.
357		 */
358		if (*statfmt != FMT_MAGIC) {
359			addchar(stdout, *statfmt, &nl);
360			statfmt++;
361			continue;
362		}
363
364		/*
365		 * The current format "substring" starts here,
366		 * and then we skip the magic.
367		 */
368		subfmt = statfmt;
369		statfmt++;
370
371		/*
372		 * Some simple one-character "formats".
373		 */
374		switch (*statfmt) {
375		case SIMPLE_NEWLINE:
376			addchar(stdout, '\n', &nl);
377			statfmt++;
378			continue;
379		case SIMPLE_TAB:
380			addchar(stdout, '\t', &nl);
381			statfmt++;
382			continue;
383		case SIMPLE_PERCENT:
384			addchar(stdout, '%', &nl);
385			statfmt++;
386			continue;
387		case SIMPLE_NUMBER: {
388			char num[12], *p;
389
390			snprintf(num, sizeof(num), "%d", fn);
391			for (p = &num[0]; *p; p++)
392				addchar(stdout, *p, &nl);
393			statfmt++;
394			continue;
395		}
396		}
397
398		/*
399		 * This must be an actual format string.  Format strings are
400		 * similar to printf(3) formats up to a point, and are of
401		 * the form:
402		 *
403		 *	%	required start of format
404		 *	[-# +0]	opt. format characters
405		 *	size	opt. field width
406		 *	.	opt. decimal separator, followed by
407		 *	prec	opt. precision
408		 *	fmt	opt. output specifier (string, numeric, etc.)
409		 *	sub	opt. sub field specifier (high, middle, low)
410		 *	datum	required field specifier (size, mode, etc)
411		 *
412		 * Only the % and the datum selector are required.  All data
413		 * have reasonable default output forms.  The "sub" specifier
414		 * only applies to certain data (mode, dev, rdev, filetype).
415		 * The symlink output defaults to STRING, yet will only emit
416		 * the leading " -> " if STRING is explicitly specified.  The
417		 * sizerdev datum will generate rdev output for character or
418		 * block devices, and size output for all others.
419		 */
420		flags = 0;
421		do {
422			if      (*statfmt == FMT_POUND)
423				flags |= FLAG_POUND;
424			else if (*statfmt == FMT_SPACE)
425				flags |= FLAG_SPACE;
426			else if (*statfmt == FMT_PLUS)
427				flags |= FLAG_PLUS;
428			else if (*statfmt == FMT_ZERO)
429				flags |= FLAG_ZERO;
430			else if (*statfmt == FMT_MINUS)
431				flags |= FLAG_MINUS;
432			else
433				break;
434			statfmt++;
435		} while (1/*CONSTCOND*/);
436
437		size = -1;
438		if (isdigit((unsigned)*statfmt)) {
439			size = 0;
440			while (isdigit((unsigned)*statfmt)) {
441				size = (size * 10) + (*statfmt - '0');
442				statfmt++;
443				if (size < 0)
444					goto badfmt;
445			}
446		}
447
448		prec = -1;
449		if (*statfmt == FMT_DOT) {
450			statfmt++;
451
452			prec = 0;
453			while (isdigit((unsigned)*statfmt)) {
454				prec = (prec * 10) + (*statfmt - '0');
455				statfmt++;
456				if (prec < 0)
457					goto badfmt;
458			}
459		}
460
461#define fmtcase(x, y)		case (y): (x) = (y); statfmt++; break
462#define fmtcasef(x, y, z)	case (y): (x) = (z); statfmt++; break
463		switch (*statfmt) {
464			fmtcasef(ofmt, FMT_DECIMAL,	FMTF_DECIMAL);
465			fmtcasef(ofmt, FMT_OCTAL,	FMTF_OCTAL);
466			fmtcasef(ofmt, FMT_UNSIGNED,	FMTF_UNSIGNED);
467			fmtcasef(ofmt, FMT_HEX,		FMTF_HEX);
468			fmtcasef(ofmt, FMT_FLOAT,	FMTF_FLOAT);
469			fmtcasef(ofmt, FMT_STRING,	FMTF_STRING);
470		default:
471			ofmt = 0;
472			break;
473		}
474
475		switch (*statfmt) {
476			fmtcase(hilo, HIGH_PIECE);
477			fmtcase(hilo, MIDDLE_PIECE);
478			fmtcase(hilo, LOW_PIECE);
479		default:
480			hilo = 0;
481			break;
482		}
483
484		switch (*statfmt) {
485			fmtcase(what, SHOW_st_dev);
486			fmtcase(what, SHOW_st_ino);
487			fmtcase(what, SHOW_st_mode);
488			fmtcase(what, SHOW_st_nlink);
489			fmtcase(what, SHOW_st_uid);
490			fmtcase(what, SHOW_st_gid);
491			fmtcase(what, SHOW_st_rdev);
492			fmtcase(what, SHOW_st_atime);
493			fmtcase(what, SHOW_st_mtime);
494			fmtcase(what, SHOW_st_ctime);
495			fmtcase(what, SHOW_st_btime);
496			fmtcase(what, SHOW_st_size);
497			fmtcase(what, SHOW_st_blocks);
498			fmtcase(what, SHOW_st_blksize);
499			fmtcase(what, SHOW_st_flags);
500			fmtcase(what, SHOW_st_gen);
501			fmtcase(what, SHOW_symlink);
502			fmtcase(what, SHOW_filetype);
503			fmtcase(what, SHOW_filename);
504			fmtcase(what, SHOW_sizerdev);
505		default:
506			goto badfmt;
507		}
508#undef fmtcasef
509#undef fmtcase
510
511		t = format1(st,
512		     file,
513		     subfmt, statfmt - subfmt,
514		     buf, sizeof(buf),
515		     flags, size, prec, ofmt, hilo, what);
516
517		for (i = 0; i < t && i < sizeof(buf); i++)
518			addchar(stdout, buf[i], &nl);
519
520		continue;
521
522	badfmt:
523		errx(1, "%.*s: bad format",
524		    (int)(statfmt - subfmt + 1), subfmt);
525	}
526
527	if (!nl && !nonl)
528		(void)fputc('\n', stdout);
529	(void)fflush(stdout);
530}
531
532/*
533 * Arranges output according to a single parsed format substring.
534 */
535int
536format1(const struct stat *st,
537    const char *file,
538    const char *fmt, int flen,
539    char *buf, size_t blen,
540    int flags, int size, int prec, int ofmt,
541    int hilo, int what)
542{
543	u_int64_t data;
544	char *sdata, lfmt[24], tmp[20];
545	char smode[12], sid[12], path[PATH_MAX + 4];
546	struct passwd *pw;
547	struct group *gr;
548	const struct timespec *tsp;
549	struct timespec ts;
550	struct tm *tm;
551	int l, small, formats;
552
553	tsp = NULL;
554	formats = 0;
555	small = 0;
556
557	/*
558	 * First, pick out the data and tweak it based on hilo or
559	 * specified output format (symlink output only).
560	 */
561	switch (what) {
562	case SHOW_st_dev:
563	case SHOW_st_rdev:
564		small = (sizeof(st->st_dev) == 4);
565		data = (what == SHOW_st_dev) ? st->st_dev : st->st_rdev;
566#if HAVE_DEVNAME
567		sdata = (what == SHOW_st_dev) ?
568		    devname(st->st_dev, S_IFBLK) :
569		    devname(st->st_rdev,
570		    S_ISCHR(st->st_mode) ? S_IFCHR :
571		    S_ISBLK(st->st_mode) ? S_IFBLK :
572		    0U);
573		if (sdata == NULL)
574			sdata = "???";
575#endif /* HAVE_DEVNAME */
576		if (hilo == HIGH_PIECE) {
577			data = major(data);
578			hilo = 0;
579		}
580		else if (hilo == LOW_PIECE) {
581			data = minor((unsigned)data);
582			hilo = 0;
583		}
584		formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
585#if HAVE_DEVNAME
586		    FMTF_STRING;
587#else /* HAVE_DEVNAME */
588		    0;
589#endif /* HAVE_DEVNAME */
590		if (ofmt == 0)
591			ofmt = FMTF_UNSIGNED;
592		break;
593	case SHOW_st_ino:
594		small = (sizeof(st->st_ino) == 4);
595		data = st->st_ino;
596		sdata = NULL;
597		formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
598		if (ofmt == 0)
599			ofmt = FMTF_UNSIGNED;
600		break;
601	case SHOW_st_mode:
602		small = (sizeof(st->st_mode) == 4);
603		data = st->st_mode;
604		strmode(st->st_mode, smode);
605		sdata = smode;
606		l = strlen(sdata);
607		if (sdata[l - 1] == ' ')
608			sdata[--l] = '\0';
609		if (hilo == HIGH_PIECE) {
610			data >>= 12;
611			sdata += 1;
612			sdata[3] = '\0';
613			hilo = 0;
614		}
615		else if (hilo == MIDDLE_PIECE) {
616			data = (data >> 9) & 07;
617			sdata += 4;
618			sdata[3] = '\0';
619			hilo = 0;
620		}
621		else if (hilo == LOW_PIECE) {
622			data &= 0777;
623			sdata += 7;
624			sdata[3] = '\0';
625			hilo = 0;
626		}
627		formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
628		    FMTF_STRING;
629		if (ofmt == 0)
630			ofmt = FMTF_OCTAL;
631		break;
632	case SHOW_st_nlink:
633		small = (sizeof(st->st_dev) == 4);
634		data = st->st_nlink;
635		sdata = NULL;
636		formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
637		if (ofmt == 0)
638			ofmt = FMTF_UNSIGNED;
639		break;
640	case SHOW_st_uid:
641		small = (sizeof(st->st_uid) == 4);
642		data = st->st_uid;
643		if ((pw = getpwuid(st->st_uid)) != NULL)
644			sdata = pw->pw_name;
645		else {
646			snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_uid);
647			sdata = sid;
648		}
649		formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
650		    FMTF_STRING;
651		if (ofmt == 0)
652			ofmt = FMTF_UNSIGNED;
653		break;
654	case SHOW_st_gid:
655		small = (sizeof(st->st_gid) == 4);
656		data = st->st_gid;
657		if ((gr = getgrgid(st->st_gid)) != NULL)
658			sdata = gr->gr_name;
659		else {
660			snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_gid);
661			sdata = sid;
662		}
663		formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
664		    FMTF_STRING;
665		if (ofmt == 0)
666			ofmt = FMTF_UNSIGNED;
667		break;
668	case SHOW_st_atime:
669		tsp = &st->st_atimespec;
670		/* FALLTHROUGH */
671	case SHOW_st_mtime:
672		if (tsp == NULL)
673			tsp = &st->st_mtimespec;
674		/* FALLTHROUGH */
675	case SHOW_st_ctime:
676		if (tsp == NULL)
677			tsp = &st->st_ctimespec;
678		/* FALLTHROUGH */
679#if HAVE_STRUCT_STAT_ST_BIRTHTIME
680	case SHOW_st_btime:
681		if (tsp == NULL)
682			tsp = &st->st_birthtimespec;
683#endif /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
684		ts = *tsp;		/* copy so we can muck with it */
685		small = (sizeof(ts.tv_sec) == 4);
686		data = ts.tv_sec;
687		small = 1;
688		tm = localtime(&ts.tv_sec);
689		(void)strftime(path, sizeof(path), timefmt, tm);
690		sdata = path;
691		formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
692		    FMTF_FLOAT | FMTF_STRING;
693		if (ofmt == 0)
694			ofmt = FMTF_DECIMAL;
695		break;
696	case SHOW_st_size:
697		small = (sizeof(st->st_size) == 4);
698		data = st->st_size;
699		sdata = NULL;
700		formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
701		if (ofmt == 0)
702			ofmt = FMTF_UNSIGNED;
703		break;
704	case SHOW_st_blocks:
705		small = (sizeof(st->st_blocks) == 4);
706		data = st->st_blocks;
707		sdata = NULL;
708		formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
709		if (ofmt == 0)
710			ofmt = FMTF_UNSIGNED;
711		break;
712	case SHOW_st_blksize:
713		small = (sizeof(st->st_blksize) == 4);
714		data = st->st_blksize;
715		sdata = NULL;
716		formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
717		if (ofmt == 0)
718			ofmt = FMTF_UNSIGNED;
719		break;
720#if HAVE_STRUCT_STAT_ST_FLAGS
721	case SHOW_st_flags:
722		small = (sizeof(st->st_flags) == 4);
723		data = st->st_flags;
724		sdata = NULL;
725		formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
726		if (ofmt == 0)
727			ofmt = FMTF_UNSIGNED;
728		break;
729#endif /* HAVE_STRUCT_STAT_ST_FLAGS */
730#if HAVE_STRUCT_STAT_ST_GEN
731	case SHOW_st_gen:
732		small = (sizeof(st->st_gen) == 4);
733		data = st->st_gen;
734		sdata = NULL;
735		formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
736		if (ofmt == 0)
737			ofmt = FMTF_UNSIGNED;
738		break;
739#endif /* HAVE_STRUCT_STAT_ST_GEN */
740	case SHOW_symlink:
741		small = 0;
742		data = 0;
743		if (S_ISLNK(st->st_mode)) {
744			snprintf(path, sizeof(path), " -> ");
745			l = readlink(file, path + 4, sizeof(path) - 4 - 1);
746			if (l == -1) {
747				linkfail = 1;
748				l = 0;
749				path[0] = '\0';
750			}
751			path[l + 4] = '\0';
752			sdata = path + (ofmt == FMTF_STRING ? 0 : 4);
753		}
754		else {
755			linkfail = 1;
756			sdata = "";
757		}
758		formats = FMTF_STRING;
759		if (ofmt == 0)
760			ofmt = FMTF_STRING;
761		break;
762	case SHOW_filetype:
763		small = 0;
764		data = 0;
765		sdata = smode;
766		sdata[0] = '\0';
767		if (hilo == 0 || hilo == LOW_PIECE) {
768			switch (st->st_mode & S_IFMT) {
769			case S_IFIFO:	(void)strcat(sdata, "|");	break;
770			case S_IFDIR:	(void)strcat(sdata, "/");	break;
771			case S_IFREG:
772				if (st->st_mode &
773				    (S_IXUSR | S_IXGRP | S_IXOTH))
774					(void)strcat(sdata, "*");
775				break;
776			case S_IFLNK:	(void)strcat(sdata, "@");	break;
777			case S_IFSOCK:	(void)strcat(sdata, "=");	break;
778#ifdef S_IFWHT
779			case S_IFWHT:	(void)strcat(sdata, "%");	break;
780#endif /* S_IFWHT */
781#ifdef S_IFDOOR
782			case S_IFDOOR:	(void)strcat(sdata, ">");	break;
783#endif /* S_IFDOOR */
784			}
785			hilo = 0;
786		}
787		else if (hilo == HIGH_PIECE) {
788			switch (st->st_mode & S_IFMT) {
789			case S_IFIFO:	sdata = "Fifo File";		break;
790			case S_IFCHR:	sdata = "Character Device";	break;
791			case S_IFDIR:	sdata = "Directory";		break;
792			case S_IFBLK:	sdata = "Block Device";		break;
793			case S_IFREG:	sdata = "Regular File";		break;
794			case S_IFLNK:	sdata = "Symbolic Link";	break;
795			case S_IFSOCK:	sdata = "Socket";		break;
796#ifdef S_IFWHT
797			case S_IFWHT:	sdata = "Whiteout File";	break;
798#endif /* S_IFWHT */
799#ifdef S_IFDOOR
800			case S_IFDOOR:	sdata = "Door";			break;
801#endif /* S_IFDOOR */
802			default:	sdata = "???";			break;
803			}
804			hilo = 0;
805		}
806		formats = FMTF_STRING;
807		if (ofmt == 0)
808			ofmt = FMTF_STRING;
809		break;
810	case SHOW_filename:
811		small = 0;
812		data = 0;
813		if (file == NULL)
814			(void)strncpy(path, "(stdin)", sizeof(path));
815		else
816			(void)strncpy(path, file, sizeof(path));
817		sdata = path;
818		formats = FMTF_STRING;
819		if (ofmt == 0)
820			ofmt = FMTF_STRING;
821		break;
822	case SHOW_sizerdev:
823		if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
824			char majdev[20], mindev[20];
825			int l1, l2;
826
827			l1 = format1(st,
828			    file,
829			    fmt, flen,
830			    majdev, sizeof(majdev),
831			    flags, size, prec,
832			    ofmt, HIGH_PIECE, SHOW_st_rdev);
833			l2 = format1(st,
834			    file,
835			    fmt, flen,
836			    mindev, sizeof(mindev),
837			    flags, size, prec,
838			    ofmt, LOW_PIECE, SHOW_st_rdev);
839			return (snprintf(buf, blen, "%.*s,%.*s",
840			    l1, majdev, l2, mindev));
841		}
842		else {
843			return (format1(st,
844			    file,
845			    fmt, flen,
846			    buf, blen,
847			    flags, size, prec,
848			    ofmt, 0, SHOW_st_size));
849		}
850		/*NOTREACHED*/
851	default:
852		errx(1, "%.*s: bad format", (int)flen, fmt);
853	}
854
855	/*
856	 * If a subdatum was specified but not supported, or an output
857	 * format was selected that is not supported, that's an error.
858	 */
859	if (hilo != 0 || (ofmt & formats) == 0)
860		errx(1, "%.*s: bad format", (int)flen, fmt);
861
862	/*
863	 * Assemble the format string for passing to printf(3).
864	 */
865	lfmt[0] = '\0';
866	(void)strcat(lfmt, "%");
867	if (flags & FLAG_POUND)
868		(void)strcat(lfmt, "#");
869	if (flags & FLAG_SPACE)
870		(void)strcat(lfmt, " ");
871	if (flags & FLAG_PLUS)
872		(void)strcat(lfmt, "+");
873	if (flags & FLAG_MINUS)
874		(void)strcat(lfmt, "-");
875	if (flags & FLAG_ZERO)
876		(void)strcat(lfmt, "0");
877
878	/*
879	 * Only the timespecs support the FLOAT output format, and that
880	 * requires work that differs from the other formats.
881	 */
882	if (ofmt == FMTF_FLOAT) {
883		/*
884		 * Nothing after the decimal point, so just print seconds.
885		 */
886		if (prec == 0) {
887			if (size != -1) {
888				(void)snprintf(tmp, sizeof(tmp), "%d", size);
889				(void)strcat(lfmt, tmp);
890			}
891			(void)strcat(lfmt, "d");
892			return (snprintf(buf, blen, lfmt, ts.tv_sec));
893		}
894
895		/*
896		 * Unspecified precision gets all the precision we have:
897		 * 9 digits.
898		 */
899		if (prec == -1)
900			prec = 9;
901
902		/*
903		 * Adjust the size for the decimal point and the digits
904		 * that will follow.
905		 */
906		size -= prec + 1;
907
908		/*
909		 * Any leftover size that's legitimate will be used.
910		 */
911		if (size > 0) {
912			(void)snprintf(tmp, sizeof(tmp), "%d", size);
913			(void)strcat(lfmt, tmp);
914		}
915		(void)strcat(lfmt, "d");
916
917		/*
918		 * The stuff after the decimal point always needs zero
919		 * filling.
920		 */
921		(void)strcat(lfmt, ".%0");
922
923		/*
924		 * We can "print" at most nine digits of precision.  The
925		 * rest we will pad on at the end.
926		 */
927		(void)snprintf(tmp, sizeof(tmp), "%dd", prec > 9 ? 9 : prec);
928		(void)strcat(lfmt, tmp);
929
930		/*
931		 * For precision of less that nine digits, trim off the
932		 * less significant figures.
933		 */
934		for (; prec < 9; prec++)
935			ts.tv_nsec /= 10;
936
937		/*
938		 * Use the format, and then tack on any zeroes that
939		 * might be required to make up the requested precision.
940		 */
941		l = snprintf(buf, blen, lfmt, ts.tv_sec, ts.tv_nsec);
942		for (; prec > 9 && l < blen; prec--, l++)
943			(void)strcat(buf, "0");
944		return (l);
945	}
946
947	/*
948	 * Add on size and precision, if specified, to the format.
949	 */
950	if (size != -1) {
951		(void)snprintf(tmp, sizeof(tmp), "%d", size);
952		(void)strcat(lfmt, tmp);
953	}
954	if (prec != -1) {
955		(void)snprintf(tmp, sizeof(tmp), ".%d", prec);
956		(void)strcat(lfmt, tmp);
957	}
958
959	/*
960	 * String output uses the temporary sdata.
961	 */
962	if (ofmt == FMTF_STRING) {
963		if (sdata == NULL)
964			errx(1, "%.*s: bad format", (int)flen, fmt);
965		(void)strcat(lfmt, "s");
966		return (snprintf(buf, blen, lfmt, sdata));
967	}
968
969	/*
970	 * Ensure that sign extension does not cause bad looking output
971	 * for some forms.
972	 */
973	if (small && ofmt != FMTF_DECIMAL)
974		data = (u_int32_t)data;
975
976	/*
977	 * The four "numeric" output forms.
978	 */
979	(void)strcat(lfmt, "ll");
980	switch (ofmt) {
981	case FMTF_DECIMAL:	(void)strcat(lfmt, "d");	break;
982	case FMTF_OCTAL:		(void)strcat(lfmt, "o");	break;
983	case FMTF_UNSIGNED:	(void)strcat(lfmt, "u");	break;
984	case FMTF_HEX:		(void)strcat(lfmt, "x");	break;
985	}
986
987	return (snprintf(buf, blen, lfmt, data));
988}
989