stat.c revision 1.15 1 /* $NetBSD: stat.c,v 1.15 2003/10/27 00:12:43 lukem 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.15 2003/10/27 00:12:43 lukem 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 <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: (%04OLp/%.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_nlink 'l'
164 #define SHOW_st_uid 'u'
165 #define SHOW_st_gid 'g'
166 #define SHOW_st_rdev 'r'
167 #define SHOW_st_atime 'a'
168 #define SHOW_st_mtime 'm'
169 #define SHOW_st_ctime 'c'
170 #define SHOW_st_btime 'B'
171 #define SHOW_st_size 'z'
172 #define SHOW_st_blocks 'b'
173 #define SHOW_st_blksize 'k'
174 #define SHOW_st_flags 'f'
175 #define SHOW_st_gen 'v'
176 #define SHOW_symlink 'Y'
177 #define SHOW_filetype 'T'
178 #define SHOW_filename 'N'
179 #define SHOW_sizerdev 'Z'
180
181 void usage(const char *);
182 void output(const struct stat *, const char *,
183 const char *, int, int, int);
184 int format1(const struct stat *, /* stat info */
185 const char *, /* the file name */
186 const char *, int, /* the format string itself */
187 char *, size_t, /* a place to put the output */
188 int, int, int, int, /* the parsed format */
189 int, int);
190
191 char *timefmt;
192 int linkfail;
193
194 #define addchar(s, c, nl) \
195 do { \
196 (void)fputc((c), (s)); \
197 (*nl) = ((c) == '\n'); \
198 } while (0/*CONSTCOND*/)
199
200 int
201 main(int argc, char *argv[])
202 {
203 struct stat st;
204 int ch, rc, errs, am_readlink;
205 int lsF, fmtchar, usestat, fn, nonl, quiet;
206 char *statfmt, *options, *synopsis;
207
208 am_readlink = 0;
209 lsF = 0;
210 fmtchar = '\0';
211 usestat = 0;
212 nonl = 0;
213 quiet = 0;
214 linkfail = 0;
215 statfmt = NULL;
216 timefmt = NULL;
217
218 if (strcmp(getprogname(), "readlink") == 0) {
219 am_readlink = 1;
220 options = "n";
221 synopsis = "[-n] [file ...]";
222 statfmt = "%Y";
223 fmtchar = 'f';
224 quiet = 1;
225 } else {
226 options = "f:FlLnqrst:x";
227 synopsis = "[-FlLnqrsx] [-f format] [-t timefmt] [file ...]";
228 }
229
230 while ((ch = getopt(argc, argv, options)) != -1)
231 switch (ch) {
232 case 'F':
233 lsF = 1;
234 break;
235 case 'L':
236 usestat = 1;
237 break;
238 case 'n':
239 nonl = 1;
240 break;
241 case 'q':
242 quiet = 1;
243 break;
244 case 'f':
245 statfmt = optarg;
246 /* FALLTHROUGH */
247 case 'l':
248 case 'r':
249 case 's':
250 case 'x':
251 if (fmtchar != 0)
252 errx(1, "can't use format '%c' with '%c'",
253 fmtchar, ch);
254 fmtchar = ch;
255 break;
256 case 't':
257 timefmt = optarg;
258 break;
259 default:
260 usage(synopsis);
261 }
262
263 argc -= optind;
264 argv += optind;
265 fn = 1;
266
267 if (fmtchar == '\0') {
268 if (lsF)
269 fmtchar = 'l';
270 else {
271 fmtchar = 'f';
272 statfmt = DEF_FORMAT;
273 }
274 }
275
276 if (lsF && fmtchar != 'l')
277 errx(1, "can't use format '%c' with -F", fmtchar);
278
279 switch (fmtchar) {
280 case 'f':
281 /* statfmt already set */
282 break;
283 case 'l':
284 statfmt = lsF ? LSF_FORMAT : LS_FORMAT;
285 break;
286 case 'r':
287 statfmt = RAW_FORMAT;
288 break;
289 case 's':
290 statfmt = SHELL_FORMAT;
291 break;
292 case 'x':
293 statfmt = LINUX_FORMAT;
294 if (timefmt == NULL)
295 timefmt = "%c";
296 break;
297 default:
298 usage(synopsis);
299 /*NOTREACHED*/
300 }
301
302 if (timefmt == NULL)
303 timefmt = TIME_FORMAT;
304
305 errs = 0;
306 do {
307 if (argc == 0)
308 rc = fstat(STDIN_FILENO, &st);
309 else if (usestat)
310 rc = stat(argv[0], &st);
311 else
312 rc = lstat(argv[0], &st);
313
314 if (rc == -1) {
315 errs = 1;
316 linkfail = 1;
317 if (!quiet)
318 warn("%s: stat",
319 argc == 0 ? "(stdin)" : argv[0]);
320 }
321 else
322 output(&st, argv[0], statfmt, fn, nonl, quiet);
323
324 argv++;
325 argc--;
326 fn++;
327 } while (argc > 0);
328
329 return (am_readlink ? linkfail : errs);
330 }
331
332 void
333 usage(const char *synopsis)
334 {
335
336 (void)fprintf(stderr, "usage: %s %s\n", getprogname(), synopsis);
337 exit(1);
338 }
339
340 /*
341 * Parses a format string.
342 */
343 void
344 output(const struct stat *st, const char *file,
345 const char *statfmt, int fn, int nonl, int quiet)
346 {
347 int flags, size, prec, ofmt, hilo, what;
348 char buf[PATH_MAX];
349 const char *subfmt;
350 int nl, t, i;
351
352 nl = 1;
353 while (*statfmt != '\0') {
354
355 /*
356 * Non-format characters go straight out.
357 */
358 if (*statfmt != FMT_MAGIC) {
359 addchar(stdout, *statfmt, &nl);
360 statfmt++;
361 continue;
362 }
363
364 /*
365 * The current format "substring" starts here,
366 * and then we skip the magic.
367 */
368 subfmt = statfmt;
369 statfmt++;
370
371 /*
372 * Some simple one-character "formats".
373 */
374 switch (*statfmt) {
375 case SIMPLE_NEWLINE:
376 addchar(stdout, '\n', &nl);
377 statfmt++;
378 continue;
379 case SIMPLE_TAB:
380 addchar(stdout, '\t', &nl);
381 statfmt++;
382 continue;
383 case SIMPLE_PERCENT:
384 addchar(stdout, '%', &nl);
385 statfmt++;
386 continue;
387 case SIMPLE_NUMBER: {
388 char num[12], *p;
389
390 snprintf(num, sizeof(num), "%d", fn);
391 for (p = &num[0]; *p; p++)
392 addchar(stdout, *p, &nl);
393 statfmt++;
394 continue;
395 }
396 }
397
398 /*
399 * This must be an actual format string. Format strings are
400 * similar to printf(3) formats up to a point, and are of
401 * the form:
402 *
403 * % required start of format
404 * [-# +0] opt. format characters
405 * size opt. field width
406 * . opt. decimal separator, followed by
407 * prec opt. precision
408 * fmt opt. output specifier (string, numeric, etc.)
409 * sub opt. sub field specifier (high, middle, low)
410 * datum required field specifier (size, mode, etc)
411 *
412 * Only the % and the datum selector are required. All data
413 * have reasonable default output forms. The "sub" specifier
414 * only applies to certain data (mode, dev, rdev, filetype).
415 * The symlink output defaults to STRING, yet will only emit
416 * the leading " -> " if STRING is explicitly specified. The
417 * sizerdev datum will generate rdev output for character or
418 * block devices, and size output for all others.
419 */
420 flags = 0;
421 do {
422 if (*statfmt == FMT_POUND)
423 flags |= FLAG_POUND;
424 else if (*statfmt == FMT_SPACE)
425 flags |= FLAG_SPACE;
426 else if (*statfmt == FMT_PLUS)
427 flags |= FLAG_PLUS;
428 else if (*statfmt == FMT_ZERO)
429 flags |= FLAG_ZERO;
430 else if (*statfmt == FMT_MINUS)
431 flags |= FLAG_MINUS;
432 else
433 break;
434 statfmt++;
435 } while (1/*CONSTCOND*/);
436
437 size = -1;
438 if (isdigit((unsigned)*statfmt)) {
439 size = 0;
440 while (isdigit((unsigned)*statfmt)) {
441 size = (size * 10) + (*statfmt - '0');
442 statfmt++;
443 if (size < 0)
444 goto badfmt;
445 }
446 }
447
448 prec = -1;
449 if (*statfmt == FMT_DOT) {
450 statfmt++;
451
452 prec = 0;
453 while (isdigit((unsigned)*statfmt)) {
454 prec = (prec * 10) + (*statfmt - '0');
455 statfmt++;
456 if (prec < 0)
457 goto badfmt;
458 }
459 }
460
461 #define fmtcase(x, y) case (y): (x) = (y); statfmt++; break
462 #define fmtcasef(x, y, z) case (y): (x) = (z); statfmt++; break
463 switch (*statfmt) {
464 fmtcasef(ofmt, FMT_DECIMAL, FMTF_DECIMAL);
465 fmtcasef(ofmt, FMT_OCTAL, FMTF_OCTAL);
466 fmtcasef(ofmt, FMT_UNSIGNED, FMTF_UNSIGNED);
467 fmtcasef(ofmt, FMT_HEX, FMTF_HEX);
468 fmtcasef(ofmt, FMT_FLOAT, FMTF_FLOAT);
469 fmtcasef(ofmt, FMT_STRING, FMTF_STRING);
470 default:
471 ofmt = 0;
472 break;
473 }
474
475 switch (*statfmt) {
476 fmtcase(hilo, HIGH_PIECE);
477 fmtcase(hilo, MIDDLE_PIECE);
478 fmtcase(hilo, LOW_PIECE);
479 default:
480 hilo = 0;
481 break;
482 }
483
484 switch (*statfmt) {
485 fmtcase(what, SHOW_st_dev);
486 fmtcase(what, SHOW_st_ino);
487 fmtcase(what, SHOW_st_mode);
488 fmtcase(what, SHOW_st_nlink);
489 fmtcase(what, SHOW_st_uid);
490 fmtcase(what, SHOW_st_gid);
491 fmtcase(what, SHOW_st_rdev);
492 fmtcase(what, SHOW_st_atime);
493 fmtcase(what, SHOW_st_mtime);
494 fmtcase(what, SHOW_st_ctime);
495 fmtcase(what, SHOW_st_btime);
496 fmtcase(what, SHOW_st_size);
497 fmtcase(what, SHOW_st_blocks);
498 fmtcase(what, SHOW_st_blksize);
499 fmtcase(what, SHOW_st_flags);
500 fmtcase(what, SHOW_st_gen);
501 fmtcase(what, SHOW_symlink);
502 fmtcase(what, SHOW_filetype);
503 fmtcase(what, SHOW_filename);
504 fmtcase(what, SHOW_sizerdev);
505 default:
506 goto badfmt;
507 }
508 #undef fmtcasef
509 #undef fmtcase
510
511 t = format1(st,
512 file,
513 subfmt, statfmt - subfmt,
514 buf, sizeof(buf),
515 flags, size, prec, ofmt, hilo, what);
516
517 for (i = 0; i < t && i < sizeof(buf); i++)
518 addchar(stdout, buf[i], &nl);
519
520 continue;
521
522 badfmt:
523 errx(1, "%.*s: bad format",
524 (int)(statfmt - subfmt + 1), subfmt);
525 }
526
527 if (!nl && !nonl)
528 (void)fputc('\n', stdout);
529 (void)fflush(stdout);
530 }
531
532 /*
533 * Arranges output according to a single parsed format substring.
534 */
535 int
536 format1(const struct stat *st,
537 const char *file,
538 const char *fmt, int flen,
539 char *buf, size_t blen,
540 int flags, int size, int prec, int ofmt,
541 int hilo, int what)
542 {
543 u_int64_t data;
544 char *sdata, lfmt[24], tmp[20];
545 char smode[12], sid[12], path[PATH_MAX + 4];
546 struct passwd *pw;
547 struct group *gr;
548 struct tm *tm;
549 time_t secs;
550 long nsecs;
551 int l, small, formats, gottime;
552
553 formats = 0;
554 small = 0;
555 gottime = 0;
556 secs = 0;
557 nsecs = 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 gottime = 1;
672 secs = st->st_atime;
673 #ifdef HAVE_STRUCT_STAT_ST_MTIMENSEC
674 nsecs = st->st_atimensec;
675 #endif
676 /* FALLTHROUGH */
677 case SHOW_st_mtime:
678 if (!gottime) {
679 secs = st->st_mtime;
680 #ifdef HAVE_STRUCT_STAT_ST_MTIMENSEC
681 nsecs = st->st_mtimensec;
682 #endif
683 }
684 /* FALLTHROUGH */
685 case SHOW_st_ctime:
686 if (!gottime) {
687 secs = st->st_ctime;
688 #ifdef HAVE_STRUCT_STAT_ST_MTIMENSEC
689 nsecs = st->st_ctimensec;
690 #endif
691 }
692 /* FALLTHROUGH */
693 #if HAVE_STRUCT_STAT_ST_BIRTHTIME
694 case SHOW_st_btime:
695 if (!gottime) {
696 secs = st->st_birthtimespec.tv_sec;
697 nsecs = st->st_birthtimespec.tv_nsec;
698 }
699 #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIME */
700 small = (sizeof(secs) == 4);
701 data = secs;
702 small = 1;
703 tm = localtime(&secs);
704 (void)strftime(path, sizeof(path), timefmt, tm);
705 sdata = path;
706 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX |
707 FMTF_FLOAT | FMTF_STRING;
708 if (ofmt == 0)
709 ofmt = FMTF_DECIMAL;
710 break;
711 case SHOW_st_size:
712 small = (sizeof(st->st_size) == 4);
713 data = st->st_size;
714 sdata = NULL;
715 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
716 if (ofmt == 0)
717 ofmt = FMTF_UNSIGNED;
718 break;
719 case SHOW_st_blocks:
720 small = (sizeof(st->st_blocks) == 4);
721 data = st->st_blocks;
722 sdata = NULL;
723 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
724 if (ofmt == 0)
725 ofmt = FMTF_UNSIGNED;
726 break;
727 case SHOW_st_blksize:
728 small = (sizeof(st->st_blksize) == 4);
729 data = st->st_blksize;
730 sdata = NULL;
731 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
732 if (ofmt == 0)
733 ofmt = FMTF_UNSIGNED;
734 break;
735 #if HAVE_STRUCT_STAT_ST_FLAGS
736 case SHOW_st_flags:
737 small = (sizeof(st->st_flags) == 4);
738 data = st->st_flags;
739 sdata = NULL;
740 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
741 if (ofmt == 0)
742 ofmt = FMTF_UNSIGNED;
743 break;
744 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */
745 #if HAVE_STRUCT_STAT_ST_GEN
746 case SHOW_st_gen:
747 small = (sizeof(st->st_gen) == 4);
748 data = st->st_gen;
749 sdata = NULL;
750 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
751 if (ofmt == 0)
752 ofmt = FMTF_UNSIGNED;
753 break;
754 #endif /* HAVE_STRUCT_STAT_ST_GEN */
755 case SHOW_symlink:
756 small = 0;
757 data = 0;
758 if (S_ISLNK(st->st_mode)) {
759 snprintf(path, sizeof(path), " -> ");
760 l = readlink(file, path + 4, sizeof(path) - 4 - 1);
761 if (l == -1) {
762 linkfail = 1;
763 l = 0;
764 path[0] = '\0';
765 }
766 path[l + 4] = '\0';
767 sdata = path + (ofmt == FMTF_STRING ? 0 : 4);
768 }
769 else {
770 linkfail = 1;
771 sdata = "";
772 }
773 formats = FMTF_STRING;
774 if (ofmt == 0)
775 ofmt = FMTF_STRING;
776 break;
777 case SHOW_filetype:
778 small = 0;
779 data = 0;
780 sdata = smode;
781 sdata[0] = '\0';
782 if (hilo == 0 || hilo == LOW_PIECE) {
783 switch (st->st_mode & S_IFMT) {
784 case S_IFIFO: (void)strcat(sdata, "|"); break;
785 case S_IFDIR: (void)strcat(sdata, "/"); break;
786 case S_IFREG:
787 if (st->st_mode &
788 (S_IXUSR | S_IXGRP | S_IXOTH))
789 (void)strcat(sdata, "*");
790 break;
791 case S_IFLNK: (void)strcat(sdata, "@"); break;
792 case S_IFSOCK: (void)strcat(sdata, "="); break;
793 #ifdef S_IFWHT
794 case S_IFWHT: (void)strcat(sdata, "%"); break;
795 #endif /* S_IFWHT */
796 #ifdef S_IFDOOR
797 case S_IFDOOR: (void)strcat(sdata, ">"); break;
798 #endif /* S_IFDOOR */
799 }
800 hilo = 0;
801 }
802 else if (hilo == HIGH_PIECE) {
803 switch (st->st_mode & S_IFMT) {
804 case S_IFIFO: sdata = "Fifo File"; break;
805 case S_IFCHR: sdata = "Character Device"; break;
806 case S_IFDIR: sdata = "Directory"; break;
807 case S_IFBLK: sdata = "Block Device"; break;
808 case S_IFREG: sdata = "Regular File"; break;
809 case S_IFLNK: sdata = "Symbolic Link"; break;
810 case S_IFSOCK: sdata = "Socket"; break;
811 #ifdef S_IFWHT
812 case S_IFWHT: sdata = "Whiteout File"; break;
813 #endif /* S_IFWHT */
814 #ifdef S_IFDOOR
815 case S_IFDOOR: sdata = "Door"; break;
816 #endif /* S_IFDOOR */
817 default: sdata = "???"; break;
818 }
819 hilo = 0;
820 }
821 formats = FMTF_STRING;
822 if (ofmt == 0)
823 ofmt = FMTF_STRING;
824 break;
825 case SHOW_filename:
826 small = 0;
827 data = 0;
828 if (file == NULL)
829 (void)strncpy(path, "(stdin)", sizeof(path));
830 else
831 (void)strncpy(path, file, sizeof(path));
832 sdata = path;
833 formats = FMTF_STRING;
834 if (ofmt == 0)
835 ofmt = FMTF_STRING;
836 break;
837 case SHOW_sizerdev:
838 if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
839 char majdev[20], mindev[20];
840 int l1, l2;
841
842 l1 = format1(st,
843 file,
844 fmt, flen,
845 majdev, sizeof(majdev),
846 flags, size, prec,
847 ofmt, HIGH_PIECE, SHOW_st_rdev);
848 l2 = format1(st,
849 file,
850 fmt, flen,
851 mindev, sizeof(mindev),
852 flags, size, prec,
853 ofmt, LOW_PIECE, SHOW_st_rdev);
854 return (snprintf(buf, blen, "%.*s,%.*s",
855 l1, majdev, l2, mindev));
856 }
857 else {
858 return (format1(st,
859 file,
860 fmt, flen,
861 buf, blen,
862 flags, size, prec,
863 ofmt, 0, SHOW_st_size));
864 }
865 /*NOTREACHED*/
866 default:
867 errx(1, "%.*s: bad format", (int)flen, fmt);
868 }
869
870 /*
871 * If a subdatum was specified but not supported, or an output
872 * format was selected that is not supported, that's an error.
873 */
874 if (hilo != 0 || (ofmt & formats) == 0)
875 errx(1, "%.*s: bad format", (int)flen, fmt);
876
877 /*
878 * Assemble the format string for passing to printf(3).
879 */
880 lfmt[0] = '\0';
881 (void)strcat(lfmt, "%");
882 if (flags & FLAG_POUND)
883 (void)strcat(lfmt, "#");
884 if (flags & FLAG_SPACE)
885 (void)strcat(lfmt, " ");
886 if (flags & FLAG_PLUS)
887 (void)strcat(lfmt, "+");
888 if (flags & FLAG_MINUS)
889 (void)strcat(lfmt, "-");
890 if (flags & FLAG_ZERO)
891 (void)strcat(lfmt, "0");
892
893 /*
894 * Only the timespecs support the FLOAT output format, and that
895 * requires work that differs from the other formats.
896 */
897 if (ofmt == FMTF_FLOAT) {
898 /*
899 * Nothing after the decimal point, so just print seconds.
900 */
901 if (prec == 0) {
902 if (size != -1) {
903 (void)snprintf(tmp, sizeof(tmp), "%d", size);
904 (void)strcat(lfmt, tmp);
905 }
906 (void)strcat(lfmt, "d");
907 return (snprintf(buf, blen, lfmt, secs));
908 }
909
910 /*
911 * Unspecified precision gets all the precision we have:
912 * 9 digits.
913 */
914 if (prec == -1)
915 prec = 9;
916
917 /*
918 * Adjust the size for the decimal point and the digits
919 * that will follow.
920 */
921 size -= prec + 1;
922
923 /*
924 * Any leftover size that's legitimate will be used.
925 */
926 if (size > 0) {
927 (void)snprintf(tmp, sizeof(tmp), "%d", size);
928 (void)strcat(lfmt, tmp);
929 }
930 (void)strcat(lfmt, "d");
931
932 /*
933 * The stuff after the decimal point always needs zero
934 * filling.
935 */
936 (void)strcat(lfmt, ".%0");
937
938 /*
939 * We can "print" at most nine digits of precision. The
940 * rest we will pad on at the end.
941 */
942 (void)snprintf(tmp, sizeof(tmp), "%dd", prec > 9 ? 9 : prec);
943 (void)strcat(lfmt, tmp);
944
945 /*
946 * For precision of less that nine digits, trim off the
947 * less significant figures.
948 */
949 for (; prec < 9; prec++)
950 nsecs /= 10;
951
952 /*
953 * Use the format, and then tack on any zeroes that
954 * might be required to make up the requested precision.
955 */
956 l = snprintf(buf, blen, lfmt, secs, nsecs);
957 for (; prec > 9 && l < blen; prec--, l++)
958 (void)strcat(buf, "0");
959 return (l);
960 }
961
962 /*
963 * Add on size and precision, if specified, to the format.
964 */
965 if (size != -1) {
966 (void)snprintf(tmp, sizeof(tmp), "%d", size);
967 (void)strcat(lfmt, tmp);
968 }
969 if (prec != -1) {
970 (void)snprintf(tmp, sizeof(tmp), ".%d", prec);
971 (void)strcat(lfmt, tmp);
972 }
973
974 /*
975 * String output uses the temporary sdata.
976 */
977 if (ofmt == FMTF_STRING) {
978 if (sdata == NULL)
979 errx(1, "%.*s: bad format", (int)flen, fmt);
980 (void)strcat(lfmt, "s");
981 return (snprintf(buf, blen, lfmt, sdata));
982 }
983
984 /*
985 * Ensure that sign extension does not cause bad looking output
986 * for some forms.
987 */
988 if (small && ofmt != FMTF_DECIMAL)
989 data = (u_int32_t)data;
990
991 /*
992 * The four "numeric" output forms.
993 */
994 (void)strcat(lfmt, "ll");
995 switch (ofmt) {
996 case FMTF_DECIMAL: (void)strcat(lfmt, "d"); break;
997 case FMTF_OCTAL: (void)strcat(lfmt, "o"); break;
998 case FMTF_UNSIGNED: (void)strcat(lfmt, "u"); break;
999 case FMTF_HEX: (void)strcat(lfmt, "x"); break;
1000 }
1001
1002 return (snprintf(buf, blen, lfmt, data));
1003 }
1004