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/types.h>
70 #if HAVE_STRUCT_STAT_ST_FLAGS
73 #define SHELL_F " st_flags=%f"
74 #else /* HAVE_STRUCT_STAT_ST_FLAGS */
78 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
80 #if HAVE_STRUCT_STAT_ST_BIRTHTIME
81 #define DEF_B "\"%SB\" "
83 #define SHELL_B "st_birthtime=%B "
84 #else /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
88 #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
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 */
97 "%d %i %Sp %l %Su %Sg %r %z \"%Sa\" \"%Sm\" \"%Sc\" " DEF_B \
99 #define RAW_FORMAT "%d %i %#p %l %u %g %r %z %a %m %c " RAW_B \
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 \
110 " Size: %-11z FileType: %HT%n" \
111 " Mode: (%04OLp/%.10Sp) Uid: (%5u/%8Su) Gid: (%5g/%8Sg)%n" \
112 "Device: %Hd,%Ld Inode: %i Links: %l%n" \
117 #define TIME_FORMAT "%b %e %T %Y"
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
126 * These format characters must all be unique, except the magic one.
128 #define FMT_MAGIC '%'
131 #define SIMPLE_NEWLINE 'n'
132 #define SIMPLE_TAB 't'
133 #define SIMPLE_PERCENT '%'
134 #define SIMPLE_NUMBER '@'
136 #define FMT_POUND '#'
137 #define FMT_SPACE ' '
140 #define FMT_MINUS '-'
142 #define FMT_DECIMAL 'D'
143 #define FMT_OCTAL 'O'
144 #define FMT_UNSIGNED 'U'
146 #define FMT_FLOAT 'F'
147 #define FMT_STRING 'S'
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
156 #define HIGH_PIECE 'H'
157 #define MIDDLE_PIECE 'M'
158 #define LOW_PIECE 'L'
160 #define SHOW_st_dev 'd'
161 #define SHOW_st_ino 'i'
162 #define SHOW_st_mode 'p'
163 #define SHOW_st_nlink 'l'
164 #define SHOW_st_uid 'u'
165 #define SHOW_st_gid 'g'
166 #define SHOW_st_rdev 'r'
167 #define SHOW_st_atime 'a'
168 #define SHOW_st_mtime 'm'
169 #define SHOW_st_ctime 'c'
170 #define SHOW_st_btime 'B'
171 #define SHOW_st_size 'z'
172 #define SHOW_st_blocks 'b'
173 #define SHOW_st_blksize 'k'
174 #define SHOW_st_flags 'f'
175 #define SHOW_st_gen 'v'
176 #define SHOW_symlink 'Y'
177 #define SHOW_filetype 'T'
178 #define SHOW_filename 'N'
179 #define SHOW_sizerdev 'Z'
181 void usage(const char *);
182 void output(const struct stat *, const char *,
183 const char *, int, int, int);
184 int format1(const struct stat *, /* stat info */
185 const char *, /* the file name */
186 const char *, int, /* the format string itself */
187 char *, size_t, /* a place to put the output */
188 int, int, int, int, /* the parsed format */
194 #define addchar(s, c, nl) \
196 (void)fputc((c), (s)); \
197 (*nl) = ((c) == '\n'); \
198 } while (0/*CONSTCOND*/)
201 main(int argc, char *argv[])
204 int ch, rc, errs, am_readlink;
205 int lsF, fmtchar, usestat, fn, nonl, quiet;
206 char *statfmt, *options, *synopsis;
218 if (strcmp(getprogname(), "readlink") == 0) {
221 synopsis = "[-n] [file ...]";
226 options = "f:FlLnqrst:x";
227 synopsis = "[-FlLnqrsx] [-f format] [-t timefmt] [file ...]";
230 while ((ch = getopt(argc, argv, options)) != -1)
252 errx(1, "can't use format '%c' with '%c'",
267 if (fmtchar == '\0') {
272 statfmt = DEF_FORMAT;
276 if (lsF && fmtchar != 'l')
277 errx(1, "can't use format '%c' with -F", fmtchar);
281 /* statfmt already set */
284 statfmt = lsF ? LSF_FORMAT : LS_FORMAT;
287 statfmt = RAW_FORMAT;
290 statfmt = SHELL_FORMAT;
293 statfmt = LINUX_FORMAT;
303 timefmt = TIME_FORMAT;
308 rc = fstat(STDIN_FILENO, &st);
310 rc = stat(argv[0], &st);
312 rc = lstat(argv[0], &st);
319 argc == 0 ? "(stdin)" : argv[0]);
322 output(&st, argv[0], statfmt, fn, nonl, quiet);
329 return (am_readlink ? linkfail : errs);
333 usage(const char *synopsis)
336 (void)fprintf(stderr, "usage: %s %s\n", getprogname(), synopsis);
341 * Parses a format string.
344 output(const struct stat *st, const char *file,
345 const char *statfmt, int fn, int nonl, int quiet)
347 int flags, size, prec, ofmt, hilo, what;
353 while (*statfmt != '\0') {
356 * Non-format characters go straight out.
358 if (*statfmt != FMT_MAGIC) {
359 addchar(stdout, *statfmt, &nl);
365 * The current format "substring" starts here,
366 * and then we skip the magic.
372 * Some simple one-character "formats".
376 addchar(stdout, '\n', &nl);
380 addchar(stdout, '\t', &nl);
384 addchar(stdout, '%', &nl);
387 case SIMPLE_NUMBER: {
390 snprintf(num, sizeof(num), "%d", fn);
391 for (p = &num[0]; *p; p++)
392 addchar(stdout, *p, &nl);
399 * This must be an actual format string. Format strings are
400 * similar to printf(3) formats up to a point, and are of
403 * % required start of format
404 * [-# +0] opt. format characters
405 * size opt. field width
406 * . opt. decimal separator, followed by
407 * prec opt. precision
408 * fmt opt. output specifier (string, numeric, etc.)
409 * sub opt. sub field specifier (high, middle, low)
410 * datum required field specifier (size, mode, etc)
412 * Only the % and the datum selector are required. All data
413 * have reasonable default output forms. The "sub" specifier
414 * only applies to certain data (mode, dev, rdev, filetype).
415 * The symlink output defaults to STRING, yet will only emit
416 * the leading " -> " if STRING is explicitly specified. The
417 * sizerdev datum will generate rdev output for character or
418 * block devices, and size output for all others.
422 if (*statfmt == FMT_POUND)
424 else if (*statfmt == FMT_SPACE)
426 else if (*statfmt == FMT_PLUS)
428 else if (*statfmt == FMT_ZERO)
430 else if (*statfmt == FMT_MINUS)
435 } while (1/*CONSTCOND*/);
438 if (isdigit((unsigned)*statfmt)) {
440 while (isdigit((unsigned)*statfmt)) {
441 size = (size * 10) + (*statfmt - '0');
449 if (*statfmt == FMT_DOT) {
453 while (isdigit((unsigned)*statfmt)) {
454 prec = (prec * 10) + (*statfmt - '0');
461 #define fmtcase(x, y) case (y): (x) = (y); statfmt++; break
462 #define fmtcasef(x, y, z) case (y): (x) = (z); statfmt++; break
464 fmtcasef(ofmt, FMT_DECIMAL, FMTF_DECIMAL);
465 fmtcasef(ofmt, FMT_OCTAL, FMTF_OCTAL);
466 fmtcasef(ofmt, FMT_UNSIGNED, FMTF_UNSIGNED);
467 fmtcasef(ofmt, FMT_HEX, FMTF_HEX);
468 fmtcasef(ofmt, FMT_FLOAT, FMTF_FLOAT);
469 fmtcasef(ofmt, FMT_STRING, FMTF_STRING);
476 fmtcase(hilo, HIGH_PIECE);
477 fmtcase(hilo, MIDDLE_PIECE);
478 fmtcase(hilo, LOW_PIECE);
485 fmtcase(what, SHOW_st_dev);
486 fmtcase(what, SHOW_st_ino);
487 fmtcase(what, SHOW_st_mode);
488 fmtcase(what, SHOW_st_nlink);
489 fmtcase(what, SHOW_st_uid);
490 fmtcase(what, SHOW_st_gid);
491 fmtcase(what, SHOW_st_rdev);
492 fmtcase(what, SHOW_st_atime);
493 fmtcase(what, SHOW_st_mtime);
494 fmtcase(what, SHOW_st_ctime);
495 fmtcase(what, SHOW_st_btime);
496 fmtcase(what, SHOW_st_size);
497 fmtcase(what, SHOW_st_blocks);
498 fmtcase(what, SHOW_st_blksize);
499 fmtcase(what, SHOW_st_flags);
500 fmtcase(what, SHOW_st_gen);
501 fmtcase(what, SHOW_symlink);
502 fmtcase(what, SHOW_filetype);
503 fmtcase(what, SHOW_filename);
504 fmtcase(what, SHOW_sizerdev);
513 subfmt, statfmt - subfmt,
515 flags, size, prec, ofmt, hilo, what);
517 for (i = 0; i < t && i < sizeof(buf); i++)
518 addchar(stdout, buf[i], &nl);
523 errx(1, "%.*s: bad format",
524 (int)(statfmt - subfmt + 1), subfmt);
528 (void)fputc('\n', stdout);
529 (void)fflush(stdout);
533 * Arranges output according to a single parsed format substring.
536 format1(const struct stat *st,
538 const char *fmt, int flen,
539 char *buf, size_t blen,
540 int flags, int size, int prec, int ofmt,
544 char *sdata, lfmt[24], tmp[20];
545 char smode[12], sid[12], path[PATH_MAX + 4];
548 const struct timespec *tsp;
551 int l, small, formats;
558 * First, pick out the data and tweak it based on hilo or
559 * specified output format (symlink output only).
564 small = (sizeof(st->st_dev) == 4);
565 data = (what == SHOW_st_dev) ? st->st_dev : st->st_rdev;
567 sdata = (what == SHOW_st_dev) ?
568 devname(st->st_dev, S_IFBLK) :
570 S_ISCHR(st->st_mode) ? S_IFCHR :
571 S_ISBLK(st->st_mode) ? S_IFBLK :
575 #endif /* HAVE_DEVNAME */
576 if (hilo == HIGH_PIECE) {
580 else if (hilo == LOW_PIECE) {
581 data = minor((unsigned)data);
584 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
587 #else /* HAVE_DEVNAME */
589 #endif /* HAVE_DEVNAME */
591 ofmt = FMTF_UNSIGNED;
594 small = (sizeof(st->st_ino) == 4);
597 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
599 ofmt = FMTF_UNSIGNED;
602 small = (sizeof(st->st_mode) == 4);
604 strmode(st->st_mode, smode);
607 if (sdata[l - 1] == ' ')
609 if (hilo == HIGH_PIECE) {
615 else if (hilo == MIDDLE_PIECE) {
616 data = (data >> 9) & 07;
621 else if (hilo == LOW_PIECE) {
627 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
633 small = (sizeof(st->st_dev) == 4);
636 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
638 ofmt = FMTF_UNSIGNED;
641 small = (sizeof(st->st_uid) == 4);
643 if ((pw = getpwuid(st->st_uid)) != NULL)
646 snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_uid);
649 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
652 ofmt = FMTF_UNSIGNED;
655 small = (sizeof(st->st_gid) == 4);
657 if ((gr = getgrgid(st->st_gid)) != NULL)
660 snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_gid);
663 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
666 ofmt = FMTF_UNSIGNED;
669 tsp = &st->st_atimespec;
673 tsp = &st->st_mtimespec;
677 tsp = &st->st_ctimespec;
679 #if HAVE_STRUCT_STAT_ST_BIRTHTIME
682 tsp = &st->st_birthtimespec;
683 #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
684 ts = *tsp; /* copy so we can muck with it */
685 small = (sizeof(ts.tv_sec) == 4);
688 tm = localtime(&ts.tv_sec);
689 (void)strftime(path, sizeof(path), timefmt, tm);
691 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
692 FMTF_FLOAT | FMTF_STRING;
697 small = (sizeof(st->st_size) == 4);
700 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
702 ofmt = FMTF_UNSIGNED;
705 small = (sizeof(st->st_blocks) == 4);
706 data = st->st_blocks;
708 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
710 ofmt = FMTF_UNSIGNED;
712 case SHOW_st_blksize:
713 small = (sizeof(st->st_blksize) == 4);
714 data = st->st_blksize;
716 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
718 ofmt = FMTF_UNSIGNED;
720 #if HAVE_STRUCT_STAT_ST_FLAGS
722 small = (sizeof(st->st_flags) == 4);
725 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
727 ofmt = FMTF_UNSIGNED;
729 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
730 #if HAVE_STRUCT_STAT_ST_GEN
732 small = (sizeof(st->st_gen) == 4);
735 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
737 ofmt = FMTF_UNSIGNED;
739 #endif /* HAVE_STRUCT_STAT_ST_GEN */
743 if (S_ISLNK(st->st_mode)) {
744 snprintf(path, sizeof(path), " -> ");
745 l = readlink(file, path + 4, sizeof(path) - 4 - 1);
752 sdata = path + (ofmt == FMTF_STRING ? 0 : 4);
758 formats = FMTF_STRING;
767 if (hilo == 0 || hilo == LOW_PIECE) {
768 switch (st->st_mode & S_IFMT) {
769 case S_IFIFO: (void)strcat(sdata, "|"); break;
770 case S_IFDIR: (void)strcat(sdata, "/"); break;
773 (S_IXUSR | S_IXGRP | S_IXOTH))
774 (void)strcat(sdata, "*");
776 case S_IFLNK: (void)strcat(sdata, "@"); break;
777 case S_IFSOCK: (void)strcat(sdata, "="); break;
779 case S_IFWHT: (void)strcat(sdata, "%"); break;
782 case S_IFDOOR: (void)strcat(sdata, ">"); break;
783 #endif /* S_IFDOOR */
787 else if (hilo == HIGH_PIECE) {
788 switch (st->st_mode & S_IFMT) {
789 case S_IFIFO: sdata = "Fifo File"; break;
790 case S_IFCHR: sdata = "Character Device"; break;
791 case S_IFDIR: sdata = "Directory"; break;
792 case S_IFBLK: sdata = "Block Device"; break;
793 case S_IFREG: sdata = "Regular File"; break;
794 case S_IFLNK: sdata = "Symbolic Link"; break;
795 case S_IFSOCK: sdata = "Socket"; break;
797 case S_IFWHT: sdata = "Whiteout File"; break;
800 case S_IFDOOR: sdata = "Door"; break;
801 #endif /* S_IFDOOR */
802 default: sdata = "???"; break;
806 formats = FMTF_STRING;
814 (void)strncpy(path, "(stdin)", sizeof(path));
816 (void)strncpy(path, file, sizeof(path));
818 formats = FMTF_STRING;
823 if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
824 char majdev[20], mindev[20];
830 majdev, sizeof(majdev),
832 ofmt, HIGH_PIECE, SHOW_st_rdev);
836 mindev, sizeof(mindev),
838 ofmt, LOW_PIECE, SHOW_st_rdev);
839 return (snprintf(buf, blen, "%.*s,%.*s",
840 l1, majdev, l2, mindev));
848 ofmt, 0, SHOW_st_size));
852 errx(1, "%.*s: bad format", (int)flen, fmt);
856 * If a subdatum was specified but not supported, or an output
857 * format was selected that is not supported, that's an error.
859 if (hilo != 0 || (ofmt & formats) == 0)
860 errx(1, "%.*s: bad format", (int)flen, fmt);
863 * Assemble the format string for passing to printf(3).
866 (void)strcat(lfmt, "%");
867 if (flags & FLAG_POUND)
868 (void)strcat(lfmt, "#");
869 if (flags & FLAG_SPACE)
870 (void)strcat(lfmt, " ");
871 if (flags & FLAG_PLUS)
872 (void)strcat(lfmt, "+");
873 if (flags & FLAG_MINUS)
874 (void)strcat(lfmt, "-");
875 if (flags & FLAG_ZERO)
876 (void)strcat(lfmt, "0");
879 * Only the timespecs support the FLOAT output format, and that
880 * requires work that differs from the other formats.
882 if (ofmt == FMTF_FLOAT) {
884 * Nothing after the decimal point, so just print seconds.
888 (void)snprintf(tmp, sizeof(tmp), "%d", size);
889 (void)strcat(lfmt, tmp);
891 (void)strcat(lfmt, "d");
892 return (snprintf(buf, blen, lfmt, ts.tv_sec));
896 * Unspecified precision gets all the precision we have:
903 * Adjust the size for the decimal point and the digits
909 * Any leftover size that's legitimate will be used.
912 (void)snprintf(tmp, sizeof(tmp), "%d", size);
913 (void)strcat(lfmt, tmp);
915 (void)strcat(lfmt, "d");
918 * The stuff after the decimal point always needs zero
921 (void)strcat(lfmt, ".%0");
924 * We can "print" at most nine digits of precision. The
925 * rest we will pad on at the end.
927 (void)snprintf(tmp, sizeof(tmp), "%dd", prec > 9 ? 9 : prec);
928 (void)strcat(lfmt, tmp);
931 * For precision of less that nine digits, trim off the
932 * less significant figures.
934 for (; prec < 9; prec++)
938 * Use the format, and then tack on any zeroes that
939 * might be required to make up the requested precision.
941 l = snprintf(buf, blen, lfmt, ts.tv_sec, ts.tv_nsec);
942 for (; prec > 9 && l < blen; prec--, l++)
943 (void)strcat(buf, "0");
948 * Add on size and precision, if specified, to the format.
951 (void)snprintf(tmp, sizeof(tmp), "%d", size);
952 (void)strcat(lfmt, tmp);
955 (void)snprintf(tmp, sizeof(tmp), ".%d", prec);
956 (void)strcat(lfmt, tmp);
960 * String output uses the temporary sdata.
962 if (ofmt == FMTF_STRING) {
964 errx(1, "%.*s: bad format", (int)flen, fmt);
965 (void)strcat(lfmt, "s");
966 return (snprintf(buf, blen, lfmt, sdata));
970 * Ensure that sign extension does not cause bad looking output
973 if (small && ofmt != FMTF_DECIMAL)
974 data = (u_int32_t)data;
977 * The four "numeric" output forms.
979 (void)strcat(lfmt, "ll");
981 case FMTF_DECIMAL: (void)strcat(lfmt, "d"); break;
982 case FMTF_OCTAL: (void)strcat(lfmt, "o"); break;
983 case FMTF_UNSIGNED: (void)strcat(lfmt, "u"); break;
984 case FMTF_HEX: (void)strcat(lfmt, "x"); break;
987 return (snprintf(buf, blen, lfmt, data));