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.
36 * $NetBSD: stat.c,v 1.13 2003/07/25 03:21:17 atatat Exp $
37 * $FreeBSD: src/usr.bin/stat/stat.c,v 1.6 2003/10/06 01:55:17 dougb Exp $
42 #else /* HAVE_CONFIG_H */
43 #define HAVE_STRUCT_STAT_ST_FLAGS 1
44 #define HAVE_STRUCT_STAT_ST_GEN 1
45 #define HAVE_STRUCT_STAT_ST_BIRTHTIME 0
46 #define HAVE_STRUCT_STAT_ST_MTIMENSEC 1
47 #define HAVE_DEVNAME 1
48 #endif /* HAVE_CONFIG_H */
50 #include <sys/types.h>
64 #if HAVE_STRUCT_STAT_ST_FLAGS
67 #define SHELL_F " st_flags=%f"
68 #else /* HAVE_STRUCT_STAT_ST_FLAGS */
72 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
74 #if HAVE_STRUCT_STAT_ST_BIRTHTIME
75 #define DEF_B "\"%SB\" "
77 #define SHELL_B "st_birthtime=%B "
78 #else /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
82 #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
84 #if HAVE_STRUCT_STAT_ST_ATIM
85 #define st_atimespec st_atim
86 #define st_ctimespec st_ctim
87 #define st_mtimespec st_mtim
88 #endif /* HAVE_STRUCT_STAT_ST_ATIM */
91 "%d %i %Sp %l %Su %Sg %r %z \"%Sa\" \"%Sm\" \"%Sc\" " DEF_B \
93 #define RAW_FORMAT "%d %i %#p %l %u %g %r %z %a %m %c " RAW_B \
95 #define LS_FORMAT "%Sp %l %Su %Sg %Z %Sm %N%SY"
96 #define LSF_FORMAT "%Sp %l %Su %Sg %Z %Sm %N%T%SY"
97 #define SHELL_FORMAT \
98 "st_dev=%d st_ino=%i st_mode=%#p st_nlink=%l " \
99 "st_uid=%u st_gid=%g st_rdev=%r st_size=%z " \
100 "st_atime=%a st_mtime=%m st_ctime=%c " SHELL_B \
101 "st_blksize=%k st_blocks=%b" SHELL_F
102 #define LINUX_FORMAT \
104 " Size: %-11z FileType: %HT%n" \
105 " Mode: (%04OLp/%.10Sp) Uid: (%5u/%8Su) Gid: (%5g/%8Sg)%n" \
106 "Device: %Hd,%Ld Inode: %i Links: %l%n" \
111 #define TIME_FORMAT "%b %e %T %Y"
113 #define FLAG_POUND 0x01
114 #define FLAG_SPACE 0x02
115 #define FLAG_PLUS 0x04
116 #define FLAG_ZERO 0x08
117 #define FLAG_MINUS 0x10
120 * These format characters must all be unique, except the magic one.
122 #define FMT_MAGIC '%'
125 #define SIMPLE_NEWLINE 'n'
126 #define SIMPLE_TAB 't'
127 #define SIMPLE_PERCENT '%'
128 #define SIMPLE_NUMBER '@'
130 #define FMT_POUND '#'
131 #define FMT_SPACE ' '
134 #define FMT_MINUS '-'
136 #define FMT_DECIMAL 'D'
137 #define FMT_OCTAL 'O'
138 #define FMT_UNSIGNED 'U'
140 #define FMT_FLOAT 'F'
141 #define FMT_STRING 'S'
143 #define FMTF_DECIMAL 0x01
144 #define FMTF_OCTAL 0x02
145 #define FMTF_UNSIGNED 0x04
146 #define FMTF_HEX 0x08
147 #define FMTF_FLOAT 0x10
148 #define FMTF_STRING 0x20
150 #define HIGH_PIECE 'H'
151 #define MIDDLE_PIECE 'M'
152 #define LOW_PIECE 'L'
154 #define SHOW_st_dev 'd'
155 #define SHOW_st_ino 'i'
156 #define SHOW_st_mode 'p'
157 #define SHOW_st_nlink 'l'
158 #define SHOW_st_uid 'u'
159 #define SHOW_st_gid 'g'
160 #define SHOW_st_rdev 'r'
161 #define SHOW_st_atime 'a'
162 #define SHOW_st_mtime 'm'
163 #define SHOW_st_ctime 'c'
164 #define SHOW_st_btime 'B'
165 #define SHOW_st_size 'z'
166 #define SHOW_st_blocks 'b'
167 #define SHOW_st_blksize 'k'
168 #define SHOW_st_flags 'f'
169 #define SHOW_st_gen 'v'
170 #define SHOW_symlink 'Y'
171 #define SHOW_filetype 'T'
172 #define SHOW_filename 'N'
173 #define SHOW_sizerdev 'Z'
175 void usage(const char *);
176 void output(const struct stat *, const char *,
177 const char *, int, int, int);
178 int format1(const struct stat *, /* stat info */
179 const char *, /* the file name */
180 const char *, int, /* the format string itself */
181 char *, size_t, /* a place to put the output */
182 int, int, int, int, /* the parsed format */
188 #define addchar(s, c, nl) \
190 (void)fputc((c), (s)); \
191 (*nl) = ((c) == '\n'); \
192 } while (0/*CONSTCOND*/)
195 main(int argc, char *argv[])
198 int ch, rc, errs, am_readlink;
199 int lsF, fmtchar, usestat, fn, nonl, quiet;
200 char *statfmt, *options, *synopsis;
212 if (strcmp(getprogname(), "readlink") == 0) {
215 synopsis = "[-n] [file ...]";
220 options = "f:FlLnqrst:x";
221 synopsis = "[-FlLnqrsx] [-f format] [-t timefmt] [file ...]";
224 while ((ch = getopt(argc, argv, options)) != -1)
246 errx(1, "can't use format '%c' with '%c'",
261 if (fmtchar == '\0') {
266 statfmt = DEF_FORMAT;
270 if (lsF && fmtchar != 'l')
271 errx(1, "can't use format '%c' with -F", fmtchar);
275 /* statfmt already set */
278 statfmt = lsF ? LSF_FORMAT : LS_FORMAT;
281 statfmt = RAW_FORMAT;
284 statfmt = SHELL_FORMAT;
287 statfmt = LINUX_FORMAT;
297 timefmt = TIME_FORMAT;
302 rc = fstat(STDIN_FILENO, &st);
304 rc = stat(argv[0], &st);
306 rc = lstat(argv[0], &st);
313 argc == 0 ? "(stdin)" : argv[0]);
316 output(&st, argv[0], statfmt, fn, nonl, quiet);
323 return (am_readlink ? linkfail : errs);
327 usage(const char *synopsis)
330 (void)fprintf(stderr, "usage: %s %s\n", getprogname(), synopsis);
335 * Parses a format string.
338 output(const struct stat *st, const char *file,
339 const char *statfmt, int fn, int nonl, int quiet)
341 int flags, size, prec, ofmt, hilo, what;
347 while (*statfmt != '\0') {
350 * Non-format characters go straight out.
352 if (*statfmt != FMT_MAGIC) {
353 addchar(stdout, *statfmt, &nl);
359 * The current format "substring" starts here,
360 * and then we skip the magic.
366 * Some simple one-character "formats".
370 addchar(stdout, '\n', &nl);
374 addchar(stdout, '\t', &nl);
378 addchar(stdout, '%', &nl);
381 case SIMPLE_NUMBER: {
384 snprintf(num, sizeof(num), "%d", fn);
385 for (p = &num[0]; *p; p++)
386 addchar(stdout, *p, &nl);
393 * This must be an actual format string. Format strings are
394 * similar to printf(3) formats up to a point, and are of
397 * % required start of format
398 * [-# +0] opt. format characters
399 * size opt. field width
400 * . opt. decimal separator, followed by
401 * prec opt. precision
402 * fmt opt. output specifier (string, numeric, etc.)
403 * sub opt. sub field specifier (high, middle, low)
404 * datum required field specifier (size, mode, etc)
406 * Only the % and the datum selector are required. All data
407 * have reasonable default output forms. The "sub" specifier
408 * only applies to certain data (mode, dev, rdev, filetype).
409 * The symlink output defaults to STRING, yet will only emit
410 * the leading " -> " if STRING is explicitly specified. The
411 * sizerdev datum will generate rdev output for character or
412 * block devices, and size output for all others.
416 if (*statfmt == FMT_POUND)
418 else if (*statfmt == FMT_SPACE)
420 else if (*statfmt == FMT_PLUS)
422 else if (*statfmt == FMT_ZERO)
424 else if (*statfmt == FMT_MINUS)
429 } while (1/*CONSTCOND*/);
432 if (isdigit((unsigned)*statfmt)) {
434 while (isdigit((unsigned)*statfmt)) {
435 size = (size * 10) + (*statfmt - '0');
443 if (*statfmt == FMT_DOT) {
447 while (isdigit((unsigned)*statfmt)) {
448 prec = (prec * 10) + (*statfmt - '0');
455 #define fmtcase(x, y) case (y): (x) = (y); statfmt++; break
456 #define fmtcasef(x, y, z) case (y): (x) = (z); statfmt++; break
458 fmtcasef(ofmt, FMT_DECIMAL, FMTF_DECIMAL);
459 fmtcasef(ofmt, FMT_OCTAL, FMTF_OCTAL);
460 fmtcasef(ofmt, FMT_UNSIGNED, FMTF_UNSIGNED);
461 fmtcasef(ofmt, FMT_HEX, FMTF_HEX);
462 fmtcasef(ofmt, FMT_FLOAT, FMTF_FLOAT);
463 fmtcasef(ofmt, FMT_STRING, FMTF_STRING);
470 fmtcase(hilo, HIGH_PIECE);
471 fmtcase(hilo, MIDDLE_PIECE);
472 fmtcase(hilo, LOW_PIECE);
479 fmtcase(what, SHOW_st_dev);
480 fmtcase(what, SHOW_st_ino);
481 fmtcase(what, SHOW_st_mode);
482 fmtcase(what, SHOW_st_nlink);
483 fmtcase(what, SHOW_st_uid);
484 fmtcase(what, SHOW_st_gid);
485 fmtcase(what, SHOW_st_rdev);
486 fmtcase(what, SHOW_st_atime);
487 fmtcase(what, SHOW_st_mtime);
488 fmtcase(what, SHOW_st_ctime);
489 fmtcase(what, SHOW_st_btime);
490 fmtcase(what, SHOW_st_size);
491 fmtcase(what, SHOW_st_blocks);
492 fmtcase(what, SHOW_st_blksize);
493 fmtcase(what, SHOW_st_flags);
494 fmtcase(what, SHOW_st_gen);
495 fmtcase(what, SHOW_symlink);
496 fmtcase(what, SHOW_filetype);
497 fmtcase(what, SHOW_filename);
498 fmtcase(what, SHOW_sizerdev);
507 subfmt, statfmt - subfmt,
509 flags, size, prec, ofmt, hilo, what);
511 for (i = 0; i < t && i < sizeof(buf); i++)
512 addchar(stdout, buf[i], &nl);
517 errx(1, "%.*s: bad format",
518 (int)(statfmt - subfmt + 1), subfmt);
522 (void)fputc('\n', stdout);
523 (void)fflush(stdout);
527 * Arranges output according to a single parsed format substring.
530 format1(const struct stat *st,
532 const char *fmt, int flen,
533 char *buf, size_t blen,
534 int flags, int size, int prec, int ofmt,
538 char *sdata, lfmt[24], tmp[20];
539 char smode[12], sid[12], path[PATH_MAX + 4];
542 const struct timespec *tsp;
545 int l, small, formats;
552 * First, pick out the data and tweak it based on hilo or
553 * specified output format (symlink output only).
558 small = (sizeof(st->st_dev) == 4);
559 data = (what == SHOW_st_dev) ? st->st_dev : st->st_rdev;
561 sdata = (what == SHOW_st_dev) ?
562 devname(st->st_dev, S_IFBLK) :
564 S_ISCHR(st->st_mode) ? S_IFCHR :
565 S_ISBLK(st->st_mode) ? S_IFBLK :
569 #endif /* HAVE_DEVNAME */
570 if (hilo == HIGH_PIECE) {
574 else if (hilo == LOW_PIECE) {
575 data = minor((unsigned)data);
578 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
581 #else /* HAVE_DEVNAME */
583 #endif /* HAVE_DEVNAME */
585 ofmt = FMTF_UNSIGNED;
588 small = (sizeof(st->st_ino) == 4);
591 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
593 ofmt = FMTF_UNSIGNED;
596 small = (sizeof(st->st_mode) == 4);
598 strmode(st->st_mode, smode);
601 if (sdata[l - 1] == ' ')
603 if (hilo == HIGH_PIECE) {
609 else if (hilo == MIDDLE_PIECE) {
610 data = (data >> 9) & 07;
615 else if (hilo == LOW_PIECE) {
621 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
627 small = (sizeof(st->st_dev) == 4);
630 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
632 ofmt = FMTF_UNSIGNED;
635 small = (sizeof(st->st_uid) == 4);
637 if ((pw = getpwuid(st->st_uid)) != NULL)
640 snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_uid);
643 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
646 ofmt = FMTF_UNSIGNED;
649 small = (sizeof(st->st_gid) == 4);
651 if ((gr = getgrgid(st->st_gid)) != NULL)
654 snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_gid);
657 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
660 ofmt = FMTF_UNSIGNED;
663 tsp = &st->st_atimespec;
667 tsp = &st->st_mtimespec;
671 tsp = &st->st_ctimespec;
673 #if HAVE_STRUCT_STAT_ST_BIRTHTIME
676 tsp = &st->st_birthtimespec;
677 #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
678 ts = *tsp; /* copy so we can muck with it */
679 small = (sizeof(ts.tv_sec) == 4);
682 tm = localtime(&ts.tv_sec);
683 strftime(path, sizeof(path), timefmt, tm);
685 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
686 FMTF_FLOAT | FMTF_STRING;
691 small = (sizeof(st->st_size) == 4);
694 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
696 ofmt = FMTF_UNSIGNED;
699 small = (sizeof(st->st_blocks) == 4);
700 data = st->st_blocks;
702 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
704 ofmt = FMTF_UNSIGNED;
706 case SHOW_st_blksize:
707 small = (sizeof(st->st_blksize) == 4);
708 data = st->st_blksize;
710 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
712 ofmt = FMTF_UNSIGNED;
714 #if HAVE_STRUCT_STAT_ST_FLAGS
716 small = (sizeof(st->st_flags) == 4);
719 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
721 ofmt = FMTF_UNSIGNED;
723 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
724 #if HAVE_STRUCT_STAT_ST_GEN
726 small = (sizeof(st->st_gen) == 4);
729 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
731 ofmt = FMTF_UNSIGNED;
733 #endif /* HAVE_STRUCT_STAT_ST_GEN */
737 if (S_ISLNK(st->st_mode)) {
738 snprintf(path, sizeof(path), " -> ");
739 l = readlink(file, path + 4, sizeof(path) - 4 - 1);
746 sdata = path + (ofmt == FMTF_STRING ? 0 : 4);
752 formats = FMTF_STRING;
761 if (hilo == 0 || hilo == LOW_PIECE) {
762 switch (st->st_mode & S_IFMT) {
763 case S_IFIFO: (void)strcat(sdata, "|"); break;
764 case S_IFDIR: (void)strcat(sdata, "/"); break;
767 (S_IXUSR | S_IXGRP | S_IXOTH))
768 (void)strcat(sdata, "*");
770 case S_IFLNK: (void)strcat(sdata, "@"); break;
771 case S_IFSOCK: (void)strcat(sdata, "="); break;
773 case S_IFWHT: (void)strcat(sdata, "%"); break;
776 case S_IFDOOR: (void)strcat(sdata, ">"); break;
777 #endif /* S_IFDOOR */
781 else if (hilo == HIGH_PIECE) {
782 switch (st->st_mode & S_IFMT) {
783 case S_IFIFO: sdata = "Fifo File"; break;
784 case S_IFCHR: sdata = "Character Device"; break;
785 case S_IFDIR: sdata = "Directory"; break;
786 case S_IFBLK: sdata = "Block Device"; break;
787 case S_IFREG: sdata = "Regular File"; break;
788 case S_IFLNK: sdata = "Symbolic Link"; break;
789 case S_IFSOCK: sdata = "Socket"; break;
791 case S_IFWHT: sdata = "Whiteout File"; break;
794 case S_IFDOOR: sdata = "Door"; break;
795 #endif /* S_IFDOOR */
796 default: sdata = "???"; break;
800 formats = FMTF_STRING;
808 (void)strncpy(path, "(stdin)", sizeof(path));
810 (void)strncpy(path, file, sizeof(path));
812 formats = FMTF_STRING;
817 if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
818 char majdev[20], mindev[20];
824 majdev, sizeof(majdev),
826 ofmt, HIGH_PIECE, SHOW_st_rdev);
830 mindev, sizeof(mindev),
832 ofmt, LOW_PIECE, SHOW_st_rdev);
833 return (snprintf(buf, blen, "%.*s,%.*s",
834 l1, majdev, l2, mindev));
842 ofmt, 0, SHOW_st_size));
846 errx(1, "%.*s: bad format", (int)flen, fmt);
850 * If a subdatum was specified but not supported, or an output
851 * format was selected that is not supported, that's an error.
853 if (hilo != 0 || (ofmt & formats) == 0)
854 errx(1, "%.*s: bad format", (int)flen, fmt);
857 * Assemble the format string for passing to printf(3).
860 (void)strcat(lfmt, "%");
861 if (flags & FLAG_POUND)
862 (void)strcat(lfmt, "#");
863 if (flags & FLAG_SPACE)
864 (void)strcat(lfmt, " ");
865 if (flags & FLAG_PLUS)
866 (void)strcat(lfmt, "+");
867 if (flags & FLAG_MINUS)
868 (void)strcat(lfmt, "-");
869 if (flags & FLAG_ZERO)
870 (void)strcat(lfmt, "0");
873 * Only the timespecs support the FLOAT output format, and that
874 * requires work that differs from the other formats.
876 if (ofmt == FMTF_FLOAT) {
878 * Nothing after the decimal point, so just print seconds.
882 (void)snprintf(tmp, sizeof(tmp), "%d", size);
883 (void)strcat(lfmt, tmp);
885 (void)strcat(lfmt, "d");
886 return (snprintf(buf, blen, lfmt, ts.tv_sec));
890 * Unspecified precision gets all the precision we have:
897 * Adjust the size for the decimal point and the digits
903 * Any leftover size that's legitimate will be used.
906 (void)snprintf(tmp, sizeof(tmp), "%d", size);
907 (void)strcat(lfmt, tmp);
909 (void)strcat(lfmt, "d");
912 * The stuff after the decimal point always needs zero
915 (void)strcat(lfmt, ".%0");
918 * We can "print" at most nine digits of precision. The
919 * rest we will pad on at the end.
921 (void)snprintf(tmp, sizeof(tmp), "%dd", prec > 9 ? 9 : prec);
922 (void)strcat(lfmt, tmp);
925 * For precision of less that nine digits, trim off the
926 * less significant figures.
928 for (; prec < 9; prec++)
932 * Use the format, and then tack on any zeroes that
933 * might be required to make up the requested precision.
935 l = snprintf(buf, blen, lfmt, ts.tv_sec, ts.tv_nsec);
936 for (; prec > 9 && l < blen; prec--, l++)
937 (void)strcat(buf, "0");
942 * Add on size and precision, if specified, to the format.
945 (void)snprintf(tmp, sizeof(tmp), "%d", size);
946 (void)strcat(lfmt, tmp);
949 (void)snprintf(tmp, sizeof(tmp), ".%d", prec);
950 (void)strcat(lfmt, tmp);
954 * String output uses the temporary sdata.
956 if (ofmt == FMTF_STRING) {
958 errx(1, "%.*s: bad format", (int)flen, fmt);
959 (void)strcat(lfmt, "s");
960 return (snprintf(buf, blen, lfmt, sdata));
964 * Ensure that sign extension does not cause bad looking output
967 if (small && ofmt != FMTF_DECIMAL)
968 data = (u_int32_t)data;
971 * The four "numeric" output forms.
973 (void)strcat(lfmt, "ll");
975 case FMTF_DECIMAL: (void)strcat(lfmt, "d"); break;
976 case FMTF_OCTAL: (void)strcat(lfmt, "o"); break;
977 case FMTF_UNSIGNED: (void)strcat(lfmt, "u"); break;
978 case FMTF_HEX: (void)strcat(lfmt, "x"); break;
981 return (snprintf(buf, blen, lfmt, data));