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