file_cmds-184.tar.gz
[apple/file_cmds.git] / stat / stat.c
1 /*
2 * Copyright (c) 2002 The NetBSD Foundation, Inc.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to The NetBSD Foundation
6 * by Andrew Brown.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the NetBSD
19 * Foundation, Inc. and its contributors.
20 * 4. Neither the name of The NetBSD Foundation nor the names of its
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
25 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
28 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37 #include <sys/cdefs.h>
38 #if 0
39 #ifndef lint
40 __RCSID("$NetBSD: stat.c,v 1.13 2003/07/25 03:21:17 atatat Exp $");
41 #endif
42 #endif
43
44 __FBSDID("$FreeBSD: src/usr.bin/stat/stat.c,v 1.6 2003/10/06 01:55:17 dougb Exp $");
45
46 #if HAVE_CONFIG_H
47 #include "config.h"
48 #else /* HAVE_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_CONFIG_H */
55
56 #include <sys/types.h>
57 #include <sys/stat.h>
58
59 #include <ctype.h>
60 #include <err.h>
61 #include <grp.h>
62 #include <limits.h>
63 #include <pwd.h>
64 #include <stdio.h>
65 #include <stdlib.h>
66 #include <string.h>
67 #include <time.h>
68 #include <unistd.h>
69
70 #ifdef __APPLE__
71 #define stat stat64
72 #define fstat fstat64
73 #define lstat lstat64
74 #endif /* __APPLE__ */
75
76 #if HAVE_STRUCT_STAT_ST_FLAGS
77 #define DEF_F "%#Xf "
78 #define RAW_F "%f "
79 #define SHELL_F " st_flags=%f"
80 #else /* HAVE_STRUCT_STAT_ST_FLAGS */
81 #define DEF_F
82 #define RAW_F
83 #define SHELL_F
84 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
85
86 #if HAVE_STRUCT_STAT_ST_BIRTHTIME
87 #define DEF_B "\"%SB\" "
88 #define RAW_B "%B "
89 #define SHELL_B "st_birthtime=%B "
90 #else /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
91 #define DEF_B
92 #define RAW_B
93 #define SHELL_B
94 #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
95
96 #if HAVE_STRUCT_STAT_ST_ATIM
97 #define st_atimespec st_atim
98 #define st_ctimespec st_ctim
99 #define st_mtimespec st_mtim
100 #endif /* HAVE_STRUCT_STAT_ST_ATIM */
101
102 #define DEF_FORMAT \
103 "%d %i %Sp %l %Su %Sg %r %z \"%Sa\" \"%Sm\" \"%Sc\" " DEF_B \
104 "%k %b " DEF_F "%N"
105 #define RAW_FORMAT "%d %i %#p %l %u %g %r %z %a %m %c " RAW_B \
106 "%k %b " RAW_F "%N"
107 #define LS_FORMAT "%Sp %l %Su %Sg %Z %Sm %N%SY"
108 #define LSF_FORMAT "%Sp %l %Su %Sg %Z %Sm %N%T%SY"
109 #define SHELL_FORMAT \
110 "st_dev=%d st_ino=%i st_mode=%#p st_nlink=%l " \
111 "st_uid=%u st_gid=%g st_rdev=%r st_size=%z " \
112 "st_atime=%a st_mtime=%m st_ctime=%c " SHELL_B \
113 "st_blksize=%k st_blocks=%b" SHELL_F
114 #define LINUX_FORMAT \
115 " File: \"%N\"%n" \
116 " Size: %-11z FileType: %HT%n" \
117 " Mode: (%04OA/%.10Sp) Uid: (%5u/%8Su) Gid: (%5g/%8Sg)%n" \
118 "Device: %Hd,%Ld Inode: %i Links: %l%n" \
119 "Access: %Sa%n" \
120 "Modify: %Sm%n" \
121 "Change: %Sc"
122
123 #define TIME_FORMAT "%b %e %T %Y"
124
125 #define FLAG_POUND 0x01
126 #define FLAG_SPACE 0x02
127 #define FLAG_PLUS 0x04
128 #define FLAG_ZERO 0x08
129 #define FLAG_MINUS 0x10
130
131 /*
132 * These format characters must all be unique, except the magic one.
133 */
134 #define FMT_MAGIC '%'
135 #define FMT_DOT '.'
136
137 #define SIMPLE_NEWLINE 'n'
138 #define SIMPLE_TAB 't'
139 #define SIMPLE_PERCENT '%'
140 #define SIMPLE_NUMBER '@'
141
142 #define FMT_POUND '#'
143 #define FMT_SPACE ' '
144 #define FMT_PLUS '+'
145 #define FMT_ZERO '0'
146 #define FMT_MINUS '-'
147
148 #define FMT_DECIMAL 'D'
149 #define FMT_OCTAL 'O'
150 #define FMT_UNSIGNED 'U'
151 #define FMT_HEX 'X'
152 #define FMT_FLOAT 'F'
153 #define FMT_STRING 'S'
154
155 #define FMTF_DECIMAL 0x01
156 #define FMTF_OCTAL 0x02
157 #define FMTF_UNSIGNED 0x04
158 #define FMTF_HEX 0x08
159 #define FMTF_FLOAT 0x10
160 #define FMTF_STRING 0x20
161
162 #define HIGH_PIECE 'H'
163 #define MIDDLE_PIECE 'M'
164 #define LOW_PIECE 'L'
165
166 #define SHOW_st_dev 'd'
167 #define SHOW_st_ino 'i'
168 #define SHOW_st_mode 'p'
169 #define SHOW_st_mode2 'A'
170 #define SHOW_st_nlink 'l'
171 #define SHOW_st_uid 'u'
172 #define SHOW_st_gid 'g'
173 #define SHOW_st_rdev 'r'
174 #define SHOW_st_atime 'a'
175 #define SHOW_st_mtime 'm'
176 #define SHOW_st_ctime 'c'
177 #define SHOW_st_btime 'B'
178 #define SHOW_st_size 'z'
179 #define SHOW_st_blocks 'b'
180 #define SHOW_st_blksize 'k'
181 #define SHOW_st_flags 'f'
182 #define SHOW_st_gen 'v'
183 #define SHOW_symlink 'Y'
184 #define SHOW_filetype 'T'
185 #define SHOW_filename 'N'
186 #define SHOW_sizerdev 'Z'
187
188 void usage(const char *);
189 void output(const struct stat *, const char *,
190 const char *, int, int, int);
191 int format1(const struct stat *, /* stat info */
192 const char *, /* the file name */
193 const char *, int, /* the format string itself */
194 char *, size_t, /* a place to put the output */
195 int, int, int, int, /* the parsed format */
196 int, int);
197
198 char *timefmt;
199 int linkfail;
200
201 #define addchar(s, c, nl) \
202 do { \
203 (void)fputc((c), (s)); \
204 (*nl) = ((c) == '\n'); \
205 } while (0/*CONSTCOND*/)
206
207 int
208 main(int argc, char *argv[])
209 {
210 struct stat st;
211 int ch, rc, errs, am_readlink;
212 int lsF, fmtchar, usestat, fn, nonl, quiet;
213 char *statfmt, *options, *synopsis;
214
215 am_readlink = 0;
216 lsF = 0;
217 fmtchar = '\0';
218 usestat = 0;
219 nonl = 0;
220 quiet = 0;
221 linkfail = 0;
222 statfmt = NULL;
223 timefmt = NULL;
224
225 if (strcmp(getprogname(), "readlink") == 0) {
226 am_readlink = 1;
227 options = "n";
228 synopsis = "[-n] [file ...]";
229 statfmt = "%Y";
230 fmtchar = 'f';
231 quiet = 1;
232 } else {
233 options = "f:FlLnqrst:x";
234 synopsis = "[-FlLnqrsx] [-f format] [-t timefmt] [file ...]";
235 }
236
237 while ((ch = getopt(argc, argv, options)) != -1)
238 switch (ch) {
239 case 'F':
240 lsF = 1;
241 break;
242 case 'L':
243 usestat = 1;
244 break;
245 case 'n':
246 nonl = 1;
247 break;
248 case 'q':
249 quiet = 1;
250 break;
251 case 'f':
252 statfmt = optarg;
253 /* FALLTHROUGH */
254 case 'l':
255 case 'r':
256 case 's':
257 case 'x':
258 if (fmtchar != 0)
259 errx(1, "can't use format '%c' with '%c'",
260 fmtchar, ch);
261 fmtchar = ch;
262 break;
263 case 't':
264 timefmt = optarg;
265 break;
266 default:
267 usage(synopsis);
268 }
269
270 argc -= optind;
271 argv += optind;
272 fn = 1;
273
274 if (fmtchar == '\0') {
275 if (lsF)
276 fmtchar = 'l';
277 else {
278 fmtchar = 'f';
279 statfmt = DEF_FORMAT;
280 }
281 }
282
283 if (lsF && fmtchar != 'l')
284 errx(1, "can't use format '%c' with -F", fmtchar);
285
286 switch (fmtchar) {
287 case 'f':
288 /* statfmt already set */
289 break;
290 case 'l':
291 statfmt = lsF ? LSF_FORMAT : LS_FORMAT;
292 break;
293 case 'r':
294 statfmt = RAW_FORMAT;
295 break;
296 case 's':
297 statfmt = SHELL_FORMAT;
298 break;
299 case 'x':
300 statfmt = LINUX_FORMAT;
301 if (timefmt == NULL)
302 timefmt = "%c";
303 break;
304 default:
305 usage(synopsis);
306 /*NOTREACHED*/
307 }
308
309 if (timefmt == NULL)
310 timefmt = TIME_FORMAT;
311
312 errs = 0;
313 do {
314 if (argc == 0)
315 rc = fstat(STDIN_FILENO, &st);
316 else if (usestat)
317 rc = stat(argv[0], &st);
318 else
319 rc = lstat(argv[0], &st);
320
321 if (rc == -1) {
322 errs = 1;
323 linkfail = 1;
324 if (!quiet)
325 warn("%s: stat",
326 argc == 0 ? "(stdin)" : argv[0]);
327 }
328 else
329 output(&st, argv[0], statfmt, fn, nonl, quiet);
330
331 argv++;
332 argc--;
333 fn++;
334 } while (argc > 0);
335
336 return (am_readlink ? linkfail : errs);
337 }
338
339 void
340 usage(const char *synopsis)
341 {
342
343 (void)fprintf(stderr, "usage: %s %s\n", getprogname(), synopsis);
344 exit(1);
345 }
346
347 /*
348 * Parses a format string.
349 */
350 void
351 output(const struct stat *st, const char *file,
352 const char *statfmt, int fn, int nonl, int quiet)
353 {
354 int flags, size, prec, ofmt, hilo, what;
355 char buf[PATH_MAX];
356 const char *subfmt;
357 int nl, t, i;
358
359 nl = 1;
360 while (*statfmt != '\0') {
361
362 /*
363 * Non-format characters go straight out.
364 */
365 if (*statfmt != FMT_MAGIC) {
366 addchar(stdout, *statfmt, &nl);
367 statfmt++;
368 continue;
369 }
370
371 /*
372 * The current format "substring" starts here,
373 * and then we skip the magic.
374 */
375 subfmt = statfmt;
376 statfmt++;
377
378 /*
379 * Some simple one-character "formats".
380 */
381 switch (*statfmt) {
382 case SIMPLE_NEWLINE:
383 addchar(stdout, '\n', &nl);
384 statfmt++;
385 continue;
386 case SIMPLE_TAB:
387 addchar(stdout, '\t', &nl);
388 statfmt++;
389 continue;
390 case SIMPLE_PERCENT:
391 addchar(stdout, '%', &nl);
392 statfmt++;
393 continue;
394 case SIMPLE_NUMBER: {
395 char num[12], *p;
396
397 snprintf(num, sizeof(num), "%d", fn);
398 for (p = &num[0]; *p; p++)
399 addchar(stdout, *p, &nl);
400 statfmt++;
401 continue;
402 }
403 }
404
405 /*
406 * This must be an actual format string. Format strings are
407 * similar to printf(3) formats up to a point, and are of
408 * the form:
409 *
410 * % required start of format
411 * [-# +0] opt. format characters
412 * size opt. field width
413 * . opt. decimal separator, followed by
414 * prec opt. precision
415 * fmt opt. output specifier (string, numeric, etc.)
416 * sub opt. sub field specifier (high, middle, low)
417 * datum required field specifier (size, mode, etc)
418 *
419 * Only the % and the datum selector are required. All data
420 * have reasonable default output forms. The "sub" specifier
421 * only applies to certain data (mode, dev, rdev, filetype).
422 * The symlink output defaults to STRING, yet will only emit
423 * the leading " -> " if STRING is explicitly specified. The
424 * sizerdev datum will generate rdev output for character or
425 * block devices, and size output for all others.
426 */
427 flags = 0;
428 do {
429 if (*statfmt == FMT_POUND)
430 flags |= FLAG_POUND;
431 else if (*statfmt == FMT_SPACE)
432 flags |= FLAG_SPACE;
433 else if (*statfmt == FMT_PLUS)
434 flags |= FLAG_PLUS;
435 else if (*statfmt == FMT_ZERO)
436 flags |= FLAG_ZERO;
437 else if (*statfmt == FMT_MINUS)
438 flags |= FLAG_MINUS;
439 else
440 break;
441 statfmt++;
442 } while (1/*CONSTCOND*/);
443
444 size = -1;
445 if (isdigit((unsigned)*statfmt)) {
446 size = 0;
447 while (isdigit((unsigned)*statfmt)) {
448 size = (size * 10) + (*statfmt - '0');
449 statfmt++;
450 if (size < 0)
451 goto badfmt;
452 }
453 }
454
455 prec = -1;
456 if (*statfmt == FMT_DOT) {
457 statfmt++;
458
459 prec = 0;
460 while (isdigit((unsigned)*statfmt)) {
461 prec = (prec * 10) + (*statfmt - '0');
462 statfmt++;
463 if (prec < 0)
464 goto badfmt;
465 }
466 }
467
468 #define fmtcase(x, y) case (y): (x) = (y); statfmt++; break
469 #define fmtcasef(x, y, z) case (y): (x) = (z); statfmt++; break
470 switch (*statfmt) {
471 fmtcasef(ofmt, FMT_DECIMAL, FMTF_DECIMAL);
472 fmtcasef(ofmt, FMT_OCTAL, FMTF_OCTAL);
473 fmtcasef(ofmt, FMT_UNSIGNED, FMTF_UNSIGNED);
474 fmtcasef(ofmt, FMT_HEX, FMTF_HEX);
475 fmtcasef(ofmt, FMT_FLOAT, FMTF_FLOAT);
476 fmtcasef(ofmt, FMT_STRING, FMTF_STRING);
477 default:
478 ofmt = 0;
479 break;
480 }
481
482 switch (*statfmt) {
483 fmtcase(hilo, HIGH_PIECE);
484 fmtcase(hilo, MIDDLE_PIECE);
485 fmtcase(hilo, LOW_PIECE);
486 default:
487 hilo = 0;
488 break;
489 }
490
491 switch (*statfmt) {
492 fmtcase(what, SHOW_st_dev);
493 fmtcase(what, SHOW_st_ino);
494 fmtcase(what, SHOW_st_mode);
495 fmtcase(what, SHOW_st_mode2);
496 fmtcase(what, SHOW_st_nlink);
497 fmtcase(what, SHOW_st_uid);
498 fmtcase(what, SHOW_st_gid);
499 fmtcase(what, SHOW_st_rdev);
500 fmtcase(what, SHOW_st_atime);
501 fmtcase(what, SHOW_st_mtime);
502 fmtcase(what, SHOW_st_ctime);
503 fmtcase(what, SHOW_st_btime);
504 fmtcase(what, SHOW_st_size);
505 fmtcase(what, SHOW_st_blocks);
506 fmtcase(what, SHOW_st_blksize);
507 fmtcase(what, SHOW_st_flags);
508 fmtcase(what, SHOW_st_gen);
509 fmtcase(what, SHOW_symlink);
510 fmtcase(what, SHOW_filetype);
511 fmtcase(what, SHOW_filename);
512 fmtcase(what, SHOW_sizerdev);
513 default:
514 goto badfmt;
515 }
516 #undef fmtcasef
517 #undef fmtcase
518
519 t = format1(st,
520 file,
521 subfmt, statfmt - subfmt,
522 buf, sizeof(buf),
523 flags, size, prec, ofmt, hilo, what);
524
525 for (i = 0; i < t && i < sizeof(buf); i++)
526 addchar(stdout, buf[i], &nl);
527
528 continue;
529
530 badfmt:
531 errx(1, "%.*s: bad format",
532 (int)(statfmt - subfmt + 1), subfmt);
533 }
534
535 if (!nl && !nonl)
536 (void)fputc('\n', stdout);
537 (void)fflush(stdout);
538 }
539
540 /*
541 * Arranges output according to a single parsed format substring.
542 */
543 int
544 format1(const struct stat *st,
545 const char *file,
546 const char *fmt, int flen,
547 char *buf, size_t blen,
548 int flags, int size, int prec, int ofmt,
549 int hilo, int what)
550 {
551 u_int64_t data;
552 char *sdata, lfmt[24], tmp[20];
553 char smode[12], sid[12], path[PATH_MAX + 4];
554 struct passwd *pw;
555 struct group *gr;
556 const struct timespec *tsp;
557 struct timespec ts;
558 struct tm *tm;
559 int l, small, formats;
560
561 tsp = NULL;
562 formats = 0;
563 small = 0;
564
565 /*
566 * First, pick out the data and tweak it based on hilo or
567 * specified output format (symlink output only).
568 */
569 switch (what) {
570 case SHOW_st_dev:
571 case SHOW_st_rdev:
572 small = (sizeof(st->st_dev) == 4);
573 data = (what == SHOW_st_dev) ? st->st_dev : st->st_rdev;
574 #if HAVE_DEVNAME
575 sdata = (what == SHOW_st_dev) ?
576 devname(st->st_dev, S_IFBLK) :
577 devname(st->st_rdev,
578 S_ISCHR(st->st_mode) ? S_IFCHR :
579 S_ISBLK(st->st_mode) ? S_IFBLK :
580 0U);
581 if (sdata == NULL)
582 sdata = "???";
583 #endif /* HAVE_DEVNAME */
584 if (hilo == HIGH_PIECE) {
585 data = major(data);
586 hilo = 0;
587 }
588 else if (hilo == LOW_PIECE) {
589 data = minor((unsigned)data);
590 hilo = 0;
591 }
592 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
593 #if HAVE_DEVNAME
594 FMTF_STRING;
595 #else /* HAVE_DEVNAME */
596 0;
597 #endif /* HAVE_DEVNAME */
598 if (ofmt == 0)
599 ofmt = FMTF_UNSIGNED;
600 break;
601 case SHOW_st_ino:
602 small = (sizeof(st->st_ino) == 4);
603 data = st->st_ino;
604 sdata = NULL;
605 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
606 if (ofmt == 0)
607 ofmt = FMTF_UNSIGNED;
608 break;
609 case SHOW_st_mode:
610 case SHOW_st_mode2:
611 small = (sizeof(st->st_mode) == 4);
612 data = st->st_mode;
613 strmode(st->st_mode, smode);
614 sdata = smode;
615 l = strlen(sdata);
616 if (sdata[l - 1] == ' ')
617 sdata[--l] = '\0';
618 if (what == SHOW_st_mode2)
619 data &= 07777;
620 if (hilo == HIGH_PIECE) {
621 data >>= 12;
622 sdata += 1;
623 sdata[3] = '\0';
624 hilo = 0;
625 }
626 else if (hilo == MIDDLE_PIECE) {
627 data = (data >> 9) & 07;
628 sdata += 4;
629 sdata[3] = '\0';
630 hilo = 0;
631 }
632 else if (hilo == LOW_PIECE) {
633 data &= 0777;
634 sdata += 7;
635 sdata[3] = '\0';
636 hilo = 0;
637 }
638 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
639 FMTF_STRING;
640 if (ofmt == 0)
641 ofmt = FMTF_OCTAL;
642 break;
643 case SHOW_st_nlink:
644 small = (sizeof(st->st_dev) == 4);
645 data = st->st_nlink;
646 sdata = NULL;
647 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
648 if (ofmt == 0)
649 ofmt = FMTF_UNSIGNED;
650 break;
651 case SHOW_st_uid:
652 small = (sizeof(st->st_uid) == 4);
653 data = st->st_uid;
654 if ((pw = getpwuid(st->st_uid)) != NULL)
655 sdata = pw->pw_name;
656 else {
657 snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_uid);
658 sdata = sid;
659 }
660 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
661 FMTF_STRING;
662 if (ofmt == 0)
663 ofmt = FMTF_UNSIGNED;
664 break;
665 case SHOW_st_gid:
666 small = (sizeof(st->st_gid) == 4);
667 data = st->st_gid;
668 if ((gr = getgrgid(st->st_gid)) != NULL)
669 sdata = gr->gr_name;
670 else {
671 snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_gid);
672 sdata = sid;
673 }
674 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
675 FMTF_STRING;
676 if (ofmt == 0)
677 ofmt = FMTF_UNSIGNED;
678 break;
679 case SHOW_st_atime:
680 tsp = &st->st_atimespec;
681 /* FALLTHROUGH */
682 case SHOW_st_mtime:
683 if (tsp == NULL)
684 tsp = &st->st_mtimespec;
685 /* FALLTHROUGH */
686 case SHOW_st_ctime:
687 if (tsp == NULL)
688 tsp = &st->st_ctimespec;
689 /* FALLTHROUGH */
690 #if HAVE_STRUCT_STAT_ST_BIRTHTIME
691 case SHOW_st_btime:
692 if (tsp == NULL)
693 tsp = &st->st_birthtimespec;
694 #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
695 ts = *tsp; /* copy so we can muck with it */
696 small = (sizeof(ts.tv_sec) == 4);
697 data = ts.tv_sec;
698 small = 1;
699 tm = localtime(&ts.tv_sec);
700 (void)strftime(path, sizeof(path), timefmt, tm);
701 sdata = path;
702 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
703 FMTF_FLOAT | FMTF_STRING;
704 if (ofmt == 0)
705 ofmt = FMTF_DECIMAL;
706 break;
707 case SHOW_st_size:
708 small = (sizeof(st->st_size) == 4);
709 data = st->st_size;
710 sdata = NULL;
711 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
712 if (ofmt == 0)
713 ofmt = FMTF_UNSIGNED;
714 break;
715 case SHOW_st_blocks:
716 small = (sizeof(st->st_blocks) == 4);
717 data = st->st_blocks;
718 sdata = NULL;
719 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
720 if (ofmt == 0)
721 ofmt = FMTF_UNSIGNED;
722 break;
723 case SHOW_st_blksize:
724 small = (sizeof(st->st_blksize) == 4);
725 data = st->st_blksize;
726 sdata = NULL;
727 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
728 if (ofmt == 0)
729 ofmt = FMTF_UNSIGNED;
730 break;
731 #if HAVE_STRUCT_STAT_ST_FLAGS
732 case SHOW_st_flags:
733 small = (sizeof(st->st_flags) == 4);
734 data = st->st_flags;
735 sdata = NULL;
736 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
737 if (ofmt == 0)
738 ofmt = FMTF_UNSIGNED;
739 break;
740 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
741 #if HAVE_STRUCT_STAT_ST_GEN
742 case SHOW_st_gen:
743 small = (sizeof(st->st_gen) == 4);
744 data = st->st_gen;
745 sdata = NULL;
746 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
747 if (ofmt == 0)
748 ofmt = FMTF_UNSIGNED;
749 break;
750 #endif /* HAVE_STRUCT_STAT_ST_GEN */
751 case SHOW_symlink:
752 small = 0;
753 data = 0;
754 if (S_ISLNK(st->st_mode)) {
755 snprintf(path, sizeof(path), " -> ");
756 l = readlink(file, path + 4, sizeof(path) - 4 - 1);
757 if (l == -1) {
758 linkfail = 1;
759 l = 0;
760 path[0] = '\0';
761 }
762 path[l + 4] = '\0';
763 sdata = path + (ofmt == FMTF_STRING ? 0 : 4);
764 }
765 else {
766 linkfail = 1;
767 sdata = "";
768 }
769 formats = FMTF_STRING;
770 if (ofmt == 0)
771 ofmt = FMTF_STRING;
772 break;
773 case SHOW_filetype:
774 small = 0;
775 data = 0;
776 sdata = smode;
777 sdata[0] = '\0';
778 if (hilo == 0 || hilo == LOW_PIECE) {
779 switch (st->st_mode & S_IFMT) {
780 case S_IFIFO: (void)strcat(sdata, "|"); break;
781 case S_IFDIR: (void)strcat(sdata, "/"); break;
782 case S_IFREG:
783 if (st->st_mode &
784 (S_IXUSR | S_IXGRP | S_IXOTH))
785 (void)strcat(sdata, "*");
786 break;
787 case S_IFLNK: (void)strcat(sdata, "@"); break;
788 case S_IFSOCK: (void)strcat(sdata, "="); break;
789 #ifdef S_IFWHT
790 case S_IFWHT: (void)strcat(sdata, "%"); break;
791 #endif /* S_IFWHT */
792 #ifdef S_IFDOOR
793 case S_IFDOOR: (void)strcat(sdata, ">"); break;
794 #endif /* S_IFDOOR */
795 }
796 hilo = 0;
797 }
798 else if (hilo == HIGH_PIECE) {
799 switch (st->st_mode & S_IFMT) {
800 case S_IFIFO: sdata = "Fifo File"; break;
801 case S_IFCHR: sdata = "Character Device"; break;
802 case S_IFDIR: sdata = "Directory"; break;
803 case S_IFBLK: sdata = "Block Device"; break;
804 case S_IFREG: sdata = "Regular File"; break;
805 case S_IFLNK: sdata = "Symbolic Link"; break;
806 case S_IFSOCK: sdata = "Socket"; break;
807 #ifdef S_IFWHT
808 case S_IFWHT: sdata = "Whiteout File"; break;
809 #endif /* S_IFWHT */
810 #ifdef S_IFDOOR
811 case S_IFDOOR: sdata = "Door"; break;
812 #endif /* S_IFDOOR */
813 default: sdata = "???"; break;
814 }
815 hilo = 0;
816 }
817 formats = FMTF_STRING;
818 if (ofmt == 0)
819 ofmt = FMTF_STRING;
820 break;
821 case SHOW_filename:
822 small = 0;
823 data = 0;
824 if (file == NULL)
825 (void)strncpy(path, "(stdin)", sizeof(path));
826 else
827 (void)strncpy(path, file, sizeof(path));
828 sdata = path;
829 formats = FMTF_STRING;
830 if (ofmt == 0)
831 ofmt = FMTF_STRING;
832 break;
833 case SHOW_sizerdev:
834 if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
835 char majdev[20], mindev[20];
836 int l1, l2;
837
838 l1 = format1(st,
839 file,
840 fmt, flen,
841 majdev, sizeof(majdev),
842 flags, size, prec,
843 ofmt, HIGH_PIECE, SHOW_st_rdev);
844 l2 = format1(st,
845 file,
846 fmt, flen,
847 mindev, sizeof(mindev),
848 flags, size, prec,
849 ofmt, LOW_PIECE, SHOW_st_rdev);
850 return (snprintf(buf, blen, "%.*s,%.*s",
851 l1, majdev, l2, mindev));
852 }
853 else {
854 return (format1(st,
855 file,
856 fmt, flen,
857 buf, blen,
858 flags, size, prec,
859 ofmt, 0, SHOW_st_size));
860 }
861 /*NOTREACHED*/
862 default:
863 errx(1, "%.*s: bad format", (int)flen, fmt);
864 }
865
866 /*
867 * If a subdatum was specified but not supported, or an output
868 * format was selected that is not supported, that's an error.
869 */
870 if (hilo != 0 || (ofmt & formats) == 0)
871 errx(1, "%.*s: bad format", (int)flen, fmt);
872
873 /*
874 * Assemble the format string for passing to printf(3).
875 */
876 lfmt[0] = '\0';
877 (void)strcat(lfmt, "%");
878 if (flags & FLAG_POUND)
879 (void)strcat(lfmt, "#");
880 if (flags & FLAG_SPACE)
881 (void)strcat(lfmt, " ");
882 if (flags & FLAG_PLUS)
883 (void)strcat(lfmt, "+");
884 if (flags & FLAG_MINUS)
885 (void)strcat(lfmt, "-");
886 if (flags & FLAG_ZERO)
887 (void)strcat(lfmt, "0");
888
889 /*
890 * Only the timespecs support the FLOAT output format, and that
891 * requires work that differs from the other formats.
892 */
893 if (ofmt == FMTF_FLOAT) {
894 /*
895 * Nothing after the decimal point, so just print seconds.
896 */
897 if (prec == 0) {
898 if (size != -1) {
899 (void)snprintf(tmp, sizeof(tmp), "%d", size);
900 (void)strcat(lfmt, tmp);
901 }
902 (void)strcat(lfmt, "d");
903 return (snprintf(buf, blen, lfmt, ts.tv_sec));
904 }
905
906 /*
907 * Unspecified precision gets all the precision we have:
908 * 9 digits.
909 */
910 if (prec == -1)
911 prec = 9;
912
913 /*
914 * Adjust the size for the decimal point and the digits
915 * that will follow.
916 */
917 size -= prec + 1;
918
919 /*
920 * Any leftover size that's legitimate will be used.
921 */
922 if (size > 0) {
923 (void)snprintf(tmp, sizeof(tmp), "%d", size);
924 (void)strcat(lfmt, tmp);
925 }
926 (void)strcat(lfmt, "d");
927
928 /*
929 * The stuff after the decimal point always needs zero
930 * filling.
931 */
932 (void)strcat(lfmt, ".%0");
933
934 /*
935 * We can "print" at most nine digits of precision. The
936 * rest we will pad on at the end.
937 */
938 (void)snprintf(tmp, sizeof(tmp), "%dd", prec > 9 ? 9 : prec);
939 (void)strcat(lfmt, tmp);
940
941 /*
942 * For precision of less that nine digits, trim off the
943 * less significant figures.
944 */
945 for (; prec < 9; prec++)
946 ts.tv_nsec /= 10;
947
948 /*
949 * Use the format, and then tack on any zeroes that
950 * might be required to make up the requested precision.
951 */
952 l = snprintf(buf, blen, lfmt, ts.tv_sec, ts.tv_nsec);
953 for (; prec > 9 && l < blen; prec--, l++)
954 (void)strcat(buf, "0");
955 return (l);
956 }
957
958 /*
959 * Add on size and precision, if specified, to the format.
960 */
961 if (size != -1) {
962 (void)snprintf(tmp, sizeof(tmp), "%d", size);
963 (void)strcat(lfmt, tmp);
964 }
965 if (prec != -1) {
966 (void)snprintf(tmp, sizeof(tmp), ".%d", prec);
967 (void)strcat(lfmt, tmp);
968 }
969
970 /*
971 * String output uses the temporary sdata.
972 */
973 if (ofmt == FMTF_STRING) {
974 if (sdata == NULL)
975 errx(1, "%.*s: bad format", (int)flen, fmt);
976 (void)strcat(lfmt, "s");
977 return (snprintf(buf, blen, lfmt, sdata));
978 }
979
980 /*
981 * Ensure that sign extension does not cause bad looking output
982 * for some forms.
983 */
984 if (small && ofmt != FMTF_DECIMAL)
985 data = (u_int32_t)data;
986
987 /*
988 * The four "numeric" output forms.
989 */
990 (void)strcat(lfmt, "ll");
991 switch (ofmt) {
992 case FMTF_DECIMAL: (void)strcat(lfmt, "d"); break;
993 case FMTF_OCTAL: (void)strcat(lfmt, "o"); break;
994 case FMTF_UNSIGNED: (void)strcat(lfmt, "u"); break;
995 case FMTF_HEX: (void)strcat(lfmt, "x"); break;
996 }
997
998 return (snprintf(buf, blen, lfmt, data));
999 }