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