2 * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 * This code is derived from software contributed to The NetBSD Foundation
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the NetBSD
19 * Foundation, Inc. and its contributors.
20 * 4. Neither the name of The NetBSD Foundation nor the names of its
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
25 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
28 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
37 #include <sys/cdefs.h>
40 __RCSID("$NetBSD: stat.c,v 1.13 2003/07/25 03:21:17 atatat Exp $");
44 __FBSDID("$FreeBSD$");
48 #else /* HAVE_CONFIG_H */
49 #define HAVE_STRUCT_STAT_ST_FLAGS 1
50 #define HAVE_STRUCT_STAT_ST_GEN 1
51 #define HAVE_STRUCT_STAT_ST_BIRTHTIME 1
52 #define HAVE_STRUCT_STAT_ST_MTIMENSEC 1
53 #define HAVE_DEVNAME 1
54 #endif /* HAVE_CONFIG_H */
56 #include <sys/param.h>
57 #include <sys/types.h>
72 #if HAVE_STRUCT_STAT_ST_FLAGS
75 #define SHELL_F " st_flags=%f"
76 #else /* HAVE_STRUCT_STAT_ST_FLAGS */
80 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
82 #if HAVE_STRUCT_STAT_ST_BIRTHTIME
83 #define DEF_B "\"%SB\" "
85 #define SHELL_B "st_birthtime=%B "
86 #else /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
90 #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
92 #if HAVE_STRUCT_STAT_ST_ATIM
93 #define st_atimespec st_atim
94 #define st_ctimespec st_ctim
95 #define st_mtimespec st_mtim
96 #endif /* HAVE_STRUCT_STAT_ST_ATIM */
99 "%d %i %Sp %l %Su %Sg %r %z \"%Sa\" \"%Sm\" \"%Sc\" " DEF_B \
101 #define RAW_FORMAT "%d %i %#p %l %u %g %r %z %a %m %c " RAW_B \
103 #define LS_FORMAT "%Sp %l %Su %Sg %Z %Sm %N%SY"
104 #define LSF_FORMAT "%Sp %l %Su %Sg %Z %Sm %N%T%SY"
105 #define SHELL_FORMAT \
106 "st_dev=%d st_ino=%i st_mode=%#p st_nlink=%l " \
107 "st_uid=%u st_gid=%g st_rdev=%r st_size=%z " \
108 "st_atime=%a st_mtime=%m st_ctime=%c " SHELL_B \
109 "st_blksize=%k st_blocks=%b" SHELL_F
110 #define LINUX_FORMAT \
112 " Size: %-11z FileType: %HT%n" \
113 " Mode: (%OMp%03OLp/%.10Sp) Uid: (%5u/%8Su) Gid: (%5g/%8Sg)%n" \
114 "Device: %Hd,%Ld Inode: %i Links: %l%n" \
119 #define TIME_FORMAT "%b %e %T %Y"
121 #define FLAG_POUND 0x01
122 #define FLAG_SPACE 0x02
123 #define FLAG_PLUS 0x04
124 #define FLAG_ZERO 0x08
125 #define FLAG_MINUS 0x10
128 * These format characters must all be unique, except the magic one.
130 #define FMT_MAGIC '%'
133 #define SIMPLE_NEWLINE 'n'
134 #define SIMPLE_TAB 't'
135 #define SIMPLE_PERCENT '%'
136 #define SIMPLE_NUMBER '@'
138 #define FMT_POUND '#'
139 #define FMT_SPACE ' '
142 #define FMT_MINUS '-'
144 #define FMT_DECIMAL 'D'
145 #define FMT_OCTAL 'O'
146 #define FMT_UNSIGNED 'U'
148 #define FMT_FLOAT 'F'
149 #define FMT_STRING 'S'
151 #define FMTF_DECIMAL 0x01
152 #define FMTF_OCTAL 0x02
153 #define FMTF_UNSIGNED 0x04
154 #define FMTF_HEX 0x08
155 #define FMTF_FLOAT 0x10
156 #define FMTF_STRING 0x20
158 #define HIGH_PIECE 'H'
159 #define MIDDLE_PIECE 'M'
160 #define LOW_PIECE 'L'
162 #define SHOW_st_dev 'd'
163 #define SHOW_st_ino 'i'
164 #define SHOW_st_mode 'p'
165 #define SHOW_st_nlink 'l'
166 #define SHOW_st_uid 'u'
167 #define SHOW_st_gid 'g'
168 #define SHOW_st_rdev 'r'
169 #define SHOW_st_atime 'a'
170 #define SHOW_st_mtime 'm'
171 #define SHOW_st_ctime 'c'
172 #define SHOW_st_btime 'B'
173 #define SHOW_st_size 'z'
174 #define SHOW_st_blocks 'b'
175 #define SHOW_st_blksize 'k'
176 #define SHOW_st_flags 'f'
177 #define SHOW_st_gen 'v'
178 #define SHOW_symlink 'Y'
179 #define SHOW_filetype 'T'
180 #define SHOW_filename 'N'
181 #define SHOW_sizerdev 'Z'
183 void usage(const char *);
184 void output(const struct stat *, const char *,
185 const char *, int, int, int);
186 int format1(const struct stat *, /* stat info */
187 const char *, /* the file name */
188 const char *, int, /* the format string itself */
189 char *, size_t, /* a place to put the output */
190 int, int, int, int, /* the parsed format */
196 #define addchar(s, c, nl) \
198 (void)fputc((c), (s)); \
199 (*nl) = ((c) == '\n'); \
200 } while (0/*CONSTCOND*/)
203 main(int argc, char *argv[])
206 int ch, rc, errs, am_readlink;
207 int lsF, fmtchar, usestat, fn, nonl, quiet;
208 char *statfmt, *options, *synopsis;
209 char dname[sizeof _PATH_DEV + SPECNAMELEN] = _PATH_DEV;
222 if (strcmp(getprogname(), "readlink") == 0) {
225 synopsis = "[-n] [file ...]";
230 options = "f:FlLnqrst:x";
231 synopsis = "[-FlLnqrsx] [-f format] [-t timefmt] [file ...]";
234 while ((ch = getopt(argc, argv, options)) != -1)
256 errx(1, "can't use format '%c' with '%c'",
271 if (fmtchar == '\0') {
276 statfmt = DEF_FORMAT;
280 if (lsF && fmtchar != 'l')
281 errx(1, "can't use format '%c' with -F", fmtchar);
285 /* statfmt already set */
288 statfmt = lsF ? LSF_FORMAT : LS_FORMAT;
291 statfmt = RAW_FORMAT;
294 statfmt = SHELL_FORMAT;
297 statfmt = LINUX_FORMAT;
307 timefmt = TIME_FORMAT;
312 if (fdevname_r(STDIN_FILENO, dname +
313 sizeof _PATH_DEV - 1, SPECNAMELEN) != NULL)
317 rc = fstat(STDIN_FILENO, &st);
321 rc = stat(file, &st);
323 rc = lstat(file, &st);
330 warn("%s: stat", file);
333 output(&st, file, statfmt, fn, nonl, quiet);
340 return (am_readlink ? linkfail : errs);
344 usage(const char *synopsis)
347 (void)fprintf(stderr, "usage: %s %s\n", getprogname(), synopsis);
352 * Parses a format string.
355 output(const struct stat *st, const char *file,
356 const char *statfmt, int fn, int nonl, int quiet)
358 int flags, size, prec, ofmt, hilo, what;
364 while (*statfmt != '\0') {
367 * Non-format characters go straight out.
369 if (*statfmt != FMT_MAGIC) {
370 addchar(stdout, *statfmt, &nl);
376 * The current format "substring" starts here,
377 * and then we skip the magic.
383 * Some simple one-character "formats".
387 addchar(stdout, '\n', &nl);
391 addchar(stdout, '\t', &nl);
395 addchar(stdout, '%', &nl);
398 case SIMPLE_NUMBER: {
401 snprintf(num, sizeof(num), "%d", fn);
402 for (p = &num[0]; *p; p++)
403 addchar(stdout, *p, &nl);
410 * This must be an actual format string. Format strings are
411 * similar to printf(3) formats up to a point, and are of
414 * % required start of format
415 * [-# +0] opt. format characters
416 * size opt. field width
417 * . opt. decimal separator, followed by
418 * prec opt. precision
419 * fmt opt. output specifier (string, numeric, etc.)
420 * sub opt. sub field specifier (high, middle, low)
421 * datum required field specifier (size, mode, etc)
423 * Only the % and the datum selector are required. All data
424 * have reasonable default output forms. The "sub" specifier
425 * only applies to certain data (mode, dev, rdev, filetype).
426 * The symlink output defaults to STRING, yet will only emit
427 * the leading " -> " if STRING is explicitly specified. The
428 * sizerdev datum will generate rdev output for character or
429 * block devices, and size output for all others.
433 if (*statfmt == FMT_POUND)
435 else if (*statfmt == FMT_SPACE)
437 else if (*statfmt == FMT_PLUS)
439 else if (*statfmt == FMT_ZERO)
441 else if (*statfmt == FMT_MINUS)
446 } while (1/*CONSTCOND*/);
449 if (isdigit((unsigned)*statfmt)) {
451 while (isdigit((unsigned)*statfmt)) {
452 size = (size * 10) + (*statfmt - '0');
460 if (*statfmt == FMT_DOT) {
464 while (isdigit((unsigned)*statfmt)) {
465 prec = (prec * 10) + (*statfmt - '0');
472 #define fmtcase(x, y) case (y): (x) = (y); statfmt++; break
473 #define fmtcasef(x, y, z) case (y): (x) = (z); statfmt++; break
475 fmtcasef(ofmt, FMT_DECIMAL, FMTF_DECIMAL);
476 fmtcasef(ofmt, FMT_OCTAL, FMTF_OCTAL);
477 fmtcasef(ofmt, FMT_UNSIGNED, FMTF_UNSIGNED);
478 fmtcasef(ofmt, FMT_HEX, FMTF_HEX);
479 fmtcasef(ofmt, FMT_FLOAT, FMTF_FLOAT);
480 fmtcasef(ofmt, FMT_STRING, FMTF_STRING);
487 fmtcase(hilo, HIGH_PIECE);
488 fmtcase(hilo, MIDDLE_PIECE);
489 fmtcase(hilo, LOW_PIECE);
496 fmtcase(what, SHOW_st_dev);
497 fmtcase(what, SHOW_st_ino);
498 fmtcase(what, SHOW_st_mode);
499 fmtcase(what, SHOW_st_nlink);
500 fmtcase(what, SHOW_st_uid);
501 fmtcase(what, SHOW_st_gid);
502 fmtcase(what, SHOW_st_rdev);
503 fmtcase(what, SHOW_st_atime);
504 fmtcase(what, SHOW_st_mtime);
505 fmtcase(what, SHOW_st_ctime);
506 fmtcase(what, SHOW_st_btime);
507 fmtcase(what, SHOW_st_size);
508 fmtcase(what, SHOW_st_blocks);
509 fmtcase(what, SHOW_st_blksize);
510 fmtcase(what, SHOW_st_flags);
511 fmtcase(what, SHOW_st_gen);
512 fmtcase(what, SHOW_symlink);
513 fmtcase(what, SHOW_filetype);
514 fmtcase(what, SHOW_filename);
515 fmtcase(what, SHOW_sizerdev);
524 subfmt, statfmt - subfmt,
526 flags, size, prec, ofmt, hilo, what);
528 for (i = 0; i < t && i < sizeof(buf); i++)
529 addchar(stdout, buf[i], &nl);
534 errx(1, "%.*s: bad format",
535 (int)(statfmt - subfmt + 1), subfmt);
539 (void)fputc('\n', stdout);
540 (void)fflush(stdout);
544 * Arranges output according to a single parsed format substring.
547 format1(const struct stat *st,
549 const char *fmt, int flen,
550 char *buf, size_t blen,
551 int flags, int size, int prec, int ofmt,
555 char *sdata, lfmt[24], tmp[20];
556 char smode[12], sid[12], path[PATH_MAX + 4];
559 const struct timespec *tsp;
562 int l, small, formats;
569 * First, pick out the data and tweak it based on hilo or
570 * specified output format (symlink output only).
575 small = (sizeof(st->st_dev) == 4);
576 data = (what == SHOW_st_dev) ? st->st_dev : st->st_rdev;
578 sdata = (what == SHOW_st_dev) ?
579 devname(st->st_dev, S_IFBLK) :
581 S_ISCHR(st->st_mode) ? S_IFCHR :
582 S_ISBLK(st->st_mode) ? S_IFBLK :
586 #endif /* HAVE_DEVNAME */
587 if (hilo == HIGH_PIECE) {
591 else if (hilo == LOW_PIECE) {
592 data = minor((unsigned)data);
595 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
598 #else /* HAVE_DEVNAME */
600 #endif /* HAVE_DEVNAME */
602 ofmt = FMTF_UNSIGNED;
605 small = (sizeof(st->st_ino) == 4);
608 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
610 ofmt = FMTF_UNSIGNED;
613 small = (sizeof(st->st_mode) == 4);
615 strmode(st->st_mode, smode);
618 if (sdata[l - 1] == ' ')
620 if (hilo == HIGH_PIECE) {
626 else if (hilo == MIDDLE_PIECE) {
627 data = (data >> 9) & 07;
632 else if (hilo == LOW_PIECE) {
638 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
644 small = (sizeof(st->st_dev) == 4);
647 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
649 ofmt = FMTF_UNSIGNED;
652 small = (sizeof(st->st_uid) == 4);
654 if ((pw = getpwuid(st->st_uid)) != NULL)
657 snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_uid);
660 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
663 ofmt = FMTF_UNSIGNED;
666 small = (sizeof(st->st_gid) == 4);
668 if ((gr = getgrgid(st->st_gid)) != NULL)
671 snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_gid);
674 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
677 ofmt = FMTF_UNSIGNED;
680 tsp = &st->st_atimespec;
684 tsp = &st->st_mtimespec;
688 tsp = &st->st_ctimespec;
690 #if HAVE_STRUCT_STAT_ST_BIRTHTIME
693 tsp = &st->st_birthtimespec;
694 #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
695 ts = *tsp; /* copy so we can muck with it */
696 small = (sizeof(ts.tv_sec) == 4);
699 tm = localtime(&ts.tv_sec);
700 (void)strftime(path, sizeof(path), timefmt, tm);
702 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
703 FMTF_FLOAT | FMTF_STRING;
708 small = (sizeof(st->st_size) == 4);
711 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
713 ofmt = FMTF_UNSIGNED;
716 small = (sizeof(st->st_blocks) == 4);
717 data = st->st_blocks;
719 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
721 ofmt = FMTF_UNSIGNED;
723 case SHOW_st_blksize:
724 small = (sizeof(st->st_blksize) == 4);
725 data = st->st_blksize;
727 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
729 ofmt = FMTF_UNSIGNED;
731 #if HAVE_STRUCT_STAT_ST_FLAGS
733 small = (sizeof(st->st_flags) == 4);
736 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
738 ofmt = FMTF_UNSIGNED;
740 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
741 #if HAVE_STRUCT_STAT_ST_GEN
743 small = (sizeof(st->st_gen) == 4);
746 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
748 ofmt = FMTF_UNSIGNED;
750 #endif /* HAVE_STRUCT_STAT_ST_GEN */
754 if (S_ISLNK(st->st_mode)) {
755 snprintf(path, sizeof(path), " -> ");
756 l = readlink(file, path + 4, sizeof(path) - 4 - 1);
763 sdata = path + (ofmt == FMTF_STRING ? 0 : 4);
769 formats = FMTF_STRING;
778 if (hilo == 0 || hilo == LOW_PIECE) {
779 switch (st->st_mode & S_IFMT) {
780 case S_IFIFO: (void)strcat(sdata, "|"); break;
781 case S_IFDIR: (void)strcat(sdata, "/"); break;
784 (S_IXUSR | S_IXGRP | S_IXOTH))
785 (void)strcat(sdata, "*");
787 case S_IFLNK: (void)strcat(sdata, "@"); break;
788 case S_IFSOCK: (void)strcat(sdata, "="); break;
790 case S_IFWHT: (void)strcat(sdata, "%"); break;
793 case S_IFDOOR: (void)strcat(sdata, ">"); break;
794 #endif /* S_IFDOOR */
798 else if (hilo == HIGH_PIECE) {
799 switch (st->st_mode & S_IFMT) {
800 case S_IFIFO: sdata = "Fifo File"; break;
801 case S_IFCHR: sdata = "Character Device"; break;
802 case S_IFDIR: sdata = "Directory"; break;
803 case S_IFBLK: sdata = "Block Device"; break;
804 case S_IFREG: sdata = "Regular File"; break;
805 case S_IFLNK: sdata = "Symbolic Link"; break;
806 case S_IFSOCK: sdata = "Socket"; break;
808 case S_IFWHT: sdata = "Whiteout File"; break;
811 case S_IFDOOR: sdata = "Door"; break;
812 #endif /* S_IFDOOR */
813 default: sdata = "???"; break;
817 formats = FMTF_STRING;
824 (void)strncpy(path, file, sizeof(path));
826 formats = FMTF_STRING;
831 if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
832 char majdev[20], mindev[20];
838 majdev, sizeof(majdev),
840 ofmt, HIGH_PIECE, SHOW_st_rdev);
844 mindev, sizeof(mindev),
846 ofmt, LOW_PIECE, SHOW_st_rdev);
847 return (snprintf(buf, blen, "%.*s,%.*s",
848 l1, majdev, l2, mindev));
856 ofmt, 0, SHOW_st_size));
860 errx(1, "%.*s: bad format", (int)flen, fmt);
864 * If a subdatum was specified but not supported, or an output
865 * format was selected that is not supported, that's an error.
867 if (hilo != 0 || (ofmt & formats) == 0)
868 errx(1, "%.*s: bad format", (int)flen, fmt);
871 * Assemble the format string for passing to printf(3).
874 (void)strcat(lfmt, "%");
875 if (flags & FLAG_POUND)
876 (void)strcat(lfmt, "#");
877 if (flags & FLAG_SPACE)
878 (void)strcat(lfmt, " ");
879 if (flags & FLAG_PLUS)
880 (void)strcat(lfmt, "+");
881 if (flags & FLAG_MINUS)
882 (void)strcat(lfmt, "-");
883 if (flags & FLAG_ZERO)
884 (void)strcat(lfmt, "0");
887 * Only the timespecs support the FLOAT output format, and that
888 * requires work that differs from the other formats.
890 if (ofmt == FMTF_FLOAT) {
892 * Nothing after the decimal point, so just print seconds.
896 (void)snprintf(tmp, sizeof(tmp), "%d", size);
897 (void)strcat(lfmt, tmp);
899 (void)strcat(lfmt, "d");
900 return (snprintf(buf, blen, lfmt, ts.tv_sec));
904 * Unspecified precision gets all the precision we have:
911 * Adjust the size for the decimal point and the digits
917 * Any leftover size that's legitimate will be used.
920 (void)snprintf(tmp, sizeof(tmp), "%d", size);
921 (void)strcat(lfmt, tmp);
923 (void)strcat(lfmt, "d");
926 * The stuff after the decimal point always needs zero
929 (void)strcat(lfmt, ".%0");
932 * We can "print" at most nine digits of precision. The
933 * rest we will pad on at the end.
935 (void)snprintf(tmp, sizeof(tmp), "%dd", prec > 9 ? 9 : prec);
936 (void)strcat(lfmt, tmp);
939 * For precision of less that nine digits, trim off the
940 * less significant figures.
942 for (; prec < 9; prec++)
946 * Use the format, and then tack on any zeroes that
947 * might be required to make up the requested precision.
949 l = snprintf(buf, blen, lfmt, ts.tv_sec, ts.tv_nsec);
950 for (; prec > 9 && l < blen; prec--, l++)
951 (void)strcat(buf, "0");
956 * Add on size and precision, if specified, to the format.
959 (void)snprintf(tmp, sizeof(tmp), "%d", size);
960 (void)strcat(lfmt, tmp);
963 (void)snprintf(tmp, sizeof(tmp), ".%d", prec);
964 (void)strcat(lfmt, tmp);
968 * String output uses the temporary sdata.
970 if (ofmt == FMTF_STRING) {
972 errx(1, "%.*s: bad format", (int)flen, fmt);
973 (void)strcat(lfmt, "s");
974 return (snprintf(buf, blen, lfmt, sdata));
978 * Ensure that sign extension does not cause bad looking output
981 if (small && ofmt != FMTF_DECIMAL)
982 data = (u_int32_t)data;
985 * The four "numeric" output forms.
987 (void)strcat(lfmt, "ll");
989 case FMTF_DECIMAL: (void)strcat(lfmt, "d"); break;
990 case FMTF_OCTAL: (void)strcat(lfmt, "o"); break;
991 case FMTF_UNSIGNED: (void)strcat(lfmt, "u"); break;
992 case FMTF_HEX: (void)strcat(lfmt, "x"); break;
995 return (snprintf(buf, blen, lfmt, data));