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 int hex2byte(const char [2]); 190 #if HAVE_STRUCT_STAT_ST_FLAGS 191 char *xfflagstostr(unsigned long); 192 #endif 193 194 static const char *timefmt; 195 static int linkfail; 196 197 #define addchar(s, c, nl) \ 198 do { \ 199 (void)fputc((c), (s)); \ 200 (*nl) = ((c) == '\n'); \ 201 } while (0/*CONSTCOND*/) 202 203 int 204 main(int argc, char *argv[]) 205 { 206 struct stat st; 207 int ch, rc, errs, am_readlink; 208 int lsF, fmtchar, usestat, nfs_handle, fn, nonl, quiet; 209 const char *statfmt, *options, *synopsis; 210 char dname[sizeof _PATH_DEV + SPECNAMELEN] = _PATH_DEV; 211 fhandle_t fhnd; 212 const char *file; 213 214 am_readlink = 0; 215 lsF = 0; 216 fmtchar = '\0'; 217 usestat = 0; 218 nfs_handle = 0; 219 nonl = 0; 220 quiet = 0; 221 linkfail = 0; 222 statfmt = NULL; 223 timefmt = NULL; 224 225 if (strcmp(getprogname(), "readlink") == 0) { 226 am_readlink = 1; 227 options = "fn"; 228 synopsis = "[-fn] [file ...]"; 229 statfmt = "%Y"; 230 fmtchar = 'f'; 231 quiet = 1; 232 } else { 233 options = "f:FHlLnqrst:x"; 234 synopsis = "[-FLnq] [-f format | -l | -r | -s | -x] " 235 "[-t timefmt] [file|handle ...]"; 236 } 237 238 while ((ch = getopt(argc, argv, options)) != -1) 239 switch (ch) { 240 case 'F': 241 lsF = 1; 242 break; 243 case 'H': 244 nfs_handle = 1; 245 break; 246 case 'L': 247 usestat = 1; 248 break; 249 case 'n': 250 nonl = 1; 251 break; 252 case 'q': 253 quiet = 1; 254 break; 255 case 'f': 256 if (am_readlink) { 257 statfmt = "%R"; 258 break; 259 } 260 statfmt = optarg; 261 /* FALLTHROUGH */ 262 case 'l': 263 case 'r': 264 case 's': 265 case 'x': 266 if (fmtchar != 0) 267 errx(1, "can't use format '%c' with '%c'", 268 fmtchar, ch); 269 fmtchar = ch; 270 break; 271 case 't': 272 timefmt = optarg; 273 break; 274 default: 275 usage(synopsis); 276 } 277 278 argc -= optind; 279 argv += optind; 280 fn = 1; 281 282 if (fmtchar == '\0') { 283 if (lsF) 284 fmtchar = 'l'; 285 else { 286 fmtchar = 'f'; 287 statfmt = DEF_FORMAT; 288 } 289 } 290 291 if (lsF && fmtchar != 'l') 292 errx(1, "can't use format '%c' with -F", fmtchar); 293 294 switch (fmtchar) { 295 case 'f': 296 /* statfmt already set */ 297 break; 298 case 'l': 299 statfmt = lsF ? LSF_FORMAT : LS_FORMAT; 300 break; 301 case 'r': 302 statfmt = RAW_FORMAT; 303 break; 304 case 's': 305 statfmt = SHELL_FORMAT; 306 break; 307 case 'x': 308 statfmt = LINUX_FORMAT; 309 if (timefmt == NULL) 310 timefmt = "%c"; 311 break; 312 default: 313 usage(synopsis); 314 /*NOTREACHED*/ 315 } 316 317 if (timefmt == NULL) 318 timefmt = TIME_FORMAT; 319 320 errs = 0; 321 do { 322 if (argc == 0) { 323 if (fdevname_r(STDIN_FILENO, dname + 324 sizeof _PATH_DEV - 1, SPECNAMELEN) != NULL) 325 file = dname; 326 else 327 file = "(stdin)"; 328 rc = fstat(STDIN_FILENO, &st); 329 } else { 330 int j; 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) != 0) { 338 rc = -1; 339 } else { 340 while (j) { 341 rc = hex2byte(&file[j - 2]); 342 if (rc == -1) 343 break; 344 ((char*) &fhnd)[j / 2 - 1] = rc; 345 j -= 2; 346 } 347 } 348 if (rc == -1) 349 errno = EINVAL; 350 else 351 rc = fhstat(&fhnd, &st); 352 353 } else if (usestat) { 354 /* 355 * Try stat() and if it fails, fall back to 356 * lstat() just in case we're examining a 357 * broken symlink. 358 */ 359 if ((rc = stat(file, &st)) == -1 && 360 errno == ENOENT && 361 (rc = lstat(file, &st)) == -1) 362 errno = ENOENT; 363 } 364 else 365 rc = lstat(file, &st); 366 } 367 368 if (rc == -1) { 369 errs = 1; 370 linkfail = 1; 371 if (!quiet) 372 warn("%s: stat", file); 373 } 374 else 375 output(&st, file, statfmt, fn, nonl); 376 377 argv++; 378 argc--; 379 fn++; 380 } while (argc > 0); 381 382 return (am_readlink ? linkfail : errs); 383 } 384 385 #if HAVE_STRUCT_STAT_ST_FLAGS 386 /* 387 * fflagstostr() wrapper that leaks only once 388 */ 389 char * 390 xfflagstostr(unsigned long fflags) 391 { 392 static char *str = NULL; 393 394 if (str != NULL) 395 free(str); 396 397 str = fflagstostr(fflags); 398 if (str == NULL) 399 err(1, "fflagstostr"); 400 return (str); 401 } 402 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */ 403 404 void 405 usage(const char *synopsis) 406 { 407 408 (void)fprintf(stderr, "usage: %s %s\n", getprogname(), synopsis); 409 exit(1); 410 } 411 412 /* 413 * Parses a format string. 414 */ 415 void 416 output(const struct stat *st, const char *file, 417 const char *statfmt, int fn, int nonl) 418 { 419 int flags, size, prec, ofmt, hilo, what; 420 char buf[PATH_MAX + 4 + 1]; 421 const char *subfmt; 422 int nl, t, i; 423 424 nl = 1; 425 while (*statfmt != '\0') { 426 427 /* 428 * Non-format characters go straight out. 429 */ 430 if (*statfmt != FMT_MAGIC) { 431 addchar(stdout, *statfmt, &nl); 432 statfmt++; 433 continue; 434 } 435 436 /* 437 * The current format "substring" starts here, 438 * and then we skip the magic. 439 */ 440 subfmt = statfmt; 441 statfmt++; 442 443 /* 444 * Some simple one-character "formats". 445 */ 446 switch (*statfmt) { 447 case SIMPLE_NEWLINE: 448 addchar(stdout, '\n', &nl); 449 statfmt++; 450 continue; 451 case SIMPLE_TAB: 452 addchar(stdout, '\t', &nl); 453 statfmt++; 454 continue; 455 case SIMPLE_PERCENT: 456 addchar(stdout, '%', &nl); 457 statfmt++; 458 continue; 459 case SIMPLE_NUMBER: { 460 char num[12], *p; 461 462 snprintf(num, sizeof(num), "%d", fn); 463 for (p = &num[0]; *p; p++) 464 addchar(stdout, *p, &nl); 465 statfmt++; 466 continue; 467 } 468 } 469 470 /* 471 * This must be an actual format string. Format strings are 472 * similar to printf(3) formats up to a point, and are of 473 * the form: 474 * 475 * % required start of format 476 * [-# +0] opt. format characters 477 * size opt. field width 478 * . opt. decimal separator, followed by 479 * prec opt. precision 480 * fmt opt. output specifier (string, numeric, etc.) 481 * sub opt. sub field specifier (high, middle, low) 482 * datum required field specifier (size, mode, etc) 483 * 484 * Only the % and the datum selector are required. All data 485 * have reasonable default output forms. The "sub" specifier 486 * only applies to certain data (mode, dev, rdev, filetype). 487 * The symlink output defaults to STRING, yet will only emit 488 * the leading " -> " if STRING is explicitly specified. The 489 * sizerdev datum will generate rdev output for character or 490 * block devices, and size output for all others. 491 */ 492 flags = 0; 493 do { 494 if (*statfmt == FMT_POUND) 495 flags |= FLAG_POUND; 496 else if (*statfmt == FMT_SPACE) 497 flags |= FLAG_SPACE; 498 else if (*statfmt == FMT_PLUS) 499 flags |= FLAG_PLUS; 500 else if (*statfmt == FMT_ZERO) 501 flags |= FLAG_ZERO; 502 else if (*statfmt == FMT_MINUS) 503 flags |= FLAG_MINUS; 504 else 505 break; 506 statfmt++; 507 } while (1/*CONSTCOND*/); 508 509 size = -1; 510 if (isdigit((unsigned)*statfmt)) { 511 size = 0; 512 while (isdigit((unsigned)*statfmt)) { 513 size = (size * 10) + (*statfmt - '0'); 514 statfmt++; 515 if (size < 0) 516 goto badfmt; 517 } 518 } 519 520 prec = -1; 521 if (*statfmt == FMT_DOT) { 522 statfmt++; 523 524 prec = 0; 525 while (isdigit((unsigned)*statfmt)) { 526 prec = (prec * 10) + (*statfmt - '0'); 527 statfmt++; 528 if (prec < 0) 529 goto badfmt; 530 } 531 } 532 533 #define fmtcase(x, y) case (y): (x) = (y); statfmt++; break 534 #define fmtcasef(x, y, z) case (y): (x) = (z); statfmt++; break 535 switch (*statfmt) { 536 fmtcasef(ofmt, FMT_DECIMAL, FMTF_DECIMAL); 537 fmtcasef(ofmt, FMT_OCTAL, FMTF_OCTAL); 538 fmtcasef(ofmt, FMT_UNSIGNED, FMTF_UNSIGNED); 539 fmtcasef(ofmt, FMT_HEX, FMTF_HEX); 540 fmtcasef(ofmt, FMT_FLOAT, FMTF_FLOAT); 541 fmtcasef(ofmt, FMT_STRING, FMTF_STRING); 542 default: 543 ofmt = 0; 544 break; 545 } 546 547 switch (*statfmt) { 548 fmtcase(hilo, HIGH_PIECE); 549 fmtcase(hilo, MIDDLE_PIECE); 550 fmtcase(hilo, LOW_PIECE); 551 default: 552 hilo = 0; 553 break; 554 } 555 556 switch (*statfmt) { 557 fmtcase(what, SHOW_realpath); 558 fmtcase(what, SHOW_st_dev); 559 fmtcase(what, SHOW_st_ino); 560 fmtcase(what, SHOW_st_mode); 561 fmtcase(what, SHOW_st_nlink); 562 fmtcase(what, SHOW_st_uid); 563 fmtcase(what, SHOW_st_gid); 564 fmtcase(what, SHOW_st_rdev); 565 fmtcase(what, SHOW_st_atime); 566 fmtcase(what, SHOW_st_mtime); 567 fmtcase(what, SHOW_st_ctime); 568 fmtcase(what, SHOW_st_btime); 569 fmtcase(what, SHOW_st_size); 570 fmtcase(what, SHOW_st_blocks); 571 fmtcase(what, SHOW_st_blksize); 572 fmtcase(what, SHOW_st_flags); 573 fmtcase(what, SHOW_st_gen); 574 fmtcase(what, SHOW_symlink); 575 fmtcase(what, SHOW_filetype); 576 fmtcase(what, SHOW_filename); 577 fmtcase(what, SHOW_sizerdev); 578 default: 579 goto badfmt; 580 } 581 #undef fmtcasef 582 #undef fmtcase 583 584 t = format1(st, 585 file, 586 subfmt, statfmt - subfmt, 587 buf, sizeof(buf), 588 flags, size, prec, ofmt, hilo, what); 589 590 for (i = 0; i < t && i < (int)(sizeof(buf) - 1); i++) 591 addchar(stdout, buf[i], &nl); 592 593 continue; 594 595 badfmt: 596 errx(1, "%.*s: bad format", 597 (int)(statfmt - subfmt + 1), subfmt); 598 } 599 600 if (!nl && !nonl) 601 (void)fputc('\n', stdout); 602 (void)fflush(stdout); 603 } 604 605 /* 606 * Arranges output according to a single parsed format substring. 607 */ 608 int 609 format1(const struct stat *st, 610 const char *file, 611 const char *fmt, int flen, 612 char *buf, size_t blen, 613 int flags, int size, int prec, int ofmt, 614 int hilo, int what) 615 { 616 u_int64_t data; 617 char *stmp, lfmt[24], tmp[20]; 618 const char *sdata; 619 char smode[12], sid[12], path[PATH_MAX + 4]; 620 struct passwd *pw; 621 struct group *gr; 622 const struct timespec *tsp; 623 struct timespec ts; 624 struct tm *tm; 625 int l, small, formats; 626 627 tsp = NULL; 628 formats = 0; 629 small = 0; 630 631 /* 632 * First, pick out the data and tweak it based on hilo or 633 * specified output format (symlink output only). 634 */ 635 switch (what) { 636 case SHOW_st_dev: 637 case SHOW_st_rdev: 638 small = (sizeof(st->st_dev) == 4); 639 data = (what == SHOW_st_dev) ? st->st_dev : st->st_rdev; 640 #if HAVE_DEVNAME 641 sdata = (what == SHOW_st_dev) ? 642 devname(st->st_dev, S_IFBLK) : 643 devname(st->st_rdev, 644 S_ISCHR(st->st_mode) ? S_IFCHR : 645 S_ISBLK(st->st_mode) ? S_IFBLK : 646 0U); 647 if (sdata == NULL) 648 sdata = "???"; 649 #endif /* HAVE_DEVNAME */ 650 if (hilo == HIGH_PIECE) { 651 data = major(data); 652 hilo = 0; 653 } 654 else if (hilo == LOW_PIECE) { 655 data = minor((unsigned)data); 656 hilo = 0; 657 } 658 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX | 659 #if HAVE_DEVNAME 660 FMTF_STRING; 661 #else /* HAVE_DEVNAME */ 662 0; 663 #endif /* HAVE_DEVNAME */ 664 if (ofmt == 0) 665 ofmt = FMTF_UNSIGNED; 666 break; 667 case SHOW_st_ino: 668 small = (sizeof(st->st_ino) == 4); 669 data = st->st_ino; 670 sdata = NULL; 671 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX; 672 if (ofmt == 0) 673 ofmt = FMTF_UNSIGNED; 674 break; 675 case SHOW_st_mode: 676 small = (sizeof(st->st_mode) == 4); 677 data = st->st_mode; 678 strmode(st->st_mode, smode); 679 stmp = smode; 680 l = strlen(stmp); 681 if (stmp[l - 1] == ' ') 682 stmp[--l] = '\0'; 683 if (hilo == HIGH_PIECE) { 684 data >>= 12; 685 stmp += 1; 686 stmp[3] = '\0'; 687 hilo = 0; 688 } 689 else if (hilo == MIDDLE_PIECE) { 690 data = (data >> 9) & 07; 691 stmp += 4; 692 stmp[3] = '\0'; 693 hilo = 0; 694 } 695 else if (hilo == LOW_PIECE) { 696 data &= 0777; 697 stmp += 7; 698 stmp[3] = '\0'; 699 hilo = 0; 700 } 701 sdata = stmp; 702 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX | 703 FMTF_STRING; 704 if (ofmt == 0) 705 ofmt = FMTF_OCTAL; 706 break; 707 case SHOW_st_nlink: 708 small = (sizeof(st->st_dev) == 4); 709 data = st->st_nlink; 710 sdata = NULL; 711 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX; 712 if (ofmt == 0) 713 ofmt = FMTF_UNSIGNED; 714 break; 715 case SHOW_st_uid: 716 small = (sizeof(st->st_uid) == 4); 717 data = st->st_uid; 718 if ((pw = getpwuid(st->st_uid)) != NULL) 719 sdata = pw->pw_name; 720 else { 721 snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_uid); 722 sdata = sid; 723 } 724 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX | 725 FMTF_STRING; 726 if (ofmt == 0) 727 ofmt = FMTF_UNSIGNED; 728 break; 729 case SHOW_st_gid: 730 small = (sizeof(st->st_gid) == 4); 731 data = st->st_gid; 732 if ((gr = getgrgid(st->st_gid)) != NULL) 733 sdata = gr->gr_name; 734 else { 735 snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_gid); 736 sdata = sid; 737 } 738 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX | 739 FMTF_STRING; 740 if (ofmt == 0) 741 ofmt = FMTF_UNSIGNED; 742 break; 743 case SHOW_st_atime: 744 tsp = &st->st_atimespec; 745 /* FALLTHROUGH */ 746 case SHOW_st_mtime: 747 if (tsp == NULL) 748 tsp = &st->st_mtimespec; 749 /* FALLTHROUGH */ 750 case SHOW_st_ctime: 751 if (tsp == NULL) 752 tsp = &st->st_ctimespec; 753 /* FALLTHROUGH */ 754 #if HAVE_STRUCT_STAT_ST_BIRTHTIME 755 case SHOW_st_btime: 756 if (tsp == NULL) 757 tsp = &st->st_birthtimespec; 758 #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIME */ 759 ts = *tsp; /* copy so we can muck with it */ 760 small = (sizeof(ts.tv_sec) == 4); 761 data = ts.tv_sec; 762 tm = localtime(&ts.tv_sec); 763 if (tm == NULL) { 764 ts.tv_sec = 0; 765 tm = localtime(&ts.tv_sec); 766 } 767 (void)strftime(path, sizeof(path), timefmt, tm); 768 sdata = path; 769 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX | 770 FMTF_FLOAT | FMTF_STRING; 771 if (ofmt == 0) 772 ofmt = FMTF_DECIMAL; 773 break; 774 case SHOW_st_size: 775 small = (sizeof(st->st_size) == 4); 776 data = st->st_size; 777 sdata = NULL; 778 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX; 779 if (ofmt == 0) 780 ofmt = FMTF_UNSIGNED; 781 break; 782 case SHOW_st_blocks: 783 small = (sizeof(st->st_blocks) == 4); 784 data = st->st_blocks; 785 sdata = NULL; 786 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX; 787 if (ofmt == 0) 788 ofmt = FMTF_UNSIGNED; 789 break; 790 case SHOW_st_blksize: 791 small = (sizeof(st->st_blksize) == 4); 792 data = st->st_blksize; 793 sdata = NULL; 794 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX; 795 if (ofmt == 0) 796 ofmt = FMTF_UNSIGNED; 797 break; 798 #if HAVE_STRUCT_STAT_ST_FLAGS 799 case SHOW_st_flags: 800 small = (sizeof(st->st_flags) == 4); 801 data = st->st_flags; 802 sdata = xfflagstostr(st->st_flags); 803 if (*sdata == '\0') 804 sdata = "-"; 805 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX | 806 FMTF_STRING; 807 if (ofmt == 0) 808 ofmt = FMTF_UNSIGNED; 809 break; 810 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */ 811 #if HAVE_STRUCT_STAT_ST_GEN 812 case SHOW_st_gen: 813 small = (sizeof(st->st_gen) == 4); 814 data = st->st_gen; 815 sdata = NULL; 816 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX; 817 if (ofmt == 0) 818 ofmt = FMTF_UNSIGNED; 819 break; 820 #endif /* HAVE_STRUCT_STAT_ST_GEN */ 821 case SHOW_realpath: 822 small = 0; 823 data = 0; 824 if (file == NULL) { 825 (void)strlcpy(path, "(stdin)", sizeof(path)); 826 sdata = path; 827 } else { 828 snprintf(path, sizeof(path), " -> "); 829 if (realpath(file, path + 4) == NULL) { 830 linkfail = 1; 831 l = 0; 832 path[0] = '\0'; 833 } 834 sdata = path + (ofmt == FMTF_STRING ? 0 : 4); 835 } 836 837 formats = FMTF_STRING; 838 if (ofmt == 0) 839 ofmt = FMTF_STRING; 840 break; 841 case SHOW_symlink: 842 small = 0; 843 data = 0; 844 if (S_ISLNK(st->st_mode)) { 845 snprintf(path, sizeof(path), " -> "); 846 l = readlink(file, path + 4, sizeof(path) - 4 - 1); 847 if (l == -1) { 848 linkfail = 1; 849 l = 0; 850 path[0] = '\0'; 851 } 852 path[l + 4] = '\0'; 853 sdata = path + (ofmt == FMTF_STRING ? 0 : 4); 854 } 855 else { 856 linkfail = 1; 857 sdata = ""; 858 } 859 formats = FMTF_STRING; 860 if (ofmt == 0) 861 ofmt = FMTF_STRING; 862 break; 863 case SHOW_filetype: 864 small = 0; 865 data = 0; 866 sdata = ""; 867 if (hilo == 0 || hilo == LOW_PIECE) { 868 switch (st->st_mode & S_IFMT) { 869 case S_IFIFO: sdata = "|"; break; 870 case S_IFDIR: sdata = "/"; break; 871 case S_IFREG: 872 if (st->st_mode & 873 (S_IXUSR | S_IXGRP | S_IXOTH)) 874 sdata = "*"; 875 break; 876 case S_IFLNK: sdata = "@"; break; 877 case S_IFSOCK: sdata = "="; break; 878 #ifdef S_IFWHT 879 case S_IFWHT: sdata = "%"; break; 880 #endif /* S_IFWHT */ 881 #ifdef S_IFDOOR 882 case S_IFDOOR: sdata = ">"; break; 883 #endif /* S_IFDOOR */ 884 } 885 hilo = 0; 886 } 887 else if (hilo == HIGH_PIECE) { 888 switch (st->st_mode & S_IFMT) { 889 case S_IFIFO: sdata = "Fifo File"; break; 890 case S_IFCHR: sdata = "Character Device"; break; 891 case S_IFDIR: sdata = "Directory"; break; 892 case S_IFBLK: sdata = "Block Device"; break; 893 case S_IFREG: sdata = "Regular File"; break; 894 case S_IFLNK: sdata = "Symbolic Link"; break; 895 case S_IFSOCK: sdata = "Socket"; break; 896 #ifdef S_IFWHT 897 case S_IFWHT: sdata = "Whiteout File"; break; 898 #endif /* S_IFWHT */ 899 #ifdef S_IFDOOR 900 case S_IFDOOR: sdata = "Door"; break; 901 #endif /* S_IFDOOR */ 902 default: sdata = "???"; break; 903 } 904 hilo = 0; 905 } 906 formats = FMTF_STRING; 907 if (ofmt == 0) 908 ofmt = FMTF_STRING; 909 break; 910 case SHOW_filename: 911 small = 0; 912 data = 0; 913 (void)strlcpy(path, file, sizeof(path)); 914 sdata = path; 915 formats = FMTF_STRING; 916 if (ofmt == 0) 917 ofmt = FMTF_STRING; 918 break; 919 case SHOW_sizerdev: 920 if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) { 921 char majdev[20], mindev[20]; 922 int l1, l2; 923 924 l1 = format1(st, 925 file, 926 fmt, flen, 927 majdev, sizeof(majdev), 928 flags, size, prec, 929 ofmt, HIGH_PIECE, SHOW_st_rdev); 930 l2 = format1(st, 931 file, 932 fmt, flen, 933 mindev, sizeof(mindev), 934 flags, size, prec, 935 ofmt, LOW_PIECE, SHOW_st_rdev); 936 return (snprintf(buf, blen, "%.*s,%.*s", 937 l1, majdev, l2, mindev)); 938 } 939 else { 940 return (format1(st, 941 file, 942 fmt, flen, 943 buf, blen, 944 flags, size, prec, 945 ofmt, 0, SHOW_st_size)); 946 } 947 /*NOTREACHED*/ 948 default: 949 errx(1, "%.*s: bad format", (int)flen, fmt); 950 } 951 952 /* 953 * If a subdatum was specified but not supported, or an output 954 * format was selected that is not supported, that's an error. 955 */ 956 if (hilo != 0 || (ofmt & formats) == 0) 957 errx(1, "%.*s: bad format", (int)flen, fmt); 958 959 /* 960 * Assemble the format string for passing to printf(3). 961 */ 962 lfmt[0] = '\0'; 963 (void)strcat(lfmt, "%"); 964 if (flags & FLAG_POUND) 965 (void)strcat(lfmt, "#"); 966 if (flags & FLAG_SPACE) 967 (void)strcat(lfmt, " "); 968 if (flags & FLAG_PLUS) 969 (void)strcat(lfmt, "+"); 970 if (flags & FLAG_MINUS) 971 (void)strcat(lfmt, "-"); 972 if (flags & FLAG_ZERO) 973 (void)strcat(lfmt, "0"); 974 975 /* 976 * Only the timespecs support the FLOAT output format, and that 977 * requires work that differs from the other formats. 978 */ 979 if (ofmt == FMTF_FLOAT) { 980 /* 981 * Nothing after the decimal point, so just print seconds. 982 */ 983 if (prec == 0) { 984 if (size != -1) { 985 (void)snprintf(tmp, sizeof(tmp), "%d", size); 986 (void)strcat(lfmt, tmp); 987 } 988 (void)strcat(lfmt, "lld"); 989 return (snprintf(buf, blen, lfmt, 990 (long long)ts.tv_sec)); 991 } 992 993 /* 994 * Unspecified precision gets all the precision we have: 995 * 9 digits. 996 */ 997 if (prec == -1) 998 prec = 9; 999 1000 /* 1001 * Adjust the size for the decimal point and the digits 1002 * that will follow. 1003 */ 1004 size -= prec + 1; 1005 1006 /* 1007 * Any leftover size that's legitimate will be used. 1008 */ 1009 if (size > 0) { 1010 (void)snprintf(tmp, sizeof(tmp), "%d", size); 1011 (void)strcat(lfmt, tmp); 1012 } 1013 /* Seconds: time_t cast to long long. */ 1014 (void)strcat(lfmt, "lld"); 1015 1016 /* 1017 * The stuff after the decimal point always needs zero 1018 * filling. 1019 */ 1020 (void)strcat(lfmt, ".%0"); 1021 1022 /* 1023 * We can "print" at most nine digits of precision. The 1024 * rest we will pad on at the end. 1025 * 1026 * Nanoseconds: long. 1027 */ 1028 (void)snprintf(tmp, sizeof(tmp), "%dld", MIN(prec, 9)); 1029 (void)strcat(lfmt, tmp); 1030 1031 /* 1032 * For precision of less that nine digits, trim off the 1033 * less significant figures. 1034 */ 1035 for (; prec < 9; prec++) 1036 ts.tv_nsec /= 10; 1037 1038 /* 1039 * Use the format, and then tack on any zeroes that 1040 * might be required to make up the requested precision. 1041 */ 1042 l = snprintf(buf, blen, lfmt, (long long)ts.tv_sec, ts.tv_nsec); 1043 for (; prec > 9 && l < (int)blen; prec--, l++) 1044 (void)strcat(buf, "0"); 1045 return (l); 1046 } 1047 1048 /* 1049 * Add on size and precision, if specified, to the format. 1050 */ 1051 if (size != -1) { 1052 (void)snprintf(tmp, sizeof(tmp), "%d", size); 1053 (void)strcat(lfmt, tmp); 1054 } 1055 if (prec != -1) { 1056 (void)snprintf(tmp, sizeof(tmp), ".%d", prec); 1057 (void)strcat(lfmt, tmp); 1058 } 1059 1060 /* 1061 * String output uses the temporary sdata. 1062 */ 1063 if (ofmt == FMTF_STRING) { 1064 if (sdata == NULL) 1065 errx(1, "%.*s: bad format", (int)flen, fmt); 1066 (void)strcat(lfmt, "s"); 1067 return (snprintf(buf, blen, lfmt, sdata)); 1068 } 1069 1070 /* 1071 * Ensure that sign extension does not cause bad looking output 1072 * for some forms. 1073 */ 1074 if (small && ofmt != FMTF_DECIMAL) 1075 data = (u_int32_t)data; 1076 1077 /* 1078 * The four "numeric" output forms. 1079 */ 1080 (void)strcat(lfmt, "ll"); 1081 switch (ofmt) { 1082 case FMTF_DECIMAL: (void)strcat(lfmt, "d"); break; 1083 case FMTF_OCTAL: (void)strcat(lfmt, "o"); break; 1084 case FMTF_UNSIGNED: (void)strcat(lfmt, "u"); break; 1085 case FMTF_HEX: (void)strcat(lfmt, "x"); break; 1086 } 1087 1088 return (snprintf(buf, blen, lfmt, data)); 1089 } 1090 1091 1092 #define hex2nibble(c) (c <= '9' ? c - '0' : toupper(c) - 'A' + 10) 1093 int 1094 hex2byte(const char c[2]) { 1095 if (!(ishexnumber(c[0]) && ishexnumber(c[1]))) 1096 return -1; 1097 return (hex2nibble(c[0]) << 4) + hex2nibble(c[1]); 1098 } 1099