file_cmds-220.4.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 #if HAVE_STRUCT_STAT_ST_FLAGS
71 #define DEF_F "%#Xf "
72 #define RAW_F "%f "
73 #define SHELL_F " st_flags=%f"
74 #else /* HAVE_STRUCT_STAT_ST_FLAGS */
75 #define DEF_F
76 #define RAW_F
77 #define SHELL_F
78 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
79
80 #if HAVE_STRUCT_STAT_ST_BIRTHTIME
81 #define DEF_B "\"%SB\" "
82 #define RAW_B "%B "
83 #define SHELL_B "st_birthtime=%B "
84 #else /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
85 #define DEF_B
86 #define RAW_B
87 #define SHELL_B
88 #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
89
90 #if HAVE_STRUCT_STAT_ST_ATIM
91 #define st_atimespec st_atim
92 #define st_ctimespec st_ctim
93 #define st_mtimespec st_mtim
94 #endif /* HAVE_STRUCT_STAT_ST_ATIM */
95
96 #define DEF_FORMAT \
97 "%d %i %Sp %l %Su %Sg %r %z \"%Sa\" \"%Sm\" \"%Sc\" " DEF_B \
98 "%k %b " DEF_F "%N"
99 #define RAW_FORMAT "%d %i %#p %l %u %g %r %z %a %m %c " RAW_B \
100 "%k %b " RAW_F "%N"
101 #define LS_FORMAT "%Sp %l %Su %Sg %Z %Sm %N%SY"
102 #define LSF_FORMAT "%Sp %l %Su %Sg %Z %Sm %N%T%SY"
103 #define SHELL_FORMAT \
104 "st_dev=%d st_ino=%i st_mode=%#p st_nlink=%l " \
105 "st_uid=%u st_gid=%g st_rdev=%r st_size=%z " \
106 "st_atime=%a st_mtime=%m st_ctime=%c " SHELL_B \
107 "st_blksize=%k st_blocks=%b" SHELL_F
108 #define LINUX_FORMAT \
109 " File: \"%N\"%n" \
110 " Size: %-11z FileType: %HT%n" \
111 " Mode: (%04OA/%.10Sp) Uid: (%5u/%8Su) Gid: (%5g/%8Sg)%n" \
112 "Device: %Hd,%Ld Inode: %i Links: %l%n" \
113 "Access: %Sa%n" \
114 "Modify: %Sm%n" \
115 "Change: %Sc"
116
117 #define TIME_FORMAT "%b %e %T %Y"
118
119 #define FLAG_POUND 0x01
120 #define FLAG_SPACE 0x02
121 #define FLAG_PLUS 0x04
122 #define FLAG_ZERO 0x08
123 #define FLAG_MINUS 0x10
124
125 /*
126 * These format characters must all be unique, except the magic one.
127 */
128 #define FMT_MAGIC '%'
129 #define FMT_DOT '.'
130
131 #define SIMPLE_NEWLINE 'n'
132 #define SIMPLE_TAB 't'
133 #define SIMPLE_PERCENT '%'
134 #define SIMPLE_NUMBER '@'
135
136 #define FMT_POUND '#'
137 #define FMT_SPACE ' '
138 #define FMT_PLUS '+'
139 #define FMT_ZERO '0'
140 #define FMT_MINUS '-'
141
142 #define FMT_DECIMAL 'D'
143 #define FMT_OCTAL 'O'
144 #define FMT_UNSIGNED 'U'
145 #define FMT_HEX 'X'
146 #define FMT_FLOAT 'F'
147 #define FMT_STRING 'S'
148
149 #define FMTF_DECIMAL 0x01
150 #define FMTF_OCTAL 0x02
151 #define FMTF_UNSIGNED 0x04
152 #define FMTF_HEX 0x08
153 #define FMTF_FLOAT 0x10
154 #define FMTF_STRING 0x20
155
156 #define HIGH_PIECE 'H'
157 #define MIDDLE_PIECE 'M'
158 #define LOW_PIECE 'L'
159
160 #define SHOW_st_dev 'd'
161 #define SHOW_st_ino 'i'
162 #define SHOW_st_mode 'p'
163 #define SHOW_st_mode2 'A'
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 rc = stat(argv[0], &st);
312 else
313 rc = lstat(argv[0], &st);
314
315 if (rc == -1) {
316 errs = 1;
317 linkfail = 1;
318 if (!quiet)
319 warn("%s: stat",
320 argc == 0 ? "(stdin)" : argv[0]);
321 }
322 else
323 output(&st, argv[0], statfmt, fn, nonl, quiet);
324
325 argv++;
326 argc--;
327 fn++;
328 } while (argc > 0);
329
330 return (am_readlink ? linkfail : errs);
331 }
332
333 void
334 usage(const char *synopsis)
335 {
336
337 (void)fprintf(stderr, "usage: %s %s\n", getprogname(), synopsis);
338 exit(1);
339 }
340
341 /*
342 * Parses a format string.
343 */
344 void
345 output(const struct stat *st, const char *file,
346 const char *statfmt, int fn, int nonl, int quiet)
347 {
348 int flags, size, prec, ofmt, hilo, what;
349 char buf[PATH_MAX];
350 const char *subfmt;
351 int nl, t, i;
352
353 nl = 1;
354 while (*statfmt != '\0') {
355
356 /*
357 * Non-format characters go straight out.
358 */
359 if (*statfmt != FMT_MAGIC) {
360 addchar(stdout, *statfmt, &nl);
361 statfmt++;
362 continue;
363 }
364
365 /*
366 * The current format "substring" starts here,
367 * and then we skip the magic.
368 */
369 subfmt = statfmt;
370 statfmt++;
371
372 /*
373 * Some simple one-character "formats".
374 */
375 switch (*statfmt) {
376 case SIMPLE_NEWLINE:
377 addchar(stdout, '\n', &nl);
378 statfmt++;
379 continue;
380 case SIMPLE_TAB:
381 addchar(stdout, '\t', &nl);
382 statfmt++;
383 continue;
384 case SIMPLE_PERCENT:
385 addchar(stdout, '%', &nl);
386 statfmt++;
387 continue;
388 case SIMPLE_NUMBER: {
389 char num[12], *p;
390
391 snprintf(num, sizeof(num), "%d", fn);
392 for (p = &num[0]; *p; p++)
393 addchar(stdout, *p, &nl);
394 statfmt++;
395 continue;
396 }
397 }
398
399 /*
400 * This must be an actual format string. Format strings are
401 * similar to printf(3) formats up to a point, and are of
402 * the form:
403 *
404 * % required start of format
405 * [-# +0] opt. format characters
406 * size opt. field width
407 * . opt. decimal separator, followed by
408 * prec opt. precision
409 * fmt opt. output specifier (string, numeric, etc.)
410 * sub opt. sub field specifier (high, middle, low)
411 * datum required field specifier (size, mode, etc)
412 *
413 * Only the % and the datum selector are required. All data
414 * have reasonable default output forms. The "sub" specifier
415 * only applies to certain data (mode, dev, rdev, filetype).
416 * The symlink output defaults to STRING, yet will only emit
417 * the leading " -> " if STRING is explicitly specified. The
418 * sizerdev datum will generate rdev output for character or
419 * block devices, and size output for all others.
420 */
421 flags = 0;
422 do {
423 if (*statfmt == FMT_POUND)
424 flags |= FLAG_POUND;
425 else if (*statfmt == FMT_SPACE)
426 flags |= FLAG_SPACE;
427 else if (*statfmt == FMT_PLUS)
428 flags |= FLAG_PLUS;
429 else if (*statfmt == FMT_ZERO)
430 flags |= FLAG_ZERO;
431 else if (*statfmt == FMT_MINUS)
432 flags |= FLAG_MINUS;
433 else
434 break;
435 statfmt++;
436 } while (1/*CONSTCOND*/);
437
438 size = -1;
439 if (isdigit((unsigned)*statfmt)) {
440 size = 0;
441 while (isdigit((unsigned)*statfmt)) {
442 size = (size * 10) + (*statfmt - '0');
443 statfmt++;
444 if (size < 0)
445 goto badfmt;
446 }
447 }
448
449 prec = -1;
450 if (*statfmt == FMT_DOT) {
451 statfmt++;
452
453 prec = 0;
454 while (isdigit((unsigned)*statfmt)) {
455 prec = (prec * 10) + (*statfmt - '0');
456 statfmt++;
457 if (prec < 0)
458 goto badfmt;
459 }
460 }
461
462 #define fmtcase(x, y) case (y): (x) = (y); statfmt++; break
463 #define fmtcasef(x, y, z) case (y): (x) = (z); statfmt++; break
464 switch (*statfmt) {
465 fmtcasef(ofmt, FMT_DECIMAL, FMTF_DECIMAL);
466 fmtcasef(ofmt, FMT_OCTAL, FMTF_OCTAL);
467 fmtcasef(ofmt, FMT_UNSIGNED, FMTF_UNSIGNED);
468 fmtcasef(ofmt, FMT_HEX, FMTF_HEX);
469 fmtcasef(ofmt, FMT_FLOAT, FMTF_FLOAT);
470 fmtcasef(ofmt, FMT_STRING, FMTF_STRING);
471 default:
472 ofmt = 0;
473 break;
474 }
475
476 switch (*statfmt) {
477 fmtcase(hilo, HIGH_PIECE);
478 fmtcase(hilo, MIDDLE_PIECE);
479 fmtcase(hilo, LOW_PIECE);
480 default:
481 hilo = 0;
482 break;
483 }
484
485 switch (*statfmt) {
486 fmtcase(what, SHOW_st_dev);
487 fmtcase(what, SHOW_st_ino);
488 fmtcase(what, SHOW_st_mode);
489 fmtcase(what, SHOW_st_mode2);
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 case SHOW_st_mode2:
605 small = (sizeof(st->st_mode) == 4);
606 data = st->st_mode;
607 strmode(st->st_mode, smode);
608 sdata = smode;
609 l = strlen(sdata);
610 if (sdata[l - 1] == ' ')
611 sdata[--l] = '\0';
612 if (what == SHOW_st_mode2)
613 data &= 07777;
614 if (hilo == HIGH_PIECE) {
615 data >>= 12;
616 sdata += 1;
617 sdata[3] = '\0';
618 hilo = 0;
619 }
620 else if (hilo == MIDDLE_PIECE) {
621 data = (data >> 9) & 07;
622 sdata += 4;
623 sdata[3] = '\0';
624 hilo = 0;
625 }
626 else if (hilo == LOW_PIECE) {
627 data &= 0777;
628 sdata += 7;
629 sdata[3] = '\0';
630 hilo = 0;
631 }
632 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
633 FMTF_STRING;
634 if (ofmt == 0)
635 ofmt = FMTF_OCTAL;
636 break;
637 case SHOW_st_nlink:
638 small = (sizeof(st->st_dev) == 4);
639 data = st->st_nlink;
640 sdata = NULL;
641 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
642 if (ofmt == 0)
643 ofmt = FMTF_UNSIGNED;
644 break;
645 case SHOW_st_uid:
646 small = (sizeof(st->st_uid) == 4);
647 data = st->st_uid;
648 if ((pw = getpwuid(st->st_uid)) != NULL)
649 sdata = pw->pw_name;
650 else {
651 snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_uid);
652 sdata = sid;
653 }
654 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
655 FMTF_STRING;
656 if (ofmt == 0)
657 ofmt = FMTF_UNSIGNED;
658 break;
659 case SHOW_st_gid:
660 small = (sizeof(st->st_gid) == 4);
661 data = st->st_gid;
662 if ((gr = getgrgid(st->st_gid)) != NULL)
663 sdata = gr->gr_name;
664 else {
665 snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_gid);
666 sdata = sid;
667 }
668 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
669 FMTF_STRING;
670 if (ofmt == 0)
671 ofmt = FMTF_UNSIGNED;
672 break;
673 case SHOW_st_atime:
674 tsp = &st->st_atimespec;
675 /* FALLTHROUGH */
676 case SHOW_st_mtime:
677 if (tsp == NULL)
678 tsp = &st->st_mtimespec;
679 /* FALLTHROUGH */
680 case SHOW_st_ctime:
681 if (tsp == NULL)
682 tsp = &st->st_ctimespec;
683 /* FALLTHROUGH */
684 #if HAVE_STRUCT_STAT_ST_BIRTHTIME
685 case SHOW_st_btime:
686 if (tsp == NULL)
687 tsp = &st->st_birthtimespec;
688 #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
689 ts = *tsp; /* copy so we can muck with it */
690 small = (sizeof(ts.tv_sec) == 4);
691 data = ts.tv_sec;
692 tm = localtime(&ts.tv_sec);
693 if (tm == NULL) {
694 ts.tv_sec = 0;
695 tm = localtime(&ts.tv_sec);
696 }
697 (void)strftime(path, sizeof(path), timefmt, tm);
698 sdata = path;
699 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
700 FMTF_FLOAT | FMTF_STRING;
701 if (ofmt == 0)
702 ofmt = FMTF_DECIMAL;
703 break;
704 case SHOW_st_size:
705 small = (sizeof(st->st_size) == 4);
706 data = st->st_size;
707 sdata = NULL;
708 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
709 if (ofmt == 0)
710 ofmt = FMTF_UNSIGNED;
711 break;
712 case SHOW_st_blocks:
713 small = (sizeof(st->st_blocks) == 4);
714 data = st->st_blocks;
715 sdata = NULL;
716 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
717 if (ofmt == 0)
718 ofmt = FMTF_UNSIGNED;
719 break;
720 case SHOW_st_blksize:
721 small = (sizeof(st->st_blksize) == 4);
722 data = st->st_blksize;
723 sdata = NULL;
724 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
725 if (ofmt == 0)
726 ofmt = FMTF_UNSIGNED;
727 break;
728 #if HAVE_STRUCT_STAT_ST_FLAGS
729 case SHOW_st_flags:
730 small = (sizeof(st->st_flags) == 4);
731 data = st->st_flags;
732 sdata = NULL;
733 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
734 if (ofmt == 0)
735 ofmt = FMTF_UNSIGNED;
736 break;
737 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
738 #if HAVE_STRUCT_STAT_ST_GEN
739 case SHOW_st_gen:
740 small = (sizeof(st->st_gen) == 4);
741 data = st->st_gen;
742 sdata = NULL;
743 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
744 if (ofmt == 0)
745 ofmt = FMTF_UNSIGNED;
746 break;
747 #endif /* HAVE_STRUCT_STAT_ST_GEN */
748 case SHOW_symlink:
749 small = 0;
750 data = 0;
751 if (S_ISLNK(st->st_mode)) {
752 snprintf(path, sizeof(path), " -> ");
753 l = readlink(file, path + 4, sizeof(path) - 4 - 1);
754 if (l == -1) {
755 linkfail = 1;
756 l = 0;
757 path[0] = '\0';
758 }
759 path[l + 4] = '\0';
760 sdata = path + (ofmt == FMTF_STRING ? 0 : 4);
761 }
762 else {
763 linkfail = 1;
764 sdata = "";
765 }
766 formats = FMTF_STRING;
767 if (ofmt == 0)
768 ofmt = FMTF_STRING;
769 break;
770 case SHOW_filetype:
771 small = 0;
772 data = 0;
773 sdata = smode;
774 sdata[0] = '\0';
775 if (hilo == 0 || hilo == LOW_PIECE) {
776 switch (st->st_mode & S_IFMT) {
777 case S_IFIFO: (void)strcat(sdata, "|"); break;
778 case S_IFDIR: (void)strcat(sdata, "/"); break;
779 case S_IFREG:
780 if (st->st_mode &
781 (S_IXUSR | S_IXGRP | S_IXOTH))
782 (void)strcat(sdata, "*");
783 break;
784 case S_IFLNK: (void)strcat(sdata, "@"); break;
785 case S_IFSOCK: (void)strcat(sdata, "="); break;
786 #ifdef S_IFWHT
787 case S_IFWHT: (void)strcat(sdata, "%"); break;
788 #endif /* S_IFWHT */
789 #ifdef S_IFDOOR
790 case S_IFDOOR: (void)strcat(sdata, ">"); break;
791 #endif /* S_IFDOOR */
792 }
793 hilo = 0;
794 }
795 else if (hilo == HIGH_PIECE) {
796 switch (st->st_mode & S_IFMT) {
797 case S_IFIFO: sdata = "Fifo File"; break;
798 case S_IFCHR: sdata = "Character Device"; break;
799 case S_IFDIR: sdata = "Directory"; break;
800 case S_IFBLK: sdata = "Block Device"; break;
801 case S_IFREG: sdata = "Regular File"; break;
802 case S_IFLNK: sdata = "Symbolic Link"; break;
803 case S_IFSOCK: sdata = "Socket"; break;
804 #ifdef S_IFWHT
805 case S_IFWHT: sdata = "Whiteout File"; break;
806 #endif /* S_IFWHT */
807 #ifdef S_IFDOOR
808 case S_IFDOOR: sdata = "Door"; break;
809 #endif /* S_IFDOOR */
810 default: sdata = "???"; break;
811 }
812 hilo = 0;
813 }
814 formats = FMTF_STRING;
815 if (ofmt == 0)
816 ofmt = FMTF_STRING;
817 break;
818 case SHOW_filename:
819 small = 0;
820 data = 0;
821 if (file == NULL)
822 (void)strncpy(path, "(stdin)", sizeof(path));
823 else
824 (void)strncpy(path, file, sizeof(path));
825 sdata = path;
826 formats = FMTF_STRING;
827 if (ofmt == 0)
828 ofmt = FMTF_STRING;
829 break;
830 case SHOW_sizerdev:
831 if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
832 char majdev[20], mindev[20];
833 int l1, l2;
834
835 l1 = format1(st,
836 file,
837 fmt, flen,
838 majdev, sizeof(majdev),
839 flags, size, prec,
840 ofmt, HIGH_PIECE, SHOW_st_rdev);
841 l2 = format1(st,
842 file,
843 fmt, flen,
844 mindev, sizeof(mindev),
845 flags, size, prec,
846 ofmt, LOW_PIECE, SHOW_st_rdev);
847 return (snprintf(buf, blen, "%.*s,%.*s",
848 l1, majdev, l2, mindev));
849 }
850 else {
851 return (format1(st,
852 file,
853 fmt, flen,
854 buf, blen,
855 flags, size, prec,
856 ofmt, 0, SHOW_st_size));
857 }
858 /*NOTREACHED*/
859 default:
860 errx(1, "%.*s: bad format", (int)flen, fmt);
861 }
862
863 /*
864 * If a subdatum was specified but not supported, or an output
865 * format was selected that is not supported, that's an error.
866 */
867 if (hilo != 0 || (ofmt & formats) == 0)
868 errx(1, "%.*s: bad format", (int)flen, fmt);
869
870 /*
871 * Assemble the format string for passing to printf(3).
872 */
873 lfmt[0] = '\0';
874 (void)strcat(lfmt, "%");
875 if (flags & FLAG_POUND)
876 (void)strcat(lfmt, "#");
877 if (flags & FLAG_SPACE)
878 (void)strcat(lfmt, " ");
879 if (flags & FLAG_PLUS)
880 (void)strcat(lfmt, "+");
881 if (flags & FLAG_MINUS)
882 (void)strcat(lfmt, "-");
883 if (flags & FLAG_ZERO)
884 (void)strcat(lfmt, "0");
885
886 /*
887 * Only the timespecs support the FLOAT output format, and that
888 * requires work that differs from the other formats.
889 */
890 if (ofmt == FMTF_FLOAT) {
891 /*
892 * Nothing after the decimal point, so just print seconds.
893 */
894 if (prec == 0) {
895 if (size != -1) {
896 (void)snprintf(tmp, sizeof(tmp), "%d", size);
897 (void)strcat(lfmt, tmp);
898 }
899 (void)strcat(lfmt, "d");
900 return (snprintf(buf, blen, lfmt, ts.tv_sec));
901 }
902
903 /*
904 * Unspecified precision gets all the precision we have:
905 * 9 digits.
906 */
907 if (prec == -1)
908 prec = 9;
909
910 /*
911 * Adjust the size for the decimal point and the digits
912 * that will follow.
913 */
914 size -= prec + 1;
915
916 /*
917 * Any leftover size that's legitimate will be used.
918 */
919 if (size > 0) {
920 (void)snprintf(tmp, sizeof(tmp), "%d", size);
921 (void)strcat(lfmt, tmp);
922 }
923 (void)strcat(lfmt, "d");
924
925 /*
926 * The stuff after the decimal point always needs zero
927 * filling.
928 */
929 (void)strcat(lfmt, ".%0");
930
931 /*
932 * We can "print" at most nine digits of precision. The
933 * rest we will pad on at the end.
934 */
935 (void)snprintf(tmp, sizeof(tmp), "%dd", prec > 9 ? 9 : prec);
936 (void)strcat(lfmt, tmp);
937
938 /*
939 * For precision of less that nine digits, trim off the
940 * less significant figures.
941 */
942 for (; prec < 9; prec++)
943 ts.tv_nsec /= 10;
944
945 /*
946 * Use the format, and then tack on any zeroes that
947 * might be required to make up the requested precision.
948 */
949 l = snprintf(buf, blen, lfmt, ts.tv_sec, ts.tv_nsec);
950 for (; prec > 9 && l < blen; prec--, l++)
951 (void)strcat(buf, "0");
952 return (l);
953 }
954
955 /*
956 * Add on size and precision, if specified, to the format.
957 */
958 if (size != -1) {
959 (void)snprintf(tmp, sizeof(tmp), "%d", size);
960 (void)strcat(lfmt, tmp);
961 }
962 if (prec != -1) {
963 (void)snprintf(tmp, sizeof(tmp), ".%d", prec);
964 (void)strcat(lfmt, tmp);
965 }
966
967 /*
968 * String output uses the temporary sdata.
969 */
970 if (ofmt == FMTF_STRING) {
971 if (sdata == NULL)
972 errx(1, "%.*s: bad format", (int)flen, fmt);
973 (void)strcat(lfmt, "s");
974 return (snprintf(buf, blen, lfmt, sdata));
975 }
976
977 /*
978 * Ensure that sign extension does not cause bad looking output
979 * for some forms.
980 */
981 if (small && ofmt != FMTF_DECIMAL)
982 data = (u_int32_t)data;
983
984 /*
985 * The four "numeric" output forms.
986 */
987 (void)strcat(lfmt, "ll");
988 switch (ofmt) {
989 case FMTF_DECIMAL: (void)strcat(lfmt, "d"); break;
990 case FMTF_OCTAL: (void)strcat(lfmt, "o"); break;
991 case FMTF_UNSIGNED: (void)strcat(lfmt, "u"); break;
992 case FMTF_HEX: (void)strcat(lfmt, "x"); break;
993 }
994
995 return (snprintf(buf, blen, lfmt, data));
996 }