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