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 654 tsp = NULL; 655 formats = 0; 656 small = 0; 657 658 /* 659 * First, pick out the data and tweak it based on hilo or 660 * specified output format (symlink output only). 661 */ 662 switch (what) { 663 case SHOW_st_dev: 664 case SHOW_st_rdev: 665 small = (sizeof(st->st_dev) == 4); 666 data = (what == SHOW_st_dev) ? st->st_dev : st->st_rdev; 667 #if HAVE_DEVNAME 668 sdata = devname(what == SHOW_st_dev ? st->st_dev : 669 st->st_rdev, S_ISCHR(st->st_mode) ? S_IFCHR : 670 (S_ISBLK(st->st_mode) ? S_IFBLK : 0)); 671 #endif /* HAVE_DEVNAME */ 672 if (hilo == HIGH_PIECE) { 673 data = major(data); 674 hilo = 0; 675 } 676 else if (hilo == LOW_PIECE) { 677 data = minor((unsigned)data); 678 hilo = 0; 679 } 680 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX | 681 #if HAVE_DEVNAME 682 FMTF_STRING; 683 #else /* HAVE_DEVNAME */ 684 0; 685 #endif /* HAVE_DEVNAME */ 686 if (ofmt == 0) 687 ofmt = FMTF_UNSIGNED; 688 break; 689 case SHOW_st_ino: 690 small = (sizeof(st->st_ino) == 4); 691 data = st->st_ino; 692 sdata = NULL; 693 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX; 694 if (ofmt == 0) 695 ofmt = FMTF_UNSIGNED; 696 break; 697 case SHOW_st_mode: 698 small = (sizeof(st->st_mode) == 4); 699 data = st->st_mode; 700 strmode(st->st_mode, smode); 701 stmp = smode; 702 l = strlen(stmp); 703 if (stmp[l - 1] == ' ') 704 stmp[--l] = '\0'; 705 if (hilo == HIGH_PIECE) { 706 data >>= 12; 707 stmp += 1; 708 stmp[3] = '\0'; 709 hilo = 0; 710 } 711 else if (hilo == MIDDLE_PIECE) { 712 data = (data >> 9) & 07; 713 stmp += 4; 714 stmp[3] = '\0'; 715 hilo = 0; 716 } 717 else if (hilo == LOW_PIECE) { 718 data &= 0777; 719 stmp += 7; 720 stmp[3] = '\0'; 721 hilo = 0; 722 } 723 sdata = stmp; 724 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX | 725 FMTF_STRING; 726 if (ofmt == 0) 727 ofmt = FMTF_OCTAL; 728 break; 729 case SHOW_st_nlink: 730 small = (sizeof(st->st_dev) == 4); 731 data = st->st_nlink; 732 sdata = NULL; 733 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX; 734 if (ofmt == 0) 735 ofmt = FMTF_UNSIGNED; 736 break; 737 case SHOW_st_uid: 738 small = (sizeof(st->st_uid) == 4); 739 data = st->st_uid; 740 sdata = user_from_uid(st->st_uid, 1); 741 if (sdata == NULL) { 742 snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_uid); 743 sdata = sid; 744 } 745 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX | 746 FMTF_STRING; 747 if (ofmt == 0) 748 ofmt = FMTF_UNSIGNED; 749 break; 750 case SHOW_st_gid: 751 small = (sizeof(st->st_gid) == 4); 752 data = st->st_gid; 753 sdata = group_from_gid(st->st_gid, 1); 754 if (sdata == NULL) { 755 snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_gid); 756 sdata = sid; 757 } 758 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX | 759 FMTF_STRING; 760 if (ofmt == 0) 761 ofmt = FMTF_UNSIGNED; 762 break; 763 case SHOW_st_atime: 764 tsp = &st->st_atimespec; 765 /* FALLTHROUGH */ 766 case SHOW_st_mtime: 767 if (tsp == NULL) 768 tsp = &st->st_mtimespec; 769 /* FALLTHROUGH */ 770 case SHOW_st_ctime: 771 if (tsp == NULL) 772 tsp = &st->st_ctimespec; 773 /* FALLTHROUGH */ 774 #if HAVE_STRUCT_STAT_ST_BIRTHTIME 775 case SHOW_st_btime: 776 if (tsp == NULL) 777 tsp = &st->st_birthtimespec; 778 #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIME */ 779 ts = *tsp; /* copy so we can muck with it */ 780 small = (sizeof(ts.tv_sec) == 4); 781 data = ts.tv_sec; 782 tm = localtime(&ts.tv_sec); 783 if (tm == NULL) { 784 ts.tv_sec = 0; 785 tm = localtime(&ts.tv_sec); 786 } 787 (void)setlocale(LC_TIME, ""); 788 (void)strftime(path, sizeof(path), timefmt, tm); 789 sdata = path; 790 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX | 791 FMTF_FLOAT | FMTF_STRING; 792 if (ofmt == 0) 793 ofmt = FMTF_DECIMAL; 794 break; 795 case SHOW_st_size: 796 small = (sizeof(st->st_size) == 4); 797 data = st->st_size; 798 sdata = NULL; 799 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX; 800 if (ofmt == 0) 801 ofmt = FMTF_UNSIGNED; 802 break; 803 case SHOW_st_blocks: 804 small = (sizeof(st->st_blocks) == 4); 805 data = st->st_blocks; 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_blksize: 812 small = (sizeof(st->st_blksize) == 4); 813 data = st->st_blksize; 814 sdata = NULL; 815 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX; 816 if (ofmt == 0) 817 ofmt = FMTF_UNSIGNED; 818 break; 819 #if HAVE_STRUCT_STAT_ST_FLAGS 820 case SHOW_st_flags: 821 small = (sizeof(st->st_flags) == 4); 822 data = st->st_flags; 823 sdata = xfflagstostr(st->st_flags); 824 if (*sdata == '\0') 825 sdata = "-"; 826 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX | 827 FMTF_STRING; 828 if (ofmt == 0) 829 ofmt = FMTF_UNSIGNED; 830 break; 831 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */ 832 #if HAVE_STRUCT_STAT_ST_GEN 833 case SHOW_st_gen: 834 small = (sizeof(st->st_gen) == 4); 835 data = st->st_gen; 836 sdata = NULL; 837 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX; 838 if (ofmt == 0) 839 ofmt = FMTF_UNSIGNED; 840 break; 841 #endif /* HAVE_STRUCT_STAT_ST_GEN */ 842 case SHOW_realpath: 843 small = 0; 844 data = 0; 845 if (file == NULL) { 846 (void)strlcpy(path, "(stdin)", sizeof(path)); 847 sdata = path; 848 } else { 849 snprintf(path, sizeof(path), " -> "); 850 if (realpath(file, path + 4) == NULL) { 851 linkfail = 1; 852 l = 0; 853 path[0] = '\0'; 854 } 855 sdata = path + (ofmt == FMTF_STRING ? 0 : 4); 856 } 857 858 formats = FMTF_STRING; 859 if (ofmt == 0) 860 ofmt = FMTF_STRING; 861 break; 862 case SHOW_symlink: 863 small = 0; 864 data = 0; 865 if (S_ISLNK(st->st_mode)) { 866 snprintf(path, sizeof(path), " -> "); 867 l = readlink(file, path + 4, sizeof(path) - 4 - 1); 868 if (l == -1) { 869 linkfail = 1; 870 l = 0; 871 path[0] = '\0'; 872 } 873 path[l + 4] = '\0'; 874 sdata = path + (ofmt == FMTF_STRING ? 0 : 4); 875 } 876 else { 877 linkfail = 1; 878 sdata = ""; 879 } 880 formats = FMTF_STRING; 881 if (ofmt == 0) 882 ofmt = FMTF_STRING; 883 break; 884 case SHOW_filetype: 885 small = 0; 886 data = 0; 887 sdata = ""; 888 if (hilo == 0 || hilo == LOW_PIECE) { 889 switch (st->st_mode & S_IFMT) { 890 case S_IFIFO: sdata = "|"; break; 891 case S_IFDIR: sdata = "/"; break; 892 case S_IFREG: 893 if (st->st_mode & 894 (S_IXUSR | S_IXGRP | S_IXOTH)) 895 sdata = "*"; 896 break; 897 case S_IFLNK: sdata = "@"; break; 898 case S_IFSOCK: sdata = "="; break; 899 #ifdef S_IFWHT 900 case S_IFWHT: sdata = "%"; break; 901 #endif /* S_IFWHT */ 902 #ifdef S_IFDOOR 903 case S_IFDOOR: sdata = ">"; break; 904 #endif /* S_IFDOOR */ 905 } 906 hilo = 0; 907 } 908 else if (hilo == HIGH_PIECE) { 909 switch (st->st_mode & S_IFMT) { 910 case S_IFIFO: sdata = "Fifo File"; break; 911 case S_IFCHR: sdata = "Character Device"; break; 912 case S_IFDIR: sdata = "Directory"; break; 913 case S_IFBLK: sdata = "Block Device"; break; 914 case S_IFREG: sdata = "Regular File"; break; 915 case S_IFLNK: sdata = "Symbolic Link"; break; 916 case S_IFSOCK: sdata = "Socket"; break; 917 #ifdef S_IFWHT 918 case S_IFWHT: sdata = "Whiteout File"; break; 919 #endif /* S_IFWHT */ 920 #ifdef S_IFDOOR 921 case S_IFDOOR: sdata = "Door"; break; 922 #endif /* S_IFDOOR */ 923 default: sdata = "???"; break; 924 } 925 hilo = 0; 926 } 927 formats = FMTF_STRING; 928 if (ofmt == 0) 929 ofmt = FMTF_STRING; 930 break; 931 case SHOW_filename: 932 small = 0; 933 data = 0; 934 (void)strlcpy(path, file, sizeof(path)); 935 sdata = path; 936 formats = FMTF_STRING; 937 if (ofmt == 0) 938 ofmt = FMTF_STRING; 939 break; 940 case SHOW_sizerdev: 941 if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) { 942 char majdev[20], mindev[20]; 943 int l1, l2; 944 945 l1 = format1(st, 946 file, 947 fmt, flen, 948 majdev, sizeof(majdev), 949 flags, size, prec, 950 ofmt, HIGH_PIECE, SHOW_st_rdev); 951 l2 = format1(st, 952 file, 953 fmt, flen, 954 mindev, sizeof(mindev), 955 flags, size, prec, 956 ofmt, LOW_PIECE, SHOW_st_rdev); 957 return (snprintf(buf, blen, "%.*s,%.*s", 958 l1, majdev, l2, mindev)); 959 } 960 else { 961 return (format1(st, 962 file, 963 fmt, flen, 964 buf, blen, 965 flags, size, prec, 966 ofmt, 0, SHOW_st_size)); 967 } 968 /*NOTREACHED*/ 969 default: 970 errx(1, "%.*s: bad format", (int)flen, fmt); 971 } 972 973 /* 974 * If a subdatum was specified but not supported, or an output 975 * format was selected that is not supported, that's an error. 976 */ 977 if (hilo != 0 || (ofmt & formats) == 0) 978 errx(1, "%.*s: bad format", (int)flen, fmt); 979 980 /* 981 * Assemble the format string for passing to printf(3). 982 */ 983 lfmt[0] = '\0'; 984 (void)strcat(lfmt, "%"); 985 if (flags & FLAG_POUND) 986 (void)strcat(lfmt, "#"); 987 if (flags & FLAG_SPACE) 988 (void)strcat(lfmt, " "); 989 if (flags & FLAG_PLUS) 990 (void)strcat(lfmt, "+"); 991 if (flags & FLAG_MINUS) 992 (void)strcat(lfmt, "-"); 993 if (flags & FLAG_ZERO) 994 (void)strcat(lfmt, "0"); 995 996 /* 997 * Only the timespecs support the FLOAT output format, and that 998 * requires work that differs from the other formats. 999 */ 1000 if (ofmt == FMTF_FLOAT) { 1001 /* 1002 * Nothing after the decimal point, so just print seconds. 1003 */ 1004 if (prec == 0) { 1005 if (size != -1) { 1006 (void)snprintf(tmp, sizeof(tmp), "%d", size); 1007 (void)strcat(lfmt, tmp); 1008 } 1009 (void)strcat(lfmt, "lld"); 1010 return (snprintf(buf, blen, lfmt, 1011 (long long)ts.tv_sec)); 1012 } 1013 1014 /* 1015 * Unspecified precision gets all the precision we have: 1016 * 9 digits. 1017 */ 1018 if (prec == -1) 1019 prec = 9; 1020 1021 /* 1022 * Adjust the size for the decimal point and the digits 1023 * that will follow. 1024 */ 1025 size -= prec + 1; 1026 1027 /* 1028 * Any leftover size that's legitimate will be used. 1029 */ 1030 if (size > 0) { 1031 (void)snprintf(tmp, sizeof(tmp), "%d", size); 1032 (void)strcat(lfmt, tmp); 1033 } 1034 /* Seconds: time_t cast to long long. */ 1035 (void)strcat(lfmt, "lld"); 1036 1037 /* 1038 * The stuff after the decimal point always needs zero 1039 * filling. 1040 */ 1041 (void)strcat(lfmt, ".%0"); 1042 1043 /* 1044 * We can "print" at most nine digits of precision. The 1045 * rest we will pad on at the end. 1046 * 1047 * Nanoseconds: long. 1048 */ 1049 (void)snprintf(tmp, sizeof(tmp), "%dld", MIN(prec, 9)); 1050 (void)strcat(lfmt, tmp); 1051 1052 /* 1053 * For precision of less than nine digits, trim off the 1054 * less significant figures. 1055 */ 1056 for (; prec < 9; prec++) 1057 ts.tv_nsec /= 10; 1058 1059 /* 1060 * Use the format, and then tack on any zeroes that 1061 * might be required to make up the requested precision. 1062 */ 1063 l = snprintf(buf, blen, lfmt, (long long)ts.tv_sec, ts.tv_nsec); 1064 for (; prec > 9 && l < (int)blen; prec--, l++) 1065 (void)strcat(buf, "0"); 1066 return (l); 1067 } 1068 1069 /* 1070 * Add on size and precision, if specified, to the format. 1071 */ 1072 if (size != -1) { 1073 (void)snprintf(tmp, sizeof(tmp), "%d", size); 1074 (void)strcat(lfmt, tmp); 1075 } 1076 if (prec != -1) { 1077 (void)snprintf(tmp, sizeof(tmp), ".%d", prec); 1078 (void)strcat(lfmt, tmp); 1079 } 1080 1081 /* 1082 * String output uses the temporary sdata. 1083 */ 1084 if (ofmt == FMTF_STRING) { 1085 if (sdata == NULL) 1086 errx(1, "%.*s: bad format", (int)flen, fmt); 1087 (void)strcat(lfmt, "s"); 1088 return (snprintf(buf, blen, lfmt, sdata)); 1089 } 1090 1091 /* 1092 * Ensure that sign extension does not cause bad looking output 1093 * for some forms. 1094 */ 1095 if (small && ofmt != FMTF_DECIMAL) 1096 data = (u_int32_t)data; 1097 1098 /* 1099 * The four "numeric" output forms. 1100 */ 1101 (void)strcat(lfmt, "ll"); 1102 switch (ofmt) { 1103 case FMTF_DECIMAL: (void)strcat(lfmt, "d"); break; 1104 case FMTF_OCTAL: (void)strcat(lfmt, "o"); break; 1105 case FMTF_UNSIGNED: (void)strcat(lfmt, "u"); break; 1106 case FMTF_HEX: (void)strcat(lfmt, "x"); break; 1107 } 1108 1109 return (snprintf(buf, blen, lfmt, data)); 1110 } 1111 1112 1113 #define hex2nibble(c) (c <= '9' ? c - '0' : toupper(c) - 'A' + 10) 1114 static int 1115 hex2byte(const char c[2]) { 1116 if (!(ishexnumber(c[0]) && ishexnumber(c[1]))) 1117 return -1; 1118 return (hex2nibble(c[0]) << 4) + hex2nibble(c[1]); 1119 } 1120 1121 static int 1122 fdlistholes(int fd, const char *fn) 1123 { 1124 struct stat sb; 1125 off_t pos = 0, off; 1126 long l; 1127 1128 if (fstat(fd, &sb) < 0) 1129 return (-1); 1130 if (S_ISDIR(sb.st_mode)) { 1131 if ((l = fpathconf(fd, _PC_MIN_HOLE_SIZE)) < 0) 1132 return (-1); 1133 printf("%ld", l); 1134 } else if (!S_ISREG(sb.st_mode)) { 1135 errno = ESPIPE; 1136 return (-1); 1137 } else { 1138 for (;;) { 1139 if ((off = lseek(fd, pos, SEEK_HOLE)) < 0) { 1140 if (errno != ENXIO) 1141 return (-1); 1142 /* 1143 * This can only happen if the file was 1144 * truncated while we were scanning it, or 1145 * on the initial seek if the file is 1146 * empty. Report the virtual hole at the 1147 * end of the file at this position. 1148 */ 1149 off = pos; 1150 } 1151 printf("%jd", (intmax_t)off); 1152 pos = off; 1153 if ((off = lseek(fd, pos, SEEK_DATA)) < 0) { 1154 if (errno != ENXIO) 1155 return (-1); 1156 /* 1157 * There are no more data regions in the 1158 * file, or it got truncated. However, we 1159 * may not be at the end yet. 1160 */ 1161 if ((off = lseek(fd, 0, SEEK_END)) > pos) 1162 printf("-%jd", (intmax_t)off - 1); 1163 break; 1164 } 1165 printf("-%jd,", (intmax_t)off - 1); 1166 pos = off; 1167 } 1168 } 1169 printf(" %s", fn); 1170 if (!nonl) 1171 printf("\n"); 1172 return (0); 1173 } 1174 1175 static int 1176 listholes(const char *fn) 1177 { 1178 int fd, ret; 1179 1180 if ((fd = open(fn, O_RDONLY)) < 0) 1181 return (-1); 1182 ret = fdlistholes(fd, fn); 1183 close(fd); 1184 return (ret); 1185 } 1186