No empty .Rs/.Re
[netbsd-mini2440.git] / usr.bin / stat / stat.c
blob60293c48373d4ed822d444b251d3f3d581d40547
1 /* $NetBSD: stat.c,v 1.27 2008/05/16 17:58:33 atatat Exp $ */
3 /*
4 * Copyright (c) 2002 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 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
34 #endif
36 #include <sys/cdefs.h>
37 #if !defined(lint)
38 __RCSID("$NetBSD: stat.c,v 1.27 2008/05/16 17:58:33 atatat Exp $");
39 #endif
41 #if ! HAVE_NBTOOL_CONFIG_H
42 #define HAVE_STRUCT_STAT_ST_FLAGS 1
43 #define HAVE_STRUCT_STAT_ST_GEN 1
44 #define HAVE_STRUCT_STAT_ST_BIRTHTIME 1
45 #define HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC 1
46 #define HAVE_STRUCT_STAT_ST_MTIMENSEC 1
47 #define HAVE_DEVNAME 1
48 #endif /* HAVE_NBTOOL_CONFIG_H */
50 #include <sys/types.h>
51 #include <sys/stat.h>
53 #include <ctype.h>
54 #include <err.h>
55 #include <errno.h>
56 #include <grp.h>
57 #include <limits.h>
58 #include <pwd.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <time.h>
63 #include <unistd.h>
65 #if HAVE_STRUCT_STAT_ST_FLAGS
66 #define DEF_F "%#Xf "
67 #define RAW_F "%f "
68 #define SHELL_F " st_flags=%f"
69 #else /* HAVE_STRUCT_STAT_ST_FLAGS */
70 #define DEF_F
71 #define RAW_F
72 #define SHELL_F
73 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
75 #if HAVE_STRUCT_STAT_ST_BIRTHTIME
76 #define DEF_B "\"%SB\" "
77 #define RAW_B "%B "
78 #define SHELL_B "st_birthtime=%B "
79 #else /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
80 #define DEF_B
81 #define RAW_B
82 #define SHELL_B
83 #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
85 #if HAVE_STRUCT_STAT_ST_ATIM
86 #define st_atimespec st_atim
87 #define st_ctimespec st_ctim
88 #define st_mtimespec st_mtim
89 #endif /* HAVE_STRUCT_STAT_ST_ATIM */
91 #define DEF_FORMAT \
92 "%d %i %Sp %l %Su %Sg %r %z \"%Sa\" \"%Sm\" \"%Sc\" " DEF_B \
93 "%k %b " DEF_F "%N"
94 #define RAW_FORMAT "%d %i %#p %l %u %g %r %z %a %m %c " RAW_B \
95 "%k %b " RAW_F "%N"
96 #define LS_FORMAT "%Sp %l %Su %Sg %Z %Sm %N%SY"
97 #define LSF_FORMAT "%Sp %l %Su %Sg %Z %Sm %N%T%SY"
98 #define SHELL_FORMAT \
99 "st_dev=%d st_ino=%i st_mode=%#p st_nlink=%l " \
100 "st_uid=%u st_gid=%g st_rdev=%r st_size=%z " \
101 "st_atime=%a st_mtime=%m st_ctime=%c " SHELL_B \
102 "st_blksize=%k st_blocks=%b" SHELL_F
103 #define LINUX_FORMAT \
104 " File: \"%N\"%n" \
105 " Size: %-11z FileType: %HT%n" \
106 " Mode: (%04OLp/%.10Sp) Uid: (%5u/%8Su) Gid: (%5g/%8Sg)%n" \
107 "Device: %Hd,%Ld Inode: %i Links: %l%n" \
108 "Access: %Sa%n" \
109 "Modify: %Sm%n" \
110 "Change: %Sc"
112 #define TIME_FORMAT "%b %e %T %Y"
114 #define FLAG_POUND 0x01
115 #define FLAG_SPACE 0x02
116 #define FLAG_PLUS 0x04
117 #define FLAG_ZERO 0x08
118 #define FLAG_MINUS 0x10
121 * These format characters must all be unique, except the magic one.
123 #define FMT_MAGIC '%'
124 #define FMT_DOT '.'
126 #define SIMPLE_NEWLINE 'n'
127 #define SIMPLE_TAB 't'
128 #define SIMPLE_PERCENT '%'
129 #define SIMPLE_NUMBER '@'
131 #define FMT_POUND '#'
132 #define FMT_SPACE ' '
133 #define FMT_PLUS '+'
134 #define FMT_ZERO '0'
135 #define FMT_MINUS '-'
137 #define FMT_DECIMAL 'D'
138 #define FMT_OCTAL 'O'
139 #define FMT_UNSIGNED 'U'
140 #define FMT_HEX 'X'
141 #define FMT_FLOAT 'F'
142 #define FMT_STRING 'S'
144 #define FMTF_DECIMAL 0x01
145 #define FMTF_OCTAL 0x02
146 #define FMTF_UNSIGNED 0x04
147 #define FMTF_HEX 0x08
148 #define FMTF_FLOAT 0x10
149 #define FMTF_STRING 0x20
151 #define HIGH_PIECE 'H'
152 #define MIDDLE_PIECE 'M'
153 #define LOW_PIECE 'L'
155 #define SHOW_realpath 'R'
156 #define SHOW_st_dev 'd'
157 #define SHOW_st_ino 'i'
158 #define SHOW_st_mode 'p'
159 #define SHOW_st_nlink 'l'
160 #define SHOW_st_uid 'u'
161 #define SHOW_st_gid 'g'
162 #define SHOW_st_rdev 'r'
163 #define SHOW_st_atime 'a'
164 #define SHOW_st_mtime 'm'
165 #define SHOW_st_ctime 'c'
166 #define SHOW_st_btime 'B'
167 #define SHOW_st_size 'z'
168 #define SHOW_st_blocks 'b'
169 #define SHOW_st_blksize 'k'
170 #define SHOW_st_flags 'f'
171 #define SHOW_st_gen 'v'
172 #define SHOW_symlink 'Y'
173 #define SHOW_filetype 'T'
174 #define SHOW_filename 'N'
175 #define SHOW_sizerdev 'Z'
177 void usage(const char *);
178 void output(const struct stat *, const char *,
179 const char *, int, int, int);
180 int format1(const struct stat *, /* stat info */
181 const char *, /* the file name */
182 const char *, int, /* the format string itself */
183 char *, size_t, /* a place to put the output */
184 int, int, int, int, /* the parsed format */
185 int, int);
187 const char *timefmt;
188 int linkfail;
190 #define addchar(s, c, nl) \
191 do { \
192 (void)fputc((c), (s)); \
193 (*nl) = ((c) == '\n'); \
194 } while (0/*CONSTCOND*/)
197 main(int argc, char *argv[])
199 struct stat st;
200 int ch, rc, errs, am_readlink;
201 int lsF, fmtchar, usestat, fn, nonl, quiet;
202 const char *statfmt, *options, *synopsis;
204 am_readlink = 0;
205 lsF = 0;
206 fmtchar = '\0';
207 usestat = 0;
208 nonl = 0;
209 quiet = 0;
210 linkfail = 0;
211 statfmt = NULL;
212 timefmt = NULL;
214 if (strcmp(getprogname(), "readlink") == 0) {
215 am_readlink = 1;
216 options = "fn";
217 synopsis = "[-fn] [file ...]";
218 statfmt = "%Y";
219 fmtchar = 'f';
220 quiet = 1;
221 } else {
222 options = "f:FlLnqrst:x";
223 synopsis = "[-FlLnqrsx] [-f format] [-t timefmt] [file ...]";
226 while ((ch = getopt(argc, argv, options)) != -1)
227 switch (ch) {
228 case 'F':
229 lsF = 1;
230 break;
231 case 'L':
232 usestat = 1;
233 break;
234 case 'n':
235 nonl = 1;
236 break;
237 case 'q':
238 quiet = 1;
239 break;
240 case 'f':
241 if (am_readlink) {
242 statfmt = "%R";
243 break;
245 statfmt = optarg;
246 /* FALLTHROUGH */
247 case 'l':
248 case 'r':
249 case 's':
250 case 'x':
251 if (fmtchar != 0)
252 errx(1, "can't use format '%c' with '%c'",
253 fmtchar, ch);
254 fmtchar = ch;
255 break;
256 case 't':
257 timefmt = optarg;
258 break;
259 default:
260 usage(synopsis);
263 argc -= optind;
264 argv += optind;
265 fn = 1;
267 if (fmtchar == '\0') {
268 if (lsF)
269 fmtchar = 'l';
270 else {
271 fmtchar = 'f';
272 statfmt = DEF_FORMAT;
276 if (lsF && fmtchar != 'l')
277 errx(1, "can't use format '%c' with -F", fmtchar);
279 switch (fmtchar) {
280 case 'f':
281 /* statfmt already set */
282 break;
283 case 'l':
284 statfmt = lsF ? LSF_FORMAT : LS_FORMAT;
285 break;
286 case 'r':
287 statfmt = RAW_FORMAT;
288 break;
289 case 's':
290 statfmt = SHELL_FORMAT;
291 break;
292 case 'x':
293 statfmt = LINUX_FORMAT;
294 if (timefmt == NULL)
295 timefmt = "%c";
296 break;
297 default:
298 usage(synopsis);
299 /*NOTREACHED*/
302 if (timefmt == NULL)
303 timefmt = TIME_FORMAT;
305 errs = 0;
306 do {
307 if (argc == 0)
308 rc = fstat(STDIN_FILENO, &st);
309 else if (usestat) {
311 * Try stat() and if it fails, fall back to
312 * lstat() just in case we're examining a
313 * broken symlink.
315 if ((rc = stat(argv[0], &st)) == -1 &&
316 errno == ENOENT &&
317 (rc = lstat(argv[0], &st)) == -1)
318 errno = ENOENT;
320 else
321 rc = lstat(argv[0], &st);
323 if (rc == -1) {
324 errs = 1;
325 linkfail = 1;
326 if (!quiet)
327 warn("%s: %s",
328 argc == 0 ? "(stdin)" : argv[0],
329 usestat ? "stat" : "lstat");
331 else
332 output(&st, argv[0], statfmt, fn, nonl, quiet);
334 argv++;
335 argc--;
336 fn++;
337 } while (argc > 0);
339 return (am_readlink ? linkfail : errs);
342 void
343 usage(const char *synopsis)
346 (void)fprintf(stderr, "usage: %s %s\n", getprogname(), synopsis);
347 exit(1);
351 * Parses a format string.
353 void
354 output(const struct stat *st, const char *file,
355 const char *statfmt, int fn, int nonl, int quiet)
357 int flags, size, prec, ofmt, hilo, what;
358 char buf[PATH_MAX + 4 + 1];
359 const char *subfmt;
360 int nl, t, i;
362 nl = 1;
363 while (*statfmt != '\0') {
366 * Non-format characters go straight out.
368 if (*statfmt != FMT_MAGIC) {
369 addchar(stdout, *statfmt, &nl);
370 statfmt++;
371 continue;
375 * The current format "substring" starts here,
376 * and then we skip the magic.
378 subfmt = statfmt;
379 statfmt++;
382 * Some simple one-character "formats".
384 switch (*statfmt) {
385 case SIMPLE_NEWLINE:
386 addchar(stdout, '\n', &nl);
387 statfmt++;
388 continue;
389 case SIMPLE_TAB:
390 addchar(stdout, '\t', &nl);
391 statfmt++;
392 continue;
393 case SIMPLE_PERCENT:
394 addchar(stdout, '%', &nl);
395 statfmt++;
396 continue;
397 case SIMPLE_NUMBER: {
398 char num[12], *p;
400 snprintf(num, sizeof(num), "%d", fn);
401 for (p = &num[0]; *p; p++)
402 addchar(stdout, *p, &nl);
403 statfmt++;
404 continue;
409 * This must be an actual format string. Format strings are
410 * similar to printf(3) formats up to a point, and are of
411 * the form:
413 * % required start of format
414 * [-# +0] opt. format characters
415 * size opt. field width
416 * . opt. decimal separator, followed by
417 * prec opt. precision
418 * fmt opt. output specifier (string, numeric, etc.)
419 * sub opt. sub field specifier (high, middle, low)
420 * datum required field specifier (size, mode, etc)
422 * Only the % and the datum selector are required. All data
423 * have reasonable default output forms. The "sub" specifier
424 * only applies to certain data (mode, dev, rdev, filetype).
425 * The symlink output defaults to STRING, yet will only emit
426 * the leading " -> " if STRING is explicitly specified. The
427 * sizerdev datum will generate rdev output for character or
428 * block devices, and size output for all others.
430 flags = 0;
431 do {
432 if (*statfmt == FMT_POUND)
433 flags |= FLAG_POUND;
434 else if (*statfmt == FMT_SPACE)
435 flags |= FLAG_SPACE;
436 else if (*statfmt == FMT_PLUS)
437 flags |= FLAG_PLUS;
438 else if (*statfmt == FMT_ZERO)
439 flags |= FLAG_ZERO;
440 else if (*statfmt == FMT_MINUS)
441 flags |= FLAG_MINUS;
442 else
443 break;
444 statfmt++;
445 } while (1/*CONSTCOND*/);
447 size = -1;
448 if (isdigit((unsigned)*statfmt)) {
449 size = 0;
450 while (isdigit((unsigned)*statfmt)) {
451 size = (size * 10) + (*statfmt - '0');
452 statfmt++;
453 if (size < 0)
454 goto badfmt;
458 prec = -1;
459 if (*statfmt == FMT_DOT) {
460 statfmt++;
462 prec = 0;
463 while (isdigit((unsigned)*statfmt)) {
464 prec = (prec * 10) + (*statfmt - '0');
465 statfmt++;
466 if (prec < 0)
467 goto badfmt;
471 #define fmtcase(x, y) case (y): (x) = (y); statfmt++; break
472 #define fmtcasef(x, y, z) case (y): (x) = (z); statfmt++; break
473 switch (*statfmt) {
474 fmtcasef(ofmt, FMT_DECIMAL, FMTF_DECIMAL);
475 fmtcasef(ofmt, FMT_OCTAL, FMTF_OCTAL);
476 fmtcasef(ofmt, FMT_UNSIGNED, FMTF_UNSIGNED);
477 fmtcasef(ofmt, FMT_HEX, FMTF_HEX);
478 fmtcasef(ofmt, FMT_FLOAT, FMTF_FLOAT);
479 fmtcasef(ofmt, FMT_STRING, FMTF_STRING);
480 default:
481 ofmt = 0;
482 break;
485 switch (*statfmt) {
486 fmtcase(hilo, HIGH_PIECE);
487 fmtcase(hilo, MIDDLE_PIECE);
488 fmtcase(hilo, LOW_PIECE);
489 default:
490 hilo = 0;
491 break;
494 switch (*statfmt) {
495 fmtcase(what, SHOW_realpath);
496 fmtcase(what, SHOW_st_dev);
497 fmtcase(what, SHOW_st_ino);
498 fmtcase(what, SHOW_st_mode);
499 fmtcase(what, SHOW_st_nlink);
500 fmtcase(what, SHOW_st_uid);
501 fmtcase(what, SHOW_st_gid);
502 fmtcase(what, SHOW_st_rdev);
503 fmtcase(what, SHOW_st_atime);
504 fmtcase(what, SHOW_st_mtime);
505 fmtcase(what, SHOW_st_ctime);
506 fmtcase(what, SHOW_st_btime);
507 fmtcase(what, SHOW_st_size);
508 fmtcase(what, SHOW_st_blocks);
509 fmtcase(what, SHOW_st_blksize);
510 fmtcase(what, SHOW_st_flags);
511 fmtcase(what, SHOW_st_gen);
512 fmtcase(what, SHOW_symlink);
513 fmtcase(what, SHOW_filetype);
514 fmtcase(what, SHOW_filename);
515 fmtcase(what, SHOW_sizerdev);
516 default:
517 goto badfmt;
519 #undef fmtcasef
520 #undef fmtcase
522 t = format1(st,
523 file,
524 subfmt, statfmt - subfmt,
525 buf, sizeof(buf),
526 flags, size, prec, ofmt, hilo, what);
528 for (i = 0; i < t && i < (int)(sizeof(buf) - 1); i++)
529 addchar(stdout, buf[i], &nl);
531 continue;
533 badfmt:
534 errx(1, "%.*s: bad format",
535 (int)(statfmt - subfmt + 1), subfmt);
538 if (!nl && !nonl)
539 (void)fputc('\n', stdout);
540 (void)fflush(stdout);
544 * Arranges output according to a single parsed format substring.
547 format1(const struct stat *st,
548 const char *file,
549 const char *fmt, int flen,
550 char *buf, size_t blen,
551 int flags, int size, int prec, int ofmt,
552 int hilo, int what)
554 u_int64_t data;
555 char *stmp, lfmt[24], tmp[20];
556 const char *sdata;
557 char smode[12], sid[12], path[PATH_MAX + 4];
558 struct passwd *pw;
559 struct group *gr;
560 struct tm *tm;
561 time_t secs;
562 long nsecs;
563 int l, small, formats, gottime, shift;
565 formats = 0;
566 small = 0;
567 gottime = 0;
568 secs = 0;
569 nsecs = 0;
570 shift = 0;
573 * First, pick out the data and tweak it based on hilo or
574 * specified output format (symlink output only).
576 switch (what) {
577 case SHOW_st_dev:
578 case SHOW_st_rdev:
579 small = (sizeof(st->st_dev) == 4);
580 data = (what == SHOW_st_dev) ? st->st_dev : st->st_rdev;
581 #if HAVE_DEVNAME
582 sdata = (what == SHOW_st_dev) ?
583 devname(st->st_dev, S_IFBLK) :
584 devname(st->st_rdev,
585 S_ISCHR(st->st_mode) ? S_IFCHR :
586 S_ISBLK(st->st_mode) ? S_IFBLK :
587 0U);
588 if (sdata == NULL)
589 sdata = "???";
590 #endif /* HAVE_DEVNAME */
591 if (hilo == HIGH_PIECE) {
592 data = major(data);
593 hilo = 0;
595 else if (hilo == LOW_PIECE) {
596 data = minor((unsigned)data);
597 hilo = 0;
599 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
600 #if HAVE_DEVNAME
601 FMTF_STRING;
602 #else /* HAVE_DEVNAME */
604 #endif /* HAVE_DEVNAME */
605 if (ofmt == 0)
606 ofmt = FMTF_UNSIGNED;
607 break;
608 case SHOW_st_ino:
609 small = (sizeof(st->st_ino) == 4);
610 data = st->st_ino;
611 sdata = NULL;
612 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
613 if (ofmt == 0)
614 ofmt = FMTF_UNSIGNED;
615 break;
616 case SHOW_st_mode:
617 small = (sizeof(st->st_mode) == 4);
618 data = st->st_mode;
619 strmode(st->st_mode, smode);
620 stmp = smode;
621 l = strlen(stmp);
622 if (stmp[l - 1] == ' ')
623 stmp[--l] = '\0';
624 if (hilo == HIGH_PIECE) {
625 data >>= 12;
626 stmp += 1;
627 stmp[3] = '\0';
628 hilo = 0;
630 else if (hilo == MIDDLE_PIECE) {
631 data = (data >> 9) & 07;
632 stmp += 4;
633 stmp[3] = '\0';
634 hilo = 0;
636 else if (hilo == LOW_PIECE) {
637 data &= 0777;
638 stmp += 7;
639 stmp[3] = '\0';
640 hilo = 0;
642 sdata = stmp;
643 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
644 FMTF_STRING;
645 if (ofmt == 0)
646 ofmt = FMTF_OCTAL;
647 break;
648 case SHOW_st_nlink:
649 small = (sizeof(st->st_dev) == 4);
650 data = st->st_nlink;
651 sdata = NULL;
652 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
653 if (ofmt == 0)
654 ofmt = FMTF_UNSIGNED;
655 break;
656 case SHOW_st_uid:
657 small = (sizeof(st->st_uid) == 4);
658 data = st->st_uid;
659 if ((pw = getpwuid(st->st_uid)) != NULL)
660 sdata = pw->pw_name;
661 else {
662 snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_uid);
663 sdata = sid;
665 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
666 FMTF_STRING;
667 if (ofmt == 0)
668 ofmt = FMTF_UNSIGNED;
669 break;
670 case SHOW_st_gid:
671 small = (sizeof(st->st_gid) == 4);
672 data = st->st_gid;
673 if ((gr = getgrgid(st->st_gid)) != NULL)
674 sdata = gr->gr_name;
675 else {
676 snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_gid);
677 sdata = sid;
679 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
680 FMTF_STRING;
681 if (ofmt == 0)
682 ofmt = FMTF_UNSIGNED;
683 break;
684 case SHOW_st_atime:
685 gottime = 1;
686 secs = st->st_atime;
687 #if HAVE_STRUCT_STAT_ST_MTIMENSEC
688 nsecs = st->st_atimensec;
689 #endif
690 /* FALLTHROUGH */
691 case SHOW_st_mtime:
692 if (!gottime) {
693 gottime = 1;
694 secs = st->st_mtime;
695 #if HAVE_STRUCT_STAT_ST_MTIMENSEC
696 nsecs = st->st_mtimensec;
697 #endif
699 /* FALLTHROUGH */
700 case SHOW_st_ctime:
701 if (!gottime) {
702 gottime = 1;
703 secs = st->st_ctime;
704 #if HAVE_STRUCT_STAT_ST_MTIMENSEC
705 nsecs = st->st_ctimensec;
706 #endif
708 /* FALLTHROUGH */
709 #if HAVE_STRUCT_STAT_ST_BIRTHTIME
710 case SHOW_st_btime:
711 if (!gottime) {
712 gottime = 1;
713 secs = st->st_birthtime;
714 #if HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC
715 nsecs = st->st_birthtimensec;
716 #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC */
718 #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
719 small = (sizeof(secs) == 4);
720 data = secs;
721 small = 1;
722 tm = localtime(&secs);
723 (void)strftime(path, sizeof(path), timefmt, tm);
724 sdata = path;
725 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
726 FMTF_FLOAT | FMTF_STRING;
727 if (ofmt == 0)
728 ofmt = FMTF_DECIMAL;
729 break;
730 case SHOW_st_size:
731 small = (sizeof(st->st_size) == 4);
732 data = st->st_size;
733 sdata = NULL;
734 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
735 if (ofmt == 0)
736 ofmt = FMTF_UNSIGNED;
737 switch (hilo) {
738 case HIGH_PIECE:
739 shift = 30; /* gigabytes */
740 hilo = 0;
741 break;
742 case MIDDLE_PIECE:
743 shift = 20; /* megabytes */
744 hilo = 0;
745 break;
746 case LOW_PIECE:
747 shift = 10; /* kilobytes */
748 hilo = 0;
749 break;
751 break;
752 case SHOW_st_blocks:
753 small = (sizeof(st->st_blocks) == 4);
754 data = st->st_blocks;
755 sdata = NULL;
756 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
757 if (ofmt == 0)
758 ofmt = FMTF_UNSIGNED;
759 break;
760 case SHOW_st_blksize:
761 small = (sizeof(st->st_blksize) == 4);
762 data = st->st_blksize;
763 sdata = NULL;
764 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
765 if (ofmt == 0)
766 ofmt = FMTF_UNSIGNED;
767 break;
768 #if HAVE_STRUCT_STAT_ST_FLAGS
769 case SHOW_st_flags:
770 small = (sizeof(st->st_flags) == 4);
771 data = st->st_flags;
772 sdata = NULL;
773 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
774 if (ofmt == 0)
775 ofmt = FMTF_UNSIGNED;
776 break;
777 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
778 #if HAVE_STRUCT_STAT_ST_GEN
779 case SHOW_st_gen:
780 small = (sizeof(st->st_gen) == 4);
781 data = st->st_gen;
782 sdata = NULL;
783 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
784 if (ofmt == 0)
785 ofmt = FMTF_UNSIGNED;
786 break;
787 #endif /* HAVE_STRUCT_STAT_ST_GEN */
788 case SHOW_realpath:
789 small = 0;
790 data = 0;
791 if (file == NULL) {
792 (void)strncpy(path, "(stdin)", sizeof(path));
793 sdata = path;
794 } else {
795 snprintf(path, sizeof(path), " -> ");
796 if (realpath(file, path + 4) == NULL) {
797 linkfail = 1;
798 l = 0;
799 path[0] = '\0';
801 sdata = path + (ofmt == FMTF_STRING ? 0 : 4);
804 formats = FMTF_STRING;
805 if (ofmt == 0)
806 ofmt = FMTF_STRING;
807 break;
808 case SHOW_symlink:
809 small = 0;
810 data = 0;
811 if (S_ISLNK(st->st_mode)) {
812 snprintf(path, sizeof(path), " -> ");
813 l = readlink(file, path + 4, sizeof(path) - 4 - 1);
814 if (l == -1) {
815 linkfail = 1;
816 l = 0;
817 path[0] = '\0';
819 path[l + 4] = '\0';
820 sdata = path + (ofmt == FMTF_STRING ? 0 : 4);
822 else {
823 linkfail = 1;
824 sdata = "";
826 formats = FMTF_STRING;
827 if (ofmt == 0)
828 ofmt = FMTF_STRING;
829 break;
830 case SHOW_filetype:
831 small = 0;
832 data = 0;
833 sdata = "";
834 if (hilo == 0 || hilo == LOW_PIECE) {
835 switch (st->st_mode & S_IFMT) {
836 case S_IFIFO: sdata = "|"; break;
837 case S_IFDIR: sdata = "/"; break;
838 case S_IFREG:
839 if (st->st_mode &
840 (S_IXUSR | S_IXGRP | S_IXOTH))
841 sdata = "*";
842 break;
843 case S_IFLNK: sdata = "@"; break;
844 #ifdef S_IFSOCK
845 case S_IFSOCK: sdata = "="; break;
846 #endif
847 #ifdef S_IFWHT
848 case S_IFWHT: sdata = "%"; break;
849 #endif /* S_IFWHT */
850 #ifdef S_IFDOOR
851 case S_IFDOOR: sdata = ">"; break;
852 #endif /* S_IFDOOR */
854 hilo = 0;
856 else if (hilo == HIGH_PIECE) {
857 switch (st->st_mode & S_IFMT) {
858 case S_IFIFO: sdata = "Fifo File"; break;
859 case S_IFCHR: sdata = "Character Device"; break;
860 case S_IFDIR: sdata = "Directory"; break;
861 case S_IFBLK: sdata = "Block Device"; break;
862 case S_IFREG: sdata = "Regular File"; break;
863 case S_IFLNK: sdata = "Symbolic Link"; break;
864 #ifdef S_IFSOCK
865 case S_IFSOCK: sdata = "Socket"; break;
866 #endif
867 #ifdef S_IFWHT
868 case S_IFWHT: sdata = "Whiteout File"; break;
869 #endif /* S_IFWHT */
870 #ifdef S_IFDOOR
871 case S_IFDOOR: sdata = "Door"; break;
872 #endif /* S_IFDOOR */
873 default: sdata = "???"; break;
875 hilo = 0;
877 formats = FMTF_STRING;
878 if (ofmt == 0)
879 ofmt = FMTF_STRING;
880 break;
881 case SHOW_filename:
882 small = 0;
883 data = 0;
884 if (file == NULL) {
885 (void)strncpy(path, "(stdin)", sizeof(path));
886 if (hilo == HIGH_PIECE || hilo == LOW_PIECE)
887 hilo = 0;
889 else if (hilo == 0)
890 (void)strncpy(path, file, sizeof(path));
891 else {
892 char *s;
893 (void)strncpy(path, file, sizeof(path));
894 s = strrchr(path, '/');
895 if (s != NULL) {
896 /* trim off trailing /'s */
897 while (s != path &&
898 s[0] == '/' && s[1] == '\0')
899 *s-- = '\0';
900 s = strrchr(path, '/');
902 if (hilo == HIGH_PIECE) {
903 if (s == NULL)
904 (void)strncpy(path, ".", sizeof(path));
905 else {
906 while (s != path && s[0] == '/')
907 *s-- = '\0';
909 hilo = 0;
911 else if (hilo == LOW_PIECE) {
912 if (s != NULL && s[1] != '\0')
913 (void)strncpy(path, s + 1,
914 sizeof(path));
915 hilo = 0;
918 sdata = path;
919 formats = FMTF_STRING;
920 if (ofmt == 0)
921 ofmt = FMTF_STRING;
922 break;
923 case SHOW_sizerdev:
924 if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
925 char majdev[20], mindev[20];
926 int l1, l2;
928 l1 = format1(st,
929 file,
930 fmt, flen,
931 majdev, sizeof(majdev),
932 flags, size, prec,
933 ofmt, HIGH_PIECE, SHOW_st_rdev);
934 l2 = format1(st,
935 file,
936 fmt, flen,
937 mindev, sizeof(mindev),
938 flags, size, prec,
939 ofmt, LOW_PIECE, SHOW_st_rdev);
940 return (snprintf(buf, blen, "%.*s,%.*s",
941 l1, majdev, l2, mindev));
943 else {
944 return (format1(st,
945 file,
946 fmt, flen,
947 buf, blen,
948 flags, size, prec,
949 ofmt, 0, SHOW_st_size));
951 /*NOTREACHED*/
952 default:
953 errx(1, "%.*s: bad format", (int)flen, fmt);
957 * If a subdatum was specified but not supported, or an output
958 * format was selected that is not supported, that's an error.
960 if (hilo != 0 || (ofmt & formats) == 0)
961 errx(1, "%.*s: bad format", (int)flen, fmt);
964 * Assemble the format string for passing to printf(3).
966 lfmt[0] = '\0';
967 (void)strcat(lfmt, "%");
968 if (flags & FLAG_POUND)
969 (void)strcat(lfmt, "#");
970 if (flags & FLAG_SPACE)
971 (void)strcat(lfmt, " ");
972 if (flags & FLAG_PLUS)
973 (void)strcat(lfmt, "+");
974 if (flags & FLAG_MINUS)
975 (void)strcat(lfmt, "-");
976 if (flags & FLAG_ZERO)
977 (void)strcat(lfmt, "0");
980 * Only the timespecs support the FLOAT output format, and that
981 * requires work that differs from the other formats.
983 if (ofmt == FMTF_FLOAT) {
985 * Nothing after the decimal point, so just print seconds.
987 if (prec == 0) {
988 if (size != -1) {
989 (void)snprintf(tmp, sizeof(tmp), "%d", size);
990 (void)strcat(lfmt, tmp);
992 (void)strcat(lfmt, "d");
993 return (snprintf(buf, blen, lfmt, secs));
997 * Unspecified precision gets all the precision we have:
998 * 9 digits.
1000 if (prec == -1)
1001 prec = 9;
1004 * Adjust the size for the decimal point and the digits
1005 * that will follow.
1007 size -= prec + 1;
1010 * Any leftover size that's legitimate will be used.
1012 if (size > 0) {
1013 (void)snprintf(tmp, sizeof(tmp), "%d", size);
1014 (void)strcat(lfmt, tmp);
1016 (void)strcat(lfmt, "d");
1019 * The stuff after the decimal point always needs zero
1020 * filling.
1022 (void)strcat(lfmt, ".%0");
1025 * We can "print" at most nine digits of precision. The
1026 * rest we will pad on at the end.
1028 (void)snprintf(tmp, sizeof(tmp), "%dd", prec > 9 ? 9 : prec);
1029 (void)strcat(lfmt, tmp);
1032 * For precision of less that nine digits, trim off the
1033 * less significant figures.
1035 for (; prec < 9; prec++)
1036 nsecs /= 10;
1039 * Use the format, and then tack on any zeroes that
1040 * might be required to make up the requested precision.
1042 l = snprintf(buf, blen, lfmt, secs, nsecs);
1043 for (; prec > 9 && l < (int)blen; prec--, l++)
1044 (void)strcat(buf, "0");
1045 return (l);
1049 * Add on size and precision, if specified, to the format.
1051 if (size != -1) {
1052 (void)snprintf(tmp, sizeof(tmp), "%d", size);
1053 (void)strcat(lfmt, tmp);
1055 if (prec != -1) {
1056 (void)snprintf(tmp, sizeof(tmp), ".%d", prec);
1057 (void)strcat(lfmt, tmp);
1061 * String output uses the temporary sdata.
1063 if (ofmt == FMTF_STRING) {
1064 if (sdata == NULL)
1065 errx(1, "%.*s: bad format", (int)flen, fmt);
1066 (void)strcat(lfmt, "s");
1067 return (snprintf(buf, blen, lfmt, sdata));
1071 * Ensure that sign extension does not cause bad looking output
1072 * for some forms.
1074 if (small && ofmt != FMTF_DECIMAL)
1075 data = (u_int32_t)data;
1078 * The four "numeric" output forms.
1080 (void)strcat(lfmt, "ll");
1081 switch (ofmt) {
1082 case FMTF_DECIMAL: (void)strcat(lfmt, "d"); break;
1083 case FMTF_OCTAL: (void)strcat(lfmt, "o"); break;
1084 case FMTF_UNSIGNED: (void)strcat(lfmt, "u"); break;
1085 case FMTF_HEX: (void)strcat(lfmt, "x"); break;
1089 * shift and round to nearest for kilobytes, megabytes,
1090 * gigabytes.
1092 if (shift > 0) {
1093 data >>= (shift - 1);
1094 data++;
1095 data >>= 1;
1098 return (snprintf(buf, blen, lfmt, data));