1/* $NetBSD: stat.c,v 1.6 2002/07/09 21:25:00 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#if 0 |
40#ifndef lint |
41__RCSID("$NetBSD: stat.c,v 1.6 2002/07/09 21:25:00 atatat Exp $"); |
42#endif |
43#endif |
44 |
45#include <sys/cdefs.h> 46__FBSDID("$FreeBSD: head/usr.bin/stat/stat.c 101773 2002-08-13 08:23:49Z dougb $"); 47 |
48#include <sys/stat.h> |
49#include <sys/syslimits.h> 50 51#include <ctype.h> |
52#include <err.h> |
53#include <grp.h> 54#include <pwd.h> |
55#include <stdio.h> |
56#include <stdlib.h> |
57#include <string.h> 58#include <time.h> 59#include <unistd.h> |
60 61#define DEF_FORMAT \ 62 "%d %i %Sp %l %Su %Sg %r %z \"%Sa\" \"%Sm\" \"%Sc\" %k %b %N" 63#define RAW_FORMAT "%d %i %#p %l %u %g %r %z %a %m %c %k %b %N" 64#define LS_FORMAT "%Sp %l %Su %Sg %Z %Sm %N%SY" 65#define LSF_FORMAT "%Sp %l %Su %Sg %Z %Sm %N%T%SY" 66#define SHELL_FORMAT \ 67 "st_dev=%d st_ino=%i st_mode=%#p st_nlink=%l " \ --- 36 unchanged lines hidden (view full) --- 104 105#define FMT_DECIMAL 'D' 106#define FMT_OCTAL 'O' 107#define FMT_UNSIGNED 'U' 108#define FMT_HEX 'X' 109#define FMT_FLOAT 'F' 110#define FMT_STRING 'S' 111 |
112#define FMTF_DECIMAL 0x01 113#define FMTF_OCTAL 0x02 114#define FMTF_UNSIGNED 0x04 115#define FMTF_HEX 0x08 116#define FMTF_FLOAT 0x10 117#define FMTF_STRING 0x20 118 |
119#define HIGH_PIECE 'H' 120#define MIDDLE_PIECE 'M' 121#define LOW_PIECE 'L' 122 123#define SHOW_st_dev 'd' 124#define SHOW_st_ino 'i' 125#define SHOW_st_mode 'p' 126#define SHOW_st_nlink 'l' --- 8 unchanged lines hidden (view full) --- 135#define SHOW_st_blksize 'k' 136#define SHOW_st_flags 'f' 137#define SHOW_st_gen 'v' 138#define SHOW_symlink 'Y' 139#define SHOW_filetype 'T' 140#define SHOW_filename 'N' 141#define SHOW_sizerdev 'Z' 142 |
143void usage(const char *); |
144void output(const struct stat *, const char *, |
145 const char *, int, int, int); |
146int format1(const struct stat *, /* stat info */ 147 const char *, /* the file name */ 148 const char *, int, /* the format string itself */ 149 char *, size_t, /* a place to put the output */ 150 int, int, int, int, /* the parsed format */ 151 int, int); 152 153char *timefmt; |
154int linkfail; |
155 |
156#define addchar(s, c, nl) \ |
157 do { \ |
158 (void)fputc((c), (s)); \ 159 (*nl) = ((c) == '\n'); \ |
160 } while (0/*CONSTCOND*/) 161 162int 163main(int argc, char *argv[]) 164{ 165 struct stat st; |
166 int ch, rc, errs, am_readlink; 167 int lsF, fmtchar, usestat, fn, nonl, quiet; 168 char *statfmt, *options, *synopsis; |
169 |
170 am_readlink = 0; |
171 lsF = 0; 172 fmtchar = '\0'; 173 usestat = 0; 174 nonl = 0; |
175 quiet = 0; 176 linkfail = 0; |
177 statfmt = NULL; 178 timefmt = NULL; 179 |
180 if (strcmp(getprogname(), "readlink") == 0) { 181 am_readlink = 1; 182 options = "n"; 183 synopsis = "[-n] [file ...]"; 184 statfmt = "%Y"; 185 fmtchar = 'f'; 186 quiet = 1; 187 } else { 188 options = "f:FlLnqrst:x"; 189 synopsis = "[-FlLnqrsx] [-f format] [-t timefmt] [file ...]"; 190 } 191 192 while ((ch = getopt(argc, argv, options)) != -1) |
193 switch (ch) { 194 case 'F': 195 lsF = 1; 196 break; 197 case 'L': 198 usestat = 1; 199 break; 200 case 'n': 201 nonl = 1; 202 break; |
203 case 'q': 204 quiet = 1; 205 break; |
206 case 'f': 207 statfmt = optarg; 208 /* FALLTHROUGH */ 209 case 'l': 210 case 'r': 211 case 's': 212 case 'x': 213 if (fmtchar != 0) 214 errx(1, "can't use format '%c' with '%c'", 215 fmtchar, ch); 216 fmtchar = ch; 217 break; 218 case 't': 219 timefmt = optarg; 220 break; 221 default: |
222 usage(synopsis); |
223 } 224 225 argc -= optind; 226 argv += optind; 227 fn = 1; 228 229 if (fmtchar == '\0') { 230 if (lsF) --- 21 unchanged lines hidden (view full) --- 252 statfmt = SHELL_FORMAT; 253 break; 254 case 'x': 255 statfmt = LINUX_FORMAT; 256 if (timefmt == NULL) 257 timefmt = "%c"; 258 break; 259 default: |
260 usage(synopsis); |
261 /*NOTREACHED*/ 262 } 263 264 if (timefmt == NULL) 265 timefmt = TIME_FORMAT; 266 267 errs = 0; 268 do { 269 if (argc == 0) 270 rc = fstat(STDIN_FILENO, &st); 271 else if (usestat) 272 rc = stat(argv[0], &st); 273 else 274 rc = lstat(argv[0], &st); 275 276 if (rc == -1) { 277 errs = 1; |
278 linkfail = 1; 279 if (!quiet) 280 warn("%s: stat", 281 argc == 0 ? "(stdin)" : argv[0]); |
282 } 283 else |
284 output(&st, argv[0], statfmt, fn, nonl, quiet); |
285 286 argv++; 287 argc--; 288 fn++; 289 } while (argc > 0); 290 |
291 return (am_readlink ? linkfail : errs); |
292} 293 294void |
295usage(const char *synopsis) |
296{ 297 |
298 (void)fprintf(stderr, "usage: %s %s\n", getprogname(), synopsis); |
299 exit(1); 300} 301 302/* 303 * Parses a format string. 304 */ 305void 306output(const struct stat *st, const char *file, |
307 const char *statfmt, int fn, int nonl, int quiet) |
308{ 309 int flags, size, prec, ofmt, hilo, what; |
310 char buf[PATH_MAX]; |
311 const char *subfmt; 312 int nl, t, i; |
313 |
314 nl = 1; |
315 while (*statfmt != '\0') { 316 317 /* 318 * Non-format characters go straight out. 319 */ 320 if (*statfmt != FMT_MAGIC) { |
321 addchar(stdout, *statfmt, &nl); |
322 statfmt++; 323 continue; 324 } 325 326 /* 327 * The current format "substring" starts here, 328 * and then we skip the magic. 329 */ 330 subfmt = statfmt; 331 statfmt++; 332 333 /* 334 * Some simple one-character "formats". 335 */ 336 switch (*statfmt) { 337 case SIMPLE_NEWLINE: |
338 addchar(stdout, '\n', &nl); |
339 statfmt++; 340 continue; 341 case SIMPLE_TAB: |
342 addchar(stdout, '\t', &nl); |
343 statfmt++; 344 continue; 345 case SIMPLE_PERCENT: |
346 addchar(stdout, '%', &nl); |
347 statfmt++; 348 continue; 349 case SIMPLE_NUMBER: { 350 char num[12], *p; 351 352 snprintf(num, sizeof(num), "%d", fn); 353 for (p = &num[0]; *p; p++) |
354 addchar(stdout, *p, &nl); |
355 statfmt++; 356 continue; 357 } 358 } 359 360 /* 361 * This must be an actual format string. Format strings are 362 * similar to printf(3) formats up to a point, and are of --- 52 unchanged lines hidden (view full) --- 415 while (isdigit((unsigned)*statfmt)) { 416 prec = (prec * 10) + (*statfmt - '0'); 417 statfmt++; 418 if (prec < 0) 419 goto badfmt; 420 } 421 } 422 |
423#define fmtcase(x, y) case (y): (x) = (y); statfmt++; break 424#define fmtcasef(x, y, z) case (y): (x) = (z); statfmt++; break |
425 switch (*statfmt) { |
426 fmtcasef(ofmt, FMT_DECIMAL, FMTF_DECIMAL); 427 fmtcasef(ofmt, FMT_OCTAL, FMTF_OCTAL); 428 fmtcasef(ofmt, FMT_UNSIGNED, FMTF_UNSIGNED); 429 fmtcasef(ofmt, FMT_HEX, FMTF_HEX); 430 fmtcasef(ofmt, FMT_FLOAT, FMTF_FLOAT); 431 fmtcasef(ofmt, FMT_STRING, FMTF_STRING); |
432 default: 433 ofmt = 0; 434 break; 435 } 436 437 switch (*statfmt) { 438 fmtcase(hilo, HIGH_PIECE); 439 fmtcase(hilo, MIDDLE_PIECE); --- 21 unchanged lines hidden (view full) --- 461 fmtcase(what, SHOW_st_gen); 462 fmtcase(what, SHOW_symlink); 463 fmtcase(what, SHOW_filetype); 464 fmtcase(what, SHOW_filename); 465 fmtcase(what, SHOW_sizerdev); 466 default: 467 goto badfmt; 468 } |
469#undef fmtcasef |
470#undef fmtcase 471 472 t = format1(st, 473 file, 474 subfmt, statfmt - subfmt, |
475 buf, sizeof(buf), |
476 flags, size, prec, ofmt, hilo, what); 477 |
478 for (i = 0; i < t && i < sizeof(buf); i++) 479 addchar(stdout, buf[i], &nl); |
480 481 continue; 482 483 badfmt: 484 errx(1, "%.*s: bad format", 485 (int)(statfmt - subfmt + 1), subfmt); 486 } 487 |
488 if (!nl && !nonl) |
489 (void)fputc('\n', stdout); 490 (void)fflush(stdout); |
491} 492 493/* 494 * Arranges output according to a single parsed format substring. 495 */ 496int 497format1(const struct stat *st, 498 const char *file, 499 const char *fmt, int flen, 500 char *buf, size_t blen, 501 int flags, int size, int prec, int ofmt, 502 int hilo, int what) 503{ 504 u_int64_t data; 505 char *sdata, lfmt[24], tmp[20]; |
506 char smode[12], sid[12], path[PATH_MAX + 4]; |
507 struct passwd *pw; 508 struct group *gr; 509 const struct timespec *tsp; 510 struct timespec ts; 511 struct tm *tm; 512 int l, small, formats; 513 514 tsp = NULL; --- 20 unchanged lines hidden (view full) --- 535 if (hilo == HIGH_PIECE) { 536 data = major(data); 537 hilo = 0; 538 } 539 else if (hilo == LOW_PIECE) { 540 data = minor((unsigned)data); 541 hilo = 0; 542 } |
543 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX | 544 FMTF_STRING; |
545 if (ofmt == 0) |
546 ofmt = FMTF_UNSIGNED; |
547 break; 548 case SHOW_st_ino: 549 small = (sizeof(st->st_ino) == 4); 550 data = st->st_ino; 551 sdata = NULL; |
552 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX; |
553 if (ofmt == 0) |
554 ofmt = FMTF_UNSIGNED; |
555 break; 556 case SHOW_st_mode: 557 small = (sizeof(st->st_mode) == 4); 558 data = st->st_mode; 559 strmode(st->st_mode, smode); 560 sdata = smode; 561 l = strlen(sdata); 562 if (sdata[l - 1] == ' ') --- 11 unchanged lines hidden (view full) --- 574 hilo = 0; 575 } 576 else if (hilo == LOW_PIECE) { 577 data &= 0777; 578 sdata += 7; 579 sdata[3] = '\0'; 580 hilo = 0; 581 } |
582 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX | 583 FMTF_STRING; |
584 if (ofmt == 0) |
585 ofmt = FMTF_OCTAL; |
586 break; 587 case SHOW_st_nlink: 588 small = (sizeof(st->st_dev) == 4); 589 data = st->st_nlink; 590 sdata = NULL; |
591 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX; |
592 if (ofmt == 0) |
593 ofmt = FMTF_UNSIGNED; |
594 break; 595 case SHOW_st_uid: 596 small = (sizeof(st->st_uid) == 4); 597 data = st->st_uid; 598 if ((pw = getpwuid(st->st_uid)) != NULL) 599 sdata = pw->pw_name; 600 else { 601 snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_uid); 602 sdata = sid; 603 } |
604 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX | 605 FMTF_STRING; |
606 if (ofmt == 0) |
607 ofmt = FMTF_UNSIGNED; |
608 break; 609 case SHOW_st_gid: 610 small = (sizeof(st->st_gid) == 4); 611 data = st->st_gid; 612 if ((gr = getgrgid(st->st_gid)) != NULL) 613 sdata = gr->gr_name; 614 else { 615 snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_gid); 616 sdata = sid; 617 } |
618 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX | 619 FMTF_STRING; |
620 if (ofmt == 0) |
621 ofmt = FMTF_UNSIGNED; |
622 break; 623 case SHOW_st_atime: 624 tsp = &st->st_atimespec; 625 /* FALLTHROUGH */ 626 case SHOW_st_mtime: 627 if (tsp == NULL) 628 tsp = &st->st_mtimespec; 629 /* FALLTHROUGH */ 630 case SHOW_st_ctime: 631 if (tsp == NULL) 632 tsp = &st->st_ctimespec; 633 ts = *tsp; /* copy so we can muck with it */ 634 small = (sizeof(ts.tv_sec) == 4); 635 data = ts.tv_sec; 636 small = 1; 637 tm = localtime(&ts.tv_sec); 638 (void)strftime(path, sizeof(path), timefmt, tm); 639 sdata = path; |
640 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX | 641 FMTF_FLOAT | FMTF_STRING; |
642 if (ofmt == 0) |
643 ofmt = FMTF_DECIMAL; |
644 break; 645 case SHOW_st_size: 646 small = (sizeof(st->st_size) == 4); 647 data = st->st_size; 648 sdata = NULL; |
649 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX; |
650 if (ofmt == 0) |
651 ofmt = FMTF_UNSIGNED; |
652 break; 653 case SHOW_st_blocks: 654 small = (sizeof(st->st_blocks) == 4); 655 data = st->st_blocks; 656 sdata = NULL; |
657 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX; |
658 if (ofmt == 0) |
659 ofmt = FMTF_UNSIGNED; |
660 break; 661 case SHOW_st_blksize: 662 small = (sizeof(st->st_blksize) == 4); 663 data = st->st_blksize; 664 sdata = NULL; |
665 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX; |
666 if (ofmt == 0) |
667 ofmt = FMTF_UNSIGNED; |
668 break; 669 case SHOW_st_flags: 670 small = (sizeof(st->st_flags) == 4); 671 data = st->st_flags; 672 sdata = NULL; |
673 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX; |
674 if (ofmt == 0) |
675 ofmt = FMTF_UNSIGNED; |
676 break; 677 case SHOW_st_gen: 678 small = (sizeof(st->st_gen) == 4); 679 data = st->st_gen; 680 sdata = NULL; |
681 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX; |
682 if (ofmt == 0) |
683 ofmt = FMTF_UNSIGNED; |
684 break; 685 case SHOW_symlink: 686 small = 0; 687 data = 0; 688 if (S_ISLNK(st->st_mode)) { 689 snprintf(path, sizeof(path), " -> "); 690 l = readlink(file, path + 4, sizeof(path) - 4); 691 if (l == -1) { |
692 linkfail = 1; |
693 l = 0; 694 path[0] = '\0'; 695 } 696 path[l + 4] = '\0'; |
697 sdata = path + (ofmt == FMTF_STRING ? 0 : 4); |
698 } |
699 else { 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) { --- 19 unchanged lines hidden (view full) --- 733 case S_IFREG: sdata = "Regular File"; break; 734 case S_IFLNK: sdata = "Symbolic Link"; break; 735 case S_IFSOCK: sdata = "Socket"; break; 736 case S_IFWHT: sdata = "Whiteout File"; break; 737 default: sdata = "???"; break; 738 } 739 hilo = 0; 740 } |
741 formats = FMTF_STRING; |
742 if (ofmt == 0) |
743 ofmt = FMTF_STRING; |
744 break; 745 case SHOW_filename: 746 small = 0; 747 data = 0; 748 if (file == NULL) 749 (void)strncpy(path, "(stdin)", sizeof(path)); 750 else 751 (void)strncpy(path, file, sizeof(path)); 752 sdata = path; |
753 formats = FMTF_STRING; |
754 if (ofmt == 0) |
755 ofmt = FMTF_STRING; |
756 break; 757 case SHOW_sizerdev: 758 if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) { 759 char majdev[20], mindev[20]; 760 int l1, l2; 761 762 l1 = format1(st, 763 file, --- 45 unchanged lines hidden (view full) --- 809 (void)strcat(lfmt, "-"); 810 if (flags & FLAG_ZERO) 811 (void)strcat(lfmt, "0"); 812 813 /* 814 * Only the timespecs support the FLOAT output format, and that 815 * requires work that differs from the other formats. 816 */ |
817 if (ofmt == FMTF_FLOAT) { |
818 /* 819 * Nothing after the decimal point, so just print seconds. 820 */ 821 if (prec == 0) { 822 if (size != -1) { 823 (void)snprintf(tmp, sizeof(tmp), "%d", size); 824 (void)strcat(lfmt, tmp); 825 } --- 63 unchanged lines hidden (view full) --- 889 if (prec != -1) { 890 (void)snprintf(tmp, sizeof(tmp), ".%d", prec); 891 (void)strcat(lfmt, tmp); 892 } 893 894 /* 895 * String output uses the temporary sdata. 896 */ |
897 if (ofmt == FMTF_STRING) { |
898 if (sdata == NULL) 899 errx(1, "%.*s: bad format", (int)flen, fmt); 900 (void)strcat(lfmt, "s"); 901 return (snprintf(buf, blen, lfmt, sdata)); 902 } 903 904 /* 905 * Ensure that sign extension does not cause bad looking output 906 * for some forms. 907 */ |
908 if (small && ofmt != FMTF_DECIMAL) |
909 data = (u_int32_t)data; 910 911 /* 912 * The four "numeric" output forms. 913 */ 914 (void)strcat(lfmt, "ll"); 915 switch (ofmt) { |
916 case FMTF_DECIMAL: (void)strcat(lfmt, "d"); break; 917 case FMTF_OCTAL: (void)strcat(lfmt, "o"); break; 918 case FMTF_UNSIGNED: (void)strcat(lfmt, "u"); break; 919 case FMTF_HEX: (void)strcat(lfmt, "x"); break; |
920 } 921 922 return (snprintf(buf, blen, lfmt, data)); 923} |