1 /* $NetBSD: stat.c,v 1.6 2002/07/09 21:25:00 atatat Exp $ */
4 * Copyright (c) 2002 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
41 __RCSID("$NetBSD: stat.c,v 1.8 2002/08/13 20:15:06 atatat Exp $");
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
48 #include <sys/types.h>
63 "%d %i %Sp %l %Su %Sg %r %z \"%Sa\" \"%Sm\" \"%Sc\" %k %b %N"
64 #define RAW_FORMAT "%d %i %#p %l %u %g %r %z %a %m %c %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_atimespec=%a st_mtimespec=%m st_ctimespec=%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;
634 ts = *tsp; /* copy so we can muck with it */
635 small = (sizeof(ts.tv_sec) == 4);
638 tm = localtime(&ts.tv_sec);
639 (void)strftime(path, sizeof(path), timefmt, tm);
641 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
642 FMTF_FLOAT | FMTF_STRING;
647 small = (sizeof(st->st_size) == 4);
650 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
652 ofmt = FMTF_UNSIGNED;
655 small = (sizeof(st->st_blocks) == 4);
656 data = st->st_blocks;
658 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
660 ofmt = FMTF_UNSIGNED;
662 case SHOW_st_blksize:
663 small = (sizeof(st->st_blksize) == 4);
664 data = st->st_blksize;
666 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
668 ofmt = FMTF_UNSIGNED;
671 small = (sizeof(st->st_flags) == 4);
674 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
676 ofmt = FMTF_UNSIGNED;
679 small = (sizeof(st->st_gen) == 4);
682 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
684 ofmt = FMTF_UNSIGNED;
689 if (S_ISLNK(st->st_mode)) {
690 snprintf(path, sizeof(path), " -> ");
691 l = readlink(file, path + 4, sizeof(path) - 4);
698 sdata = path + (ofmt == FMTF_STRING ? 0 : 4);
704 formats = FMTF_STRING;
713 if (hilo == 0 || hilo == LOW_PIECE) {
714 switch (st->st_mode & S_IFMT) {
715 case S_IFIFO: (void)strcat(sdata, "|"); break;
716 case S_IFDIR: (void)strcat(sdata, "/"); break;
719 (S_IXUSR | S_IXGRP | S_IXOTH))
720 (void)strcat(sdata, "*");
722 case S_IFLNK: (void)strcat(sdata, "@"); break;
723 case S_IFSOCK: (void)strcat(sdata, "="); break;
724 case S_IFWHT: (void)strcat(sdata, "%"); break;
728 else if (hilo == HIGH_PIECE) {
729 switch (st->st_mode & S_IFMT) {
730 case S_IFIFO: sdata = "Fifo File"; break;
731 case S_IFCHR: sdata = "Character Device"; break;
732 case S_IFDIR: sdata = "Directory"; break;
733 case S_IFBLK: sdata = "Block Device"; break;
734 case S_IFREG: sdata = "Regular File"; break;
735 case S_IFLNK: sdata = "Symbolic Link"; break;
736 case S_IFSOCK: sdata = "Socket"; break;
737 case S_IFWHT: sdata = "Whiteout File"; break;
738 default: sdata = "???"; break;
742 formats = FMTF_STRING;
750 (void)strncpy(path, "(stdin)", sizeof(path));
752 (void)strncpy(path, file, sizeof(path));
754 formats = FMTF_STRING;
759 if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
760 char majdev[20], mindev[20];
766 majdev, sizeof(majdev),
768 ofmt, HIGH_PIECE, SHOW_st_rdev);
772 mindev, sizeof(mindev),
774 ofmt, LOW_PIECE, SHOW_st_rdev);
775 return (snprintf(buf, blen, "%.*s,%.*s",
776 l1, majdev, l2, mindev));
784 ofmt, 0, SHOW_st_size));
788 errx(1, "%.*s: bad format", (int)flen, fmt);
792 * If a subdatum was specified but not supported, or an output
793 * format was selected that is not supported, that's an error.
795 if (hilo != 0 || (ofmt & formats) == 0)
796 errx(1, "%.*s: bad format", (int)flen, fmt);
799 * Assemble the format string for passing to printf(3).
802 (void)strcat(lfmt, "%");
803 if (flags & FLAG_POUND)
804 (void)strcat(lfmt, "#");
805 if (flags & FLAG_SPACE)
806 (void)strcat(lfmt, " ");
807 if (flags & FLAG_PLUS)
808 (void)strcat(lfmt, "+");
809 if (flags & FLAG_MINUS)
810 (void)strcat(lfmt, "-");
811 if (flags & FLAG_ZERO)
812 (void)strcat(lfmt, "0");
815 * Only the timespecs support the FLOAT output format, and that
816 * requires work that differs from the other formats.
818 if (ofmt == FMTF_FLOAT) {
820 * Nothing after the decimal point, so just print seconds.
824 (void)snprintf(tmp, sizeof(tmp), "%d", size);
825 (void)strcat(lfmt, tmp);
827 (void)strcat(lfmt, "d");
828 return (snprintf(buf, blen, lfmt, ts.tv_sec));
832 * Unspecified precision gets all the precision we have:
839 * Adjust the size for the decimal point and the digits
845 * Any leftover size that's legitimate will be used.
848 (void)snprintf(tmp, sizeof(tmp), "%d", size);
849 (void)strcat(lfmt, tmp);
851 (void)strcat(lfmt, "d");
854 * The stuff after the decimal point always needs zero
857 (void)strcat(lfmt, ".%0");
860 * We can "print" at most nine digits of precision. The
861 * rest we will pad on at the end.
863 (void)snprintf(tmp, sizeof(tmp), "%dd", prec > 9 ? 9 : prec);
864 (void)strcat(lfmt, tmp);
867 * For precision of less that nine digits, trim off the
868 * less significant figures.
870 for (; prec < 9; prec++)
874 * Use the format, and then tack on any zeroes that
875 * might be required to make up the requested precision.
877 l = snprintf(buf, blen, lfmt, ts.tv_sec, ts.tv_nsec);
878 for (; prec > 9 && l < blen; prec--, l++)
879 (void)strcat(buf, "0");
884 * Add on size and precision, if specified, to the format.
887 (void)snprintf(tmp, sizeof(tmp), "%d", size);
888 (void)strcat(lfmt, tmp);
891 (void)snprintf(tmp, sizeof(tmp), ".%d", prec);
892 (void)strcat(lfmt, tmp);
896 * String output uses the temporary sdata.
898 if (ofmt == FMTF_STRING) {
900 errx(1, "%.*s: bad format", (int)flen, fmt);
901 (void)strcat(lfmt, "s");
902 return (snprintf(buf, blen, lfmt, sdata));
906 * Ensure that sign extension does not cause bad looking output
909 if (small && ofmt != FMTF_DECIMAL)
910 data = (u_int32_t)data;
913 * The four "numeric" output forms.
915 (void)strcat(lfmt, "ll");
917 case FMTF_DECIMAL: (void)strcat(lfmt, "d"); break;
918 case FMTF_OCTAL: (void)strcat(lfmt, "o"); break;
919 case FMTF_UNSIGNED: (void)strcat(lfmt, "u"); break;
920 case FMTF_HEX: (void)strcat(lfmt, "x"); break;
923 return (snprintf(buf, blen, lfmt, data));