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