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