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