stat.c revision 1.41 1 /* $NetBSD: stat.c,v 1.41 2017/09/20 17:45:25 kre Exp $ */
2
3 /*
4 * Copyright (c) 2002-2011 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Brown.
9 *
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.
18 *
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.
30 */
31
32 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
34 /* config checked libc, we need the prototype as well */
35 #undef HAVE_DEVNAME
36 #endif
37
38 #include <sys/cdefs.h>
39 #if !defined(lint)
40 __RCSID("$NetBSD: stat.c,v 1.41 2017/09/20 17:45:25 kre Exp $");
41 #endif
42
43 #if ! HAVE_NBTOOL_CONFIG_H
44 #define HAVE_STRUCT_STAT_ST_FLAGS 1
45 #define HAVE_STRUCT_STAT_ST_GEN 1
46 #define HAVE_STRUCT_STAT_ST_BIRTHTIME 1
47 #define HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC 1
48 #define HAVE_STRUCT_STAT_ST_MTIMENSEC 1
49 #define HAVE_DEVNAME 1
50 #endif /* HAVE_NBTOOL_CONFIG_H */
51
52 #include <sys/types.h>
53 #include <sys/stat.h>
54
55 #include <ctype.h>
56 #include <err.h>
57 #include <errno.h>
58 #include <grp.h>
59 #include <limits.h>
60 #include <pwd.h>
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <string.h>
64 #include <time.h>
65 #include <unistd.h>
66 #include <vis.h>
67
68 #if HAVE_STRUCT_STAT_ST_FLAGS
69 #define DEF_F "%#Xf "
70 #define RAW_F "%f "
71 #define SHELL_F " st_flags=%f"
72 #else /* HAVE_STRUCT_STAT_ST_FLAGS */
73 #define DEF_F
74 #define RAW_F
75 #define SHELL_F
76 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
77
78 #if HAVE_STRUCT_STAT_ST_BIRTHTIME
79 #define DEF_B "\"%SB\" "
80 #define RAW_B "%B "
81 #define SHELL_B "st_birthtime=%SB "
82 #define LINUX_B "%n Birth: %SB"
83 #else /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
84 #define DEF_B
85 #define RAW_B
86 #define SHELL_B
87 #define LINUX_B
88 #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
89
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 */
95
96 #define DEF_FORMAT \
97 "%d %i %Sp %l %Su %Sg %r %z \"%Sa\" \"%Sm\" \"%Sc\" " DEF_B \
98 "%k %b " DEF_F "%N"
99 #define RAW_FORMAT "%d %i %#p %l %u %g %r %z %a %m %c " RAW_B \
100 "%k %b " RAW_F "%N"
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=%Sa st_mtime=%Sm st_ctime=%Sc " SHELL_B \
107 "st_blksize=%k st_blocks=%b" SHELL_F
108 #define LINUX_FORMAT \
109 " File: \"%N\"%n" \
110 " Size: %-11z Blocks: %-11b IO Block: %-11k %HT%n" \
111 "Device: %Hd,%Ld Inode: %i Links: %l%n" \
112 " Mode: (%Mp%03OLp/%.10Sp) Uid: (%5u/%8Su) Gid: (%5g/%8Sg)%n" \
113 "Access: %Sa%n" \
114 "Modify: %Sm%n" \
115 "Change: %Sc" \
116 LINUX_B
117
118 #define TIME_FORMAT "%b %e %T %Y"
119
120 #define FLAG_POUND 0x01
121 #define FLAG_SPACE 0x02
122 #define FLAG_PLUS 0x04
123 #define FLAG_ZERO 0x08
124 #define FLAG_MINUS 0x10
125
126 /*
127 * These format characters must all be unique, except the magic one.
128 */
129 #define FMT_MAGIC '%'
130 #define FMT_DOT '.'
131
132 #define SIMPLE_NEWLINE 'n'
133 #define SIMPLE_TAB 't'
134 #define SIMPLE_PERCENT '%'
135 #define SIMPLE_NUMBER '@'
136
137 #define FMT_POUND '#'
138 #define FMT_SPACE ' '
139 #define FMT_PLUS '+'
140 #define FMT_ZERO '0'
141 #define FMT_MINUS '-'
142
143 #define FMT_DECIMAL 'D'
144 #define FMT_OCTAL 'O'
145 #define FMT_UNSIGNED 'U'
146 #define FMT_HEX 'X'
147 #define FMT_FLOAT 'F'
148 #define FMT_STRING 'S'
149
150 #define FMTF_DECIMAL 0x01
151 #define FMTF_OCTAL 0x02
152 #define FMTF_UNSIGNED 0x04
153 #define FMTF_HEX 0x08
154 #define FMTF_FLOAT 0x10
155 #define FMTF_STRING 0x20
156
157 #define HIGH_PIECE 'H'
158 #define MIDDLE_PIECE 'M'
159 #define LOW_PIECE 'L'
160
161 #define SHOW_realpath 'R'
162 #define SHOW_st_dev 'd'
163 #define SHOW_st_ino 'i'
164 #define SHOW_st_mode 'p'
165 #define SHOW_st_nlink 'l'
166 #define SHOW_st_uid 'u'
167 #define SHOW_st_gid 'g'
168 #define SHOW_st_rdev 'r'
169 #define SHOW_st_atime 'a'
170 #define SHOW_st_mtime 'm'
171 #define SHOW_st_ctime 'c'
172 #define SHOW_st_btime 'B'
173 #define SHOW_st_size 'z'
174 #define SHOW_st_blocks 'b'
175 #define SHOW_st_blksize 'k'
176 #define SHOW_st_flags 'f'
177 #define SHOW_st_gen 'v'
178 #define SHOW_symlink 'Y'
179 #define SHOW_filetype 'T'
180 #define SHOW_filename 'N'
181 #define SHOW_sizerdev 'Z'
182
183 static void usage(const char *) __dead;
184 static void output(const struct stat *, const char *,
185 const char *, int, int, int);
186 static int format1(const struct stat *, /* stat info */
187 const char *, /* the file name */
188 const char *, int, /* the format string itself */
189 char *, size_t, /* a place to put the output */
190 int, int, int, int, /* the parsed format */
191 int, int, int);
192
193 static const char *timefmt;
194 static int linkfail;
195
196 #define addchar(s, c, nl) \
197 do { \
198 (void)fputc((c), (s)); \
199 (*nl) = ((c) == '\n'); \
200 } while (0/*CONSTCOND*/)
201
202 int
203 main(int argc, char *argv[])
204 {
205 struct stat st;
206 int ch, rc, errs, am_readlink;
207 int lsF, fmtchar, usestat, fn, nonl, quiet;
208 const char *statfmt, *options, *synopsis;
209
210 am_readlink = 0;
211 lsF = 0;
212 fmtchar = '\0';
213 usestat = 0;
214 nonl = 0;
215 quiet = 0;
216 linkfail = 0;
217 statfmt = NULL;
218 timefmt = NULL;
219
220 setprogname(argv[0]);
221
222 if (strcmp(getprogname(), "readlink") == 0) {
223 am_readlink = 1;
224 options = "fnqsv";
225 synopsis = "[-fnqsv] [file ...]";
226 statfmt = "%Y";
227 fmtchar = 'f';
228 quiet = 1;
229 } else {
230 options = "f:FlLnqrst:x";
231 synopsis = "[-FlLnqrsx] [-f format] [-t timefmt] [file ...]";
232 }
233
234 while ((ch = getopt(argc, argv, options)) != -1)
235 switch (ch) {
236 case 'F':
237 lsF = 1;
238 break;
239 case 'L':
240 usestat = 1;
241 break;
242 case 'n':
243 nonl = 1;
244 break;
245 case 'q':
246 quiet = 1;
247 break;
248 case 'f':
249 if (am_readlink) {
250 statfmt = "%R";
251 break;
252 }
253 statfmt = optarg;
254 /* FALLTHROUGH */
255 case 'l':
256 case 'r':
257 case 's':
258 if (am_readlink) {
259 quiet = 1;
260 break;
261 }
262 /*FALLTHROUGH*/
263 case 'x':
264 if (fmtchar != 0)
265 errx(1, "can't use format '%c' with '%c'",
266 fmtchar, ch);
267 fmtchar = ch;
268 break;
269 case 't':
270 timefmt = optarg;
271 break;
272 case 'v':
273 quiet = 0;
274 break;
275 default:
276 usage(synopsis);
277 }
278
279 argc -= optind;
280 argv += optind;
281 fn = 1;
282
283 if (fmtchar == '\0') {
284 if (lsF)
285 fmtchar = 'l';
286 else {
287 fmtchar = 'f';
288 statfmt = DEF_FORMAT;
289 }
290 }
291
292 if (lsF && fmtchar != 'l')
293 errx(1, "can't use format '%c' with -F", fmtchar);
294
295 switch (fmtchar) {
296 case 'f':
297 /* statfmt already set */
298 break;
299 case 'l':
300 statfmt = lsF ? LSF_FORMAT : LS_FORMAT;
301 break;
302 case 'r':
303 statfmt = RAW_FORMAT;
304 break;
305 case 's':
306 statfmt = SHELL_FORMAT;
307 if (timefmt == NULL)
308 timefmt = "%s";
309 break;
310 case 'x':
311 statfmt = LINUX_FORMAT;
312 if (timefmt == NULL)
313 timefmt = "%Y-%m-%d %H:%M:%S.%f %z";
314 break;
315 default:
316 usage(synopsis);
317 /*NOTREACHED*/
318 }
319
320 if (timefmt == NULL)
321 timefmt = TIME_FORMAT;
322
323 errs = 0;
324 do {
325 if (argc == 0)
326 rc = fstat(STDIN_FILENO, &st);
327 else if (usestat) {
328 /*
329 * Try stat() and if it fails, fall back to
330 * lstat() just in case we're examining a
331 * broken symlink.
332 */
333 if ((rc = stat(argv[0], &st)) == -1 &&
334 errno == ENOENT &&
335 (rc = lstat(argv[0], &st)) == -1)
336 errno = ENOENT;
337 }
338 else
339 rc = lstat(argv[0], &st);
340
341 if (rc == -1) {
342 errs = 1;
343 linkfail = 1;
344 if (!quiet)
345 warn("%s: %s",
346 argc == 0 ? "(stdin)" : argv[0],
347 usestat ? "stat" : "lstat");
348 }
349 else
350 output(&st, argv[0], statfmt, fn, nonl, quiet);
351
352 argv++;
353 argc--;
354 fn++;
355 } while (argc > 0);
356
357 return (am_readlink ? linkfail : errs);
358 }
359
360 static void
361 usage(const char *synopsis)
362 {
363
364 (void)fprintf(stderr, "usage: %s %s\n", getprogname(), synopsis);
365 exit(1);
366 }
367
368 /*
369 * Parses a format string.
370 */
371 static void
372 output(const struct stat *st, const char *file,
373 const char *statfmt, int fn, int nonl, int quiet)
374 {
375 int flags, size, prec, ofmt, hilo, what;
376 /*
377 * buf size is enough for an item of length PATH_MAX,
378 * multiplied by 4 for vis encoding, plus 4 for symlink
379 * " -> " prefix, plus 1 for \0 terminator.
380 */
381 char buf[PATH_MAX * 4 + 4 + 1];
382 const char *subfmt;
383 int nl, t, i;
384
385 nl = 1;
386 while (*statfmt != '\0') {
387
388 /*
389 * Non-format characters go straight out.
390 */
391 if (*statfmt != FMT_MAGIC) {
392 addchar(stdout, *statfmt, &nl);
393 statfmt++;
394 continue;
395 }
396
397 /*
398 * The current format "substring" starts here,
399 * and then we skip the magic.
400 */
401 subfmt = statfmt;
402 statfmt++;
403
404 /*
405 * Some simple one-character "formats".
406 */
407 switch (*statfmt) {
408 case SIMPLE_NEWLINE:
409 addchar(stdout, '\n', &nl);
410 statfmt++;
411 continue;
412 case SIMPLE_TAB:
413 addchar(stdout, '\t', &nl);
414 statfmt++;
415 continue;
416 case SIMPLE_PERCENT:
417 addchar(stdout, '%', &nl);
418 statfmt++;
419 continue;
420 case SIMPLE_NUMBER: {
421 char num[12], *p;
422
423 snprintf(num, sizeof(num), "%d", fn);
424 for (p = &num[0]; *p; p++)
425 addchar(stdout, *p, &nl);
426 statfmt++;
427 continue;
428 }
429 }
430
431 /*
432 * This must be an actual format string. Format strings are
433 * similar to printf(3) formats up to a point, and are of
434 * the form:
435 *
436 * % required start of format
437 * [-# +0] opt. format characters
438 * size opt. field width
439 * . opt. decimal separator, followed by
440 * prec opt. precision
441 * fmt opt. output specifier (string, numeric, etc.)
442 * sub opt. sub field specifier (high, middle, low)
443 * datum required field specifier (size, mode, etc)
444 *
445 * Only the % and the datum selector are required. All data
446 * have reasonable default output forms. The "sub" specifier
447 * only applies to certain data (mode, dev, rdev, filetype).
448 * The symlink output defaults to STRING, yet will only emit
449 * the leading " -> " if STRING is explicitly specified. The
450 * sizerdev datum will generate rdev output for character or
451 * block devices, and size output for all others.
452 * For STRING output, the # format requests vis encoding.
453 */
454 flags = 0;
455 do {
456 if (*statfmt == FMT_POUND)
457 flags |= FLAG_POUND;
458 else if (*statfmt == FMT_SPACE)
459 flags |= FLAG_SPACE;
460 else if (*statfmt == FMT_PLUS)
461 flags |= FLAG_PLUS;
462 else if (*statfmt == FMT_ZERO)
463 flags |= FLAG_ZERO;
464 else if (*statfmt == FMT_MINUS)
465 flags |= FLAG_MINUS;
466 else
467 break;
468 statfmt++;
469 } while (1/*CONSTCOND*/);
470
471 size = -1;
472 if (isdigit((unsigned)*statfmt)) {
473 size = 0;
474 while (isdigit((unsigned)*statfmt)) {
475 size = (size * 10) + (*statfmt - '0');
476 statfmt++;
477 if (size < 0)
478 goto badfmt;
479 }
480 }
481
482 prec = -1;
483 if (*statfmt == FMT_DOT) {
484 statfmt++;
485
486 prec = 0;
487 while (isdigit((unsigned)*statfmt)) {
488 prec = (prec * 10) + (*statfmt - '0');
489 statfmt++;
490 if (prec < 0)
491 goto badfmt;
492 }
493 }
494
495 #define fmtcase(x, y) case (y): (x) = (y); statfmt++; break
496 #define fmtcasef(x, y, z) case (y): (x) = (z); statfmt++; break
497 switch (*statfmt) {
498 fmtcasef(ofmt, FMT_DECIMAL, FMTF_DECIMAL);
499 fmtcasef(ofmt, FMT_OCTAL, FMTF_OCTAL);
500 fmtcasef(ofmt, FMT_UNSIGNED, FMTF_UNSIGNED);
501 fmtcasef(ofmt, FMT_HEX, FMTF_HEX);
502 fmtcasef(ofmt, FMT_FLOAT, FMTF_FLOAT);
503 fmtcasef(ofmt, FMT_STRING, FMTF_STRING);
504 default:
505 ofmt = 0;
506 break;
507 }
508
509 switch (*statfmt) {
510 fmtcase(hilo, HIGH_PIECE);
511 fmtcase(hilo, MIDDLE_PIECE);
512 fmtcase(hilo, LOW_PIECE);
513 default:
514 hilo = 0;
515 break;
516 }
517
518 switch (*statfmt) {
519 fmtcase(what, SHOW_realpath);
520 fmtcase(what, SHOW_st_dev);
521 fmtcase(what, SHOW_st_ino);
522 fmtcase(what, SHOW_st_mode);
523 fmtcase(what, SHOW_st_nlink);
524 fmtcase(what, SHOW_st_uid);
525 fmtcase(what, SHOW_st_gid);
526 fmtcase(what, SHOW_st_rdev);
527 fmtcase(what, SHOW_st_atime);
528 fmtcase(what, SHOW_st_mtime);
529 fmtcase(what, SHOW_st_ctime);
530 fmtcase(what, SHOW_st_btime);
531 fmtcase(what, SHOW_st_size);
532 fmtcase(what, SHOW_st_blocks);
533 fmtcase(what, SHOW_st_blksize);
534 fmtcase(what, SHOW_st_flags);
535 fmtcase(what, SHOW_st_gen);
536 fmtcase(what, SHOW_symlink);
537 fmtcase(what, SHOW_filetype);
538 fmtcase(what, SHOW_filename);
539 fmtcase(what, SHOW_sizerdev);
540 default:
541 goto badfmt;
542 }
543 #undef fmtcasef
544 #undef fmtcase
545
546 t = format1(st,
547 file,
548 subfmt, statfmt - subfmt,
549 buf, sizeof(buf),
550 flags, size, prec, ofmt, hilo, what, quiet);
551
552 for (i = 0; i < t && i < (int)(sizeof(buf) - 1); i++)
553 addchar(stdout, buf[i], &nl);
554
555 continue;
556
557 badfmt:
558 errx(1, "%.*s: bad format",
559 (int)(statfmt - subfmt + 1), subfmt);
560 }
561
562 if (!nl && !nonl)
563 (void)fputc('\n', stdout);
564 (void)fflush(stdout);
565 }
566
567 static const char *
568 fmttime(char *buf, size_t len, const char *fmt, time_t secs, long nsecs)
569 {
570 struct tm tm;
571 char *fmt2;
572 if (localtime_r(&secs, &tm) == NULL) {
573 secs = 0;
574 (void)localtime_r(&secs, &tm);
575 }
576 if ((fmt2 = strstr(fmt, "%f")) != NULL) {
577 size_t flen = strlen(fmt) + 1024;
578 size_t o = (size_t)(fmt2 - fmt);
579 fmt2 = calloc(flen, 1);
580 if (fmt2 != NULL) {
581 memcpy(fmt2, fmt, o);
582 int sl = snprintf(fmt2 + o, flen - o, "%.9ld", nsecs);
583 if (sl == -1)
584 sl = 0;
585 strcpy(fmt2 + sl + o, fmt + o + 2);
586 fmt = fmt2;
587 }
588 }
589
590 (void)strftime(buf, len, fmt, &tm);
591 free(fmt2);
592 return buf;
593 }
594
595 /*
596 * Arranges output according to a single parsed format substring.
597 */
598 static int
599 format1(const struct stat *st,
600 const char *file,
601 const char *fmt, int flen,
602 char *buf, size_t blen,
603 int flags, int size, int prec, int ofmt,
604 int hilo, int what, int quiet)
605 {
606 uint64_t data;
607 char *stmp, lfmt[24], tmp[20];
608 const char *sdata;
609 char smode[12], sid[12], path[PATH_MAX + 4], visbuf[PATH_MAX * 4 + 4];
610 struct passwd *pw;
611 struct group *gr;
612 time_t secs;
613 long nsecs;
614 int l;
615 int formats; /* bitmap of allowed formats for this datum */
616 int small; /* true if datum is a small integer */
617 int gottime; /* true if secs and nsecs are valid */
618 int shift; /* powers of 2 to scale numbers before printing */
619 size_t prefixlen; /* length of constant prefix for string data */
620
621 formats = 0;
622 small = 0;
623 gottime = 0;
624 secs = 0;
625 nsecs = 0;
626 shift = 0;
627 prefixlen = 0;
628
629 /*
630 * First, pick out the data and tweak it based on hilo or
631 * specified output format (symlink output only).
632 */
633 switch (what) {
634 case SHOW_st_dev:
635 case SHOW_st_rdev:
636 small = (sizeof(st->st_dev) == 4);
637 data = (what == SHOW_st_dev) ? st->st_dev : st->st_rdev;
638 #if HAVE_DEVNAME
639 sdata = (what == SHOW_st_dev) ?
640 devname(st->st_dev, S_IFBLK) :
641 devname(st->st_rdev,
642 S_ISCHR(st->st_mode) ? S_IFCHR :
643 S_ISBLK(st->st_mode) ? S_IFBLK :
644 0U);
645 if (sdata == NULL)
646 sdata = "???";
647 #endif /* HAVE_DEVNAME */
648 if (hilo == HIGH_PIECE) {
649 data = major(data);
650 hilo = 0;
651 }
652 else if (hilo == LOW_PIECE) {
653 data = minor((unsigned)data);
654 hilo = 0;
655 }
656 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
657 #if HAVE_DEVNAME
658 FMTF_STRING;
659 #else /* HAVE_DEVNAME */
660 0;
661 #endif /* HAVE_DEVNAME */
662 if (ofmt == 0) {
663 if (data == (uint64_t)-1)
664 ofmt = FMTF_DECIMAL;
665 else
666 ofmt = FMTF_UNSIGNED;
667 }
668 break;
669 case SHOW_st_ino:
670 small = (sizeof(st->st_ino) == 4);
671 data = st->st_ino;
672 sdata = NULL;
673 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
674 if (ofmt == 0)
675 ofmt = FMTF_UNSIGNED;
676 break;
677 case SHOW_st_mode:
678 small = (sizeof(st->st_mode) == 4);
679 data = st->st_mode;
680 strmode(st->st_mode, smode);
681 stmp = smode;
682 l = strlen(stmp);
683 if (stmp[l - 1] == ' ')
684 stmp[--l] = '\0';
685 if (hilo == HIGH_PIECE) {
686 data >>= 12;
687 stmp += 1;
688 stmp[3] = '\0';
689 hilo = 0;
690 }
691 else if (hilo == MIDDLE_PIECE) {
692 data = (data >> 9) & 07;
693 stmp += 4;
694 stmp[3] = '\0';
695 hilo = 0;
696 }
697 else if (hilo == LOW_PIECE) {
698 data &= 0777;
699 stmp += 7;
700 stmp[3] = '\0';
701 hilo = 0;
702 }
703 sdata = stmp;
704 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
705 FMTF_STRING;
706 if (ofmt == 0)
707 ofmt = FMTF_OCTAL;
708 break;
709 case SHOW_st_nlink:
710 small = (sizeof(st->st_dev) == 4);
711 data = st->st_nlink;
712 sdata = NULL;
713 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
714 if (ofmt == 0)
715 ofmt = FMTF_UNSIGNED;
716 break;
717 case SHOW_st_uid:
718 small = (sizeof(st->st_uid) == 4);
719 data = st->st_uid;
720 if ((pw = getpwuid(st->st_uid)) != NULL)
721 sdata = pw->pw_name;
722 else {
723 snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_uid);
724 sdata = sid;
725 }
726 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
727 FMTF_STRING;
728 if (ofmt == 0)
729 ofmt = FMTF_UNSIGNED;
730 break;
731 case SHOW_st_gid:
732 small = (sizeof(st->st_gid) == 4);
733 data = st->st_gid;
734 if ((gr = getgrgid(st->st_gid)) != NULL)
735 sdata = gr->gr_name;
736 else {
737 snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_gid);
738 sdata = sid;
739 }
740 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
741 FMTF_STRING;
742 if (ofmt == 0)
743 ofmt = FMTF_UNSIGNED;
744 break;
745 case SHOW_st_atime:
746 gottime = 1;
747 secs = st->st_atime;
748 #if HAVE_STRUCT_STAT_ST_MTIMENSEC
749 nsecs = st->st_atimensec;
750 #endif
751 /* FALLTHROUGH */
752 case SHOW_st_mtime:
753 if (!gottime) {
754 gottime = 1;
755 secs = st->st_mtime;
756 #if HAVE_STRUCT_STAT_ST_MTIMENSEC
757 nsecs = st->st_mtimensec;
758 #endif
759 }
760 /* FALLTHROUGH */
761 case SHOW_st_ctime:
762 if (!gottime) {
763 gottime = 1;
764 secs = st->st_ctime;
765 #if HAVE_STRUCT_STAT_ST_MTIMENSEC
766 nsecs = st->st_ctimensec;
767 #endif
768 }
769 /* FALLTHROUGH */
770 #if HAVE_STRUCT_STAT_ST_BIRTHTIME
771 case SHOW_st_btime:
772 if (!gottime) {
773 gottime = 1;
774 secs = st->st_birthtime;
775 #if HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC
776 nsecs = st->st_birthtimensec;
777 #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC */
778 }
779 #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
780 small = (sizeof(secs) == 4);
781 data = secs;
782 sdata = fmttime(path, sizeof(path), timefmt, secs, nsecs);
783
784 sdata = path;
785 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
786 FMTF_FLOAT | FMTF_STRING;
787 if (ofmt == 0)
788 ofmt = FMTF_DECIMAL;
789 break;
790 case SHOW_st_size:
791 small = (sizeof(st->st_size) == 4);
792 data = st->st_size;
793 sdata = NULL;
794 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
795 if (ofmt == 0)
796 ofmt = FMTF_UNSIGNED;
797 switch (hilo) {
798 case HIGH_PIECE:
799 shift = 30; /* gigabytes */
800 hilo = 0;
801 break;
802 case MIDDLE_PIECE:
803 shift = 20; /* megabytes */
804 hilo = 0;
805 break;
806 case LOW_PIECE:
807 shift = 10; /* kilobytes */
808 hilo = 0;
809 break;
810 }
811 break;
812 case SHOW_st_blocks:
813 small = (sizeof(st->st_blocks) == 4);
814 data = st->st_blocks;
815 sdata = NULL;
816 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
817 if (ofmt == 0)
818 ofmt = FMTF_UNSIGNED;
819 break;
820 case SHOW_st_blksize:
821 small = (sizeof(st->st_blksize) == 4);
822 data = st->st_blksize;
823 sdata = NULL;
824 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
825 if (ofmt == 0)
826 ofmt = FMTF_UNSIGNED;
827 break;
828 #if HAVE_STRUCT_STAT_ST_FLAGS
829 case SHOW_st_flags:
830 small = (sizeof(st->st_flags) == 4);
831 data = st->st_flags;
832 sdata = NULL;
833 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
834 if (ofmt == 0)
835 ofmt = FMTF_UNSIGNED;
836 break;
837 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
838 #if HAVE_STRUCT_STAT_ST_GEN
839 case SHOW_st_gen:
840 small = (sizeof(st->st_gen) == 4);
841 data = st->st_gen;
842 sdata = NULL;
843 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
844 if (ofmt == 0)
845 ofmt = FMTF_UNSIGNED;
846 break;
847 #endif /* HAVE_STRUCT_STAT_ST_GEN */
848 case SHOW_realpath:
849 small = 0;
850 data = 0;
851 if (file == NULL) {
852 (void)strlcpy(path, "(stdin)", sizeof(path));
853 sdata = path;
854 } else {
855 snprintf(path, sizeof(path), " -> ");
856 if (realpath(file, path + 4) == NULL) {
857 if (!quiet)
858 warn("realpath `%s'", file);
859 linkfail = 1;
860 l = 0;
861 path[0] = '\0';
862 }
863 sdata = path + (ofmt == FMTF_STRING ? 0 : 4);
864 prefixlen = (ofmt == FMTF_STRING ? 4 : 0);
865 }
866
867 formats = FMTF_STRING;
868 if (ofmt == 0)
869 ofmt = FMTF_STRING;
870 break;
871 case SHOW_symlink:
872 small = 0;
873 data = 0;
874 if (S_ISLNK(st->st_mode)) {
875 snprintf(path, sizeof(path), " -> ");
876 l = readlink(file, path + 4, sizeof(path) - 4 - 1);
877 if (l == -1) {
878 if (!quiet)
879 warn("readlink `%s'", file);
880 linkfail = 1;
881 l = 0;
882 path[0] = '\0';
883 }
884 path[l + 4] = '\0';
885 sdata = path + (ofmt == FMTF_STRING ? 0 : 4);
886 prefixlen = (ofmt == FMTF_STRING ? 4 : 0);
887 }
888 else {
889 linkfail = 1;
890 sdata = "";
891 }
892 formats = FMTF_STRING;
893 if (ofmt == 0)
894 ofmt = FMTF_STRING;
895 break;
896 case SHOW_filetype:
897 small = 0;
898 data = 0;
899 sdata = "";
900 if (hilo == 0 || hilo == LOW_PIECE) {
901 switch (st->st_mode & S_IFMT) {
902 case S_IFIFO: sdata = "|"; break;
903 case S_IFDIR: sdata = "/"; break;
904 case S_IFREG:
905 if (st->st_mode &
906 (S_IXUSR | S_IXGRP | S_IXOTH))
907 sdata = "*";
908 break;
909 case S_IFLNK: sdata = "@"; break;
910 #ifdef S_IFSOCK
911 case S_IFSOCK: sdata = "="; break;
912 #endif
913 #ifdef S_IFWHT
914 case S_IFWHT: sdata = "%"; break;
915 #endif /* S_IFWHT */
916 #ifdef S_IFDOOR
917 case S_IFDOOR: sdata = ">"; break;
918 #endif /* S_IFDOOR */
919 }
920 hilo = 0;
921 }
922 else if (hilo == HIGH_PIECE) {
923 switch (st->st_mode & S_IFMT) {
924 case S_IFIFO: sdata = "Fifo File"; break;
925 case S_IFCHR: sdata = "Character Device"; break;
926 case S_IFDIR: sdata = "Directory"; break;
927 case S_IFBLK: sdata = "Block Device"; break;
928 case S_IFREG: sdata = "Regular File"; break;
929 case S_IFLNK: sdata = "Symbolic Link"; break;
930 #ifdef S_IFSOCK
931 case S_IFSOCK: sdata = "Socket"; break;
932 #endif
933 #ifdef S_IFWHT
934 case S_IFWHT: sdata = "Whiteout File"; break;
935 #endif /* S_IFWHT */
936 #ifdef S_IFDOOR
937 case S_IFDOOR: sdata = "Door"; break;
938 #endif /* S_IFDOOR */
939 default: sdata = "???"; break;
940 }
941 hilo = 0;
942 }
943 formats = FMTF_STRING;
944 if (ofmt == 0)
945 ofmt = FMTF_STRING;
946 break;
947 case SHOW_filename:
948 small = 0;
949 data = 0;
950 if (file == NULL) {
951 (void)strlcpy(path, "(stdin)", sizeof(path));
952 if (hilo == HIGH_PIECE || hilo == LOW_PIECE)
953 hilo = 0;
954 }
955 else if (hilo == 0)
956 (void)strlcpy(path, file, sizeof(path));
957 else {
958 char *s;
959 (void)strlcpy(path, file, sizeof(path));
960 s = strrchr(path, '/');
961 if (s != NULL) {
962 /* trim off trailing /'s */
963 while (s != path &&
964 s[0] == '/' && s[1] == '\0')
965 *s-- = '\0';
966 s = strrchr(path, '/');
967 }
968 if (hilo == HIGH_PIECE) {
969 if (s == NULL)
970 (void)strlcpy(path, ".", sizeof(path));
971 else {
972 while (s != path && s[0] == '/')
973 *s-- = '\0';
974 }
975 hilo = 0;
976 }
977 else if (hilo == LOW_PIECE) {
978 if (s != NULL && s[1] != '\0')
979 (void)strlcpy(path, s + 1,
980 sizeof(path));
981 hilo = 0;
982 }
983 }
984 sdata = path;
985 formats = FMTF_STRING;
986 if (ofmt == 0)
987 ofmt = FMTF_STRING;
988 break;
989 case SHOW_sizerdev:
990 if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
991 char majdev[20], mindev[20];
992 int l1, l2;
993
994 l1 = format1(st,
995 file,
996 fmt, flen,
997 majdev, sizeof(majdev),
998 flags, size, prec,
999 ofmt, HIGH_PIECE, SHOW_st_rdev, quiet);
1000 l2 = format1(st,
1001 file,
1002 fmt, flen,
1003 mindev, sizeof(mindev),
1004 flags, size, prec,
1005 ofmt, LOW_PIECE, SHOW_st_rdev, quiet);
1006 return (snprintf(buf, blen, "%.*s,%.*s",
1007 l1, majdev, l2, mindev));
1008 }
1009 else {
1010 return (format1(st,
1011 file,
1012 fmt, flen,
1013 buf, blen,
1014 flags, size, prec,
1015 ofmt, 0, SHOW_st_size, quiet));
1016 }
1017 /*NOTREACHED*/
1018 default:
1019 errx(1, "%.*s: bad format", (int)flen, fmt);
1020 }
1021
1022 /*
1023 * If a subdatum was specified but not supported, or an output
1024 * format was selected that is not supported, that's an error.
1025 */
1026 if (hilo != 0 || (ofmt & formats) == 0)
1027 errx(1, "%.*s: bad format", (int)flen, fmt);
1028
1029 /*
1030 * FLAG_POUND with FMTF_STRING means use vis(3) encoding.
1031 * First prefixlen chars are not encoded.
1032 */
1033 if ((flags & FLAG_POUND) != 0 && ofmt == FMTF_STRING) {
1034 flags &= !FLAG_POUND;
1035 strncpy(visbuf, sdata, prefixlen);
1036 strnvis(visbuf + prefixlen, sizeof(visbuf) - prefixlen,
1037 sdata + prefixlen, VIS_WHITE | VIS_OCTAL | VIS_CSTYLE);
1038 sdata = visbuf;
1039 }
1040
1041 /*
1042 * Assemble the format string for passing to printf(3).
1043 */
1044 lfmt[0] = '\0';
1045 (void)strcat(lfmt, "%");
1046 if (flags & FLAG_POUND)
1047 (void)strcat(lfmt, "#");
1048 if (flags & FLAG_SPACE)
1049 (void)strcat(lfmt, " ");
1050 if (flags & FLAG_PLUS)
1051 (void)strcat(lfmt, "+");
1052 if (flags & FLAG_MINUS)
1053 (void)strcat(lfmt, "-");
1054 if (flags & FLAG_ZERO)
1055 (void)strcat(lfmt, "0");
1056
1057 /*
1058 * Only the timespecs support the FLOAT output format, and that
1059 * requires work that differs from the other formats.
1060 */
1061 if (ofmt == FMTF_FLOAT) {
1062 /*
1063 * Nothing after the decimal point, so just print seconds.
1064 */
1065 if (prec == 0) {
1066 if (size != -1) {
1067 (void)snprintf(tmp, sizeof(tmp), "%d", size);
1068 (void)strcat(lfmt, tmp);
1069 }
1070 (void)strcat(lfmt, "lld");
1071 return (snprintf(buf, blen, lfmt,
1072 (long long)secs));
1073 }
1074
1075 /*
1076 * Unspecified precision gets all the precision we have:
1077 * 9 digits.
1078 */
1079 if (prec == -1)
1080 prec = 9;
1081
1082 /*
1083 * Adjust the size for the decimal point and the digits
1084 * that will follow.
1085 */
1086 size -= prec + 1;
1087
1088 /*
1089 * Any leftover size that's legitimate will be used.
1090 */
1091 if (size > 0) {
1092 (void)snprintf(tmp, sizeof(tmp), "%d", size);
1093 (void)strcat(lfmt, tmp);
1094 }
1095 /* Seconds: time_t cast to long long. */
1096 (void)strcat(lfmt, "lld");
1097
1098 /*
1099 * The stuff after the decimal point always needs zero
1100 * filling.
1101 */
1102 (void)strcat(lfmt, ".%0");
1103
1104 /*
1105 * We can "print" at most nine digits of precision. The
1106 * rest we will pad on at the end.
1107 *
1108 * Nanoseconds: long.
1109 */
1110 (void)snprintf(tmp, sizeof(tmp), "%dld", prec > 9 ? 9 : prec);
1111 (void)strcat(lfmt, tmp);
1112
1113 /*
1114 * For precision of less that nine digits, trim off the
1115 * less significant figures.
1116 */
1117 for (; prec < 9; prec++)
1118 nsecs /= 10;
1119
1120 /*
1121 * Use the format, and then tack on any zeroes that
1122 * might be required to make up the requested precision.
1123 */
1124 l = snprintf(buf, blen, lfmt, (long long)secs, nsecs);
1125 for (; prec > 9 && l < (int)blen; prec--, l++)
1126 (void)strcat(buf, "0");
1127 return (l);
1128 }
1129
1130 /*
1131 * Add on size and precision, if specified, to the format.
1132 */
1133 if (size != -1) {
1134 (void)snprintf(tmp, sizeof(tmp), "%d", size);
1135 (void)strcat(lfmt, tmp);
1136 }
1137 if (prec != -1) {
1138 (void)snprintf(tmp, sizeof(tmp), ".%d", prec);
1139 (void)strcat(lfmt, tmp);
1140 }
1141
1142 /*
1143 * String output uses the temporary sdata.
1144 */
1145 if (ofmt == FMTF_STRING) {
1146 if (sdata == NULL)
1147 errx(1, "%.*s: bad format", (int)flen, fmt);
1148 (void)strcat(lfmt, "s");
1149 return (snprintf(buf, blen, lfmt, sdata));
1150 }
1151
1152 /*
1153 * Ensure that sign extension does not cause bad looking output
1154 * for some forms.
1155 */
1156 if (small && ofmt != FMTF_DECIMAL)
1157 data = (uint32_t)data;
1158
1159 /*
1160 * The four "numeric" output forms.
1161 */
1162 (void)strcat(lfmt, "ll");
1163 switch (ofmt) {
1164 case FMTF_DECIMAL: (void)strcat(lfmt, "d"); break;
1165 case FMTF_OCTAL: (void)strcat(lfmt, "o"); break;
1166 case FMTF_UNSIGNED: (void)strcat(lfmt, "u"); break;
1167 case FMTF_HEX: (void)strcat(lfmt, "x"); break;
1168 }
1169
1170 /*
1171 * shift and round to nearest for kilobytes, megabytes,
1172 * gigabytes.
1173 */
1174 if (shift > 0) {
1175 data >>= (shift - 1);
1176 data++;
1177 data >>= 1;
1178 }
1179
1180 return (snprintf(buf, blen, lfmt, data));
1181 }
1182