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 /* $DragonFly: src/usr.bin/stat/stat.c,v 1.1 2003/07/28 20:47:10 rob Exp $
39 #include <sys/cdefs.h>
42 __RCSID("$NetBSD: stat.c,v 1.10 2003/05/08 13:05:38 atatat Exp $");
46 __FBSDID("$FreeBSD: src/usr.bin/stat/stat.c,v 1.5 2003/05/11 23:02:09 dougb Exp $");
48 #include <sys/types.h>
63 "%d %i %Sp %l %Su %Sg %r %z \"%Sa\" \"%Sm\" \"%Sc\" \"%SB\" %k %b %N"
64 #define RAW_FORMAT "%d %i %#p %l %u %g %r %z %a %m %c %B %k %b %N"
65 #define LS_FORMAT "%Sp %l %Su %Sg %Z %Sm %N%SY"
66 #define LSF_FORMAT "%Sp %l %Su %Sg %Z %Sm %N%T%SY"
67 #define SHELL_FORMAT \
68 "st_dev=%d st_ino=%i st_mode=%#p st_nlink=%l " \
69 "st_uid=%u st_gid=%g st_rdev=%r st_size=%z " \
70 "st_atime=%a st_mtime=%m st_ctime=%c " \
71 "st_blksize=%k st_blocks=%b"
72 #define LINUX_FORMAT \
74 " Size: %-11z FileType: %HT%n" \
75 " Mode: (%04OLp/%.10Sp) Uid: (%5u/%8Su) Gid: (%5g/%8Sg)%n" \
76 "Device: %Hd,%Ld Inode: %i Links: %l%n" \
81 #define TIME_FORMAT "%b %e %T %Y"
83 #define FLAG_POUND 0x01
84 #define FLAG_SPACE 0x02
85 #define FLAG_PLUS 0x04
86 #define FLAG_ZERO 0x08
87 #define FLAG_MINUS 0x10
90 * These format characters must all be unique, except the magic one.
95 #define SIMPLE_NEWLINE 'n'
96 #define SIMPLE_TAB 't'
97 #define SIMPLE_PERCENT '%'
98 #define SIMPLE_NUMBER '@'
100 #define FMT_POUND '#'
101 #define FMT_SPACE ' '
104 #define FMT_MINUS '-'
106 #define FMT_DECIMAL 'D'
107 #define FMT_OCTAL 'O'
108 #define FMT_UNSIGNED 'U'
110 #define FMT_FLOAT 'F'
111 #define FMT_STRING 'S'
113 #define FMTF_DECIMAL 0x01
114 #define FMTF_OCTAL 0x02
115 #define FMTF_UNSIGNED 0x04
116 #define FMTF_HEX 0x08
117 #define FMTF_FLOAT 0x10
118 #define FMTF_STRING 0x20
120 #define HIGH_PIECE 'H'
121 #define MIDDLE_PIECE 'M'
122 #define LOW_PIECE 'L'
124 #define SHOW_st_dev 'd'
125 #define SHOW_st_ino 'i'
126 #define SHOW_st_mode 'p'
127 #define SHOW_st_nlink 'l'
128 #define SHOW_st_uid 'u'
129 #define SHOW_st_gid 'g'
130 #define SHOW_st_rdev 'r'
131 #define SHOW_st_atime 'a'
132 #define SHOW_st_mtime 'm'
133 #define SHOW_st_ctime 'c'
134 #define SHOW_st_size 'z'
135 #define SHOW_st_blocks 'b'
136 #define SHOW_st_blksize 'k'
137 #define SHOW_st_flags 'f'
138 #define SHOW_st_gen 'v'
139 #define SHOW_symlink 'Y'
140 #define SHOW_filetype 'T'
141 #define SHOW_filename 'N'
142 #define SHOW_sizerdev 'Z'
144 void usage(const char *);
145 void output(const struct stat *, const char *,
146 const char *, int, int, int);
147 int format1(const struct stat *, /* stat info */
148 const char *, /* the file name */
149 const char *, int, /* the format string itself */
150 char *, size_t, /* a place to put the output */
151 int, int, int, int, /* the parsed format */
157 #define addchar(s, c, nl) \
159 (void)fputc((c), (s)); \
160 (*nl) = ((c) == '\n'); \
161 } while (0/*CONSTCOND*/)
164 main(int argc, char *argv[])
167 int ch, rc, errs, am_readlink;
168 int lsF, fmtchar, usestat, fn, nonl, quiet;
169 char *statfmt, *options, *synopsis;
181 if (strcmp(getprogname(), "readlink") == 0) {
184 synopsis = "[-n] [file ...]";
189 options = "f:FlLnqrst:x";
190 synopsis = "[-FlLnqrsx] [-f format] [-t timefmt] [file ...]";
193 while ((ch = getopt(argc, argv, options)) != -1)
215 errx(1, "can't use format '%c' with '%c'",
230 if (fmtchar == '\0') {
235 statfmt = DEF_FORMAT;
239 if (lsF && fmtchar != 'l')
240 errx(1, "can't use format '%c' with -F", fmtchar);
244 /* statfmt already set */
247 statfmt = lsF ? LSF_FORMAT : LS_FORMAT;
250 statfmt = RAW_FORMAT;
253 statfmt = SHELL_FORMAT;
256 statfmt = LINUX_FORMAT;
266 timefmt = TIME_FORMAT;
271 rc = fstat(STDIN_FILENO, &st);
273 rc = stat(argv[0], &st);
275 rc = lstat(argv[0], &st);
282 argc == 0 ? "(stdin)" : argv[0]);
285 output(&st, argv[0], statfmt, fn, nonl, quiet);
292 return (am_readlink ? linkfail : errs);
296 usage(const char *synopsis)
299 (void)fprintf(stderr, "usage: %s %s\n", getprogname(), synopsis);
304 * Parses a format string.
307 output(const struct stat *st, const char *file,
308 const char *statfmt, int fn, int nonl, int quiet)
310 int flags, size, prec, ofmt, hilo, what;
316 while (*statfmt != '\0') {
319 * Non-format characters go straight out.
321 if (*statfmt != FMT_MAGIC) {
322 addchar(stdout, *statfmt, &nl);
328 * The current format "substring" starts here,
329 * and then we skip the magic.
335 * Some simple one-character "formats".
339 addchar(stdout, '\n', &nl);
343 addchar(stdout, '\t', &nl);
347 addchar(stdout, '%', &nl);
350 case SIMPLE_NUMBER: {
353 snprintf(num, sizeof(num), "%d", fn);
354 for (p = &num[0]; *p; p++)
355 addchar(stdout, *p, &nl);
362 * This must be an actual format string. Format strings are
363 * similar to printf(3) formats up to a point, and are of
366 * % required start of format
367 * [-# +0] opt. format characters
368 * size opt. field width
369 * . opt. decimal separator, followed by
370 * prec opt. precision
371 * fmt opt. output specifier (string, numeric, etc.)
372 * sub opt. sub field specifier (high, middle, low)
373 * datum required field specifier (size, mode, etc)
375 * Only the % and the datum selector are required. All data
376 * have reasonable default output forms. The "sub" specifier
377 * only applies to certain data (mode, dev, rdev, filetype).
378 * The symlink output defaults to STRING, yet will only emit
379 * the leading " -> " if STRING is explicitly specified. The
380 * sizerdev datum will generate rdev output for character or
381 * block devices, and size output for all others.
385 if (*statfmt == FMT_POUND)
387 else if (*statfmt == FMT_SPACE)
389 else if (*statfmt == FMT_PLUS)
391 else if (*statfmt == FMT_ZERO)
393 else if (*statfmt == FMT_MINUS)
398 } while (1/*CONSTCOND*/);
401 if (isdigit((unsigned)*statfmt)) {
403 while (isdigit((unsigned)*statfmt)) {
404 size = (size * 10) + (*statfmt - '0');
412 if (*statfmt == FMT_DOT) {
416 while (isdigit((unsigned)*statfmt)) {
417 prec = (prec * 10) + (*statfmt - '0');
424 #define fmtcase(x, y) case (y): (x) = (y); statfmt++; break
425 #define fmtcasef(x, y, z) case (y): (x) = (z); statfmt++; break
427 fmtcasef(ofmt, FMT_DECIMAL, FMTF_DECIMAL);
428 fmtcasef(ofmt, FMT_OCTAL, FMTF_OCTAL);
429 fmtcasef(ofmt, FMT_UNSIGNED, FMTF_UNSIGNED);
430 fmtcasef(ofmt, FMT_HEX, FMTF_HEX);
431 fmtcasef(ofmt, FMT_FLOAT, FMTF_FLOAT);
432 fmtcasef(ofmt, FMT_STRING, FMTF_STRING);
439 fmtcase(hilo, HIGH_PIECE);
440 fmtcase(hilo, MIDDLE_PIECE);
441 fmtcase(hilo, LOW_PIECE);
448 fmtcase(what, SHOW_st_dev);
449 fmtcase(what, SHOW_st_ino);
450 fmtcase(what, SHOW_st_mode);
451 fmtcase(what, SHOW_st_nlink);
452 fmtcase(what, SHOW_st_uid);
453 fmtcase(what, SHOW_st_gid);
454 fmtcase(what, SHOW_st_rdev);
455 fmtcase(what, SHOW_st_atime);
456 fmtcase(what, SHOW_st_mtime);
457 fmtcase(what, SHOW_st_ctime);
458 fmtcase(what, SHOW_st_size);
459 fmtcase(what, SHOW_st_blocks);
460 fmtcase(what, SHOW_st_blksize);
461 fmtcase(what, SHOW_st_flags);
462 fmtcase(what, SHOW_st_gen);
463 fmtcase(what, SHOW_symlink);
464 fmtcase(what, SHOW_filetype);
465 fmtcase(what, SHOW_filename);
466 fmtcase(what, SHOW_sizerdev);
475 subfmt, statfmt - subfmt,
477 flags, size, prec, ofmt, hilo, what);
479 for (i = 0; i < t && i < sizeof(buf); i++)
480 addchar(stdout, buf[i], &nl);
485 errx(1, "%.*s: bad format",
486 (int)(statfmt - subfmt + 1), subfmt);
490 (void)fputc('\n', stdout);
491 (void)fflush(stdout);
495 * Arranges output according to a single parsed format substring.
498 format1(const struct stat *st,
500 const char *fmt, int flen,
501 char *buf, size_t blen,
502 int flags, int size, int prec, int ofmt,
506 char *sdata, lfmt[24], tmp[20];
507 char smode[12], sid[12], path[PATH_MAX + 4];
510 const struct timespec *tsp;
513 int l, small, formats;
520 * First, pick out the data and tweak it based on hilo or
521 * specified output format (symlink output only).
526 small = (sizeof(st->st_dev) == 4);
527 data = (what == SHOW_st_dev) ? st->st_dev : st->st_rdev;
528 sdata = (what == SHOW_st_dev) ?
529 devname(st->st_dev, S_IFBLK) :
531 S_ISCHR(st->st_mode) ? S_IFCHR :
532 S_ISBLK(st->st_mode) ? S_IFBLK :
536 if (hilo == HIGH_PIECE) {
540 else if (hilo == LOW_PIECE) {
541 data = minor((unsigned)data);
544 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
547 ofmt = FMTF_UNSIGNED;
550 small = (sizeof(st->st_ino) == 4);
553 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
555 ofmt = FMTF_UNSIGNED;
558 small = (sizeof(st->st_mode) == 4);
560 strmode(st->st_mode, smode);
563 if (sdata[l - 1] == ' ')
565 if (hilo == HIGH_PIECE) {
571 else if (hilo == MIDDLE_PIECE) {
572 data = (data >> 9) & 07;
577 else if (hilo == LOW_PIECE) {
583 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
589 small = (sizeof(st->st_dev) == 4);
592 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
594 ofmt = FMTF_UNSIGNED;
597 small = (sizeof(st->st_uid) == 4);
599 if ((pw = getpwuid(st->st_uid)) != NULL)
602 snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_uid);
605 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
608 ofmt = FMTF_UNSIGNED;
611 small = (sizeof(st->st_gid) == 4);
613 if ((gr = getgrgid(st->st_gid)) != NULL)
616 snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_gid);
619 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
622 ofmt = FMTF_UNSIGNED;
625 tsp = &st->st_atimespec;
629 tsp = &st->st_mtimespec;
633 tsp = &st->st_ctimespec;
636 small = (sizeof(st->st_size) == 4);
639 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
641 ofmt = FMTF_UNSIGNED;
644 small = (sizeof(st->st_blocks) == 4);
645 data = st->st_blocks;
647 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
649 ofmt = FMTF_UNSIGNED;
651 case SHOW_st_blksize:
652 small = (sizeof(st->st_blksize) == 4);
653 data = st->st_blksize;
655 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
657 ofmt = FMTF_UNSIGNED;
660 small = (sizeof(st->st_flags) == 4);
663 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
665 ofmt = FMTF_UNSIGNED;
668 small = (sizeof(st->st_gen) == 4);
671 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
673 ofmt = FMTF_UNSIGNED;
678 if (S_ISLNK(st->st_mode)) {
679 snprintf(path, sizeof(path), " -> ");
680 l = readlink(file, path + 4, sizeof(path) - 4 - 1);
687 sdata = path + (ofmt == FMTF_STRING ? 0 : 4);
693 formats = FMTF_STRING;
702 if (hilo == 0 || hilo == LOW_PIECE) {
703 switch (st->st_mode & S_IFMT) {
704 case S_IFIFO: (void)strcat(sdata, "|"); break;
705 case S_IFDIR: (void)strcat(sdata, "/"); break;
708 (S_IXUSR | S_IXGRP | S_IXOTH))
709 (void)strcat(sdata, "*");
711 case S_IFLNK: (void)strcat(sdata, "@"); break;
712 case S_IFSOCK: (void)strcat(sdata, "="); break;
713 case S_IFWHT: (void)strcat(sdata, "%"); break;
717 else if (hilo == HIGH_PIECE) {
718 switch (st->st_mode & S_IFMT) {
719 case S_IFIFO: sdata = "Fifo File"; break;
720 case S_IFCHR: sdata = "Character Device"; break;
721 case S_IFDIR: sdata = "Directory"; break;
722 case S_IFBLK: sdata = "Block Device"; break;
723 case S_IFREG: sdata = "Regular File"; break;
724 case S_IFLNK: sdata = "Symbolic Link"; break;
725 case S_IFSOCK: sdata = "Socket"; break;
726 case S_IFWHT: sdata = "Whiteout File"; break;
727 default: sdata = "???"; break;
731 formats = FMTF_STRING;
739 (void)strncpy(path, "(stdin)", sizeof(path));
741 (void)strncpy(path, file, sizeof(path));
743 formats = FMTF_STRING;
748 if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
749 char majdev[20], mindev[20];
755 majdev, sizeof(majdev),
757 ofmt, HIGH_PIECE, SHOW_st_rdev);
761 mindev, sizeof(mindev),
763 ofmt, LOW_PIECE, SHOW_st_rdev);
764 return (snprintf(buf, blen, "%.*s,%.*s",
765 l1, majdev, l2, mindev));
773 ofmt, 0, SHOW_st_size));
777 errx(1, "%.*s: bad format", (int)flen, fmt);
781 * If a subdatum was specified but not supported, or an output
782 * format was selected that is not supported, that's an error.
784 if (hilo != 0 || (ofmt & formats) == 0)
785 errx(1, "%.*s: bad format", (int)flen, fmt);
788 * Assemble the format string for passing to printf(3).
791 (void)strcat(lfmt, "%");
792 if (flags & FLAG_POUND)
793 (void)strcat(lfmt, "#");
794 if (flags & FLAG_SPACE)
795 (void)strcat(lfmt, " ");
796 if (flags & FLAG_PLUS)
797 (void)strcat(lfmt, "+");
798 if (flags & FLAG_MINUS)
799 (void)strcat(lfmt, "-");
800 if (flags & FLAG_ZERO)
801 (void)strcat(lfmt, "0");
804 * Only the timespecs support the FLOAT output format, and that
805 * requires work that differs from the other formats.
807 if (ofmt == FMTF_FLOAT) {
809 * Nothing after the decimal point, so just print seconds.
813 (void)snprintf(tmp, sizeof(tmp), "%d", size);
814 (void)strcat(lfmt, tmp);
816 (void)strcat(lfmt, "d");
817 return (snprintf(buf, blen, lfmt, ts.tv_sec));
821 * Unspecified precision gets all the precision we have:
828 * Adjust the size for the decimal point and the digits
834 * Any leftover size that's legitimate will be used.
837 (void)snprintf(tmp, sizeof(tmp), "%d", size);
838 (void)strcat(lfmt, tmp);
840 (void)strcat(lfmt, "d");
843 * The stuff after the decimal point always needs zero
846 (void)strcat(lfmt, ".%0");
849 * We can "print" at most nine digits of precision. The
850 * rest we will pad on at the end.
852 (void)snprintf(tmp, sizeof(tmp), "%dd", prec > 9 ? 9 : prec);
853 (void)strcat(lfmt, tmp);
856 * For precision of less that nine digits, trim off the
857 * less significant figures.
859 for (; prec < 9; prec++)
863 * Use the format, and then tack on any zeroes that
864 * might be required to make up the requested precision.
866 l = snprintf(buf, blen, lfmt, ts.tv_sec, ts.tv_nsec);
867 for (; prec > 9 && l < blen; prec--, l++)
868 (void)strcat(buf, "0");
873 * Add on size and precision, if specified, to the format.
876 (void)snprintf(tmp, sizeof(tmp), "%d", size);
877 (void)strcat(lfmt, tmp);
880 (void)snprintf(tmp, sizeof(tmp), ".%d", prec);
881 (void)strcat(lfmt, tmp);
885 * String output uses the temporary sdata.
887 if (ofmt == FMTF_STRING) {
889 errx(1, "%.*s: bad format", (int)flen, fmt);
890 (void)strcat(lfmt, "s");
891 return (snprintf(buf, blen, lfmt, sdata));
895 * Ensure that sign extension does not cause bad looking output
898 if (small && ofmt != FMTF_DECIMAL)
899 data = (u_int32_t)data;
902 * The four "numeric" output forms.
904 (void)strcat(lfmt, "ll");
906 case FMTF_DECIMAL: (void)strcat(lfmt, "d"); break;
907 case FMTF_OCTAL: (void)strcat(lfmt, "o"); break;
908 case FMTF_UNSIGNED: (void)strcat(lfmt, "u"); break;
909 case FMTF_HEX: (void)strcat(lfmt, "x"); break;
912 return (snprintf(buf, blen, lfmt, data));