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