1 /* $NetBSD: stat.c,v 1.32 2010/12/16 05:42:14 dholland 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.32 2010/12/16 05:42:14 dholland 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 (void)strftime(path, sizeof(path), timefmt, tm); 723 sdata = path; 724 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX | 725 FMTF_FLOAT | FMTF_STRING; 726 if (ofmt == 0) 727 ofmt = FMTF_DECIMAL; 728 break; 729 case SHOW_st_size: 730 small = (sizeof(st->st_size) == 4); 731 data = st->st_size; 732 sdata = NULL; 733 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX; 734 if (ofmt == 0) 735 ofmt = FMTF_UNSIGNED; 736 switch (hilo) { 737 case HIGH_PIECE: 738 shift = 30; /* gigabytes */ 739 hilo = 0; 740 break; 741 case MIDDLE_PIECE: 742 shift = 20; /* megabytes */ 743 hilo = 0; 744 break; 745 case LOW_PIECE: 746 shift = 10; /* kilobytes */ 747 hilo = 0; 748 break; 749 } 750 break; 751 case SHOW_st_blocks: 752 small = (sizeof(st->st_blocks) == 4); 753 data = st->st_blocks; 754 sdata = NULL; 755 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX; 756 if (ofmt == 0) 757 ofmt = FMTF_UNSIGNED; 758 break; 759 case SHOW_st_blksize: 760 small = (sizeof(st->st_blksize) == 4); 761 data = st->st_blksize; 762 sdata = NULL; 763 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX; 764 if (ofmt == 0) 765 ofmt = FMTF_UNSIGNED; 766 break; 767 #if HAVE_STRUCT_STAT_ST_FLAGS 768 case SHOW_st_flags: 769 small = (sizeof(st->st_flags) == 4); 770 data = st->st_flags; 771 sdata = NULL; 772 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX; 773 if (ofmt == 0) 774 ofmt = FMTF_UNSIGNED; 775 break; 776 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */ 777 #if HAVE_STRUCT_STAT_ST_GEN 778 case SHOW_st_gen: 779 small = (sizeof(st->st_gen) == 4); 780 data = st->st_gen; 781 sdata = NULL; 782 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX; 783 if (ofmt == 0) 784 ofmt = FMTF_UNSIGNED; 785 break; 786 #endif /* HAVE_STRUCT_STAT_ST_GEN */ 787 case SHOW_realpath: 788 small = 0; 789 data = 0; 790 if (file == NULL) { 791 (void)strlcpy(path, "(stdin)", sizeof(path)); 792 sdata = path; 793 } else { 794 snprintf(path, sizeof(path), " -> "); 795 if (realpath(file, path + 4) == NULL) { 796 linkfail = 1; 797 l = 0; 798 path[0] = '\0'; 799 } 800 sdata = path + (ofmt == FMTF_STRING ? 0 : 4); 801 } 802 803 formats = FMTF_STRING; 804 if (ofmt == 0) 805 ofmt = FMTF_STRING; 806 break; 807 case SHOW_symlink: 808 small = 0; 809 data = 0; 810 if (S_ISLNK(st->st_mode)) { 811 snprintf(path, sizeof(path), " -> "); 812 l = readlink(file, path + 4, sizeof(path) - 4 - 1); 813 if (l == -1) { 814 linkfail = 1; 815 l = 0; 816 path[0] = '\0'; 817 } 818 path[l + 4] = '\0'; 819 sdata = path + (ofmt == FMTF_STRING ? 0 : 4); 820 } 821 else { 822 linkfail = 1; 823 sdata = ""; 824 } 825 formats = FMTF_STRING; 826 if (ofmt == 0) 827 ofmt = FMTF_STRING; 828 break; 829 case SHOW_filetype: 830 small = 0; 831 data = 0; 832 sdata = ""; 833 if (hilo == 0 || hilo == LOW_PIECE) { 834 switch (st->st_mode & S_IFMT) { 835 case S_IFIFO: sdata = "|"; break; 836 case S_IFDIR: sdata = "/"; break; 837 case S_IFREG: 838 if (st->st_mode & 839 (S_IXUSR | S_IXGRP | S_IXOTH)) 840 sdata = "*"; 841 break; 842 case S_IFLNK: sdata = "@"; break; 843 #ifdef S_IFSOCK 844 case S_IFSOCK: sdata = "="; break; 845 #endif 846 #ifdef S_IFWHT 847 case S_IFWHT: sdata = "%"; break; 848 #endif /* S_IFWHT */ 849 #ifdef S_IFDOOR 850 case S_IFDOOR: sdata = ">"; break; 851 #endif /* S_IFDOOR */ 852 } 853 hilo = 0; 854 } 855 else if (hilo == HIGH_PIECE) { 856 switch (st->st_mode & S_IFMT) { 857 case S_IFIFO: sdata = "Fifo File"; break; 858 case S_IFCHR: sdata = "Character Device"; break; 859 case S_IFDIR: sdata = "Directory"; break; 860 case S_IFBLK: sdata = "Block Device"; break; 861 case S_IFREG: sdata = "Regular File"; break; 862 case S_IFLNK: sdata = "Symbolic Link"; break; 863 #ifdef S_IFSOCK 864 case S_IFSOCK: sdata = "Socket"; break; 865 #endif 866 #ifdef S_IFWHT 867 case S_IFWHT: sdata = "Whiteout File"; break; 868 #endif /* S_IFWHT */ 869 #ifdef S_IFDOOR 870 case S_IFDOOR: sdata = "Door"; break; 871 #endif /* S_IFDOOR */ 872 default: sdata = "???"; break; 873 } 874 hilo = 0; 875 } 876 formats = FMTF_STRING; 877 if (ofmt == 0) 878 ofmt = FMTF_STRING; 879 break; 880 case SHOW_filename: 881 small = 0; 882 data = 0; 883 if (file == NULL) { 884 (void)strlcpy(path, "(stdin)", sizeof(path)); 885 if (hilo == HIGH_PIECE || hilo == LOW_PIECE) 886 hilo = 0; 887 } 888 else if (hilo == 0) 889 (void)strlcpy(path, file, sizeof(path)); 890 else { 891 char *s; 892 (void)strlcpy(path, file, sizeof(path)); 893 s = strrchr(path, '/'); 894 if (s != NULL) { 895 /* trim off trailing /'s */ 896 while (s != path && 897 s[0] == '/' && s[1] == '\0') 898 *s-- = '\0'; 899 s = strrchr(path, '/'); 900 } 901 if (hilo == HIGH_PIECE) { 902 if (s == NULL) 903 (void)strlcpy(path, ".", sizeof(path)); 904 else { 905 while (s != path && s[0] == '/') 906 *s-- = '\0'; 907 } 908 hilo = 0; 909 } 910 else if (hilo == LOW_PIECE) { 911 if (s != NULL && s[1] != '\0') 912 (void)strlcpy(path, s + 1, 913 sizeof(path)); 914 hilo = 0; 915 } 916 } 917 sdata = path; 918 formats = FMTF_STRING; 919 if (ofmt == 0) 920 ofmt = FMTF_STRING; 921 break; 922 case SHOW_sizerdev: 923 if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) { 924 char majdev[20], mindev[20]; 925 int l1, l2; 926 927 l1 = format1(st, 928 file, 929 fmt, flen, 930 majdev, sizeof(majdev), 931 flags, size, prec, 932 ofmt, HIGH_PIECE, SHOW_st_rdev); 933 l2 = format1(st, 934 file, 935 fmt, flen, 936 mindev, sizeof(mindev), 937 flags, size, prec, 938 ofmt, LOW_PIECE, SHOW_st_rdev); 939 return (snprintf(buf, blen, "%.*s,%.*s", 940 l1, majdev, l2, mindev)); 941 } 942 else { 943 return (format1(st, 944 file, 945 fmt, flen, 946 buf, blen, 947 flags, size, prec, 948 ofmt, 0, SHOW_st_size)); 949 } 950 /*NOTREACHED*/ 951 default: 952 errx(1, "%.*s: bad format", (int)flen, fmt); 953 } 954 955 /* 956 * If a subdatum was specified but not supported, or an output 957 * format was selected that is not supported, that's an error. 958 */ 959 if (hilo != 0 || (ofmt & formats) == 0) 960 errx(1, "%.*s: bad format", (int)flen, fmt); 961 962 /* 963 * Assemble the format string for passing to printf(3). 964 */ 965 lfmt[0] = '\0'; 966 (void)strcat(lfmt, "%"); 967 if (flags & FLAG_POUND) 968 (void)strcat(lfmt, "#"); 969 if (flags & FLAG_SPACE) 970 (void)strcat(lfmt, " "); 971 if (flags & FLAG_PLUS) 972 (void)strcat(lfmt, "+"); 973 if (flags & FLAG_MINUS) 974 (void)strcat(lfmt, "-"); 975 if (flags & FLAG_ZERO) 976 (void)strcat(lfmt, "0"); 977 978 /* 979 * Only the timespecs support the FLOAT output format, and that 980 * requires work that differs from the other formats. 981 */ 982 if (ofmt == FMTF_FLOAT) { 983 /* 984 * Nothing after the decimal point, so just print seconds. 985 */ 986 if (prec == 0) { 987 if (size != -1) { 988 (void)snprintf(tmp, sizeof(tmp), "%d", size); 989 (void)strcat(lfmt, tmp); 990 } 991 (void)strcat(lfmt, "lld"); 992 return (snprintf(buf, blen, lfmt, 993 (long long)secs)); 994 } 995 996 /* 997 * Unspecified precision gets all the precision we have: 998 * 9 digits. 999 */ 1000 if (prec == -1) 1001 prec = 9; 1002 1003 /* 1004 * Adjust the size for the decimal point and the digits 1005 * that will follow. 1006 */ 1007 size -= prec + 1; 1008 1009 /* 1010 * Any leftover size that's legitimate will be used. 1011 */ 1012 if (size > 0) { 1013 (void)snprintf(tmp, sizeof(tmp), "%d", size); 1014 (void)strcat(lfmt, tmp); 1015 } 1016 /* Seconds: time_t cast to long long. */ 1017 (void)strcat(lfmt, "lld"); 1018 1019 /* 1020 * The stuff after the decimal point always needs zero 1021 * filling. 1022 */ 1023 (void)strcat(lfmt, ".%0"); 1024 1025 /* 1026 * We can "print" at most nine digits of precision. The 1027 * rest we will pad on at the end. 1028 * 1029 * Nanoseconds: long. 1030 */ 1031 (void)snprintf(tmp, sizeof(tmp), "%dld", prec > 9 ? 9 : prec); 1032 (void)strcat(lfmt, tmp); 1033 1034 /* 1035 * For precision of less that nine digits, trim off the 1036 * less significant figures. 1037 */ 1038 for (; prec < 9; prec++) 1039 nsecs /= 10; 1040 1041 /* 1042 * Use the format, and then tack on any zeroes that 1043 * might be required to make up the requested precision. 1044 */ 1045 l = snprintf(buf, blen, lfmt, (long long)secs, nsecs); 1046 for (; prec > 9 && l < (int)blen; prec--, l++) 1047 (void)strcat(buf, "0"); 1048 return (l); 1049 } 1050 1051 /* 1052 * Add on size and precision, if specified, to the format. 1053 */ 1054 if (size != -1) { 1055 (void)snprintf(tmp, sizeof(tmp), "%d", size); 1056 (void)strcat(lfmt, tmp); 1057 } 1058 if (prec != -1) { 1059 (void)snprintf(tmp, sizeof(tmp), ".%d", prec); 1060 (void)strcat(lfmt, tmp); 1061 } 1062 1063 /* 1064 * String output uses the temporary sdata. 1065 */ 1066 if (ofmt == FMTF_STRING) { 1067 if (sdata == NULL) 1068 errx(1, "%.*s: bad format", (int)flen, fmt); 1069 (void)strcat(lfmt, "s"); 1070 return (snprintf(buf, blen, lfmt, sdata)); 1071 } 1072 1073 /* 1074 * Ensure that sign extension does not cause bad looking output 1075 * for some forms. 1076 */ 1077 if (small && ofmt != FMTF_DECIMAL) 1078 data = (u_int32_t)data; 1079 1080 /* 1081 * The four "numeric" output forms. 1082 */ 1083 (void)strcat(lfmt, "ll"); 1084 switch (ofmt) { 1085 case FMTF_DECIMAL: (void)strcat(lfmt, "d"); break; 1086 case FMTF_OCTAL: (void)strcat(lfmt, "o"); break; 1087 case FMTF_UNSIGNED: (void)strcat(lfmt, "u"); break; 1088 case FMTF_HEX: (void)strcat(lfmt, "x"); break; 1089 } 1090 1091 /* 1092 * shift and round to nearest for kilobytes, megabytes, 1093 * gigabytes. 1094 */ 1095 if (shift > 0) { 1096 data >>= (shift - 1); 1097 data++; 1098 data >>= 1; 1099 } 1100 1101 return (snprintf(buf, blen, lfmt, data)); 1102 } 1103