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.10 2003/05/08 13:05:38 atatat Exp $
37 * $FreeBSD: src/usr.bin/stat/stat.c,v 1.5 2003/05/11 23:02:09 dougb Exp $
38 * $DragonFly: src/usr.bin/stat/stat.c,v 1.5 2004/07/09 19:13:40 drhodus Exp $
41 #include <sys/cdefs.h>
42 #include <sys/types.h>
57 "%d %i %Sp %l %Su %Sg %r %z %a %m %c %k %b %N"
58 #define RAW_FORMAT "%d %i %#p %l %u %g %r %z %a %m %c %k %b %N"
59 #define LS_FORMAT "%Sp %l %Su %Sg %Z %m %N%SY"
60 #define LSF_FORMAT "%Sp %l %Su %Sg %Z %m %N%T%SY"
61 #define SHELL_FORMAT \
62 "st_dev=%d st_ino=%i st_mode=%#p st_nlink=%l " \
63 "st_uid=%u st_gid=%g st_rdev=%r st_size=%z " \
64 "st_atime=%a st_mtime=%m st_ctime=%c " \
65 "st_blksize=%k st_blocks=%b"
66 #define LINUX_FORMAT \
68 " Size: %-11z FileType: %HT%n" \
69 " Mode: (%04OLp/%.10Sp) Uid: (%5u/%8Su) Gid: (%5g/%8Sg)%n" \
70 "Device: %Hd,%Ld Inode: %i Links: %l%n" \
75 #define TIME_FORMAT "%b %e %T %Y"
77 #define FLAG_POUND 0x01
78 #define FLAG_SPACE 0x02
79 #define FLAG_PLUS 0x04
80 #define FLAG_ZERO 0x08
81 #define FLAG_MINUS 0x10
84 * These format characters must all be unique, except the magic one.
89 #define SIMPLE_NEWLINE 'n'
90 #define SIMPLE_TAB 't'
91 #define SIMPLE_PERCENT '%'
92 #define SIMPLE_NUMBER '@'
100 #define FMT_DECIMAL 'D'
101 #define FMT_OCTAL 'O'
102 #define FMT_UNSIGNED 'U'
104 #define FMT_FLOAT 'F'
105 #define FMT_STRING 'S'
107 #define FMTF_DECIMAL 0x01
108 #define FMTF_OCTAL 0x02
109 #define FMTF_UNSIGNED 0x04
110 #define FMTF_HEX 0x08
111 #define FMTF_FLOAT 0x10
112 #define FMTF_STRING 0x20
114 #define HIGH_PIECE 'H'
115 #define MIDDLE_PIECE 'M'
116 #define LOW_PIECE 'L'
118 #define SHOW_st_dev 'd'
119 #define SHOW_st_ino 'i'
120 #define SHOW_st_mode 'p'
121 #define SHOW_st_nlink 'l'
122 #define SHOW_st_uid 'u'
123 #define SHOW_st_gid 'g'
124 #define SHOW_st_rdev 'r'
125 #define SHOW_st_atime 'a'
126 #define SHOW_st_mtime 'm'
127 #define SHOW_st_ctime 'c'
128 #define SHOW_st_size 'z'
129 #define SHOW_st_blocks 'b'
130 #define SHOW_st_blksize 'k'
131 #define SHOW_st_flags 'f'
132 #define SHOW_st_gen 'v'
133 #define SHOW_symlink 'Y'
134 #define SHOW_filetype 'T'
135 #define SHOW_filename 'N'
136 #define SHOW_sizerdev 'Z'
138 void usage(const char *);
139 void output(const struct stat *, const char *,
140 const char *, int, int, int);
141 int format1(const struct stat *, /* stat info */
142 const char *, /* the file name */
143 const char *, int, /* the format string itself */
144 char *, size_t, /* a place to put the output */
145 int, int, int, int, /* the parsed format */
151 #define addchar(s, c, nl) \
153 (void)fputc((c), (s)); \
154 (*nl) = ((c) == '\n'); \
155 } while (0/*CONSTCOND*/)
158 main(int argc, char *argv[])
161 int ch, rc, errs, am_readlink;
162 int lsF, fmtchar, usestat, fn, nonl, quiet;
163 char *statfmt, *options, *synopsis;
175 if (strcmp(getprogname(), "readlink") == 0) {
178 synopsis = "[-n] [file ...]";
183 options = "f:FlLnqrst:x";
184 synopsis = "[-FlLnqrsx] [-f format] [-t timefmt] [file ...]";
187 while ((ch = getopt(argc, argv, options)) != -1)
209 errx(1, "can't use format '%c' with '%c'",
224 if (fmtchar == '\0') {
229 statfmt = DEF_FORMAT;
233 if (lsF && fmtchar != 'l')
234 errx(1, "can't use format '%c' with -F", fmtchar);
238 /* statfmt already set */
241 statfmt = lsF ? LSF_FORMAT : LS_FORMAT;
244 statfmt = RAW_FORMAT;
247 statfmt = SHELL_FORMAT;
250 statfmt = LINUX_FORMAT;
260 timefmt = TIME_FORMAT;
265 rc = fstat(STDIN_FILENO, &st);
267 rc = stat(argv[0], &st);
269 rc = lstat(argv[0], &st);
276 argc == 0 ? "(stdin)" : argv[0]);
279 output(&st, argv[0], statfmt, fn, nonl, quiet);
286 return (am_readlink ? linkfail : errs);
290 usage(const char *synopsis)
293 (void)fprintf(stderr, "usage: %s %s\n", getprogname(), synopsis);
298 * Parses a format string.
301 output(const struct stat *st, const char *file,
302 const char *statfmt, int fn, int nonl, int quiet)
304 int flags, size, prec, ofmt, hilo, what;
310 while (*statfmt != '\0') {
313 * Non-format characters go straight out.
315 if (*statfmt != FMT_MAGIC) {
316 addchar(stdout, *statfmt, &nl);
322 * The current format "substring" starts here,
323 * and then we skip the magic.
329 * Some simple one-character "formats".
333 addchar(stdout, '\n', &nl);
337 addchar(stdout, '\t', &nl);
341 addchar(stdout, '%', &nl);
344 case SIMPLE_NUMBER: {
347 snprintf(num, sizeof(num), "%d", fn);
348 for (p = &num[0]; *p; p++)
349 addchar(stdout, *p, &nl);
356 * This must be an actual format string. Format strings are
357 * similar to printf(3) formats up to a point, and are of
360 * % required start of format
361 * [-# +0] opt. format characters
362 * size opt. field width
363 * . opt. decimal separator, followed by
364 * prec opt. precision
365 * fmt opt. output specifier (string, numeric, etc.)
366 * sub opt. sub field specifier (high, middle, low)
367 * datum required field specifier (size, mode, etc)
369 * Only the % and the datum selector are required. All data
370 * have reasonable default output forms. The "sub" specifier
371 * only applies to certain data (mode, dev, rdev, filetype).
372 * The symlink output defaults to STRING, yet will only emit
373 * the leading " -> " if STRING is explicitly specified. The
374 * sizerdev datum will generate rdev output for character or
375 * block devices, and size output for all others.
379 if (*statfmt == FMT_POUND)
381 else if (*statfmt == FMT_SPACE)
383 else if (*statfmt == FMT_PLUS)
385 else if (*statfmt == FMT_ZERO)
387 else if (*statfmt == FMT_MINUS)
392 } while (1/*CONSTCOND*/);
395 if (isdigit((unsigned)*statfmt)) {
397 while (isdigit((unsigned)*statfmt)) {
398 size = (size * 10) + (*statfmt - '0');
406 if (*statfmt == FMT_DOT) {
410 while (isdigit((unsigned)*statfmt)) {
411 prec = (prec * 10) + (*statfmt - '0');
418 #define fmtcase(x, y) case (y): (x) = (y); statfmt++; break
419 #define fmtcasef(x, y, z) case (y): (x) = (z); statfmt++; break
421 fmtcasef(ofmt, FMT_DECIMAL, FMTF_DECIMAL);
422 fmtcasef(ofmt, FMT_OCTAL, FMTF_OCTAL);
423 fmtcasef(ofmt, FMT_UNSIGNED, FMTF_UNSIGNED);
424 fmtcasef(ofmt, FMT_HEX, FMTF_HEX);
425 fmtcasef(ofmt, FMT_FLOAT, FMTF_FLOAT);
426 fmtcasef(ofmt, FMT_STRING, FMTF_STRING);
433 fmtcase(hilo, HIGH_PIECE);
434 fmtcase(hilo, MIDDLE_PIECE);
435 fmtcase(hilo, LOW_PIECE);
442 fmtcase(what, SHOW_st_dev);
443 fmtcase(what, SHOW_st_ino);
444 fmtcase(what, SHOW_st_mode);
445 fmtcase(what, SHOW_st_nlink);
446 fmtcase(what, SHOW_st_uid);
447 fmtcase(what, SHOW_st_gid);
448 fmtcase(what, SHOW_st_rdev);
449 fmtcase(what, SHOW_st_atime);
450 fmtcase(what, SHOW_st_mtime);
451 fmtcase(what, SHOW_st_ctime);
452 fmtcase(what, SHOW_st_size);
453 fmtcase(what, SHOW_st_blocks);
454 fmtcase(what, SHOW_st_blksize);
455 fmtcase(what, SHOW_st_flags);
456 fmtcase(what, SHOW_st_gen);
457 fmtcase(what, SHOW_symlink);
458 fmtcase(what, SHOW_filetype);
459 fmtcase(what, SHOW_filename);
460 fmtcase(what, SHOW_sizerdev);
469 subfmt, statfmt - subfmt,
471 flags, size, prec, ofmt, hilo, what);
473 for (i = 0; i < t && i < sizeof(buf); i++)
474 addchar(stdout, buf[i], &nl);
479 errx(1, "%.*s: bad format",
480 (int)(statfmt - subfmt + 1), subfmt);
484 (void)fputc('\n', stdout);
485 (void)fflush(stdout);
489 * Arranges output according to a single parsed format substring.
492 format1(const struct stat *st,
494 const char *fmt, int flen,
495 char *buf, size_t blen,
496 int flags, int size, int prec, int ofmt,
500 char *sdata, lfmt[24], tmp[20];
501 char smode[12], sid[12], path[PATH_MAX + 4];
504 const struct timespec *tsp;
506 int l, small, formats;
513 * First, pick out the data and tweak it based on hilo or
514 * specified output format (symlink output only).
519 small = (sizeof(st->st_dev) == 4);
520 data = (what == SHOW_st_dev) ? st->st_dev : st->st_rdev;
521 sdata = (what == SHOW_st_dev) ?
522 devname(st->st_dev, S_IFBLK) :
524 S_ISCHR(st->st_mode) ? S_IFCHR :
525 S_ISBLK(st->st_mode) ? S_IFBLK :
529 if (hilo == HIGH_PIECE) {
533 else if (hilo == LOW_PIECE) {
534 data = minor((unsigned)data);
537 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
540 ofmt = FMTF_UNSIGNED;
543 small = (sizeof(st->st_ino) == 4);
546 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
548 ofmt = FMTF_UNSIGNED;
551 small = (sizeof(st->st_mode) == 4);
553 strmode(st->st_mode, smode);
556 if (sdata[l - 1] == ' ')
558 if (hilo == HIGH_PIECE) {
564 else if (hilo == MIDDLE_PIECE) {
565 data = (data >> 9) & 07;
570 else if (hilo == LOW_PIECE) {
576 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
582 small = (sizeof(st->st_dev) == 4);
585 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
587 ofmt = FMTF_UNSIGNED;
590 small = (sizeof(st->st_uid) == 4);
592 if ((pw = getpwuid(st->st_uid)) != NULL)
595 snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_uid);
598 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
601 ofmt = FMTF_UNSIGNED;
604 small = (sizeof(st->st_gid) == 4);
606 if ((gr = getgrgid(st->st_gid)) != NULL)
609 snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_gid);
612 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
615 ofmt = FMTF_UNSIGNED;
618 tsp = &st->st_atimespec;
622 tsp = &st->st_mtimespec;
626 tsp = &st->st_ctimespec;
629 small = (sizeof(st->st_size) == 4);
632 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
634 ofmt = FMTF_UNSIGNED;
637 small = (sizeof(st->st_blocks) == 4);
638 data = st->st_blocks;
640 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
642 ofmt = FMTF_UNSIGNED;
644 case SHOW_st_blksize:
645 small = (sizeof(st->st_blksize) == 4);
646 data = st->st_blksize;
648 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
650 ofmt = FMTF_UNSIGNED;
653 small = (sizeof(st->st_flags) == 4);
656 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
658 ofmt = FMTF_UNSIGNED;
661 small = (sizeof(st->st_gen) == 4);
664 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
666 ofmt = FMTF_UNSIGNED;
671 if (S_ISLNK(st->st_mode)) {
672 snprintf(path, sizeof(path), " -> ");
673 l = readlink(file, path + 4, sizeof(path) - 4 - 1);
680 sdata = path + (ofmt == FMTF_STRING ? 0 : 4);
686 formats = FMTF_STRING;
695 if (hilo == 0 || hilo == LOW_PIECE) {
696 switch (st->st_mode & S_IFMT) {
697 case S_IFIFO: (void)strcat(sdata, "|"); break;
698 case S_IFDIR: (void)strcat(sdata, "/"); break;
701 (S_IXUSR | S_IXGRP | S_IXOTH))
702 (void)strcat(sdata, "*");
704 case S_IFLNK: (void)strcat(sdata, "@"); break;
705 case S_IFSOCK: (void)strcat(sdata, "="); break;
706 case S_IFWHT: (void)strcat(sdata, "%"); break;
710 else if (hilo == HIGH_PIECE) {
711 switch (st->st_mode & S_IFMT) {
712 case S_IFIFO: sdata = "Fifo File"; break;
713 case S_IFCHR: sdata = "Character Device"; break;
714 case S_IFDIR: sdata = "Directory"; break;
715 case S_IFBLK: sdata = "Block Device"; break;
716 case S_IFREG: sdata = "Regular File"; break;
717 case S_IFLNK: sdata = "Symbolic Link"; break;
718 case S_IFSOCK: sdata = "Socket"; break;
719 case S_IFWHT: sdata = "Whiteout File"; break;
720 default: sdata = "???"; break;
724 formats = FMTF_STRING;
732 (void)strncpy(path, "(stdin)", sizeof(path));
734 (void)strncpy(path, file, sizeof(path));
736 formats = FMTF_STRING;
741 if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
742 char majdev[20], mindev[20];
748 majdev, sizeof(majdev),
750 ofmt, HIGH_PIECE, SHOW_st_rdev);
754 mindev, sizeof(mindev),
756 ofmt, LOW_PIECE, SHOW_st_rdev);
757 return (snprintf(buf, blen, "%.*s,%.*s",
758 l1, majdev, l2, mindev));
766 ofmt, 0, SHOW_st_size));
770 errx(1, "%.*s: bad format", (int)flen, fmt);
774 * If a subdatum was specified but not supported, or an output
775 * format was selected that is not supported, that's an error.
777 if (hilo != 0 || (ofmt & formats) == 0)
778 errx(1, "%.*s: bad format", (int)flen, fmt);
781 * Assemble the format string for passing to printf(3).
784 (void)strcat(lfmt, "%");
785 if (flags & FLAG_POUND)
786 (void)strcat(lfmt, "#");
787 if (flags & FLAG_SPACE)
788 (void)strcat(lfmt, " ");
789 if (flags & FLAG_PLUS)
790 (void)strcat(lfmt, "+");
791 if (flags & FLAG_MINUS)
792 (void)strcat(lfmt, "-");
793 if (flags & FLAG_ZERO)
794 (void)strcat(lfmt, "0");
797 * Only the timespecs support the FLOAT output format, and that
798 * requires work that differs from the other formats.
800 if (ofmt == FMTF_FLOAT) {
802 * Nothing after the decimal point, so just print seconds.
806 (void)snprintf(tmp, sizeof(tmp), "%d", size);
807 (void)strcat(lfmt, tmp);
809 (void)strcat(lfmt, "d");
810 return (snprintf(buf, blen, lfmt, ts.tv_sec));
814 * Unspecified precision gets all the precision we have:
821 * Adjust the size for the decimal point and the digits
827 * Any leftover size that's legitimate will be used.
830 (void)snprintf(tmp, sizeof(tmp), "%d", size);
831 (void)strcat(lfmt, tmp);
833 (void)strcat(lfmt, "d");
836 * The stuff after the decimal point always needs zero
839 (void)strcat(lfmt, ".%0");
842 * We can "print" at most nine digits of precision. The
843 * rest we will pad on at the end.
845 (void)snprintf(tmp, sizeof(tmp), "%dd", prec > 9 ? 9 : prec);
846 (void)strcat(lfmt, tmp);
849 * For precision of less that nine digits, trim off the
850 * less significant figures.
852 for (; prec < 9; prec++)
856 * Use the format, and then tack on any zeroes that
857 * might be required to make up the requested precision.
859 l = snprintf(buf, blen, lfmt, ts.tv_sec, ts.tv_nsec);
860 for (; prec > 9 && l < blen; prec--, l++)
861 (void)strcat(buf, "0");
866 * Add on size and precision, if specified, to the format.
869 (void)snprintf(tmp, sizeof(tmp), "%d", size);
870 (void)strcat(lfmt, tmp);
873 (void)snprintf(tmp, sizeof(tmp), ".%d", prec);
874 (void)strcat(lfmt, tmp);
878 * String output uses the temporary sdata.
880 if (ofmt == FMTF_STRING) {
882 errx(1, "%.*s: bad format", (int)flen, fmt);
883 (void)strcat(lfmt, "s");
884 return (snprintf(buf, blen, lfmt, sdata));
888 * Ensure that sign extension does not cause bad looking output
891 if (small && ofmt != FMTF_DECIMAL)
892 data = (u_int32_t)data;
895 * The four "numeric" output forms.
897 (void)strcat(lfmt, "ll");
899 case FMTF_DECIMAL: (void)strcat(lfmt, "d"); break;
900 case FMTF_OCTAL: (void)strcat(lfmt, "o"); break;
901 case FMTF_UNSIGNED: (void)strcat(lfmt, "u"); break;
902 case FMTF_HEX: (void)strcat(lfmt, "x"); break;
905 return (snprintf(buf, blen, lfmt, data));