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