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