1 /* $NetBSD: stat.c,v 1.48 2022/06/22 18:20:30 kre Exp $ */ 2 3 /* 4 * Copyright (c) 2002-2011 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 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #if HAVE_NBTOOL_CONFIG_H 33 #include "nbtool_config.h" 34 /* config checked libc, we need the prototype as well */ 35 #undef HAVE_DEVNAME 36 #endif 37 38 #include <sys/cdefs.h> 39 #if !defined(lint) 40 __RCSID("$NetBSD: stat.c,v 1.48 2022/06/22 18:20:30 kre Exp $"); 41 #endif 42 43 #if ! HAVE_NBTOOL_CONFIG_H 44 #define HAVE_STRUCT_STAT_ST_FLAGS 1 45 #define HAVE_STRUCT_STAT_ST_GEN 1 46 #define HAVE_STRUCT_STAT_ST_BIRTHTIME 1 47 #define HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC 1 48 #define HAVE_STRUCT_STAT_ST_MTIMENSEC 1 49 #define HAVE_DEVNAME 1 50 #endif /* HAVE_NBTOOL_CONFIG_H */ 51 52 #include <sys/types.h> 53 #include <sys/stat.h> 54 55 #include <ctype.h> 56 #include <err.h> 57 #include <errno.h> 58 #include <grp.h> 59 #include <limits.h> 60 #include <pwd.h> 61 #include <stdio.h> 62 #include <stdlib.h> 63 #include <string.h> 64 #include <time.h> 65 #include <unistd.h> 66 #include <vis.h> 67 68 #if HAVE_STRUCT_STAT_ST_FLAGS 69 #define DEF_F "%#Xf " 70 #define RAW_F "%f " 71 #define SHELL_F " st_flags=%f" 72 #else /* HAVE_STRUCT_STAT_ST_FLAGS */ 73 #define DEF_F 74 #define RAW_F 75 #define SHELL_F 76 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */ 77 78 #if HAVE_STRUCT_STAT_ST_BIRTHTIME 79 #define DEF_B "\"%SB\" " 80 #define RAW_B "%B " 81 #define SHELL_B "st_birthtime=%SB " 82 #define LINUX_B "%n Birth: %SB" 83 #else /* HAVE_STRUCT_STAT_ST_BIRTHTIME */ 84 #define DEF_B 85 #define RAW_B 86 #define SHELL_B 87 #define LINUX_B 88 #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIME */ 89 90 #if HAVE_STRUCT_STAT_ST_ATIM 91 #define st_atimespec st_atim 92 #define st_ctimespec st_ctim 93 #define st_mtimespec st_mtim 94 #endif /* HAVE_STRUCT_STAT_ST_ATIM */ 95 96 #define DEF_FORMAT \ 97 "%d %i %Sp %l %Su %Sg %r %z \"%Sa\" \"%Sm\" \"%Sc\" " DEF_B \ 98 "%k %b " DEF_F "%N" 99 #define RAW_FORMAT "%d %i %#p %l %u %g %r %z %a %m %c " RAW_B \ 100 "%k %b " RAW_F "%N" 101 #define LS_FORMAT "%Sp %l %Su %Sg %Z %Sm %N%SY" 102 #define LSF_FORMAT "%Sp %l %Su %Sg %Z %Sm %N%T%SY" 103 #define SHELL_FORMAT \ 104 "st_dev=%d st_ino=%i st_mode=%#p st_nlink=%l " \ 105 "st_uid=%u st_gid=%g st_rdev=%r st_size=%z " \ 106 "st_atime=%Sa st_mtime=%Sm st_ctime=%Sc " SHELL_B \ 107 "st_blksize=%k st_blocks=%b" SHELL_F 108 #define LINUX_FORMAT \ 109 " File: \"%N\"%n" \ 110 " Size: %-11z Blocks: %-11b IO Block: %-11k %HT%n" \ 111 "Device: %Hd,%Ld Inode: %i Links: %l%n" \ 112 " Mode: (%Mp%03OLp/%.10Sp) Uid: (%5u/%8Su) Gid: (%5g/%8Sg)%n" \ 113 "Access: %Sa%n" \ 114 "Modify: %Sm%n" \ 115 "Change: %Sc" \ 116 LINUX_B 117 118 #define TIME_FORMAT "%b %e %T %Y" 119 120 #define FLAG_POUND 0x01 121 #define FLAG_SPACE 0x02 122 #define FLAG_PLUS 0x04 123 #define FLAG_ZERO 0x08 124 #define FLAG_MINUS 0x10 125 126 /* 127 * These format characters must all be unique, except the magic one. 128 */ 129 #define FMT_MAGIC '%' 130 #define FMT_DOT '.' 131 132 #define SIMPLE_NEWLINE 'n' 133 #define SIMPLE_TAB 't' 134 #define SIMPLE_PERCENT '%' 135 #define SIMPLE_NUMBER '@' 136 137 #define FMT_POUND '#' 138 #define FMT_SPACE ' ' 139 #define FMT_PLUS '+' 140 #define FMT_ZERO '0' 141 #define FMT_MINUS '-' 142 143 #define FMT_DECIMAL 'D' 144 #define FMT_OCTAL 'O' 145 #define FMT_UNSIGNED 'U' 146 #define FMT_HEX 'X' 147 #define FMT_FLOAT 'F' 148 #define FMT_STRING 'S' 149 150 #define FMTF_DECIMAL 0x01 151 #define FMTF_OCTAL 0x02 152 #define FMTF_UNSIGNED 0x04 153 #define FMTF_HEX 0x08 154 #define FMTF_FLOAT 0x10 155 #define FMTF_STRING 0x20 156 157 #define HIGH_PIECE 'H' 158 #define MIDDLE_PIECE 'M' 159 #define LOW_PIECE 'L' 160 161 #define SHOW_realpath 'R' 162 #define SHOW_st_dev 'd' 163 #define SHOW_st_ino 'i' 164 #define SHOW_st_mode 'p' 165 #define SHOW_st_nlink 'l' 166 #define SHOW_st_uid 'u' 167 #define SHOW_st_gid 'g' 168 #define SHOW_st_rdev 'r' 169 #define SHOW_st_atime 'a' 170 #define SHOW_st_mtime 'm' 171 #define SHOW_st_ctime 'c' 172 #define SHOW_st_btime 'B' 173 #define SHOW_st_size 'z' 174 #define SHOW_st_blocks 'b' 175 #define SHOW_st_blksize 'k' 176 #define SHOW_st_flags 'f' 177 #define SHOW_st_gen 'v' 178 #define SHOW_symlink 'Y' 179 #define SHOW_filetype 'T' 180 #define SHOW_filename 'N' 181 #define SHOW_sizerdev 'Z' 182 183 static void usage(const char *) __dead; 184 static void output(const struct stat *, const char *, 185 const char *, int, int, int); 186 static int format1(const struct stat *, /* stat info */ 187 const char *, /* the file name */ 188 const char *, int, /* the format string itself */ 189 char *, size_t, /* a place to put the output */ 190 int, int, int, int, /* the parsed format */ 191 int, int, int); 192 193 static const char *timefmt; 194 static int linkfail; 195 196 #define addchar(s, c, nl) \ 197 do { \ 198 (void)fputc((c), (s)); \ 199 (*nl) = ((c) == '\n'); \ 200 } while (0/*CONSTCOND*/) 201 202 int 203 main(int argc, char *argv[]) 204 { 205 struct stat st; 206 int ch, rc, errs, am_readlink; 207 int lsF, fmtchar, usestat, fn, nonl, quiet; 208 const char *statfmt, *options, *synopsis; 209 210 am_readlink = 0; 211 lsF = 0; 212 fmtchar = '\0'; 213 usestat = 0; 214 nonl = 0; 215 quiet = 0; 216 linkfail = 0; 217 statfmt = NULL; 218 timefmt = NULL; 219 220 setprogname(argv[0]); 221 222 if (strcmp(getprogname(), "readlink") == 0) { 223 am_readlink = 1; 224 options = "fnqsv"; 225 synopsis = "[-fnqsv] [file ...]"; 226 statfmt = "%Y"; 227 fmtchar = 'f'; 228 quiet = 1; 229 } else { 230 options = "f:FlLnqrst:x"; 231 synopsis = "[-FlLnqrsx] [-f format] [-t timefmt] [file ...]"; 232 } 233 234 while ((ch = getopt(argc, argv, options)) != -1) 235 switch (ch) { 236 case 'F': 237 lsF = 1; 238 break; 239 case 'L': 240 usestat = 1; 241 break; 242 case 'n': 243 nonl = 1; 244 break; 245 case 'q': 246 quiet = 1; 247 break; 248 case 'f': 249 if (am_readlink) { 250 statfmt = "%R"; 251 break; 252 } 253 statfmt = optarg; 254 /* FALLTHROUGH */ 255 case 'l': 256 case 'r': 257 case 's': 258 if (am_readlink) { 259 quiet = 1; 260 break; 261 } 262 /*FALLTHROUGH*/ 263 case 'x': 264 if (fmtchar != 0) 265 errx(1, "can't use format '%c' with '%c'", 266 fmtchar, ch); 267 fmtchar = ch; 268 break; 269 case 't': 270 timefmt = optarg; 271 break; 272 case 'v': 273 quiet = 0; 274 break; 275 default: 276 usage(synopsis); 277 } 278 279 argc -= optind; 280 argv += optind; 281 fn = 1; 282 283 if (fmtchar == '\0') { 284 if (lsF) 285 fmtchar = 'l'; 286 else { 287 fmtchar = 'f'; 288 statfmt = DEF_FORMAT; 289 } 290 } 291 292 if (lsF && fmtchar != 'l') 293 errx(1, "can't use format '%c' with -F", fmtchar); 294 295 switch (fmtchar) { 296 case 'f': 297 /* statfmt already set */ 298 break; 299 case 'l': 300 statfmt = lsF ? LSF_FORMAT : LS_FORMAT; 301 break; 302 case 'r': 303 statfmt = RAW_FORMAT; 304 break; 305 case 's': 306 statfmt = SHELL_FORMAT; 307 if (timefmt == NULL) 308 timefmt = "%s"; 309 break; 310 case 'x': 311 statfmt = LINUX_FORMAT; 312 if (timefmt == NULL) 313 timefmt = "%Y-%m-%d %H:%M:%S.%f %z"; 314 break; 315 default: 316 usage(synopsis); 317 /*NOTREACHED*/ 318 } 319 320 if (timefmt == NULL) 321 timefmt = TIME_FORMAT; 322 323 errs = 0; 324 do { 325 if (argc == 0) { 326 fn = 0; 327 rc = fstat(STDIN_FILENO, &st); 328 } else if (usestat) { 329 /* 330 * Try stat() and if it fails, fall back to 331 * lstat() just in case we're examining a 332 * broken symlink. 333 */ 334 if ((rc = stat(argv[0], &st)) == -1 && 335 errno == ENOENT && 336 (rc = lstat(argv[0], &st)) == -1) 337 errno = ENOENT; 338 } else 339 rc = lstat(argv[0], &st); 340 341 if (rc == -1) { 342 errs = 1; 343 linkfail = 1; 344 if (!quiet) 345 warn("%s: %s", 346 argc == 0 ? "(stdin)" : argv[0], 347 usestat ? "stat" : "lstat"); 348 } else 349 output(&st, argv[0], statfmt, fn, nonl, quiet); 350 351 argv++; 352 argc--; 353 fn++; 354 } while (argc > 0); 355 356 return (am_readlink ? linkfail : errs); 357 } 358 359 static void 360 usage(const char *synopsis) 361 { 362 363 (void)fprintf(stderr, "usage: %s %s\n", getprogname(), synopsis); 364 exit(1); 365 } 366 367 /* 368 * Parses a format string. 369 */ 370 static void 371 output(const struct stat *st, const char *file, 372 const char *statfmt, int fn, int nonl, int quiet) 373 { 374 int flags, size, prec, ofmt, hilo, what; 375 /* 376 * buf size is enough for an item of length PATH_MAX, 377 * multiplied by 4 for vis encoding, plus 4 for symlink 378 * " -> " prefix, plus 1 for \0 terminator. 379 */ 380 char buf[PATH_MAX * 4 + 4 + 1]; 381 const char *subfmt; 382 int nl, t, i; 383 384 nl = 1; 385 while (*statfmt != '\0') { 386 387 /* 388 * Non-format characters go straight out. 389 */ 390 if (*statfmt != FMT_MAGIC) { 391 addchar(stdout, *statfmt, &nl); 392 statfmt++; 393 continue; 394 } 395 396 /* 397 * The current format "substring" starts here, 398 * and then we skip the magic. 399 */ 400 subfmt = statfmt; 401 statfmt++; 402 403 /* 404 * Some simple one-character "formats". 405 */ 406 switch (*statfmt) { 407 case SIMPLE_NEWLINE: 408 addchar(stdout, '\n', &nl); 409 statfmt++; 410 continue; 411 case SIMPLE_TAB: 412 addchar(stdout, '\t', &nl); 413 statfmt++; 414 continue; 415 case SIMPLE_PERCENT: 416 addchar(stdout, '%', &nl); 417 statfmt++; 418 continue; 419 case SIMPLE_NUMBER: { 420 char num[12], *p; 421 422 snprintf(num, sizeof(num), "%d", fn); 423 for (p = &num[0]; *p; p++) 424 addchar(stdout, *p, &nl); 425 statfmt++; 426 continue; 427 } 428 } 429 430 /* 431 * This must be an actual format string. Format strings are 432 * similar to printf(3) formats up to a point, and are of 433 * the form: 434 * 435 * % required start of format 436 * [-# +0] opt. format characters 437 * size opt. field width 438 * . opt. decimal separator, followed by 439 * prec opt. precision 440 * fmt opt. output specifier (string, numeric, etc.) 441 * sub opt. sub field specifier (high, middle, low) 442 * datum required field specifier (size, mode, etc) 443 * 444 * Only the % and the datum selector are required. All data 445 * have reasonable default output forms. The "sub" specifier 446 * only applies to certain data (mode, dev, rdev, filetype). 447 * The symlink output defaults to STRING, yet will only emit 448 * the leading " -> " if STRING is explicitly specified. The 449 * sizerdev datum will generate rdev output for character or 450 * block devices, and size output for all others. 451 * For STRING output, the # format requests vis encoding. 452 */ 453 flags = 0; 454 do { 455 if (*statfmt == FMT_POUND) 456 flags |= FLAG_POUND; 457 else if (*statfmt == FMT_SPACE) 458 flags |= FLAG_SPACE; 459 else if (*statfmt == FMT_PLUS) 460 flags |= FLAG_PLUS; 461 else if (*statfmt == FMT_ZERO) 462 flags |= FLAG_ZERO; 463 else if (*statfmt == FMT_MINUS) 464 flags |= FLAG_MINUS; 465 else 466 break; 467 statfmt++; 468 } while (1/*CONSTCOND*/); 469 470 size = -1; 471 if (isdigit((unsigned char)*statfmt)) { 472 size = 0; 473 while (isdigit((unsigned char)*statfmt)) { 474 size = (size * 10) + (*statfmt - '0'); 475 statfmt++; 476 if (size < 0) 477 goto badfmt; 478 } 479 } 480 481 prec = -1; 482 if (*statfmt == FMT_DOT) { 483 statfmt++; 484 485 prec = 0; 486 while (isdigit((unsigned char)*statfmt)) { 487 prec = (prec * 10) + (*statfmt - '0'); 488 statfmt++; 489 if (prec < 0) 490 goto badfmt; 491 } 492 } 493 494 #define fmtcase(x, y) case (y): (x) = (y); statfmt++; break 495 #define fmtcasef(x, y, z) case (y): (x) = (z); statfmt++; break 496 switch (*statfmt) { 497 fmtcasef(ofmt, FMT_DECIMAL, FMTF_DECIMAL); 498 fmtcasef(ofmt, FMT_OCTAL, FMTF_OCTAL); 499 fmtcasef(ofmt, FMT_UNSIGNED, FMTF_UNSIGNED); 500 fmtcasef(ofmt, FMT_HEX, FMTF_HEX); 501 fmtcasef(ofmt, FMT_FLOAT, FMTF_FLOAT); 502 fmtcasef(ofmt, FMT_STRING, FMTF_STRING); 503 default: 504 ofmt = 0; 505 break; 506 } 507 508 switch (*statfmt) { 509 fmtcase(hilo, HIGH_PIECE); 510 fmtcase(hilo, MIDDLE_PIECE); 511 fmtcase(hilo, LOW_PIECE); 512 default: 513 hilo = 0; 514 break; 515 } 516 517 switch (*statfmt) { 518 fmtcase(what, SHOW_realpath); 519 fmtcase(what, SHOW_st_dev); 520 fmtcase(what, SHOW_st_ino); 521 fmtcase(what, SHOW_st_mode); 522 fmtcase(what, SHOW_st_nlink); 523 fmtcase(what, SHOW_st_uid); 524 fmtcase(what, SHOW_st_gid); 525 fmtcase(what, SHOW_st_rdev); 526 fmtcase(what, SHOW_st_atime); 527 fmtcase(what, SHOW_st_mtime); 528 fmtcase(what, SHOW_st_ctime); 529 fmtcase(what, SHOW_st_btime); 530 fmtcase(what, SHOW_st_size); 531 fmtcase(what, SHOW_st_blocks); 532 fmtcase(what, SHOW_st_blksize); 533 fmtcase(what, SHOW_st_flags); 534 fmtcase(what, SHOW_st_gen); 535 fmtcase(what, SHOW_symlink); 536 fmtcase(what, SHOW_filetype); 537 fmtcase(what, SHOW_filename); 538 fmtcase(what, SHOW_sizerdev); 539 default: 540 goto badfmt; 541 } 542 #undef fmtcasef 543 #undef fmtcase 544 545 t = format1(st, 546 file, 547 subfmt, statfmt - subfmt, 548 buf, sizeof(buf), 549 flags, size, prec, ofmt, hilo, what, quiet); 550 551 for (i = 0; i < t && i < (int)(sizeof(buf) - 1); i++) 552 addchar(stdout, buf[i], &nl); 553 554 continue; 555 556 badfmt: 557 errx(1, "%.*s: bad format", 558 (int)(statfmt - subfmt + 1), subfmt); 559 } 560 561 if (!nl && !nonl) 562 (void)fputc('\n', stdout); 563 (void)fflush(stdout); 564 } 565 566 static const char * 567 fmttime(char *buf, size_t len, const char *fmt, time_t secs, long nsecs) 568 { 569 struct tm tm; 570 const char *fpb, *fp1, *fp2; /* pointers into fmt */ 571 char *fmt2 = NULL; /* replacement fmt (if not NULL) */ 572 /* XXX init of next twp for stupid gcc only */ 573 char *f2p = NULL; /* ptr into fmt2 - last added */ 574 size_t flen = 0; 575 size_t o; 576 int sl; 577 578 if (localtime_r(&secs, &tm) == NULL) { 579 secs = 0; 580 (void)localtime_r(&secs, &tm); 581 } 582 for (fp1 = fpb = fmt; (fp2 = strchr(fp1, '%')) != NULL; ) { 583 if (fp2[1] != 'f') { 584 /* make sure we don't find the 2nd '%' in "%%" */ 585 fp1 = fp2 + 1 + (fp2[1] != '\0'); 586 continue; 587 } 588 if (fmt2 == NULL) { 589 /* allow for ~100 %f's in the format ... */ 590 flen = strlen(fmt) + 1024; 591 592 if ((fmt2 = calloc(flen, 1)) == NULL) { 593 fp1 = fp2 + 2; 594 continue; 595 } 596 f2p = fmt2; 597 598 o = (size_t)(fp2 - fpb); 599 memcpy(f2p, fpb, o); /* must fit */ 600 fmt = fmt2; 601 } else { 602 o = (size_t)(fp2 - fpb); 603 if (flen > o) 604 memcpy(f2p, fpb, o); 605 } 606 if (flen < o + 10) { /* 9 digits + \0 == 10 */ 607 *f2p = '\0'; 608 break; 609 } 610 f2p += o; 611 flen -= o; 612 sl = snprintf(f2p, flen, "%.9ld", nsecs); 613 if (sl == -1) 614 sl = 0; 615 f2p += sl; 616 *f2p = '\0'; 617 flen -= sl; 618 fp1 = fp2 + 2; 619 fpb = fp1; 620 } 621 if (fmt2 != NULL) { 622 o = strlen(fpb); 623 if (flen > o) { 624 memcpy(f2p, fpb, o); 625 f2p[o] = '\0'; 626 } 627 } 628 629 (void)strftime(buf, len, fmt, &tm); 630 free(fmt2); 631 return buf; 632 } 633 634 /* 635 * Arranges output according to a single parsed format substring. 636 */ 637 static int 638 format1(const struct stat *st, 639 const char *file, 640 const char *fmt, int flen, 641 char *buf, size_t blen, 642 int flags, int size, int prec, int ofmt, 643 int hilo, int what, int quiet) 644 { 645 uint64_t data; 646 char *stmp, lfmt[24], tmp[20]; 647 const char *sdata; 648 char smode[12], sid[13], path[PATH_MAX + 4], visbuf[PATH_MAX * 4 + 4]; 649 struct passwd *pw; 650 struct group *gr; 651 time_t secs; 652 long nsecs; 653 int l; 654 int formats; /* bitmap of allowed formats for this datum */ 655 int small; /* true if datum is a small integer */ 656 int gottime; /* true if secs and nsecs are valid */ 657 int shift; /* powers of 2 to scale numbers before printing */ 658 size_t prefixlen; /* length of constant prefix for string data */ 659 660 formats = 0; 661 small = 0; 662 gottime = 0; 663 secs = 0; 664 nsecs = 0; 665 shift = 0; 666 prefixlen = 0; 667 668 /* 669 * First, pick out the data and tweak it based on hilo or 670 * specified output format (symlink output only). 671 */ 672 switch (what) { 673 case SHOW_st_dev: 674 case SHOW_st_rdev: 675 small = (sizeof(st->st_dev) == 4); 676 data = (what == SHOW_st_dev) ? st->st_dev : st->st_rdev; 677 #if HAVE_DEVNAME 678 sdata = (what == SHOW_st_dev) ? 679 devname(st->st_dev, S_IFBLK) : 680 devname(st->st_rdev, 681 S_ISCHR(st->st_mode) ? S_IFCHR : 682 S_ISBLK(st->st_mode) ? S_IFBLK : 683 0U); 684 if (sdata == NULL) 685 sdata = "???"; 686 #endif /* HAVE_DEVNAME */ 687 if (hilo == HIGH_PIECE) { 688 data = major(data); 689 hilo = 0; 690 } 691 else if (hilo == LOW_PIECE) { 692 data = minor((unsigned)data); 693 hilo = 0; 694 } 695 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX | 696 #if HAVE_DEVNAME 697 FMTF_STRING; 698 #else /* HAVE_DEVNAME */ 699 0; 700 #endif /* HAVE_DEVNAME */ 701 if (ofmt == 0) { 702 if (data == (uint64_t)-1) 703 ofmt = FMTF_DECIMAL; 704 else 705 ofmt = FMTF_UNSIGNED; 706 } 707 break; 708 case SHOW_st_ino: 709 small = (sizeof(st->st_ino) == 4); 710 data = st->st_ino; 711 sdata = NULL; 712 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX; 713 if (ofmt == 0) 714 ofmt = FMTF_UNSIGNED; 715 break; 716 case SHOW_st_mode: 717 small = (sizeof(st->st_mode) == 4); 718 data = st->st_mode; 719 strmode(st->st_mode, smode); 720 stmp = smode; 721 l = strlen(stmp); 722 if (stmp[l - 1] == ' ') 723 stmp[--l] = '\0'; 724 if (hilo == HIGH_PIECE) { 725 data >>= 12; 726 stmp += 1; 727 stmp[3] = '\0'; 728 hilo = 0; 729 } 730 else if (hilo == MIDDLE_PIECE) { 731 data = (data >> 9) & 07; 732 stmp += 4; 733 stmp[3] = '\0'; 734 hilo = 0; 735 } 736 else if (hilo == LOW_PIECE) { 737 data &= 0777; 738 stmp += 7; 739 stmp[3] = '\0'; 740 hilo = 0; 741 } 742 sdata = stmp; 743 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX | 744 FMTF_STRING; 745 if (ofmt == 0) 746 ofmt = FMTF_OCTAL; 747 break; 748 case SHOW_st_nlink: 749 small = (sizeof(st->st_dev) == 4); 750 data = st->st_nlink; 751 sdata = NULL; 752 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX; 753 if (ofmt == 0) 754 ofmt = FMTF_UNSIGNED; 755 break; 756 case SHOW_st_uid: 757 small = (sizeof(st->st_uid) == 4); 758 data = st->st_uid; 759 if ((pw = getpwuid(st->st_uid)) != NULL) 760 sdata = pw->pw_name; 761 else { 762 snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_uid); 763 sdata = sid; 764 } 765 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX | 766 FMTF_STRING; 767 if (ofmt == 0) 768 ofmt = FMTF_UNSIGNED; 769 break; 770 case SHOW_st_gid: 771 small = (sizeof(st->st_gid) == 4); 772 data = st->st_gid; 773 if ((gr = getgrgid(st->st_gid)) != NULL) 774 sdata = gr->gr_name; 775 else { 776 snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_gid); 777 sdata = sid; 778 } 779 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX | 780 FMTF_STRING; 781 if (ofmt == 0) 782 ofmt = FMTF_UNSIGNED; 783 break; 784 case SHOW_st_atime: 785 gottime = 1; 786 secs = st->st_atime; 787 #if HAVE_STRUCT_STAT_ST_MTIMENSEC 788 nsecs = st->st_atimensec; 789 #endif 790 /* FALLTHROUGH */ 791 case SHOW_st_mtime: 792 if (!gottime) { 793 gottime = 1; 794 secs = st->st_mtime; 795 #if HAVE_STRUCT_STAT_ST_MTIMENSEC 796 nsecs = st->st_mtimensec; 797 #endif 798 } 799 /* FALLTHROUGH */ 800 case SHOW_st_ctime: 801 if (!gottime) { 802 gottime = 1; 803 secs = st->st_ctime; 804 #if HAVE_STRUCT_STAT_ST_MTIMENSEC 805 nsecs = st->st_ctimensec; 806 #endif 807 } 808 #if HAVE_STRUCT_STAT_ST_BIRTHTIME 809 /* FALLTHROUGH */ 810 case SHOW_st_btime: 811 if (!gottime) { 812 gottime = 1; 813 secs = st->st_birthtime; 814 #if HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC 815 nsecs = st->st_birthtimensec; 816 #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC */ 817 } 818 #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIME */ 819 small = (sizeof(secs) == 4); 820 data = secs; 821 sdata = fmttime(path, sizeof(path), timefmt, secs, nsecs); 822 823 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX | 824 FMTF_FLOAT | FMTF_STRING; 825 if (ofmt == 0) 826 ofmt = FMTF_DECIMAL; 827 break; 828 case SHOW_st_size: 829 small = (sizeof(st->st_size) == 4); 830 data = st->st_size; 831 sdata = NULL; 832 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX; 833 if (ofmt == 0) 834 ofmt = FMTF_UNSIGNED; 835 switch (hilo) { 836 case HIGH_PIECE: 837 shift = 30; /* gigabytes */ 838 hilo = 0; 839 break; 840 case MIDDLE_PIECE: 841 shift = 20; /* megabytes */ 842 hilo = 0; 843 break; 844 case LOW_PIECE: 845 shift = 10; /* kilobytes */ 846 hilo = 0; 847 break; 848 } 849 break; 850 case SHOW_st_blocks: 851 small = (sizeof(st->st_blocks) == 4); 852 data = st->st_blocks; 853 sdata = NULL; 854 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX; 855 if (ofmt == 0) 856 ofmt = FMTF_UNSIGNED; 857 break; 858 case SHOW_st_blksize: 859 small = (sizeof(st->st_blksize) == 4); 860 data = st->st_blksize; 861 sdata = NULL; 862 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX; 863 if (ofmt == 0) 864 ofmt = FMTF_UNSIGNED; 865 break; 866 #if HAVE_STRUCT_STAT_ST_FLAGS 867 case SHOW_st_flags: 868 small = (sizeof(st->st_flags) == 4); 869 data = st->st_flags; 870 sdata = NULL; 871 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX; 872 if (ofmt == 0) 873 ofmt = FMTF_UNSIGNED; 874 break; 875 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */ 876 #if HAVE_STRUCT_STAT_ST_GEN 877 case SHOW_st_gen: 878 small = (sizeof(st->st_gen) == 4); 879 data = st->st_gen; 880 sdata = NULL; 881 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX; 882 if (ofmt == 0) 883 ofmt = FMTF_UNSIGNED; 884 break; 885 #endif /* HAVE_STRUCT_STAT_ST_GEN */ 886 case SHOW_realpath: 887 small = 0; 888 data = 0; 889 if (file == NULL) { 890 (void)strlcpy(path, "(stdin)", sizeof(path)); 891 sdata = path; 892 } else { 893 snprintf(path, sizeof(path), " -> "); 894 if (realpath(file, path + 4) == NULL) { 895 if (!quiet) 896 warn("realpath `%s'", file); 897 linkfail = 1; 898 l = 0; 899 path[0] = '\0'; 900 } 901 sdata = path + (ofmt == FMTF_STRING ? 0 : 4); 902 prefixlen = (ofmt == FMTF_STRING ? 4 : 0); 903 } 904 905 formats = FMTF_STRING; 906 if (ofmt == 0) 907 ofmt = FMTF_STRING; 908 break; 909 case SHOW_symlink: 910 small = 0; 911 data = 0; 912 if (S_ISLNK(st->st_mode)) { 913 snprintf(path, sizeof(path), " -> "); 914 l = readlink(file, path + 4, sizeof(path) - 4 - 1); 915 if (l == -1) { 916 if (!quiet) 917 warn("readlink `%s'", file); 918 linkfail = 1; 919 l = 0; 920 path[0] = '\0'; 921 } 922 path[l + 4] = '\0'; 923 sdata = path + (ofmt == FMTF_STRING ? 0 : 4); 924 prefixlen = (ofmt == FMTF_STRING ? 4 : 0); 925 } 926 else { 927 linkfail = 1; 928 sdata = ""; 929 } 930 formats = FMTF_STRING; 931 if (ofmt == 0) 932 ofmt = FMTF_STRING; 933 break; 934 case SHOW_filetype: 935 small = 0; 936 data = 0; 937 sdata = ""; 938 if (hilo == 0 || hilo == LOW_PIECE) { 939 switch (st->st_mode & S_IFMT) { 940 case S_IFIFO: sdata = "|"; break; 941 case S_IFDIR: sdata = "/"; break; 942 case S_IFREG: 943 if (st->st_mode & 944 (S_IXUSR | S_IXGRP | S_IXOTH)) 945 sdata = "*"; 946 break; 947 case S_IFLNK: sdata = "@"; break; 948 #ifdef S_IFSOCK 949 case S_IFSOCK: sdata = "="; break; 950 #endif 951 #ifdef S_IFWHT 952 case S_IFWHT: sdata = "%"; break; 953 #endif /* S_IFWHT */ 954 #ifdef S_IFDOOR 955 case S_IFDOOR: sdata = ">"; break; 956 #endif /* S_IFDOOR */ 957 } 958 hilo = 0; 959 } 960 else if (hilo == HIGH_PIECE) { 961 switch (st->st_mode & S_IFMT) { 962 case S_IFIFO: sdata = "Fifo File"; break; 963 case S_IFCHR: sdata = "Character Device"; break; 964 case S_IFDIR: sdata = "Directory"; break; 965 case S_IFBLK: sdata = "Block Device"; break; 966 case S_IFREG: sdata = "Regular File"; break; 967 case S_IFLNK: sdata = "Symbolic Link"; break; 968 #ifdef S_IFSOCK 969 case S_IFSOCK: sdata = "Socket"; break; 970 #endif 971 #ifdef S_IFWHT 972 case S_IFWHT: sdata = "Whiteout File"; break; 973 #endif /* S_IFWHT */ 974 #ifdef S_IFDOOR 975 case S_IFDOOR: sdata = "Door"; break; 976 #endif /* S_IFDOOR */ 977 default: sdata = "???"; break; 978 } 979 hilo = 0; 980 } 981 formats = FMTF_STRING; 982 if (ofmt == 0) 983 ofmt = FMTF_STRING; 984 break; 985 case SHOW_filename: 986 small = 0; 987 data = 0; 988 if (file == NULL) { 989 (void)strlcpy(path, "(stdin)", sizeof(path)); 990 if (hilo == HIGH_PIECE || hilo == LOW_PIECE) 991 hilo = 0; 992 } 993 else if (hilo == 0) 994 (void)strlcpy(path, file, sizeof(path)); 995 else { 996 char *s; 997 (void)strlcpy(path, file, sizeof(path)); 998 s = strrchr(path, '/'); 999 if (s != NULL) { 1000 /* trim off trailing /'s */ 1001 while (s != path && 1002 s[0] == '/' && s[1] == '\0') 1003 *s-- = '\0'; 1004 s = strrchr(path, '/'); 1005 } 1006 if (hilo == HIGH_PIECE) { 1007 if (s == NULL) 1008 (void)strlcpy(path, ".", sizeof(path)); 1009 else { 1010 while (s != path && s[0] == '/') 1011 *s-- = '\0'; 1012 } 1013 hilo = 0; 1014 } 1015 else if (hilo == LOW_PIECE) { 1016 if (s != NULL && s[1] != '\0') 1017 (void)strlcpy(path, s + 1, 1018 sizeof(path)); 1019 hilo = 0; 1020 } 1021 } 1022 sdata = path; 1023 formats = FMTF_STRING; 1024 if (ofmt == 0) 1025 ofmt = FMTF_STRING; 1026 break; 1027 case SHOW_sizerdev: 1028 if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) { 1029 char majdev[20], mindev[20]; 1030 int l1, l2; 1031 1032 if (size == 0) /* avoid -1/2 */ 1033 size++; /* 1/2 == 0/2 so this is safe */ 1034 l1 = format1(st, 1035 file, 1036 fmt, flen, 1037 majdev, sizeof(majdev), 1038 flags, (size - 1) / 2, prec, 1039 ofmt, HIGH_PIECE, SHOW_st_rdev, quiet); 1040 l2 = format1(st, 1041 file, 1042 fmt, flen, 1043 mindev, sizeof(mindev), 1044 flags | FLAG_MINUS , size / 2, prec, 1045 ofmt, LOW_PIECE, SHOW_st_rdev, quiet); 1046 return (snprintf(buf, blen, "%.*s,%.*s", 1047 l1, majdev, l2, mindev)); 1048 } 1049 else { 1050 return (format1(st, 1051 file, 1052 fmt, flen, 1053 buf, blen, 1054 flags, size, prec, 1055 ofmt, 0, SHOW_st_size, quiet)); 1056 } 1057 /*NOTREACHED*/ 1058 default: 1059 errx(1, "%.*s: bad format", (int)flen, fmt); 1060 } 1061 1062 /* 1063 * If a subdatum was specified but not supported, or an output 1064 * format was selected that is not supported, that's an error. 1065 */ 1066 if (hilo != 0 || (ofmt & formats) == 0) 1067 errx(1, "%.*s: bad format", (int)flen, fmt); 1068 1069 /* 1070 * FLAG_POUND with FMTF_STRING means use vis(3) encoding. 1071 * First prefixlen chars are not encoded. 1072 */ 1073 if ((flags & FLAG_POUND) != 0 && ofmt == FMTF_STRING) { 1074 flags &= !FLAG_POUND; 1075 strncpy(visbuf, sdata, prefixlen); 1076 /* Avoid GCC warnings. */ 1077 visbuf[prefixlen] = 0; 1078 strnvis(visbuf + prefixlen, sizeof(visbuf) - prefixlen, 1079 sdata + prefixlen, VIS_WHITE | VIS_OCTAL | VIS_CSTYLE); 1080 sdata = visbuf; 1081 } 1082 1083 /* 1084 * Assemble the format string for passing to printf(3). 1085 */ 1086 lfmt[0] = '\0'; 1087 (void)strcat(lfmt, "%"); 1088 if (flags & FLAG_POUND) 1089 (void)strcat(lfmt, "#"); 1090 if (flags & FLAG_SPACE) 1091 (void)strcat(lfmt, " "); 1092 if (flags & FLAG_PLUS) 1093 (void)strcat(lfmt, "+"); 1094 if (flags & FLAG_MINUS) 1095 (void)strcat(lfmt, "-"); 1096 if (flags & FLAG_ZERO) 1097 (void)strcat(lfmt, "0"); 1098 1099 /* 1100 * Only the timespecs support the FLOAT output format, and that 1101 * requires work that differs from the other formats. 1102 */ 1103 if (ofmt == FMTF_FLOAT) { 1104 /* 1105 * Nothing after the decimal point, so just print seconds. 1106 */ 1107 if (prec == 0) { 1108 if (size != -1) { 1109 (void)snprintf(tmp, sizeof(tmp), "%d", size); 1110 (void)strcat(lfmt, tmp); 1111 } 1112 (void)strcat(lfmt, "lld"); 1113 return (snprintf(buf, blen, lfmt, 1114 (long long)secs)); 1115 } 1116 1117 /* 1118 * Unspecified precision gets all the precision we have: 1119 * 9 digits. 1120 */ 1121 if (prec == -1) 1122 prec = 9; 1123 1124 /* 1125 * Adjust the size for the decimal point and the digits 1126 * that will follow. 1127 */ 1128 size -= prec + 1; 1129 1130 /* 1131 * Any leftover size that's legitimate will be used. 1132 */ 1133 if (size > 0) { 1134 (void)snprintf(tmp, sizeof(tmp), "%d", size); 1135 (void)strcat(lfmt, tmp); 1136 } 1137 /* Seconds: time_t cast to long long. */ 1138 (void)strcat(lfmt, "lld"); 1139 1140 /* 1141 * The stuff after the decimal point always needs zero 1142 * filling. 1143 */ 1144 (void)strcat(lfmt, ".%0"); 1145 1146 /* 1147 * We can "print" at most nine digits of precision. The 1148 * rest we will pad on at the end. 1149 * 1150 * Nanoseconds: long. 1151 */ 1152 (void)snprintf(tmp, sizeof(tmp), "%dld", prec > 9 ? 9 : prec); 1153 (void)strcat(lfmt, tmp); 1154 1155 /* 1156 * For precision of less that nine digits, trim off the 1157 * less significant figures. 1158 */ 1159 for (; prec < 9; prec++) 1160 nsecs /= 10; 1161 1162 /* 1163 * Use the format, and then tack on any zeroes that 1164 * might be required to make up the requested precision. 1165 */ 1166 l = snprintf(buf, blen, lfmt, (long long)secs, nsecs); 1167 for (; prec > 9 && l < (int)blen; prec--, l++) 1168 (void)strcat(buf, "0"); 1169 return (l); 1170 } 1171 1172 /* 1173 * Add on size and precision, if specified, to the format. 1174 */ 1175 if (size != -1) { 1176 (void)snprintf(tmp, sizeof(tmp), "%d", size); 1177 (void)strcat(lfmt, tmp); 1178 } 1179 if (prec != -1) { 1180 (void)snprintf(tmp, sizeof(tmp), ".%d", prec); 1181 (void)strcat(lfmt, tmp); 1182 } 1183 1184 /* 1185 * String output uses the temporary sdata. 1186 */ 1187 if (ofmt == FMTF_STRING) { 1188 if (sdata == NULL) 1189 errx(1, "%.*s: bad format", (int)flen, fmt); 1190 (void)strcat(lfmt, "s"); 1191 return (snprintf(buf, blen, lfmt, sdata)); 1192 } 1193 1194 /* 1195 * Ensure that sign extension does not cause bad looking output 1196 * for some forms. 1197 */ 1198 if (small && ofmt != FMTF_DECIMAL) 1199 data = (uint32_t)data; 1200 1201 /* 1202 * The four "numeric" output forms. 1203 */ 1204 (void)strcat(lfmt, "ll"); 1205 switch (ofmt) { 1206 case FMTF_DECIMAL: (void)strcat(lfmt, "d"); break; 1207 case FMTF_OCTAL: (void)strcat(lfmt, "o"); break; 1208 case FMTF_UNSIGNED: (void)strcat(lfmt, "u"); break; 1209 case FMTF_HEX: (void)strcat(lfmt, "x"); break; 1210 } 1211 1212 /* 1213 * shift and round to nearest for kilobytes, megabytes, 1214 * gigabytes. 1215 */ 1216 if (shift > 0) { 1217 data >>= (shift - 1); 1218 data++; 1219 data >>= 1; 1220 } 1221 1222 return (snprintf(buf, blen, lfmt, data)); 1223 } 1224