inet6: require RTF_ANNOUNCE to proxy NS
[dragonfly.git] / usr.bin / stat / stat.c
blobfd1f711fa905eb3124aa67dbcbf8ea65277ef42b
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 #include <sys/param.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <sys/mount.h>
39 #include <ctype.h>
40 #include <err.h>
41 #include <errno.h>
42 #include <grp.h>
43 #include <limits.h>
44 #include <paths.h>
45 #include <pwd.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <time.h>
50 #include <unistd.h>
52 #ifdef BOOTSTRAPPING
53 #define HAVE_DEVNAME 0
54 #define HAVE_STRUCT_STAT_ST_FLAGS 0
55 #define HAVE_STRUCT_STAT_ST_GEN 0
56 #define HAVE_STRUCT_STAT_ST_BIRTHTIME 0
57 #define HAVE_STRUCT_STAT_ST_ATIM 1
58 #else
59 #define HAVE_DEVNAME 1
60 #define HAVE_STRUCT_STAT_ST_FLAGS 1
61 #define HAVE_STRUCT_STAT_ST_GEN 1
62 #define HAVE_STRUCT_STAT_ST_BIRTHTIME 0
63 #define HAVE_STRUCT_STAT_ST_ATIM 1
64 #endif
66 #if HAVE_STRUCT_STAT_ST_FLAGS
67 #define DEF_F "%#Xf "
68 #define RAW_F "%f "
69 #define SHELL_F " st_flags=%f"
70 #else /* HAVE_STRUCT_STAT_ST_FLAGS */
71 #define DEF_F
72 #define RAW_F
73 #define SHELL_F
74 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
76 #if HAVE_STRUCT_STAT_ST_BIRTHTIME
77 #define DEF_B "\"%SB\" "
78 #define RAW_B "%B "
79 #define SHELL_B "st_birthtime=%B "
80 #else /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
81 #define DEF_B
82 #define RAW_B
83 #define SHELL_B
84 #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
86 #if HAVE_STRUCT_STAT_ST_ATIM
87 #define st_atimespec st_atim
88 #define st_ctimespec st_ctim
89 #define st_mtimespec st_mtim
90 #endif /* HAVE_STRUCT_STAT_ST_ATIM */
92 #define DEF_FORMAT \
93 "%d %i %Sp %l %Su %Sg %r %z \"%Sa\" \"%Sm\" \"%Sc\" " DEF_B \
94 "%k %b " DEF_F "%N"
95 #define RAW_FORMAT "%d %i %#p %l %u %g %r %z %a %m %c " RAW_B \
96 "%k %b " RAW_F "%N"
97 #define LS_FORMAT "%Sp %l %Su %Sg %Z %Sm %N%SY"
98 #define LSF_FORMAT "%Sp %l %Su %Sg %Z %Sm %N%T%SY"
99 #define SHELL_FORMAT \
100 "st_dev=%d st_ino=%i st_mode=%#p st_nlink=%l " \
101 "st_uid=%u st_gid=%g st_rdev=%r st_size=%z " \
102 "st_atime=%a st_mtime=%m st_ctime=%c " SHELL_B \
103 "st_blksize=%k st_blocks=%b" SHELL_F
104 #define LINUX_FORMAT \
105 " File: \"%N\"%n" \
106 " Size: %-11z FileType: %HT%n" \
107 " Mode: (%OMp%03OLp/%.10Sp) Uid: (%5u/%8Su) Gid: (%5g/%8Sg)%n" \
108 "Device: %Hd,%Ld Inode: %i Links: %l%n" \
109 "Access: %Sa%n" \
110 "Modify: %Sm%n" \
111 "Change: %Sc"
113 #define TIME_FORMAT "%b %e %T %Y"
115 #define FLAG_POUND 0x01
116 #define FLAG_SPACE 0x02
117 #define FLAG_PLUS 0x04
118 #define FLAG_ZERO 0x08
119 #define FLAG_MINUS 0x10
122 * These format characters must all be unique, except the magic one.
124 #define FMT_MAGIC '%'
125 #define FMT_DOT '.'
127 #define SIMPLE_NEWLINE 'n'
128 #define SIMPLE_TAB 't'
129 #define SIMPLE_PERCENT '%'
130 #define SIMPLE_NUMBER '@'
132 #define FMT_POUND '#'
133 #define FMT_SPACE ' '
134 #define FMT_PLUS '+'
135 #define FMT_ZERO '0'
136 #define FMT_MINUS '-'
138 #define FMT_DECIMAL 'D'
139 #define FMT_OCTAL 'O'
140 #define FMT_UNSIGNED 'U'
141 #define FMT_HEX 'X'
142 #define FMT_FLOAT 'F'
143 #define FMT_STRING 'S'
145 #define FMTF_DECIMAL 0x01
146 #define FMTF_OCTAL 0x02
147 #define FMTF_UNSIGNED 0x04
148 #define FMTF_HEX 0x08
149 #define FMTF_FLOAT 0x10
150 #define FMTF_STRING 0x20
152 #define HIGH_PIECE 'H'
153 #define MIDDLE_PIECE 'M'
154 #define LOW_PIECE 'L'
156 #define SHOW_realpath 'R'
157 #define SHOW_st_dev 'd'
158 #define SHOW_st_ino 'i'
159 #define SHOW_st_mode 'p'
160 #define SHOW_st_nlink 'l'
161 #define SHOW_st_uid 'u'
162 #define SHOW_st_gid 'g'
163 #define SHOW_st_rdev 'r'
164 #define SHOW_st_atime 'a'
165 #define SHOW_st_mtime 'm'
166 #define SHOW_st_ctime 'c'
167 #define SHOW_st_btime 'B'
168 #define SHOW_st_size 'z'
169 #define SHOW_st_blocks 'b'
170 #define SHOW_st_blksize 'k'
171 #define SHOW_st_flags 'f'
172 #define SHOW_st_gen 'v'
173 #define SHOW_symlink 'Y'
174 #define SHOW_filetype 'T'
175 #define SHOW_filename 'N'
176 #define SHOW_sizerdev 'Z'
178 static void usage(const char *) __dead2;
179 static void output(const struct stat *, const char *,
180 const char *, int, int);
181 static int format1(const struct stat *, /* stat info */
182 const char *, /* the file name */
183 const char *, int, /* the format string itself */
184 char *, size_t, /* a place to put the output */
185 int, int, int, int, /* the parsed format */
186 int, int);
187 #if HAVE_DEVNAME
188 static int hex2byte(const char [2]);
189 #endif
190 #if HAVE_STRUCT_STAT_ST_FLAGS
191 static char *xfflagstostr(unsigned long);
192 #endif
194 static const char *timefmt;
195 static int linkfail;
197 #define addchar(s, c, nl) \
198 do { \
199 fputc((c), (s)); \
200 (*nl) = ((c) == '\n'); \
201 } while (0/*CONSTCOND*/)
204 main(int argc, char *argv[])
206 struct stat st;
207 int ch, rc, errs, am_readlink;
208 int lsF, fmtchar, usestat, nfs_handle, fn, nonl, quiet;
209 const char *statfmt, *options, *synopsis;
210 #if HAVE_DEVNAME
211 char dname[sizeof _PATH_DEV + MNAMELEN] = _PATH_DEV;
212 fhandle_t fhnd;
213 #endif
214 const char *file;
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;
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 ...]";
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;
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);
280 argc -= optind;
281 argv += optind;
282 fn = 1;
284 if (fmtchar == '\0') {
285 if (lsF)
286 fmtchar = 'l';
287 else {
288 fmtchar = 'f';
289 statfmt = DEF_FORMAT;
293 if (lsF && fmtchar != 'l')
294 errx(1, "can't use format '%c' with -F", fmtchar);
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*/
319 if (timefmt == NULL)
320 timefmt = TIME_FORMAT;
322 errs = 0;
323 do {
324 if (argc == 0) {
325 #if HAVE_DEVNAME
326 if (fdevname_r(STDIN_FILENO, dname +
327 sizeof _PATH_DEV - 1, MNAMELEN) == 0)
328 file = dname;
329 else
330 #endif
331 file = "(stdin)";
332 rc = fstat(STDIN_FILENO, &st);
333 } else {
334 file = argv[0];
335 if (nfs_handle) {
336 #if HAVE_DEVNAME
337 int j;
339 rc = 0;
340 bzero(&fhnd, sizeof(fhnd));
341 j = MIN(2 * sizeof(fhnd), strlen(file));
342 if ((j & 1) != 0) {
343 rc = -1;
344 } else {
345 while (j) {
346 rc = hex2byte(&file[j - 2]);
347 if (rc == -1)
348 break;
349 ((char*) &fhnd)[j / 2 - 1] = rc;
350 j -= 2;
353 if (rc == -1)
354 errno = EINVAL;
355 else
356 rc = fhstat(&fhnd, &st);
357 #else
358 err(1, "-H is disabled in btools");
359 #endif
360 } else if (usestat) {
362 * Try stat() and if it fails, fall back to
363 * lstat() just in case we're examining a
364 * broken symlink.
366 if ((rc = stat(file, &st)) == -1 &&
367 errno == ENOENT &&
368 (rc = lstat(file, &st)) == -1)
369 errno = ENOENT;
371 else
372 rc = lstat(file, &st);
375 if (rc == -1) {
376 errs = 1;
377 linkfail = 1;
378 if (!quiet)
379 warn("%s: stat", file);
381 else
382 output(&st, file, statfmt, fn, nonl);
384 argv++;
385 argc--;
386 fn++;
387 } while (argc > 0);
389 return (am_readlink ? linkfail : errs);
392 #if HAVE_STRUCT_STAT_ST_FLAGS
394 * fflagstostr() wrapper that leaks only once
396 static char *
397 xfflagstostr(unsigned long fflags)
399 static char *str = NULL;
401 if (str != NULL)
402 free(str);
404 str = fflagstostr(fflags);
405 if (str == NULL)
406 err(1, "fflagstostr");
407 return (str);
409 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
411 static void
412 usage(const char *synopsis)
415 fprintf(stderr, "usage: %s %s\n", getprogname(), synopsis);
416 exit(1);
420 * Parses a format string.
422 static void
423 output(const struct stat *st, const char *file,
424 const char *statfmt, int fn, int nonl)
426 int flags, size, prec, ofmt, hilo, what;
427 char buf[PATH_MAX + 4 + 1];
428 const char *subfmt;
429 int nl, t, i;
431 nl = 1;
432 while (*statfmt != '\0') {
435 * Non-format characters go straight out.
437 if (*statfmt != FMT_MAGIC) {
438 addchar(stdout, *statfmt, &nl);
439 statfmt++;
440 continue;
444 * The current format "substring" starts here,
445 * and then we skip the magic.
447 subfmt = statfmt;
448 statfmt++;
451 * Some simple one-character "formats".
453 switch (*statfmt) {
454 case SIMPLE_NEWLINE:
455 addchar(stdout, '\n', &nl);
456 statfmt++;
457 continue;
458 case SIMPLE_TAB:
459 addchar(stdout, '\t', &nl);
460 statfmt++;
461 continue;
462 case SIMPLE_PERCENT:
463 addchar(stdout, '%', &nl);
464 statfmt++;
465 continue;
466 case SIMPLE_NUMBER: {
467 char num[12], *p;
469 snprintf(num, sizeof(num), "%d", fn);
470 for (p = &num[0]; *p; p++)
471 addchar(stdout, *p, &nl);
472 statfmt++;
473 continue;
478 * This must be an actual format string. Format strings are
479 * similar to printf(3) formats up to a point, and are of
480 * the form:
482 * % required start of format
483 * [-# +0] opt. format characters
484 * size opt. field width
485 * . opt. decimal separator, followed by
486 * prec opt. precision
487 * fmt opt. output specifier (string, numeric, etc.)
488 * sub opt. sub field specifier (high, middle, low)
489 * datum required field specifier (size, mode, etc)
491 * Only the % and the datum selector are required. All data
492 * have reasonable default output forms. The "sub" specifier
493 * only applies to certain data (mode, dev, rdev, filetype).
494 * The symlink output defaults to STRING, yet will only emit
495 * the leading " -> " if STRING is explicitly specified. The
496 * sizerdev datum will generate rdev output for character or
497 * block devices, and size output for all others.
499 flags = 0;
500 do {
501 if (*statfmt == FMT_POUND)
502 flags |= FLAG_POUND;
503 else if (*statfmt == FMT_SPACE)
504 flags |= FLAG_SPACE;
505 else if (*statfmt == FMT_PLUS)
506 flags |= FLAG_PLUS;
507 else if (*statfmt == FMT_ZERO)
508 flags |= FLAG_ZERO;
509 else if (*statfmt == FMT_MINUS)
510 flags |= FLAG_MINUS;
511 else
512 break;
513 statfmt++;
514 } while (1/*CONSTCOND*/);
516 size = -1;
517 if (isdigit((unsigned)*statfmt)) {
518 size = 0;
519 while (isdigit((unsigned)*statfmt)) {
520 size = (size * 10) + (*statfmt - '0');
521 statfmt++;
522 if (size < 0)
523 goto badfmt;
527 prec = -1;
528 if (*statfmt == FMT_DOT) {
529 statfmt++;
531 prec = 0;
532 while (isdigit((unsigned)*statfmt)) {
533 prec = (prec * 10) + (*statfmt - '0');
534 statfmt++;
535 if (prec < 0)
536 goto badfmt;
540 #define fmtcase(x, y) case (y): (x) = (y); statfmt++; break
541 #define fmtcasef(x, y, z) case (y): (x) = (z); statfmt++; break
542 switch (*statfmt) {
543 fmtcasef(ofmt, FMT_DECIMAL, FMTF_DECIMAL);
544 fmtcasef(ofmt, FMT_OCTAL, FMTF_OCTAL);
545 fmtcasef(ofmt, FMT_UNSIGNED, FMTF_UNSIGNED);
546 fmtcasef(ofmt, FMT_HEX, FMTF_HEX);
547 fmtcasef(ofmt, FMT_FLOAT, FMTF_FLOAT);
548 fmtcasef(ofmt, FMT_STRING, FMTF_STRING);
549 default:
550 ofmt = 0;
551 break;
554 switch (*statfmt) {
555 fmtcase(hilo, HIGH_PIECE);
556 fmtcase(hilo, MIDDLE_PIECE);
557 fmtcase(hilo, LOW_PIECE);
558 default:
559 hilo = 0;
560 break;
563 switch (*statfmt) {
564 fmtcase(what, SHOW_realpath);
565 fmtcase(what, SHOW_st_dev);
566 fmtcase(what, SHOW_st_ino);
567 fmtcase(what, SHOW_st_mode);
568 fmtcase(what, SHOW_st_nlink);
569 fmtcase(what, SHOW_st_uid);
570 fmtcase(what, SHOW_st_gid);
571 fmtcase(what, SHOW_st_rdev);
572 fmtcase(what, SHOW_st_atime);
573 fmtcase(what, SHOW_st_mtime);
574 fmtcase(what, SHOW_st_ctime);
575 fmtcase(what, SHOW_st_btime);
576 fmtcase(what, SHOW_st_size);
577 fmtcase(what, SHOW_st_blocks);
578 fmtcase(what, SHOW_st_blksize);
579 fmtcase(what, SHOW_st_flags);
580 fmtcase(what, SHOW_st_gen);
581 fmtcase(what, SHOW_symlink);
582 fmtcase(what, SHOW_filetype);
583 fmtcase(what, SHOW_filename);
584 fmtcase(what, SHOW_sizerdev);
585 default:
586 goto badfmt;
588 #undef fmtcasef
589 #undef fmtcase
591 t = format1(st,
592 file,
593 subfmt, statfmt - subfmt,
594 buf, sizeof(buf),
595 flags, size, prec, ofmt, hilo, what);
597 for (i = 0; i < t && i < (int)(sizeof(buf) - 1); i++)
598 addchar(stdout, buf[i], &nl);
600 continue;
602 badfmt:
603 errx(1, "%.*s: bad format",
604 (int)(statfmt - subfmt + 1), subfmt);
607 if (!nl && !nonl)
608 fputc('\n', stdout);
609 fflush(stdout);
613 * Arranges output according to a single parsed format substring.
615 static int
616 format1(const struct stat *st,
617 const char *file,
618 const char *fmt, int flen,
619 char *buf, size_t blen,
620 int flags, int size, int prec, int ofmt,
621 int hilo, int what)
623 u_int64_t data;
624 char *stmp, lfmt[24], tmp[20];
625 const char *sdata;
626 char smode[12], sid[12], path[PATH_MAX + 4];
627 struct passwd *pw;
628 struct group *gr;
629 const struct timespec *tsp;
630 struct timespec ts;
631 struct tm *tm;
632 int l, small, formats;
634 tsp = NULL;
635 formats = 0;
636 small = 0;
639 * First, pick out the data and tweak it based on hilo or
640 * specified output format (symlink output only).
642 switch (what) {
643 case SHOW_st_dev:
644 case SHOW_st_rdev:
645 small = (sizeof(st->st_dev) == 4);
646 data = (what == SHOW_st_dev) ? st->st_dev : st->st_rdev;
647 #if HAVE_DEVNAME
648 sdata = (what == SHOW_st_dev) ?
649 devname(st->st_dev, S_IFBLK) :
650 devname(st->st_rdev,
651 S_ISCHR(st->st_mode) ? S_IFCHR :
652 S_ISBLK(st->st_mode) ? S_IFBLK :
653 0U);
654 #else
655 sdata = NULL;
656 #endif /* HAVE_DEVNAME */
657 if (sdata == NULL)
658 sdata = "???";
659 if (hilo == HIGH_PIECE) {
660 data = major(data);
661 hilo = 0;
663 else if (hilo == LOW_PIECE) {
664 data = minor((unsigned)data);
665 hilo = 0;
667 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
668 #if HAVE_DEVNAME
669 FMTF_STRING;
670 #else /* HAVE_DEVNAME */
672 #endif /* HAVE_DEVNAME */
673 if (ofmt == 0)
674 ofmt = FMTF_UNSIGNED;
675 break;
676 case SHOW_st_ino:
677 small = (sizeof(st->st_ino) == 4);
678 data = st->st_ino;
679 sdata = NULL;
680 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
681 if (ofmt == 0)
682 ofmt = FMTF_UNSIGNED;
683 break;
684 case SHOW_st_mode:
685 small = (sizeof(st->st_mode) == 4);
686 data = st->st_mode;
687 strmode(st->st_mode, smode);
688 stmp = smode;
689 l = strlen(stmp);
690 if (stmp[l - 1] == ' ')
691 stmp[--l] = '\0';
692 if (hilo == HIGH_PIECE) {
693 data >>= 12;
694 stmp += 1;
695 stmp[3] = '\0';
696 hilo = 0;
698 else if (hilo == MIDDLE_PIECE) {
699 data = (data >> 9) & 07;
700 stmp += 4;
701 stmp[3] = '\0';
702 hilo = 0;
704 else if (hilo == LOW_PIECE) {
705 data &= 0777;
706 stmp += 7;
707 stmp[3] = '\0';
708 hilo = 0;
710 sdata = stmp;
711 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
712 FMTF_STRING;
713 if (ofmt == 0)
714 ofmt = FMTF_OCTAL;
715 break;
716 case SHOW_st_nlink:
717 small = (sizeof(st->st_dev) == 4);
718 data = st->st_nlink;
719 sdata = NULL;
720 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
721 if (ofmt == 0)
722 ofmt = FMTF_UNSIGNED;
723 break;
724 case SHOW_st_uid:
725 small = (sizeof(st->st_uid) == 4);
726 data = st->st_uid;
727 if ((pw = getpwuid(st->st_uid)) != NULL)
728 sdata = pw->pw_name;
729 else {
730 snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_uid);
731 sdata = sid;
733 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
734 FMTF_STRING;
735 if (ofmt == 0)
736 ofmt = FMTF_UNSIGNED;
737 break;
738 case SHOW_st_gid:
739 small = (sizeof(st->st_gid) == 4);
740 data = st->st_gid;
741 if ((gr = getgrgid(st->st_gid)) != NULL)
742 sdata = gr->gr_name;
743 else {
744 snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_gid);
745 sdata = sid;
747 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
748 FMTF_STRING;
749 if (ofmt == 0)
750 ofmt = FMTF_UNSIGNED;
751 break;
752 case SHOW_st_atime:
753 tsp = &st->st_atimespec;
754 /* FALLTHROUGH */
755 case SHOW_st_mtime:
756 if (tsp == NULL)
757 tsp = &st->st_mtimespec;
758 /* FALLTHROUGH */
759 case SHOW_st_ctime:
760 if (tsp == NULL)
761 tsp = &st->st_ctimespec;
762 /* FALLTHROUGH */
763 #if HAVE_STRUCT_STAT_ST_BIRTHTIME
764 case SHOW_st_btime:
765 if (tsp == NULL)
766 tsp = &st->st_birthtimespec;
767 #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
768 ts = *tsp; /* copy so we can muck with it */
769 small = (sizeof(ts.tv_sec) == 4);
770 data = ts.tv_sec;
771 tm = localtime(&ts.tv_sec);
772 if (tm == NULL) {
773 ts.tv_sec = 0;
774 tm = localtime(&ts.tv_sec);
776 strftime(path, sizeof(path), timefmt, tm);
777 sdata = path;
778 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
779 FMTF_FLOAT | FMTF_STRING;
780 if (ofmt == 0)
781 ofmt = FMTF_DECIMAL;
782 break;
783 case SHOW_st_size:
784 small = (sizeof(st->st_size) == 4);
785 data = st->st_size;
786 sdata = NULL;
787 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
788 if (ofmt == 0)
789 ofmt = FMTF_UNSIGNED;
790 break;
791 case SHOW_st_blocks:
792 small = (sizeof(st->st_blocks) == 4);
793 data = st->st_blocks;
794 sdata = NULL;
795 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
796 if (ofmt == 0)
797 ofmt = FMTF_UNSIGNED;
798 break;
799 case SHOW_st_blksize:
800 small = (sizeof(st->st_blksize) == 4);
801 data = st->st_blksize;
802 sdata = NULL;
803 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
804 if (ofmt == 0)
805 ofmt = FMTF_UNSIGNED;
806 break;
807 #if HAVE_STRUCT_STAT_ST_FLAGS
808 case SHOW_st_flags:
809 small = (sizeof(st->st_flags) == 4);
810 data = st->st_flags;
811 sdata = xfflagstostr(st->st_flags);
812 if (*sdata == '\0')
813 sdata = "-";
814 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
815 FMTF_STRING;
816 if (ofmt == 0)
817 ofmt = FMTF_UNSIGNED;
818 break;
819 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
820 #if HAVE_STRUCT_STAT_ST_GEN
821 case SHOW_st_gen:
822 small = (sizeof(st->st_gen) == 4);
823 data = st->st_gen;
824 sdata = NULL;
825 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
826 if (ofmt == 0)
827 ofmt = FMTF_UNSIGNED;
828 break;
829 #endif /* HAVE_STRUCT_STAT_ST_GEN */
830 case SHOW_realpath:
831 small = 0;
832 data = 0;
833 if (file == NULL) {
834 strlcpy(path, "(stdin)", sizeof(path));
835 sdata = path;
836 } else {
837 snprintf(path, sizeof(path), " -> ");
838 if (realpath(file, path + 4) == NULL) {
839 linkfail = 1;
840 l = 0;
841 path[0] = '\0';
843 sdata = path + (ofmt == FMTF_STRING ? 0 : 4);
846 formats = FMTF_STRING;
847 if (ofmt == 0)
848 ofmt = FMTF_STRING;
849 break;
850 case SHOW_symlink:
851 small = 0;
852 data = 0;
853 if (S_ISLNK(st->st_mode)) {
854 snprintf(path, sizeof(path), " -> ");
855 l = readlink(file, path + 4, sizeof(path) - 4 - 1);
856 if (l == -1) {
857 linkfail = 1;
858 l = 0;
859 path[0] = '\0';
861 path[l + 4] = '\0';
862 sdata = path + (ofmt == FMTF_STRING ? 0 : 4);
864 else {
865 linkfail = 1;
866 sdata = "";
868 formats = FMTF_STRING;
869 if (ofmt == 0)
870 ofmt = FMTF_STRING;
871 break;
872 case SHOW_filetype:
873 small = 0;
874 data = 0;
875 sdata = "";
876 if (hilo == 0 || hilo == LOW_PIECE) {
877 switch (st->st_mode & S_IFMT) {
878 case S_IFIFO: sdata = "|"; break;
879 case S_IFDIR: sdata = "/"; break;
880 case S_IFREG:
881 if (st->st_mode &
882 (S_IXUSR | S_IXGRP | S_IXOTH))
883 sdata = "*";
884 break;
885 case S_IFLNK: sdata = "@"; break;
886 case S_IFSOCK: sdata = "="; break;
887 #ifdef S_IFWHT
888 case S_IFWHT: sdata = "%"; break;
889 #endif /* S_IFWHT */
890 #ifdef S_IFDOOR
891 case S_IFDOOR: sdata = ">"; break;
892 #endif /* S_IFDOOR */
894 hilo = 0;
896 else if (hilo == HIGH_PIECE) {
897 switch (st->st_mode & S_IFMT) {
898 case S_IFIFO: sdata = "Fifo File"; break;
899 case S_IFCHR: sdata = "Character Device"; break;
900 case S_IFDIR: sdata = "Directory"; break;
901 case S_IFBLK: sdata = "Block Device"; break;
902 case S_IFREG: sdata = "Regular File"; break;
903 case S_IFLNK: sdata = "Symbolic Link"; break;
904 case S_IFSOCK: sdata = "Socket"; break;
905 #ifdef S_IFWHT
906 case S_IFWHT: sdata = "Whiteout File"; break;
907 #endif /* S_IFWHT */
908 #ifdef S_IFDOOR
909 case S_IFDOOR: sdata = "Door"; break;
910 #endif /* S_IFDOOR */
911 default: sdata = "???"; break;
913 hilo = 0;
915 formats = FMTF_STRING;
916 if (ofmt == 0)
917 ofmt = FMTF_STRING;
918 break;
919 case SHOW_filename:
920 small = 0;
921 data = 0;
922 strlcpy(path, file, sizeof(path));
923 sdata = path;
924 formats = FMTF_STRING;
925 if (ofmt == 0)
926 ofmt = FMTF_STRING;
927 break;
928 case SHOW_sizerdev:
929 if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
930 char majdev[20], mindev[20];
931 int l1, l2;
933 l1 = format1(st,
934 file,
935 fmt, flen,
936 majdev, sizeof(majdev),
937 flags, size, prec,
938 ofmt, HIGH_PIECE, SHOW_st_rdev);
939 l2 = format1(st,
940 file,
941 fmt, flen,
942 mindev, sizeof(mindev),
943 flags, size, prec,
944 ofmt, LOW_PIECE, SHOW_st_rdev);
945 return (snprintf(buf, blen, "%.*s,%.*s",
946 l1, majdev, l2, mindev));
948 else {
949 return (format1(st,
950 file,
951 fmt, flen,
952 buf, blen,
953 flags, size, prec,
954 ofmt, 0, SHOW_st_size));
956 /*NOTREACHED*/
957 default:
958 errx(1, "%.*s: bad format", (int)flen, fmt);
962 * If a subdatum was specified but not supported, or an output
963 * format was selected that is not supported, that's an error.
965 if (hilo != 0 || (ofmt & formats) == 0)
966 errx(1, "%.*s: bad format", (int)flen, fmt);
969 * Assemble the format string for passing to printf(3).
971 lfmt[0] = '\0';
972 strcat(lfmt, "%");
973 if (flags & FLAG_POUND)
974 strcat(lfmt, "#");
975 if (flags & FLAG_SPACE)
976 strcat(lfmt, " ");
977 if (flags & FLAG_PLUS)
978 strcat(lfmt, "+");
979 if (flags & FLAG_MINUS)
980 strcat(lfmt, "-");
981 if (flags & FLAG_ZERO)
982 strcat(lfmt, "0");
985 * Only the timespecs support the FLOAT output format, and that
986 * requires work that differs from the other formats.
988 if (ofmt == FMTF_FLOAT) {
990 * Nothing after the decimal point, so just print seconds.
992 if (prec == 0) {
993 if (size != -1) {
994 snprintf(tmp, sizeof(tmp), "%d", size);
995 strcat(lfmt, tmp);
997 strcat(lfmt, "lld");
998 return (snprintf(buf, blen, lfmt,
999 (long long)ts.tv_sec));
1003 * Unspecified precision gets all the precision we have:
1004 * 9 digits.
1006 if (prec == -1)
1007 prec = 9;
1010 * Adjust the size for the decimal point and the digits
1011 * that will follow.
1013 size -= prec + 1;
1016 * Any leftover size that's legitimate will be used.
1018 if (size > 0) {
1019 snprintf(tmp, sizeof(tmp), "%d", size);
1020 strcat(lfmt, tmp);
1022 /* Seconds: time_t cast to long long. */
1023 strcat(lfmt, "lld");
1026 * The stuff after the decimal point always needs zero
1027 * filling.
1029 strcat(lfmt, ".%0");
1032 * We can "print" at most nine digits of precision. The
1033 * rest we will pad on at the end.
1035 * Nanoseconds: long.
1037 snprintf(tmp, sizeof(tmp), "%dld", prec > 9 ? 9 : prec);
1038 strcat(lfmt, tmp);
1041 * For precision of less that nine digits, trim off the
1042 * less significant figures.
1044 for (; prec < 9; prec++)
1045 ts.tv_nsec /= 10;
1048 * Use the format, and then tack on any zeroes that
1049 * might be required to make up the requested precision.
1051 l = snprintf(buf, blen, lfmt, (long long)ts.tv_sec, ts.tv_nsec);
1052 for (; prec > 9 && l < (int)blen; prec--, l++)
1053 strcat(buf, "0");
1054 return (l);
1058 * Add on size and precision, if specified, to the format.
1060 if (size != -1) {
1061 snprintf(tmp, sizeof(tmp), "%d", size);
1062 strcat(lfmt, tmp);
1064 if (prec != -1) {
1065 snprintf(tmp, sizeof(tmp), ".%d", prec);
1066 strcat(lfmt, tmp);
1070 * String output uses the temporary sdata.
1072 if (ofmt == FMTF_STRING) {
1073 if (sdata == NULL)
1074 errx(1, "%.*s: bad format", (int)flen, fmt);
1075 strcat(lfmt, "s");
1076 return (snprintf(buf, blen, lfmt, sdata));
1080 * Ensure that sign extension does not cause bad looking output
1081 * for some forms.
1083 if (small && ofmt != FMTF_DECIMAL)
1084 data = (u_int32_t)data;
1087 * The four "numeric" output forms.
1089 strcat(lfmt, "ll");
1090 switch (ofmt) {
1091 case FMTF_DECIMAL: strcat(lfmt, "d"); break;
1092 case FMTF_OCTAL: strcat(lfmt, "o"); break;
1093 case FMTF_UNSIGNED: strcat(lfmt, "u"); break;
1094 case FMTF_HEX: strcat(lfmt, "x"); break;
1097 return (snprintf(buf, blen, lfmt, data));
1100 #if HAVE_DEVNAME
1101 #define hex2nibble(c) (c <= '9' ? c - '0' : toupper(c) - 'A' + 10)
1102 static int
1103 hex2byte(const char c[2]) {
1104 if (!(ishexnumber(c[0]) && ishexnumber(c[1])))
1105 return -1;
1106 return (hex2nibble(c[0]) << 4) + hex2nibble(c[1]);
1108 #endif