mandoc: update to 1.14.5
[unleashed.git] / bin / stat / stat.c
blob0e190b4aa125a43313322d3d2d37ba085883cc43
1 /* $NetBSD: stat.c,v 1.43 2017/09/21 22:53:19 kre Exp $ */
3 /*
4 * Copyright (c) 2002-2011 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Brown.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/types.h>
33 #include <sys/stat.h>
35 #include <sys/mkdev.h>
36 #include <ctype.h>
37 #include <err.h>
38 #include <errno.h>
39 #include <grp.h>
40 #include <limits.h>
41 #include <pwd.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <time.h>
46 #include <unistd.h>
48 #define DEF_F
49 #define RAW_F
50 #define SHELL_F
52 #define DEF_B
53 #define RAW_B
54 #define SHELL_B
55 #define LINUX_B
57 #define st_atimespec st_atim
58 #define st_ctimespec st_ctim
59 #define st_mtimespec st_mtim
61 #define st_atimensec st_atim.tv_nsec
62 #define st_mtimensec st_mtim.tv_nsec
63 #define st_ctimensec st_ctim.tv_nsec
65 #define DEF_FORMAT \
66 "%d %i %Sp %l %Su %Sg %r %z \"%Sa\" \"%Sm\" \"%Sc\" " DEF_B \
67 "%k %b " DEF_F "%N"
68 #define RAW_FORMAT "%d %i %#p %l %u %g %r %z %a %m %c " RAW_B \
69 "%k %b " RAW_F "%N"
70 #define LS_FORMAT "%Sp %l %Su %Sg %Z %Sm %N%SY"
71 #define LSF_FORMAT "%Sp %l %Su %Sg %Z %Sm %N%T%SY"
72 #define SHELL_FORMAT \
73 "st_dev=%d st_ino=%i st_mode=%#p st_nlink=%l " \
74 "st_uid=%u st_gid=%g st_rdev=%r st_size=%z " \
75 "st_atime=%Sa st_mtime=%Sm st_ctime=%Sc " SHELL_B \
76 "st_blksize=%k st_blocks=%b" SHELL_F
77 #define LINUX_FORMAT \
78 " File: \"%N\"%n" \
79 " Size: %-11z Blocks: %-11b IO Block: %-11k %HT%n" \
80 "Device: %Hd,%Ld Inode: %i Links: %l%n" \
81 " Mode: (%Mp%03OLp/%.10Sp) Uid: (%5u/%8Su) Gid: (%5g/%8Sg)%n" \
82 "Access: %Sa%n" \
83 "Modify: %Sm%n" \
84 "Change: %Sc" \
85 LINUX_B
87 #define TIME_FORMAT "%b %e %T %Y"
89 #define FLAG_POUND 0x01
90 #define FLAG_SPACE 0x02
91 #define FLAG_PLUS 0x04
92 #define FLAG_ZERO 0x08
93 #define FLAG_MINUS 0x10
96 * These format characters must all be unique, except the magic one.
98 #define FMT_MAGIC '%'
99 #define FMT_DOT '.'
101 #define SIMPLE_NEWLINE 'n'
102 #define SIMPLE_TAB 't'
103 #define SIMPLE_PERCENT '%'
104 #define SIMPLE_NUMBER '@'
106 #define FMT_POUND '#'
107 #define FMT_SPACE ' '
108 #define FMT_PLUS '+'
109 #define FMT_ZERO '0'
110 #define FMT_MINUS '-'
112 #define FMT_DECIMAL 'D'
113 #define FMT_OCTAL 'O'
114 #define FMT_UNSIGNED 'U'
115 #define FMT_HEX 'X'
116 #define FMT_FLOAT 'F'
117 #define FMT_STRING 'S'
119 #define FMTF_DECIMAL 0x01
120 #define FMTF_OCTAL 0x02
121 #define FMTF_UNSIGNED 0x04
122 #define FMTF_HEX 0x08
123 #define FMTF_FLOAT 0x10
124 #define FMTF_STRING 0x20
126 #define HIGH_PIECE 'H'
127 #define MIDDLE_PIECE 'M'
128 #define LOW_PIECE 'L'
130 #define SHOW_realpath 'R'
131 #define SHOW_st_dev 'd'
132 #define SHOW_st_ino 'i'
133 #define SHOW_st_mode 'p'
134 #define SHOW_st_nlink 'l'
135 #define SHOW_st_uid 'u'
136 #define SHOW_st_gid 'g'
137 #define SHOW_st_rdev 'r'
138 #define SHOW_st_atime 'a'
139 #define SHOW_st_mtime 'm'
140 #define SHOW_st_ctime 'c'
141 #define SHOW_st_btime 'B'
142 #define SHOW_st_size 'z'
143 #define SHOW_st_blocks 'b'
144 #define SHOW_st_blksize 'k'
145 #define SHOW_symlink 'Y'
146 #define SHOW_filetype 'T'
147 #define SHOW_filename 'N'
148 #define SHOW_sizerdev 'Z'
150 static void usage(const char *);
151 static void output(const struct stat *, const char *,
152 const char *, int, int, int);
153 static int format1(const struct stat *, /* stat info */
154 const char *, /* the file name */
155 const char *, int, /* the format string itself */
156 char *, size_t, /* a place to put the output */
157 int, int, int, int, /* the parsed format */
158 int, int, int);
160 static const char *timefmt;
161 static int linkfail;
163 #define addchar(s, c, nl) \
164 do { \
165 (void)fputc((c), (s)); \
166 (*nl) = ((c) == '\n'); \
167 } while (0/*CONSTCOND*/)
170 main(int argc, char *argv[])
172 struct stat st;
173 int ch, rc, errs, am_readlink;
174 int lsF, fmtchar, usestat, fn, nonl, quiet;
175 const char *statfmt, *options, *synopsis;
177 am_readlink = 0;
178 lsF = 0;
179 fmtchar = '\0';
180 usestat = 0;
181 nonl = 0;
182 quiet = 0;
183 linkfail = 0;
184 statfmt = NULL;
185 timefmt = NULL;
187 setprogname(argv[0]);
189 if (strcmp(getprogname(), "readlink") == 0) {
190 am_readlink = 1;
191 options = "fnqsv";
192 synopsis = "[-fnqsv] [file ...]";
193 statfmt = "%Y";
194 fmtchar = 'f';
195 quiet = 1;
196 } else {
197 options = "f:FlLnqrst:x";
198 synopsis = "[-FlLnqrsx] [-f format] [-t timefmt] [file ...]";
201 while ((ch = getopt(argc, argv, options)) != -1)
202 switch (ch) {
203 case 'F':
204 lsF = 1;
205 break;
206 case 'L':
207 usestat = 1;
208 break;
209 case 'n':
210 nonl = 1;
211 break;
212 case 'q':
213 quiet = 1;
214 break;
215 case 'f':
216 if (am_readlink) {
217 statfmt = "%R";
218 break;
220 statfmt = optarg;
221 /* FALLTHROUGH */
222 case 'l':
223 case 'r':
224 case 's':
225 if (am_readlink) {
226 quiet = 1;
227 break;
229 /*FALLTHROUGH*/
230 case 'x':
231 if (fmtchar != 0)
232 errx(1, "can't use format '%c' with '%c'",
233 fmtchar, ch);
234 fmtchar = ch;
235 break;
236 case 't':
237 timefmt = optarg;
238 break;
239 case 'v':
240 quiet = 0;
241 break;
242 default:
243 usage(synopsis);
246 argc -= optind;
247 argv += optind;
248 fn = 1;
250 if (fmtchar == '\0') {
251 if (lsF)
252 fmtchar = 'l';
253 else {
254 fmtchar = 'f';
255 statfmt = DEF_FORMAT;
259 if (lsF && fmtchar != 'l')
260 errx(1, "can't use format '%c' with -F", fmtchar);
262 switch (fmtchar) {
263 case 'f':
264 /* statfmt already set */
265 break;
266 case 'l':
267 statfmt = lsF ? LSF_FORMAT : LS_FORMAT;
268 break;
269 case 'r':
270 statfmt = RAW_FORMAT;
271 break;
272 case 's':
273 statfmt = SHELL_FORMAT;
274 if (timefmt == NULL)
275 timefmt = "%s";
276 break;
277 case 'x':
278 statfmt = LINUX_FORMAT;
279 if (timefmt == NULL)
280 timefmt = "%Y-%m-%d %H:%M:%S.%f %z";
281 break;
282 default:
283 usage(synopsis);
284 /*NOTREACHED*/
287 if (timefmt == NULL)
288 timefmt = TIME_FORMAT;
290 errs = 0;
291 do {
292 if (argc == 0)
293 rc = fstat(STDIN_FILENO, &st);
294 else if (usestat) {
296 * Try stat() and if it fails, fall back to
297 * lstat() just in case we're examining a
298 * broken symlink.
300 if ((rc = stat(argv[0], &st)) == -1 &&
301 errno == ENOENT &&
302 (rc = lstat(argv[0], &st)) == -1)
303 errno = ENOENT;
305 else
306 rc = lstat(argv[0], &st);
308 if (rc == -1) {
309 errs = 1;
310 linkfail = 1;
311 if (!quiet)
312 warn("%s: %s",
313 argc == 0 ? "(stdin)" : argv[0],
314 usestat ? "stat" : "lstat");
316 else
317 output(&st, argv[0], statfmt, fn, nonl, quiet);
319 argv++;
320 argc--;
321 fn++;
322 } while (argc > 0);
324 return (am_readlink ? linkfail : errs);
327 static void
328 usage(const char *synopsis)
331 (void)fprintf(stderr, "usage: %s %s\n", getprogname(), synopsis);
332 exit(1);
334 void
335 strmode(/* mode_t */ int mode, char *p)
337 /* print type */
338 switch (mode & S_IFMT) {
339 case S_IFDIR: /* directory */
340 *p++ = 'd';
341 break;
342 case S_IFCHR: /* character special */
343 *p++ = 'c';
344 break;
345 case S_IFBLK: /* block special */
346 *p++ = 'b';
347 break;
348 case S_IFREG: /* regular */
349 *p++ = '-';
350 break;
351 case S_IFLNK: /* symbolic link */
352 *p++ = 'l';
353 break;
354 case S_IFSOCK: /* socket */
355 *p++ = 's';
356 break;
357 #ifdef S_IFIFO
358 case S_IFIFO: /* fifo */
359 *p++ = 'p';
360 break;
361 #endif
362 #ifdef S_IFWHT
363 case S_IFWHT: /* whiteout */
364 *p++ = 'w';
365 break;
366 #endif
367 #ifdef S_IFDOOR
368 case S_IFDOOR: /* door */
369 *p++ = 'D';
370 break;
371 #endif
372 default: /* unknown */
373 *p++ = '?';
374 break;
376 /* usr */
377 if (mode & S_IRUSR)
378 *p++ = 'r';
379 else
380 *p++ = '-';
381 if (mode & S_IWUSR)
382 *p++ = 'w';
383 else
384 *p++ = '-';
385 switch (mode & (S_IXUSR | S_ISUID)) {
386 case 0:
387 *p++ = '-';
388 break;
389 case S_IXUSR:
390 *p++ = 'x';
391 break;
392 case S_ISUID:
393 *p++ = 'S';
394 break;
395 case S_IXUSR | S_ISUID:
396 *p++ = 's';
397 break;
399 /* group */
400 if (mode & S_IRGRP)
401 *p++ = 'r';
402 else
403 *p++ = '-';
404 if (mode & S_IWGRP)
405 *p++ = 'w';
406 else
407 *p++ = '-';
408 switch (mode & (S_IXGRP | S_ISGID)) {
409 case 0:
410 *p++ = '-';
411 break;
412 case S_IXGRP:
413 *p++ = 'x';
414 break;
415 case S_ISGID:
416 *p++ = 'S';
417 break;
418 case S_IXGRP | S_ISGID:
419 *p++ = 's';
420 break;
422 /* other */
423 if (mode & S_IROTH)
424 *p++ = 'r';
425 else
426 *p++ = '-';
427 if (mode & S_IWOTH)
428 *p++ = 'w';
429 else
430 *p++ = '-';
431 switch (mode & (S_IXOTH | S_ISVTX)) {
432 case 0:
433 *p++ = '-';
434 break;
435 case S_IXOTH:
436 *p++ = 'x';
437 break;
438 case S_ISVTX:
439 *p++ = 'T';
440 break;
441 case S_IXOTH | S_ISVTX:
442 *p++ = 't';
443 break;
445 *p++ = ' ';
446 *p = '\0';
450 * Parses a format string.
452 static void
453 output(const struct stat *st, const char *file,
454 const char *statfmt, int fn, int nonl, int quiet)
456 int flags, size, prec, ofmt, hilo, what;
458 * buf size is enough for an item of length PATH_MAX,
459 * multiplied by 4 for vis encoding, plus 4 for symlink
460 * " -> " prefix, plus 1 for \0 terminator.
462 char buf[PATH_MAX * 4 + 4 + 1];
463 const char *subfmt;
464 int nl, t, i;
466 nl = 1;
467 while (*statfmt != '\0') {
470 * Non-format characters go straight out.
472 if (*statfmt != FMT_MAGIC) {
473 addchar(stdout, *statfmt, &nl);
474 statfmt++;
475 continue;
479 * The current format "substring" starts here,
480 * and then we skip the magic.
482 subfmt = statfmt;
483 statfmt++;
486 * Some simple one-character "formats".
488 switch (*statfmt) {
489 case SIMPLE_NEWLINE:
490 addchar(stdout, '\n', &nl);
491 statfmt++;
492 continue;
493 case SIMPLE_TAB:
494 addchar(stdout, '\t', &nl);
495 statfmt++;
496 continue;
497 case SIMPLE_PERCENT:
498 addchar(stdout, '%', &nl);
499 statfmt++;
500 continue;
501 case SIMPLE_NUMBER: {
502 char num[12], *p;
504 snprintf(num, sizeof(num), "%d", fn);
505 for (p = &num[0]; *p; p++)
506 addchar(stdout, *p, &nl);
507 statfmt++;
508 continue;
513 * This must be an actual format string. Format strings are
514 * similar to printf(3) formats up to a point, and are of
515 * the form:
517 * % required start of format
518 * [-# +0] opt. format characters
519 * size opt. field width
520 * . opt. decimal separator, followed by
521 * prec opt. precision
522 * fmt opt. output specifier (string, numeric, etc.)
523 * sub opt. sub field specifier (high, middle, low)
524 * datum required field specifier (size, mode, etc)
526 * Only the % and the datum selector are required. All data
527 * have reasonable default output forms. The "sub" specifier
528 * only applies to certain data (mode, dev, rdev, filetype).
529 * The symlink output defaults to STRING, yet will only emit
530 * the leading " -> " if STRING is explicitly specified. The
531 * sizerdev datum will generate rdev output for character or
532 * block devices, and size output for all others.
533 * For STRING output, the # format requests vis encoding.
535 flags = 0;
536 do {
537 if (*statfmt == FMT_POUND)
538 flags |= FLAG_POUND;
539 else if (*statfmt == FMT_SPACE)
540 flags |= FLAG_SPACE;
541 else if (*statfmt == FMT_PLUS)
542 flags |= FLAG_PLUS;
543 else if (*statfmt == FMT_ZERO)
544 flags |= FLAG_ZERO;
545 else if (*statfmt == FMT_MINUS)
546 flags |= FLAG_MINUS;
547 else
548 break;
549 statfmt++;
550 } while (1/*CONSTCOND*/);
552 size = -1;
553 if (isdigit((unsigned)*statfmt)) {
554 size = 0;
555 while (isdigit((unsigned)*statfmt)) {
556 size = (size * 10) + (*statfmt - '0');
557 statfmt++;
558 if (size < 0)
559 goto badfmt;
563 prec = -1;
564 if (*statfmt == FMT_DOT) {
565 statfmt++;
567 prec = 0;
568 while (isdigit((unsigned)*statfmt)) {
569 prec = (prec * 10) + (*statfmt - '0');
570 statfmt++;
571 if (prec < 0)
572 goto badfmt;
576 #define fmtcase(x, y) case (y): (x) = (y); statfmt++; break
577 #define fmtcasef(x, y, z) case (y): (x) = (z); statfmt++; break
578 switch (*statfmt) {
579 fmtcasef(ofmt, FMT_DECIMAL, FMTF_DECIMAL);
580 fmtcasef(ofmt, FMT_OCTAL, FMTF_OCTAL);
581 fmtcasef(ofmt, FMT_UNSIGNED, FMTF_UNSIGNED);
582 fmtcasef(ofmt, FMT_HEX, FMTF_HEX);
583 fmtcasef(ofmt, FMT_FLOAT, FMTF_FLOAT);
584 fmtcasef(ofmt, FMT_STRING, FMTF_STRING);
585 default:
586 ofmt = 0;
587 break;
590 switch (*statfmt) {
591 fmtcase(hilo, HIGH_PIECE);
592 fmtcase(hilo, MIDDLE_PIECE);
593 fmtcase(hilo, LOW_PIECE);
594 default:
595 hilo = 0;
596 break;
599 switch (*statfmt) {
600 fmtcase(what, SHOW_realpath);
601 fmtcase(what, SHOW_st_dev);
602 fmtcase(what, SHOW_st_ino);
603 fmtcase(what, SHOW_st_mode);
604 fmtcase(what, SHOW_st_nlink);
605 fmtcase(what, SHOW_st_uid);
606 fmtcase(what, SHOW_st_gid);
607 fmtcase(what, SHOW_st_rdev);
608 fmtcase(what, SHOW_st_atime);
609 fmtcase(what, SHOW_st_mtime);
610 fmtcase(what, SHOW_st_ctime);
611 fmtcase(what, SHOW_st_btime);
612 fmtcase(what, SHOW_st_size);
613 fmtcase(what, SHOW_st_blocks);
614 fmtcase(what, SHOW_st_blksize);
615 fmtcase(what, SHOW_symlink);
616 fmtcase(what, SHOW_filetype);
617 fmtcase(what, SHOW_filename);
618 fmtcase(what, SHOW_sizerdev);
619 default:
620 goto badfmt;
622 #undef fmtcasef
623 #undef fmtcase
625 t = format1(st,
626 file,
627 subfmt, statfmt - subfmt,
628 buf, sizeof(buf),
629 flags, size, prec, ofmt, hilo, what, quiet);
631 for (i = 0; i < t && i < (int)(sizeof(buf) - 1); i++)
632 addchar(stdout, buf[i], &nl);
634 continue;
636 badfmt:
637 errx(1, "%.*s: bad format",
638 (int)(statfmt - subfmt + 1), subfmt);
641 if (!nl && !nonl)
642 (void)fputc('\n', stdout);
643 (void)fflush(stdout);
646 static const char *
647 fmttime(char *buf, size_t len, const char *fmt, time_t secs, long nsecs)
649 struct tm tm;
650 const char *fpb, *fp1, *fp2; /* pointers into fmt */
651 char *fmt2 = NULL; /* replacement fmt (if not NULL) */
652 /* XXX init of next twp for stupid gcc only */
653 char *f2p = NULL; /* ptr into fmt2 - last added */
654 size_t flen = 0;
655 size_t o;
656 int sl;
658 if (localtime_r(&secs, &tm) == NULL) {
659 secs = 0;
660 (void)localtime_r(&secs, &tm);
662 for (fp1 = fpb = fmt; (fp2 = strchr(fp1, '%')) != NULL; ) {
663 if (fp2[1] != 'f') {
664 /* make sure we don't find the 2nd '%' in "%%" */
665 fp1 = fp2 + 1 + (fp2[1] != '\0');
666 continue;
668 if (fmt2 == NULL) {
669 /* allow for ~100 %f's in the format ... */
670 flen = strlen(fmt) + 1024;
672 if ((fmt2 = calloc(flen, 1)) == NULL) {
673 fp1 = fp2 + 2;
674 continue;
676 f2p = fmt2;
678 o = (size_t)(fp2 - fpb);
679 memcpy(f2p, fpb, o); /* must fit */
680 fmt = fmt2;
681 } else {
682 o = (size_t)(fp2 - fpb);
683 if (flen > o)
684 memcpy(f2p, fpb, o);
686 if (flen < o + 10) { /* 9 digits + \0 == 10 */
687 *f2p = '\0';
688 break;
690 f2p += o;
691 flen -= o;
692 sl = snprintf(f2p, flen, "%.9ld", nsecs);
693 if (sl == -1)
694 sl = 0;
695 f2p += sl;
696 *f2p = '\0';
697 flen -= sl;
698 fp1 = fp2 + 2;
699 fpb = fp1;
701 if (fmt2 != NULL) {
702 o = strlen(fpb);
703 if (flen > o) {
704 memcpy(f2p, fpb, o);
705 f2p[o] = '\0';
709 (void)strftime(buf, len, fmt, &tm);
710 free(fmt2);
711 return buf;
715 * Arranges output according to a single parsed format substring.
717 static int
718 format1(const struct stat *st,
719 const char *file,
720 const char *fmt, int flen,
721 char *buf, size_t blen,
722 int flags, int size, int prec, int ofmt,
723 int hilo, int what, int quiet)
725 uint64_t data;
726 char *stmp, lfmt[24], tmp[20];
727 const char *sdata;
728 char smode[12], sid[12], path[PATH_MAX + 4], visbuf[PATH_MAX * 4 + 4];
729 struct passwd *pw;
730 struct group *gr;
731 time_t secs;
732 long nsecs;
733 int l;
734 int formats; /* bitmap of allowed formats for this datum */
735 int small; /* true if datum is a small integer */
736 int gottime; /* true if secs and nsecs are valid */
737 int shift; /* powers of 2 to scale numbers before printing */
738 size_t prefixlen; /* length of constant prefix for string data */
740 formats = 0;
741 small = 0;
742 gottime = 0;
743 secs = 0;
744 nsecs = 0;
745 shift = 0;
746 prefixlen = 0;
749 * First, pick out the data and tweak it based on hilo or
750 * specified output format (symlink output only).
752 switch (what) {
753 case SHOW_st_dev:
754 case SHOW_st_rdev:
755 small = (sizeof(st->st_dev) == 4);
756 data = (what == SHOW_st_dev) ? st->st_dev : st->st_rdev;
757 if (hilo == HIGH_PIECE) {
758 data = major(data);
759 hilo = 0;
761 else if (hilo == LOW_PIECE) {
762 data = minor((unsigned)data);
763 hilo = 0;
765 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
767 if (ofmt == 0) {
768 if (data == (uint64_t)-1)
769 ofmt = FMTF_DECIMAL;
770 else
771 ofmt = FMTF_UNSIGNED;
773 break;
774 case SHOW_st_ino:
775 small = (sizeof(st->st_ino) == 4);
776 data = st->st_ino;
777 sdata = NULL;
778 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
779 if (ofmt == 0)
780 ofmt = FMTF_UNSIGNED;
781 break;
782 case SHOW_st_mode:
783 small = (sizeof(st->st_mode) == 4);
784 data = st->st_mode;
785 strmode(st->st_mode, smode);
786 stmp = smode;
787 l = strlen(stmp);
788 if (stmp[l - 1] == ' ')
789 stmp[--l] = '\0';
790 if (hilo == HIGH_PIECE) {
791 data >>= 12;
792 stmp += 1;
793 stmp[3] = '\0';
794 hilo = 0;
796 else if (hilo == MIDDLE_PIECE) {
797 data = (data >> 9) & 07;
798 stmp += 4;
799 stmp[3] = '\0';
800 hilo = 0;
802 else if (hilo == LOW_PIECE) {
803 data &= 0777;
804 stmp += 7;
805 stmp[3] = '\0';
806 hilo = 0;
808 sdata = stmp;
809 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
810 FMTF_STRING;
811 if (ofmt == 0)
812 ofmt = FMTF_OCTAL;
813 break;
814 case SHOW_st_nlink:
815 small = (sizeof(st->st_dev) == 4);
816 data = st->st_nlink;
817 sdata = NULL;
818 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
819 if (ofmt == 0)
820 ofmt = FMTF_UNSIGNED;
821 break;
822 case SHOW_st_uid:
823 small = (sizeof(st->st_uid) == 4);
824 data = st->st_uid;
825 if ((pw = getpwuid(st->st_uid)) != NULL)
826 sdata = pw->pw_name;
827 else {
828 snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_uid);
829 sdata = sid;
831 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
832 FMTF_STRING;
833 if (ofmt == 0)
834 ofmt = FMTF_UNSIGNED;
835 break;
836 case SHOW_st_gid:
837 small = (sizeof(st->st_gid) == 4);
838 data = st->st_gid;
839 if ((gr = getgrgid(st->st_gid)) != NULL)
840 sdata = gr->gr_name;
841 else {
842 snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_gid);
843 sdata = sid;
845 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
846 FMTF_STRING;
847 if (ofmt == 0)
848 ofmt = FMTF_UNSIGNED;
849 break;
850 case SHOW_st_atime:
851 gottime = 1;
852 secs = st->st_atime;
853 nsecs = st->st_atimensec;
854 /* FALLTHROUGH */
855 case SHOW_st_mtime:
856 if (!gottime) {
857 gottime = 1;
858 secs = st->st_mtime;
859 nsecs = st->st_mtimensec;
861 /* FALLTHROUGH */
862 case SHOW_st_ctime:
863 if (!gottime) {
864 gottime = 1;
865 secs = st->st_ctime;
866 nsecs = st->st_ctimensec;
868 /* FALLTHROUGH */
869 small = (sizeof(secs) == 4);
870 data = secs;
871 sdata = fmttime(path, sizeof(path), timefmt, secs, nsecs);
873 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
874 FMTF_FLOAT | FMTF_STRING;
875 if (ofmt == 0)
876 ofmt = FMTF_DECIMAL;
877 break;
878 case SHOW_st_size:
879 small = (sizeof(st->st_size) == 4);
880 data = st->st_size;
881 sdata = NULL;
882 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
883 if (ofmt == 0)
884 ofmt = FMTF_UNSIGNED;
885 switch (hilo) {
886 case HIGH_PIECE:
887 shift = 30; /* gigabytes */
888 hilo = 0;
889 break;
890 case MIDDLE_PIECE:
891 shift = 20; /* megabytes */
892 hilo = 0;
893 break;
894 case LOW_PIECE:
895 shift = 10; /* kilobytes */
896 hilo = 0;
897 break;
899 break;
900 case SHOW_st_blocks:
901 small = (sizeof(st->st_blocks) == 4);
902 data = st->st_blocks;
903 sdata = NULL;
904 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
905 if (ofmt == 0)
906 ofmt = FMTF_UNSIGNED;
907 break;
908 case SHOW_st_blksize:
909 small = (sizeof(st->st_blksize) == 4);
910 data = st->st_blksize;
911 sdata = NULL;
912 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
913 if (ofmt == 0)
914 ofmt = FMTF_UNSIGNED;
915 break;
916 case SHOW_realpath:
917 small = 0;
918 data = 0;
919 if (file == NULL) {
920 (void)strlcpy(path, "(stdin)", sizeof(path));
921 sdata = path;
922 } else {
923 snprintf(path, sizeof(path), " -> ");
924 if (realpath(file, path + 4) == NULL) {
925 if (!quiet)
926 warn("realpath `%s'", file);
927 linkfail = 1;
928 l = 0;
929 path[0] = '\0';
931 sdata = path + (ofmt == FMTF_STRING ? 0 : 4);
932 prefixlen = (ofmt == FMTF_STRING ? 4 : 0);
935 formats = FMTF_STRING;
936 if (ofmt == 0)
937 ofmt = FMTF_STRING;
938 break;
939 case SHOW_symlink:
940 small = 0;
941 data = 0;
942 if (S_ISLNK(st->st_mode)) {
943 snprintf(path, sizeof(path), " -> ");
944 l = readlink(file, path + 4, sizeof(path) - 4 - 1);
945 if (l == -1) {
946 if (!quiet)
947 warn("readlink `%s'", file);
948 linkfail = 1;
949 l = 0;
950 path[0] = '\0';
952 path[l + 4] = '\0';
953 sdata = path + (ofmt == FMTF_STRING ? 0 : 4);
954 prefixlen = (ofmt == FMTF_STRING ? 4 : 0);
956 else {
957 linkfail = 1;
958 sdata = "";
960 formats = FMTF_STRING;
961 if (ofmt == 0)
962 ofmt = FMTF_STRING;
963 break;
964 case SHOW_filetype:
965 small = 0;
966 data = 0;
967 sdata = "";
968 if (hilo == 0 || hilo == LOW_PIECE) {
969 switch (st->st_mode & S_IFMT) {
970 case S_IFIFO: sdata = "|"; break;
971 case S_IFDIR: sdata = "/"; break;
972 case S_IFREG:
973 if (st->st_mode &
974 (S_IXUSR | S_IXGRP | S_IXOTH))
975 sdata = "*";
976 break;
977 case S_IFLNK: sdata = "@"; break;
978 #ifdef S_IFSOCK
979 case S_IFSOCK: sdata = "="; break;
980 #endif
981 #ifdef S_IFWHT
982 case S_IFWHT: sdata = "%"; break;
983 #endif /* S_IFWHT */
984 #ifdef S_IFDOOR
985 case S_IFDOOR: sdata = ">"; break;
986 #endif /* S_IFDOOR */
988 hilo = 0;
990 else if (hilo == HIGH_PIECE) {
991 switch (st->st_mode & S_IFMT) {
992 case S_IFIFO: sdata = "Fifo File"; break;
993 case S_IFCHR: sdata = "Character Device"; break;
994 case S_IFDIR: sdata = "Directory"; break;
995 case S_IFBLK: sdata = "Block Device"; break;
996 case S_IFREG: sdata = "Regular File"; break;
997 case S_IFLNK: sdata = "Symbolic Link"; break;
998 #ifdef S_IFSOCK
999 case S_IFSOCK: sdata = "Socket"; break;
1000 #endif
1001 #ifdef S_IFWHT
1002 case S_IFWHT: sdata = "Whiteout File"; break;
1003 #endif /* S_IFWHT */
1004 #ifdef S_IFDOOR
1005 case S_IFDOOR: sdata = "Door"; break;
1006 #endif /* S_IFDOOR */
1007 default: sdata = "???"; break;
1009 hilo = 0;
1011 formats = FMTF_STRING;
1012 if (ofmt == 0)
1013 ofmt = FMTF_STRING;
1014 break;
1015 case SHOW_filename:
1016 small = 0;
1017 data = 0;
1018 if (file == NULL) {
1019 (void)strlcpy(path, "(stdin)", sizeof(path));
1020 if (hilo == HIGH_PIECE || hilo == LOW_PIECE)
1021 hilo = 0;
1023 else if (hilo == 0)
1024 (void)strlcpy(path, file, sizeof(path));
1025 else {
1026 char *s;
1027 (void)strlcpy(path, file, sizeof(path));
1028 s = strrchr(path, '/');
1029 if (s != NULL) {
1030 /* trim off trailing /'s */
1031 while (s != path &&
1032 s[0] == '/' && s[1] == '\0')
1033 *s-- = '\0';
1034 s = strrchr(path, '/');
1036 if (hilo == HIGH_PIECE) {
1037 if (s == NULL)
1038 (void)strlcpy(path, ".", sizeof(path));
1039 else {
1040 while (s != path && s[0] == '/')
1041 *s-- = '\0';
1043 hilo = 0;
1045 else if (hilo == LOW_PIECE) {
1046 if (s != NULL && s[1] != '\0')
1047 (void)strlcpy(path, s + 1,
1048 sizeof(path));
1049 hilo = 0;
1052 sdata = path;
1053 formats = FMTF_STRING;
1054 if (ofmt == 0)
1055 ofmt = FMTF_STRING;
1056 break;
1057 case SHOW_sizerdev:
1058 if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
1059 char majdev[20], mindev[20];
1060 int l1, l2;
1062 l1 = format1(st,
1063 file,
1064 fmt, flen,
1065 majdev, sizeof(majdev),
1066 flags, size, prec,
1067 ofmt, HIGH_PIECE, SHOW_st_rdev, quiet);
1068 l2 = format1(st,
1069 file,
1070 fmt, flen,
1071 mindev, sizeof(mindev),
1072 flags, size, prec,
1073 ofmt, LOW_PIECE, SHOW_st_rdev, quiet);
1074 return (snprintf(buf, blen, "%.*s,%.*s",
1075 l1, majdev, l2, mindev));
1077 else {
1078 return (format1(st,
1079 file,
1080 fmt, flen,
1081 buf, blen,
1082 flags, size, prec,
1083 ofmt, 0, SHOW_st_size, quiet));
1085 /*NOTREACHED*/
1086 default:
1087 errx(1, "%.*s: bad format", (int)flen, fmt);
1091 * If a subdatum was specified but not supported, or an output
1092 * format was selected that is not supported, that's an error.
1094 if (hilo != 0 || (ofmt & formats) == 0)
1095 errx(1, "%.*s: bad format", (int)flen, fmt);
1099 * Assemble the format string for passing to printf(3).
1101 lfmt[0] = '\0';
1102 (void)strcat(lfmt, "%");
1103 if (flags & FLAG_POUND)
1104 (void)strcat(lfmt, "#");
1105 if (flags & FLAG_SPACE)
1106 (void)strcat(lfmt, " ");
1107 if (flags & FLAG_PLUS)
1108 (void)strcat(lfmt, "+");
1109 if (flags & FLAG_MINUS)
1110 (void)strcat(lfmt, "-");
1111 if (flags & FLAG_ZERO)
1112 (void)strcat(lfmt, "0");
1115 * Only the timespecs support the FLOAT output format, and that
1116 * requires work that differs from the other formats.
1118 if (ofmt == FMTF_FLOAT) {
1120 * Nothing after the decimal point, so just print seconds.
1122 if (prec == 0) {
1123 if (size != -1) {
1124 (void)snprintf(tmp, sizeof(tmp), "%d", size);
1125 (void)strcat(lfmt, tmp);
1127 (void)strcat(lfmt, "lld");
1128 return (snprintf(buf, blen, lfmt,
1129 (long long)secs));
1133 * Unspecified precision gets all the precision we have:
1134 * 9 digits.
1136 if (prec == -1)
1137 prec = 9;
1140 * Adjust the size for the decimal point and the digits
1141 * that will follow.
1143 size -= prec + 1;
1146 * Any leftover size that's legitimate will be used.
1148 if (size > 0) {
1149 (void)snprintf(tmp, sizeof(tmp), "%d", size);
1150 (void)strcat(lfmt, tmp);
1152 /* Seconds: time_t cast to long long. */
1153 (void)strcat(lfmt, "lld");
1156 * The stuff after the decimal point always needs zero
1157 * filling.
1159 (void)strcat(lfmt, ".%0");
1162 * We can "print" at most nine digits of precision. The
1163 * rest we will pad on at the end.
1165 * Nanoseconds: long.
1167 (void)snprintf(tmp, sizeof(tmp), "%dld", prec > 9 ? 9 : prec);
1168 (void)strcat(lfmt, tmp);
1171 * For precision of less that nine digits, trim off the
1172 * less significant figures.
1174 for (; prec < 9; prec++)
1175 nsecs /= 10;
1178 * Use the format, and then tack on any zeroes that
1179 * might be required to make up the requested precision.
1181 l = snprintf(buf, blen, lfmt, (long long)secs, nsecs);
1182 for (; prec > 9 && l < (int)blen; prec--, l++)
1183 (void)strcat(buf, "0");
1184 return (l);
1188 * Add on size and precision, if specified, to the format.
1190 if (size != -1) {
1191 (void)snprintf(tmp, sizeof(tmp), "%d", size);
1192 (void)strcat(lfmt, tmp);
1194 if (prec != -1) {
1195 (void)snprintf(tmp, sizeof(tmp), ".%d", prec);
1196 (void)strcat(lfmt, tmp);
1200 * String output uses the temporary sdata.
1202 if (ofmt == FMTF_STRING) {
1203 if (sdata == NULL)
1204 errx(1, "%.*s: bad format", (int)flen, fmt);
1205 (void)strcat(lfmt, "s");
1206 return (snprintf(buf, blen, lfmt, sdata));
1210 * Ensure that sign extension does not cause bad looking output
1211 * for some forms.
1213 if (small && ofmt != FMTF_DECIMAL)
1214 data = (uint32_t)data;
1217 * The four "numeric" output forms.
1219 (void)strcat(lfmt, "ll");
1220 switch (ofmt) {
1221 case FMTF_DECIMAL: (void)strcat(lfmt, "d"); break;
1222 case FMTF_OCTAL: (void)strcat(lfmt, "o"); break;
1223 case FMTF_UNSIGNED: (void)strcat(lfmt, "u"); break;
1224 case FMTF_HEX: (void)strcat(lfmt, "x"); break;
1228 * shift and round to nearest for kilobytes, megabytes,
1229 * gigabytes.
1231 if (shift > 0) {
1232 data >>= (shift - 1);
1233 data++;
1234 data >>= 1;
1237 return (snprintf(buf, blen, lfmt, data));