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