]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/stat/stat.c
This commit was generated by cvs2svn to compensate for changes in r102644,
[FreeBSD/FreeBSD.git] / usr.bin / stat / stat.c
1 /*      $NetBSD: stat.c,v 1.6 2002/07/09 21:25:00 atatat Exp $ */
2
3 /*
4  * Copyright (c) 2002 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Andrew Brown.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *      This product includes software developed by the NetBSD
21  *      Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38
39 #if 0
40 #ifndef lint
41 __RCSID("$NetBSD: stat.c,v 1.8 2002/08/13 20:15:06 atatat Exp $");
42 #endif
43 #endif
44
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
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\" %k %b %N"
64 #define RAW_FORMAT      "%d %i %#p %l %u %g %r %z %a %m %c %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_atimespec=%a st_mtimespec=%m st_ctimespec=%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                 ts = *tsp;              /* copy so we can muck with it */
635                 small = (sizeof(ts.tv_sec) == 4);
636                 data = ts.tv_sec;
637                 small = 1;
638                 tm = localtime(&ts.tv_sec);
639                 (void)strftime(path, sizeof(path), timefmt, tm);
640                 sdata = path;
641                 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
642                     FMTF_FLOAT | FMTF_STRING;
643                 if (ofmt == 0)
644                         ofmt = FMTF_DECIMAL;
645                 break;
646         case SHOW_st_size:
647                 small = (sizeof(st->st_size) == 4);
648                 data = st->st_size;
649                 sdata = NULL;
650                 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
651                 if (ofmt == 0)
652                         ofmt = FMTF_UNSIGNED;
653                 break;
654         case SHOW_st_blocks:
655                 small = (sizeof(st->st_blocks) == 4);
656                 data = st->st_blocks;
657                 sdata = NULL;
658                 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
659                 if (ofmt == 0)
660                         ofmt = FMTF_UNSIGNED;
661                 break;
662         case SHOW_st_blksize:
663                 small = (sizeof(st->st_blksize) == 4);
664                 data = st->st_blksize;
665                 sdata = NULL;
666                 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
667                 if (ofmt == 0)
668                         ofmt = FMTF_UNSIGNED;
669                 break;
670         case SHOW_st_flags:
671                 small = (sizeof(st->st_flags) == 4);
672                 data = st->st_flags;
673                 sdata = NULL;
674                 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
675                 if (ofmt == 0)
676                         ofmt = FMTF_UNSIGNED;
677                 break;
678         case SHOW_st_gen:
679                 small = (sizeof(st->st_gen) == 4);
680                 data = st->st_gen;
681                 sdata = NULL;
682                 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
683                 if (ofmt == 0)
684                         ofmt = FMTF_UNSIGNED;
685                 break;
686         case SHOW_symlink:
687                 small = 0;
688                 data = 0;
689                 if (S_ISLNK(st->st_mode)) {
690                         snprintf(path, sizeof(path), " -> ");
691                         l = readlink(file, path + 4, sizeof(path) - 4);
692                         if (l == -1) {
693                                 linkfail = 1;
694                                 l = 0;
695                                 path[0] = '\0';
696                         }
697                         path[l + 4] = '\0';
698                         sdata = path + (ofmt == FMTF_STRING ? 0 : 4);
699                 }
700                 else {
701                         linkfail = 1;
702                         sdata = "";
703                 }
704                 formats = FMTF_STRING;
705                 if (ofmt == 0)
706                         ofmt = FMTF_STRING;
707                 break;
708         case SHOW_filetype:
709                 small = 0;
710                 data = 0;
711                 sdata = smode;
712                 sdata[0] = '\0';
713                 if (hilo == 0 || hilo == LOW_PIECE) {
714                         switch (st->st_mode & S_IFMT) {
715                         case S_IFIFO:   (void)strcat(sdata, "|");       break;
716                         case S_IFDIR:   (void)strcat(sdata, "/");       break;
717                         case S_IFREG:
718                                 if (st->st_mode &
719                                     (S_IXUSR | S_IXGRP | S_IXOTH))
720                                         (void)strcat(sdata, "*");
721                                 break;
722                         case S_IFLNK:   (void)strcat(sdata, "@");       break;
723                         case S_IFSOCK:  (void)strcat(sdata, "=");       break;
724                         case S_IFWHT:   (void)strcat(sdata, "%");       break;
725                         }
726                         hilo = 0;
727                 }
728                 else if (hilo == HIGH_PIECE) {
729                         switch (st->st_mode & S_IFMT) {
730                         case S_IFIFO:   sdata = "Fifo File";            break;
731                         case S_IFCHR:   sdata = "Character Device";     break;
732                         case S_IFDIR:   sdata = "Directory";            break;
733                         case S_IFBLK:   sdata = "Block Device";         break;
734                         case S_IFREG:   sdata = "Regular File";         break;
735                         case S_IFLNK:   sdata = "Symbolic Link";        break;
736                         case S_IFSOCK:  sdata = "Socket";               break;
737                         case S_IFWHT:   sdata = "Whiteout File";        break;
738                         default:        sdata = "???";                  break;
739                         }
740                         hilo = 0;
741                 }
742                 formats = FMTF_STRING;
743                 if (ofmt == 0)
744                         ofmt = FMTF_STRING;
745                 break;
746         case SHOW_filename:
747                 small = 0;
748                 data = 0;
749                 if (file == NULL)
750                         (void)strncpy(path, "(stdin)", sizeof(path));
751                 else
752                         (void)strncpy(path, file, sizeof(path));
753                 sdata = path;
754                 formats = FMTF_STRING;
755                 if (ofmt == 0)
756                         ofmt = FMTF_STRING;
757                 break;
758         case SHOW_sizerdev:
759                 if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
760                         char majdev[20], mindev[20];
761                         int l1, l2;
762
763                         l1 = format1(st,
764                             file,
765                             fmt, flen,
766                             majdev, sizeof(majdev),
767                             flags, size, prec,
768                             ofmt, HIGH_PIECE, SHOW_st_rdev);
769                         l2 = format1(st,
770                             file,
771                             fmt, flen,
772                             mindev, sizeof(mindev),
773                             flags, size, prec,
774                             ofmt, LOW_PIECE, SHOW_st_rdev);
775                         return (snprintf(buf, blen, "%.*s,%.*s",
776                             l1, majdev, l2, mindev));
777                 }
778                 else {
779                         return (format1(st,
780                             file,
781                             fmt, flen,
782                             buf, blen,
783                             flags, size, prec,
784                             ofmt, 0, SHOW_st_size));
785                 }
786                 /*NOTREACHED*/
787         default:
788                 errx(1, "%.*s: bad format", (int)flen, fmt);
789         }
790
791         /*
792          * If a subdatum was specified but not supported, or an output
793          * format was selected that is not supported, that's an error.
794          */
795         if (hilo != 0 || (ofmt & formats) == 0)
796                 errx(1, "%.*s: bad format", (int)flen, fmt);
797
798         /*
799          * Assemble the format string for passing to printf(3).
800          */
801         lfmt[0] = '\0';
802         (void)strcat(lfmt, "%");
803         if (flags & FLAG_POUND)
804                 (void)strcat(lfmt, "#");
805         if (flags & FLAG_SPACE)
806                 (void)strcat(lfmt, " ");
807         if (flags & FLAG_PLUS)
808                 (void)strcat(lfmt, "+");
809         if (flags & FLAG_MINUS)
810                 (void)strcat(lfmt, "-");
811         if (flags & FLAG_ZERO)
812                 (void)strcat(lfmt, "0");
813
814         /*
815          * Only the timespecs support the FLOAT output format, and that
816          * requires work that differs from the other formats.
817          */ 
818         if (ofmt == FMTF_FLOAT) {
819                 /*
820                  * Nothing after the decimal point, so just print seconds.
821                  */
822                 if (prec == 0) {
823                         if (size != -1) {
824                                 (void)snprintf(tmp, sizeof(tmp), "%d", size);
825                                 (void)strcat(lfmt, tmp);
826                         }
827                         (void)strcat(lfmt, "d");
828                         return (snprintf(buf, blen, lfmt, ts.tv_sec));
829                 }
830
831                 /*
832                  * Unspecified precision gets all the precision we have:
833                  * 9 digits.
834                  */
835                 if (prec == -1)
836                         prec = 9;
837
838                 /*
839                  * Adjust the size for the decimal point and the digits
840                  * that will follow.
841                  */
842                 size -= prec + 1;
843
844                 /*
845                  * Any leftover size that's legitimate will be used.
846                  */
847                 if (size > 0) {
848                         (void)snprintf(tmp, sizeof(tmp), "%d", size);
849                         (void)strcat(lfmt, tmp);
850                 }
851                 (void)strcat(lfmt, "d");
852
853                 /*
854                  * The stuff after the decimal point always needs zero
855                  * filling.
856                  */
857                 (void)strcat(lfmt, ".%0");
858
859                 /*
860                  * We can "print" at most nine digits of precision.  The
861                  * rest we will pad on at the end.
862                  */
863                 (void)snprintf(tmp, sizeof(tmp), "%dd", prec > 9 ? 9 : prec);
864                 (void)strcat(lfmt, tmp);
865
866                 /*
867                  * For precision of less that nine digits, trim off the
868                  * less significant figures.
869                  */
870                 for (; prec < 9; prec++)
871                         ts.tv_nsec /= 10;
872
873                 /*
874                  * Use the format, and then tack on any zeroes that
875                  * might be required to make up the requested precision.
876                  */
877                 l = snprintf(buf, blen, lfmt, ts.tv_sec, ts.tv_nsec);
878                 for (; prec > 9 && l < blen; prec--, l++)
879                         (void)strcat(buf, "0");
880                 return (l);
881         }
882
883         /*
884          * Add on size and precision, if specified, to the format.
885          */
886         if (size != -1) {
887                 (void)snprintf(tmp, sizeof(tmp), "%d", size);
888                 (void)strcat(lfmt, tmp);
889         }
890         if (prec != -1) {
891                 (void)snprintf(tmp, sizeof(tmp), ".%d", prec);
892                 (void)strcat(lfmt, tmp);
893         }
894
895         /*
896          * String output uses the temporary sdata.
897          */
898         if (ofmt == FMTF_STRING) {
899                 if (sdata == NULL)
900                         errx(1, "%.*s: bad format", (int)flen, fmt);
901                 (void)strcat(lfmt, "s");
902                 return (snprintf(buf, blen, lfmt, sdata));
903         }
904
905         /*
906          * Ensure that sign extension does not cause bad looking output
907          * for some forms.
908          */
909         if (small && ofmt != FMTF_DECIMAL)
910                 data = (u_int32_t)data;
911
912         /*
913          * The four "numeric" output forms.
914          */
915         (void)strcat(lfmt, "ll");
916         switch (ofmt) {
917         case FMTF_DECIMAL:      (void)strcat(lfmt, "d");        break;
918         case FMTF_OCTAL:                (void)strcat(lfmt, "o");        break;
919         case FMTF_UNSIGNED:     (void)strcat(lfmt, "u");        break;
920         case FMTF_HEX:          (void)strcat(lfmt, "x");        break;
921         }
922
923         return (snprintf(buf, blen, lfmt, data));
924 }