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