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