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