1/* $NetBSD: stat.c,v 1.3 2002/05/31 16:45:16 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 *
--- 21 unchanged lines hidden (view full) ---
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.3 2002/05/31 16:45:16 atatat Exp $");
42__FBSDID("$FreeBSD: head/usr.bin/stat/stat.c 97953 2002-06-06 19:27:17Z dougb $");
43#endif
44
45#include <sys/types.h>
46#include <sys/param.h>
47#include <sys/stat.h>
48#include <unistd.h>
49#include <err.h>
50#include <string.h>
51#include <stdio.h>
52#include <ctype.h>
53#include <stddef.h>
54#include <stdlib.h>
55#include <pwd.h>
56#include <grp.h>
57
58#define DEF_FORMAT \
59 "%d %i %Sp %l %Su %Sg %r %z \"%Sa\" \"%Sm\" \"%Sc\" %k %b %N"
60#define RAW_FORMAT "%d %i %#p %l %u %g %r %z %a %m %c %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 " \
--- 36 unchanged lines hidden (view full) ---
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 HIGH_PIECE 'H'
110#define MIDDLE_PIECE 'M'
111#define LOW_PIECE 'L'
112
113#define SHOW_st_dev 'd'
114#define SHOW_st_ino 'i'
115#define SHOW_st_mode 'p'
116#define SHOW_st_nlink 'l'
--- 8 unchanged lines hidden (view full) ---
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
133void usage(void);
134void output(const struct stat *, const char *,
135 const char *, int, int);
136int 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
143char *timefmt;
144
145#define addchar(b, n, l, c, nl) \
146 do { \
147 if ((*(n)) < (l)) { \
148 (b)[(*(n))++] = (c); \
149 (*nl) = ((c) == '\n'); \
150 } \
151 } while (0/*CONSTCOND*/)
152
153int
154main(int argc, char *argv[])
155{
156 struct stat st;
157 int ch, rc, errs;
158 int lsF, fmtchar, usestat, fn, nonl;
159 char *statfmt;
160
161 lsF = 0;
162 fmtchar = '\0';
163 usestat = 0;
164 nonl = 0;
165 statfmt = NULL;
166 timefmt = NULL;
167
168 while ((ch = getopt(argc, argv, "f:FlLnrst:x")) != -1)
169 switch (ch) {
170 case 'F':
171 lsF = 1;
172 break;
173 case 'L':
174 usestat = 1;
175 break;
176 case 'n':
177 nonl = 1;
178 break;
179 case 'f':
180 statfmt = optarg;
181 /* FALLTHROUGH */
182 case 'l':
183 case 'r':
184 case 's':
185 case 'x':
186 if (fmtchar != 0)
187 errx(1, "can't use format '%c' with '%c'",
188 fmtchar, ch);
189 fmtchar = ch;
190 break;
191 case 't':
192 timefmt = optarg;
193 break;
194 default:
195 usage();
196 }
197
198 argc -= optind;
199 argv += optind;
200 fn = 1;
201
202 if (fmtchar == '\0') {
203 if (lsF)
--- 21 unchanged lines hidden (view full) ---
225 statfmt = SHELL_FORMAT;
226 break;
227 case 'x':
228 statfmt = LINUX_FORMAT;
229 if (timefmt == NULL)
230 timefmt = "%c";
231 break;
232 default:
233 usage();
234 /*NOTREACHED*/
235 }
236
237 if (timefmt == NULL)
238 timefmt = TIME_FORMAT;
239
240 errs = 0;
241 do {
242 if (argc == 0)
243 rc = fstat(STDIN_FILENO, &st);
244 else if (usestat)
245 rc = stat(argv[0], &st);
246 else
247 rc = lstat(argv[0], &st);
248
249 if (rc == -1) {
250 errs = 1;
251 warn("%s: stat", argc == 0 ? "(stdin)" : argv[0]);
252 }
253 else
254 output(&st, argv[0], statfmt, fn, nonl);
255
256 argv++;
257 argc--;
258 fn++;
259 } while (argc > 0);
260
261 return (errs);
262}
263
264void
265usage(void)
266{
267
268 (void)fprintf(stderr,
269 "usage: %s [-FlLnrsx] [-f format] [-t timefmt] [file ...]\n",
270 getprogname());
271 exit(1);
272}
273
274/*
275 * Parses a format string.
276 */
277void
278output(const struct stat *st, const char *file,
279 const char *statfmt, int fn, int nonl)
280{
281 int flags, size, prec, ofmt, hilo, what;
282 char buf[4096], subbuf[MAXPATHLEN];
283 const char *subfmt;
284 int nl, t, i;
285 size_t len;
286
287 len = 0;
288 while (*statfmt != '\0') {
289
290 /*
291 * Non-format characters go straight out.
292 */
293 if (*statfmt != FMT_MAGIC) {
294 addchar(buf, &len, sizeof(buf), *statfmt, &nl);
295 statfmt++;
296 continue;
297 }
298
299 /*
300 * The current format "substring" starts here,
301 * and then we skip the magic.
302 */
303 subfmt = statfmt;
304 statfmt++;
305
306 /*
307 * Some simple one-character "formats".
308 */
309 switch (*statfmt) {
310 case SIMPLE_NEWLINE:
311 addchar(buf, &len, sizeof(buf), '\n', &nl);
312 statfmt++;
313 continue;
314 case SIMPLE_TAB:
315 addchar(buf, &len, sizeof(buf), '\t', &nl);
316 statfmt++;
317 continue;
318 case SIMPLE_PERCENT:
319 addchar(buf, &len, sizeof(buf), '%', &nl);
320 statfmt++;
321 continue;
322 case SIMPLE_NUMBER: {
323 char num[12], *p;
324
325 snprintf(num, sizeof(num), "%d", fn);
326 for (p = &num[0]; *p; p++)
327 addchar(buf, &len, sizeof(buf), *p, &nl);
328 statfmt++;
329 continue;
330 }
331 }
332
333 /*
334 * This must be an actual format string. Format strings are
335 * similar to printf(3) formats up to a point, and are of
--- 52 unchanged lines hidden (view full) ---
388 while (isdigit((unsigned)*statfmt)) {
389 prec = (prec * 10) + (*statfmt - '0');
390 statfmt++;
391 if (prec < 0)
392 goto badfmt;
393 }
394 }
395
396#define fmtcase(x, y) case (y): (x) = (y); statfmt++; break
397 switch (*statfmt) {
398 fmtcase(ofmt, FMT_DECIMAL);
399 fmtcase(ofmt, FMT_OCTAL);
400 fmtcase(ofmt, FMT_UNSIGNED);
401 fmtcase(ofmt, FMT_HEX);
402 fmtcase(ofmt, FMT_FLOAT);
403 fmtcase(ofmt, FMT_STRING);
404 default:
405 ofmt = 0;
406 break;
407 }
408
409 switch (*statfmt) {
410 fmtcase(hilo, HIGH_PIECE);
411 fmtcase(hilo, MIDDLE_PIECE);
--- 21 unchanged lines hidden (view full) ---
433 fmtcase(what, SHOW_st_gen);
434 fmtcase(what, SHOW_symlink);
435 fmtcase(what, SHOW_filetype);
436 fmtcase(what, SHOW_filename);
437 fmtcase(what, SHOW_sizerdev);
438 default:
439 goto badfmt;
440 }
441#undef fmtcase
442
443 t = format1(st,
444 file,
445 subfmt, statfmt - subfmt,
446 subbuf, sizeof(subbuf),
447 flags, size, prec, ofmt, hilo, what);
448
449 for (i = 0; i < t && i < sizeof(subbuf); i++)
450 addchar(buf, &len, sizeof(buf), subbuf[i], &nl);
451
452 continue;
453
454 badfmt:
455 errx(1, "%.*s: bad format",
456 (int)(statfmt - subfmt + 1), subfmt);
457 }
458
459 (void)write(STDOUT_FILENO, buf, len);
460 if (!nl && !nonl)
461 (void)write(STDOUT_FILENO, "\n", sizeof("\n") - 1);
462}
463
464/*
465 * Arranges output according to a single parsed format substring.
466 */
467int
468format1(const struct stat *st,
469 const char *file,
470 const char *fmt, int flen,
471 char *buf, size_t blen,
472 int flags, int size, int prec, int ofmt,
473 int hilo, int what)
474{
475 u_int64_t data;
476 char *sdata, lfmt[24], tmp[20];
477 char smode[12], sid[12], path[MAXPATHLEN + 4];
478 struct passwd *pw;
479 struct group *gr;
480 const struct timespec *tsp;
481 struct timespec ts;
482 struct tm *tm;
483 int l, small, formats;
484
485 tsp = NULL;
--- 20 unchanged lines hidden (view full) ---
506 if (hilo == HIGH_PIECE) {
507 data = major(data);
508 hilo = 0;
509 }
510 else if (hilo == LOW_PIECE) {
511 data = minor((unsigned)data);
512 hilo = 0;
513 }
514 formats = FMT_DECIMAL | FMT_OCTAL | FMT_UNSIGNED | FMT_HEX |
515 FMT_STRING;
516 if (ofmt == 0)
517 ofmt = FMT_UNSIGNED;
518 break;
519 case SHOW_st_ino:
520 small = (sizeof(st->st_ino) == 4);
521 data = st->st_ino;
522 sdata = NULL;
523 formats = FMT_DECIMAL | FMT_OCTAL | FMT_UNSIGNED | FMT_HEX;
524 if (ofmt == 0)
525 ofmt = FMT_UNSIGNED;
526 break;
527 case SHOW_st_mode:
528 small = (sizeof(st->st_mode) == 4);
529 data = st->st_mode;
530 strmode(st->st_mode, smode);
531 sdata = smode;
532 l = strlen(sdata);
533 if (sdata[l - 1] == ' ')
--- 11 unchanged lines hidden (view full) ---
545 hilo = 0;
546 }
547 else if (hilo == LOW_PIECE) {
548 data &= 0777;
549 sdata += 7;
550 sdata[3] = '\0';
551 hilo = 0;
552 }
553 formats = FMT_DECIMAL | FMT_OCTAL | FMT_UNSIGNED | FMT_HEX |
554 FMT_STRING;
555 if (ofmt == 0)
556 ofmt = FMT_OCTAL;
557 break;
558 case SHOW_st_nlink:
559 small = (sizeof(st->st_dev) == 4);
560 data = st->st_nlink;
561 sdata = NULL;
562 formats = FMT_DECIMAL | FMT_OCTAL | FMT_UNSIGNED | FMT_HEX;
563 if (ofmt == 0)
564 ofmt = FMT_UNSIGNED;
565 break;
566 case SHOW_st_uid:
567 small = (sizeof(st->st_uid) == 4);
568 data = st->st_uid;
569 if ((pw = getpwuid(st->st_uid)) != NULL)
570 sdata = pw->pw_name;
571 else {
572 snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_uid);
573 sdata = sid;
574 }
575 formats = FMT_DECIMAL | FMT_OCTAL | FMT_UNSIGNED | FMT_HEX |
576 FMT_STRING;
577 if (ofmt == 0)
578 ofmt = FMT_UNSIGNED;
579 break;
580 case SHOW_st_gid:
581 small = (sizeof(st->st_gid) == 4);
582 data = st->st_gid;
583 if ((gr = getgrgid(st->st_gid)) != NULL)
584 sdata = gr->gr_name;
585 else {
586 snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_gid);
587 sdata = sid;
588 }
589 formats = FMT_DECIMAL | FMT_OCTAL | FMT_UNSIGNED | FMT_HEX |
590 FMT_STRING;
591 if (ofmt == 0)
592 ofmt = FMT_UNSIGNED;
593 break;
594 case SHOW_st_atime:
595 tsp = &st->st_atimespec;
596 /* FALLTHROUGH */
597 case SHOW_st_mtime:
598 if (tsp == NULL)
599 tsp = &st->st_mtimespec;
600 /* FALLTHROUGH */
601 case SHOW_st_ctime:
602 if (tsp == NULL)
603 tsp = &st->st_ctimespec;
604 ts = *tsp; /* copy so we can muck with it */
605 small = (sizeof(ts.tv_sec) == 4);
606 data = ts.tv_sec;
607 small = 1;
608 tm = localtime(&ts.tv_sec);
609 (void)strftime(path, sizeof(path), timefmt, tm);
610 sdata = path;
611 formats = FMT_DECIMAL | FMT_OCTAL | FMT_UNSIGNED | FMT_HEX |
612 FMT_FLOAT | FMT_STRING;
613 if (ofmt == 0)
614 ofmt = FMT_DECIMAL;
615 break;
616 case SHOW_st_size:
617 small = (sizeof(st->st_size) == 4);
618 data = st->st_size;
619 sdata = NULL;
620 formats = FMT_DECIMAL | FMT_OCTAL | FMT_UNSIGNED | FMT_HEX;
621 if (ofmt == 0)
622 ofmt = FMT_UNSIGNED;
623 break;
624 case SHOW_st_blocks:
625 small = (sizeof(st->st_blocks) == 4);
626 data = st->st_blocks;
627 sdata = NULL;
628 formats = FMT_DECIMAL | FMT_OCTAL | FMT_UNSIGNED | FMT_HEX;
629 if (ofmt == 0)
630 ofmt = FMT_UNSIGNED;
631 break;
632 case SHOW_st_blksize:
633 small = (sizeof(st->st_blksize) == 4);
634 data = st->st_blksize;
635 sdata = NULL;
636 formats = FMT_DECIMAL | FMT_OCTAL | FMT_UNSIGNED | FMT_HEX;
637 if (ofmt == 0)
638 ofmt = FMT_UNSIGNED;
639 break;
640 case SHOW_st_flags:
641 small = (sizeof(st->st_flags) == 4);
642 data = st->st_flags;
643 sdata = NULL;
644 formats = FMT_DECIMAL | FMT_OCTAL | FMT_UNSIGNED | FMT_HEX;
645 if (ofmt == 0)
646 ofmt = FMT_UNSIGNED;
647 break;
648 case SHOW_st_gen:
649 small = (sizeof(st->st_gen) == 4);
650 data = st->st_gen;
651 sdata = NULL;
652 formats = FMT_DECIMAL | FMT_OCTAL | FMT_UNSIGNED | FMT_HEX;
653 if (ofmt == 0)
654 ofmt = FMT_UNSIGNED;
655 break;
656 case SHOW_symlink:
657 small = 0;
658 data = 0;
659 if (S_ISLNK(st->st_mode)) {
660 snprintf(path, sizeof(path), " -> ");
661 l = readlink(file, path + 4, sizeof(path) - 4);
662 if (l == -1) {
663 l = 0;
664 path[0] = '\0';
665 }
666 path[l + 4] = '\0';
667 sdata = path + (ofmt == FMT_STRING ? 0 : 4);
668 }
669 else
670 sdata = "";
671 formats = FMT_STRING;
672 if (ofmt == 0)
673 ofmt = FMT_STRING;
674 break;
675 case SHOW_filetype:
676 small = 0;
677 data = 0;
678 sdata = smode;
679 sdata[0] = '\0';
680 if (hilo == 0 || hilo == LOW_PIECE) {
681 switch (st->st_mode & S_IFMT) {
--- 19 unchanged lines hidden (view full) ---
701 case S_IFREG: sdata = "Regular File"; break;
702 case S_IFLNK: sdata = "Symbolic Link"; break;
703 case S_IFSOCK: sdata = "Socket"; break;
704 case S_IFWHT: sdata = "Whiteout File"; break;
705 default: sdata = "???"; break;
706 }
707 hilo = 0;
708 }
709 formats = FMT_STRING;
710 if (ofmt == 0)
711 ofmt = FMT_STRING;
712 break;
713 case SHOW_filename:
714 small = 0;
715 data = 0;
716 if (file == NULL)
717 (void)strncpy(path, "(stdin)", sizeof(path));
718 else
719 (void)strncpy(path, file, sizeof(path));
720 sdata = path;
721 formats = FMT_STRING;
722 if (ofmt == 0)
723 ofmt = FMT_STRING;
724 break;
725 case SHOW_sizerdev:
726 if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
727 char majdev[20], mindev[20];
728 int l1, l2;
729
730 l1 = format1(st,
731 file,
--- 45 unchanged lines hidden (view full) ---
777 (void)strcat(lfmt, "-");
778 if (flags & FLAG_ZERO)
779 (void)strcat(lfmt, "0");
780
781 /*
782 * Only the timespecs support the FLOAT output format, and that
783 * requires work that differs from the other formats.
784 */
785 if (ofmt == FMT_FLOAT) {
786 /*
787 * Nothing after the decimal point, so just print seconds.
788 */
789 if (prec == 0) {
790 if (size != -1) {
791 (void)snprintf(tmp, sizeof(tmp), "%d", size);
792 (void)strcat(lfmt, tmp);
793 }
--- 63 unchanged lines hidden (view full) ---
857 if (prec != -1) {
858 (void)snprintf(tmp, sizeof(tmp), ".%d", prec);
859 (void)strcat(lfmt, tmp);
860 }
861
862 /*
863 * String output uses the temporary sdata.
864 */
865 if (ofmt == FMT_STRING) {
866 if (sdata == NULL)
867 errx(1, "%.*s: bad format", (int)flen, fmt);
868 (void)strcat(lfmt, "s");
869 return (snprintf(buf, blen, lfmt, sdata));
870 }
871
872 /*
873 * Ensure that sign extension does not cause bad looking output
874 * for some forms.
875 */
876 if (small && ofmt != FMT_DECIMAL)
877 data = (u_int32_t)data;
878
879 /*
880 * The four "numeric" output forms.
881 */
882 (void)strcat(lfmt, "ll");
883 switch (ofmt) {
884 case FMT_DECIMAL: (void)strcat(lfmt, "d"); break;
885 case FMT_OCTAL: (void)strcat(lfmt, "o"); break;
886 case FMT_UNSIGNED: (void)strcat(lfmt, "u"); break;
887 case FMT_HEX: (void)strcat(lfmt, "x"); break;
888 }
889
890 return (snprintf(buf, blen, lfmt, data));
891}
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 *
--- 21 unchanged lines hidden (view full) ---
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.3 2002/05/31 16:45:16 atatat Exp $");
42__FBSDID("$FreeBSD: head/usr.bin/stat/stat.c 97953 2002-06-06 19:27:17Z dougb $");
43#endif
44
45#include <sys/types.h>
46#include <sys/param.h>
47#include <sys/stat.h>
48#include <unistd.h>
49#include <err.h>
50#include <string.h>
51#include <stdio.h>
52#include <ctype.h>
53#include <stddef.h>
54#include <stdlib.h>
55#include <pwd.h>
56#include <grp.h>
57
58#define DEF_FORMAT \
59 "%d %i %Sp %l %Su %Sg %r %z \"%Sa\" \"%Sm\" \"%Sc\" %k %b %N"
60#define RAW_FORMAT "%d %i %#p %l %u %g %r %z %a %m %c %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 " \
--- 36 unchanged lines hidden (view full) ---
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 HIGH_PIECE 'H'
110#define MIDDLE_PIECE 'M'
111#define LOW_PIECE 'L'
112
113#define SHOW_st_dev 'd'
114#define SHOW_st_ino 'i'
115#define SHOW_st_mode 'p'
116#define SHOW_st_nlink 'l'
--- 8 unchanged lines hidden (view full) ---
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
133void usage(void);
134void output(const struct stat *, const char *,
135 const char *, int, int);
136int 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
143char *timefmt;
144
145#define addchar(b, n, l, c, nl) \
146 do { \
147 if ((*(n)) < (l)) { \
148 (b)[(*(n))++] = (c); \
149 (*nl) = ((c) == '\n'); \
150 } \
151 } while (0/*CONSTCOND*/)
152
153int
154main(int argc, char *argv[])
155{
156 struct stat st;
157 int ch, rc, errs;
158 int lsF, fmtchar, usestat, fn, nonl;
159 char *statfmt;
160
161 lsF = 0;
162 fmtchar = '\0';
163 usestat = 0;
164 nonl = 0;
165 statfmt = NULL;
166 timefmt = NULL;
167
168 while ((ch = getopt(argc, argv, "f:FlLnrst:x")) != -1)
169 switch (ch) {
170 case 'F':
171 lsF = 1;
172 break;
173 case 'L':
174 usestat = 1;
175 break;
176 case 'n':
177 nonl = 1;
178 break;
179 case 'f':
180 statfmt = optarg;
181 /* FALLTHROUGH */
182 case 'l':
183 case 'r':
184 case 's':
185 case 'x':
186 if (fmtchar != 0)
187 errx(1, "can't use format '%c' with '%c'",
188 fmtchar, ch);
189 fmtchar = ch;
190 break;
191 case 't':
192 timefmt = optarg;
193 break;
194 default:
195 usage();
196 }
197
198 argc -= optind;
199 argv += optind;
200 fn = 1;
201
202 if (fmtchar == '\0') {
203 if (lsF)
--- 21 unchanged lines hidden (view full) ---
225 statfmt = SHELL_FORMAT;
226 break;
227 case 'x':
228 statfmt = LINUX_FORMAT;
229 if (timefmt == NULL)
230 timefmt = "%c";
231 break;
232 default:
233 usage();
234 /*NOTREACHED*/
235 }
236
237 if (timefmt == NULL)
238 timefmt = TIME_FORMAT;
239
240 errs = 0;
241 do {
242 if (argc == 0)
243 rc = fstat(STDIN_FILENO, &st);
244 else if (usestat)
245 rc = stat(argv[0], &st);
246 else
247 rc = lstat(argv[0], &st);
248
249 if (rc == -1) {
250 errs = 1;
251 warn("%s: stat", argc == 0 ? "(stdin)" : argv[0]);
252 }
253 else
254 output(&st, argv[0], statfmt, fn, nonl);
255
256 argv++;
257 argc--;
258 fn++;
259 } while (argc > 0);
260
261 return (errs);
262}
263
264void
265usage(void)
266{
267
268 (void)fprintf(stderr,
269 "usage: %s [-FlLnrsx] [-f format] [-t timefmt] [file ...]\n",
270 getprogname());
271 exit(1);
272}
273
274/*
275 * Parses a format string.
276 */
277void
278output(const struct stat *st, const char *file,
279 const char *statfmt, int fn, int nonl)
280{
281 int flags, size, prec, ofmt, hilo, what;
282 char buf[4096], subbuf[MAXPATHLEN];
283 const char *subfmt;
284 int nl, t, i;
285 size_t len;
286
287 len = 0;
288 while (*statfmt != '\0') {
289
290 /*
291 * Non-format characters go straight out.
292 */
293 if (*statfmt != FMT_MAGIC) {
294 addchar(buf, &len, sizeof(buf), *statfmt, &nl);
295 statfmt++;
296 continue;
297 }
298
299 /*
300 * The current format "substring" starts here,
301 * and then we skip the magic.
302 */
303 subfmt = statfmt;
304 statfmt++;
305
306 /*
307 * Some simple one-character "formats".
308 */
309 switch (*statfmt) {
310 case SIMPLE_NEWLINE:
311 addchar(buf, &len, sizeof(buf), '\n', &nl);
312 statfmt++;
313 continue;
314 case SIMPLE_TAB:
315 addchar(buf, &len, sizeof(buf), '\t', &nl);
316 statfmt++;
317 continue;
318 case SIMPLE_PERCENT:
319 addchar(buf, &len, sizeof(buf), '%', &nl);
320 statfmt++;
321 continue;
322 case SIMPLE_NUMBER: {
323 char num[12], *p;
324
325 snprintf(num, sizeof(num), "%d", fn);
326 for (p = &num[0]; *p; p++)
327 addchar(buf, &len, sizeof(buf), *p, &nl);
328 statfmt++;
329 continue;
330 }
331 }
332
333 /*
334 * This must be an actual format string. Format strings are
335 * similar to printf(3) formats up to a point, and are of
--- 52 unchanged lines hidden (view full) ---
388 while (isdigit((unsigned)*statfmt)) {
389 prec = (prec * 10) + (*statfmt - '0');
390 statfmt++;
391 if (prec < 0)
392 goto badfmt;
393 }
394 }
395
396#define fmtcase(x, y) case (y): (x) = (y); statfmt++; break
397 switch (*statfmt) {
398 fmtcase(ofmt, FMT_DECIMAL);
399 fmtcase(ofmt, FMT_OCTAL);
400 fmtcase(ofmt, FMT_UNSIGNED);
401 fmtcase(ofmt, FMT_HEX);
402 fmtcase(ofmt, FMT_FLOAT);
403 fmtcase(ofmt, FMT_STRING);
404 default:
405 ofmt = 0;
406 break;
407 }
408
409 switch (*statfmt) {
410 fmtcase(hilo, HIGH_PIECE);
411 fmtcase(hilo, MIDDLE_PIECE);
--- 21 unchanged lines hidden (view full) ---
433 fmtcase(what, SHOW_st_gen);
434 fmtcase(what, SHOW_symlink);
435 fmtcase(what, SHOW_filetype);
436 fmtcase(what, SHOW_filename);
437 fmtcase(what, SHOW_sizerdev);
438 default:
439 goto badfmt;
440 }
441#undef fmtcase
442
443 t = format1(st,
444 file,
445 subfmt, statfmt - subfmt,
446 subbuf, sizeof(subbuf),
447 flags, size, prec, ofmt, hilo, what);
448
449 for (i = 0; i < t && i < sizeof(subbuf); i++)
450 addchar(buf, &len, sizeof(buf), subbuf[i], &nl);
451
452 continue;
453
454 badfmt:
455 errx(1, "%.*s: bad format",
456 (int)(statfmt - subfmt + 1), subfmt);
457 }
458
459 (void)write(STDOUT_FILENO, buf, len);
460 if (!nl && !nonl)
461 (void)write(STDOUT_FILENO, "\n", sizeof("\n") - 1);
462}
463
464/*
465 * Arranges output according to a single parsed format substring.
466 */
467int
468format1(const struct stat *st,
469 const char *file,
470 const char *fmt, int flen,
471 char *buf, size_t blen,
472 int flags, int size, int prec, int ofmt,
473 int hilo, int what)
474{
475 u_int64_t data;
476 char *sdata, lfmt[24], tmp[20];
477 char smode[12], sid[12], path[MAXPATHLEN + 4];
478 struct passwd *pw;
479 struct group *gr;
480 const struct timespec *tsp;
481 struct timespec ts;
482 struct tm *tm;
483 int l, small, formats;
484
485 tsp = NULL;
--- 20 unchanged lines hidden (view full) ---
506 if (hilo == HIGH_PIECE) {
507 data = major(data);
508 hilo = 0;
509 }
510 else if (hilo == LOW_PIECE) {
511 data = minor((unsigned)data);
512 hilo = 0;
513 }
514 formats = FMT_DECIMAL | FMT_OCTAL | FMT_UNSIGNED | FMT_HEX |
515 FMT_STRING;
516 if (ofmt == 0)
517 ofmt = FMT_UNSIGNED;
518 break;
519 case SHOW_st_ino:
520 small = (sizeof(st->st_ino) == 4);
521 data = st->st_ino;
522 sdata = NULL;
523 formats = FMT_DECIMAL | FMT_OCTAL | FMT_UNSIGNED | FMT_HEX;
524 if (ofmt == 0)
525 ofmt = FMT_UNSIGNED;
526 break;
527 case SHOW_st_mode:
528 small = (sizeof(st->st_mode) == 4);
529 data = st->st_mode;
530 strmode(st->st_mode, smode);
531 sdata = smode;
532 l = strlen(sdata);
533 if (sdata[l - 1] == ' ')
--- 11 unchanged lines hidden (view full) ---
545 hilo = 0;
546 }
547 else if (hilo == LOW_PIECE) {
548 data &= 0777;
549 sdata += 7;
550 sdata[3] = '\0';
551 hilo = 0;
552 }
553 formats = FMT_DECIMAL | FMT_OCTAL | FMT_UNSIGNED | FMT_HEX |
554 FMT_STRING;
555 if (ofmt == 0)
556 ofmt = FMT_OCTAL;
557 break;
558 case SHOW_st_nlink:
559 small = (sizeof(st->st_dev) == 4);
560 data = st->st_nlink;
561 sdata = NULL;
562 formats = FMT_DECIMAL | FMT_OCTAL | FMT_UNSIGNED | FMT_HEX;
563 if (ofmt == 0)
564 ofmt = FMT_UNSIGNED;
565 break;
566 case SHOW_st_uid:
567 small = (sizeof(st->st_uid) == 4);
568 data = st->st_uid;
569 if ((pw = getpwuid(st->st_uid)) != NULL)
570 sdata = pw->pw_name;
571 else {
572 snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_uid);
573 sdata = sid;
574 }
575 formats = FMT_DECIMAL | FMT_OCTAL | FMT_UNSIGNED | FMT_HEX |
576 FMT_STRING;
577 if (ofmt == 0)
578 ofmt = FMT_UNSIGNED;
579 break;
580 case SHOW_st_gid:
581 small = (sizeof(st->st_gid) == 4);
582 data = st->st_gid;
583 if ((gr = getgrgid(st->st_gid)) != NULL)
584 sdata = gr->gr_name;
585 else {
586 snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_gid);
587 sdata = sid;
588 }
589 formats = FMT_DECIMAL | FMT_OCTAL | FMT_UNSIGNED | FMT_HEX |
590 FMT_STRING;
591 if (ofmt == 0)
592 ofmt = FMT_UNSIGNED;
593 break;
594 case SHOW_st_atime:
595 tsp = &st->st_atimespec;
596 /* FALLTHROUGH */
597 case SHOW_st_mtime:
598 if (tsp == NULL)
599 tsp = &st->st_mtimespec;
600 /* FALLTHROUGH */
601 case SHOW_st_ctime:
602 if (tsp == NULL)
603 tsp = &st->st_ctimespec;
604 ts = *tsp; /* copy so we can muck with it */
605 small = (sizeof(ts.tv_sec) == 4);
606 data = ts.tv_sec;
607 small = 1;
608 tm = localtime(&ts.tv_sec);
609 (void)strftime(path, sizeof(path), timefmt, tm);
610 sdata = path;
611 formats = FMT_DECIMAL | FMT_OCTAL | FMT_UNSIGNED | FMT_HEX |
612 FMT_FLOAT | FMT_STRING;
613 if (ofmt == 0)
614 ofmt = FMT_DECIMAL;
615 break;
616 case SHOW_st_size:
617 small = (sizeof(st->st_size) == 4);
618 data = st->st_size;
619 sdata = NULL;
620 formats = FMT_DECIMAL | FMT_OCTAL | FMT_UNSIGNED | FMT_HEX;
621 if (ofmt == 0)
622 ofmt = FMT_UNSIGNED;
623 break;
624 case SHOW_st_blocks:
625 small = (sizeof(st->st_blocks) == 4);
626 data = st->st_blocks;
627 sdata = NULL;
628 formats = FMT_DECIMAL | FMT_OCTAL | FMT_UNSIGNED | FMT_HEX;
629 if (ofmt == 0)
630 ofmt = FMT_UNSIGNED;
631 break;
632 case SHOW_st_blksize:
633 small = (sizeof(st->st_blksize) == 4);
634 data = st->st_blksize;
635 sdata = NULL;
636 formats = FMT_DECIMAL | FMT_OCTAL | FMT_UNSIGNED | FMT_HEX;
637 if (ofmt == 0)
638 ofmt = FMT_UNSIGNED;
639 break;
640 case SHOW_st_flags:
641 small = (sizeof(st->st_flags) == 4);
642 data = st->st_flags;
643 sdata = NULL;
644 formats = FMT_DECIMAL | FMT_OCTAL | FMT_UNSIGNED | FMT_HEX;
645 if (ofmt == 0)
646 ofmt = FMT_UNSIGNED;
647 break;
648 case SHOW_st_gen:
649 small = (sizeof(st->st_gen) == 4);
650 data = st->st_gen;
651 sdata = NULL;
652 formats = FMT_DECIMAL | FMT_OCTAL | FMT_UNSIGNED | FMT_HEX;
653 if (ofmt == 0)
654 ofmt = FMT_UNSIGNED;
655 break;
656 case SHOW_symlink:
657 small = 0;
658 data = 0;
659 if (S_ISLNK(st->st_mode)) {
660 snprintf(path, sizeof(path), " -> ");
661 l = readlink(file, path + 4, sizeof(path) - 4);
662 if (l == -1) {
663 l = 0;
664 path[0] = '\0';
665 }
666 path[l + 4] = '\0';
667 sdata = path + (ofmt == FMT_STRING ? 0 : 4);
668 }
669 else
670 sdata = "";
671 formats = FMT_STRING;
672 if (ofmt == 0)
673 ofmt = FMT_STRING;
674 break;
675 case SHOW_filetype:
676 small = 0;
677 data = 0;
678 sdata = smode;
679 sdata[0] = '\0';
680 if (hilo == 0 || hilo == LOW_PIECE) {
681 switch (st->st_mode & S_IFMT) {
--- 19 unchanged lines hidden (view full) ---
701 case S_IFREG: sdata = "Regular File"; break;
702 case S_IFLNK: sdata = "Symbolic Link"; break;
703 case S_IFSOCK: sdata = "Socket"; break;
704 case S_IFWHT: sdata = "Whiteout File"; break;
705 default: sdata = "???"; break;
706 }
707 hilo = 0;
708 }
709 formats = FMT_STRING;
710 if (ofmt == 0)
711 ofmt = FMT_STRING;
712 break;
713 case SHOW_filename:
714 small = 0;
715 data = 0;
716 if (file == NULL)
717 (void)strncpy(path, "(stdin)", sizeof(path));
718 else
719 (void)strncpy(path, file, sizeof(path));
720 sdata = path;
721 formats = FMT_STRING;
722 if (ofmt == 0)
723 ofmt = FMT_STRING;
724 break;
725 case SHOW_sizerdev:
726 if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
727 char majdev[20], mindev[20];
728 int l1, l2;
729
730 l1 = format1(st,
731 file,
--- 45 unchanged lines hidden (view full) ---
777 (void)strcat(lfmt, "-");
778 if (flags & FLAG_ZERO)
779 (void)strcat(lfmt, "0");
780
781 /*
782 * Only the timespecs support the FLOAT output format, and that
783 * requires work that differs from the other formats.
784 */
785 if (ofmt == FMT_FLOAT) {
786 /*
787 * Nothing after the decimal point, so just print seconds.
788 */
789 if (prec == 0) {
790 if (size != -1) {
791 (void)snprintf(tmp, sizeof(tmp), "%d", size);
792 (void)strcat(lfmt, tmp);
793 }
--- 63 unchanged lines hidden (view full) ---
857 if (prec != -1) {
858 (void)snprintf(tmp, sizeof(tmp), ".%d", prec);
859 (void)strcat(lfmt, tmp);
860 }
861
862 /*
863 * String output uses the temporary sdata.
864 */
865 if (ofmt == FMT_STRING) {
866 if (sdata == NULL)
867 errx(1, "%.*s: bad format", (int)flen, fmt);
868 (void)strcat(lfmt, "s");
869 return (snprintf(buf, blen, lfmt, sdata));
870 }
871
872 /*
873 * Ensure that sign extension does not cause bad looking output
874 * for some forms.
875 */
876 if (small && ofmt != FMT_DECIMAL)
877 data = (u_int32_t)data;
878
879 /*
880 * The four "numeric" output forms.
881 */
882 (void)strcat(lfmt, "ll");
883 switch (ofmt) {
884 case FMT_DECIMAL: (void)strcat(lfmt, "d"); break;
885 case FMT_OCTAL: (void)strcat(lfmt, "o"); break;
886 case FMT_UNSIGNED: (void)strcat(lfmt, "u"); break;
887 case FMT_HEX: (void)strcat(lfmt, "x"); break;
888 }
889
890 return (snprintf(buf, blen, lfmt, data));
891}