1 /* $NetBSD: stat.c,v 1.27 2008/05/16 17:58:33 atatat 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.27 2008/05/16 17:58:33 atatat 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, 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 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 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, quiet); 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, int quiet) 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 < 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 *sdata, lfmt[24], tmp[20]; 556 char smode[12], sid[12], path[PATH_MAX + 4]; 557 struct passwd *pw; 558 struct group *gr; 559 struct tm *tm; 560 time_t secs; 561 long nsecs; 562 int l, small, formats, gottime, shift; 563 564 formats = 0; 565 small = 0; 566 gottime = 0; 567 secs = 0; 568 nsecs = 0; 569 shift = 0; 570 571 /* 572 * First, pick out the data and tweak it based on hilo or 573 * specified output format (symlink output only). 574 */ 575 switch (what) { 576 case SHOW_st_dev: 577 case SHOW_st_rdev: 578 small = (sizeof(st->st_dev) == 4); 579 data = (what == SHOW_st_dev) ? st->st_dev : st->st_rdev; 580 #if HAVE_DEVNAME 581 sdata = (what == SHOW_st_dev) ? 582 devname(st->st_dev, S_IFBLK) : 583 devname(st->st_rdev, 584 S_ISCHR(st->st_mode) ? S_IFCHR : 585 S_ISBLK(st->st_mode) ? S_IFBLK : 586 0U); 587 if (sdata == NULL) 588 sdata = "???"; 589 #endif /* HAVE_DEVNAME */ 590 if (hilo == HIGH_PIECE) { 591 data = major(data); 592 hilo = 0; 593 } 594 else if (hilo == LOW_PIECE) { 595 data = minor((unsigned)data); 596 hilo = 0; 597 } 598 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX | 599 #if HAVE_DEVNAME 600 FMTF_STRING; 601 #else /* HAVE_DEVNAME */ 602 0; 603 #endif /* HAVE_DEVNAME */ 604 if (ofmt == 0) 605 ofmt = FMTF_UNSIGNED; 606 break; 607 case SHOW_st_ino: 608 small = (sizeof(st->st_ino) == 4); 609 data = st->st_ino; 610 sdata = NULL; 611 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX; 612 if (ofmt == 0) 613 ofmt = FMTF_UNSIGNED; 614 break; 615 case SHOW_st_mode: 616 small = (sizeof(st->st_mode) == 4); 617 data = st->st_mode; 618 strmode(st->st_mode, smode); 619 sdata = smode; 620 l = strlen(sdata); 621 if (sdata[l - 1] == ' ') 622 sdata[--l] = '\0'; 623 if (hilo == HIGH_PIECE) { 624 data >>= 12; 625 sdata += 1; 626 sdata[3] = '\0'; 627 hilo = 0; 628 } 629 else if (hilo == MIDDLE_PIECE) { 630 data = (data >> 9) & 07; 631 sdata += 4; 632 sdata[3] = '\0'; 633 hilo = 0; 634 } 635 else if (hilo == LOW_PIECE) { 636 data &= 0777; 637 sdata += 7; 638 sdata[3] = '\0'; 639 hilo = 0; 640 } 641 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX | 642 FMTF_STRING; 643 if (ofmt == 0) 644 ofmt = FMTF_OCTAL; 645 break; 646 case SHOW_st_nlink: 647 small = (sizeof(st->st_dev) == 4); 648 data = st->st_nlink; 649 sdata = NULL; 650 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX; 651 if (ofmt == 0) 652 ofmt = FMTF_UNSIGNED; 653 break; 654 case SHOW_st_uid: 655 small = (sizeof(st->st_uid) == 4); 656 data = st->st_uid; 657 if ((pw = getpwuid(st->st_uid)) != NULL) 658 sdata = pw->pw_name; 659 else { 660 snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_uid); 661 sdata = sid; 662 } 663 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX | 664 FMTF_STRING; 665 if (ofmt == 0) 666 ofmt = FMTF_UNSIGNED; 667 break; 668 case SHOW_st_gid: 669 small = (sizeof(st->st_gid) == 4); 670 data = st->st_gid; 671 if ((gr = getgrgid(st->st_gid)) != NULL) 672 sdata = gr->gr_name; 673 else { 674 snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_gid); 675 sdata = sid; 676 } 677 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX | 678 FMTF_STRING; 679 if (ofmt == 0) 680 ofmt = FMTF_UNSIGNED; 681 break; 682 case SHOW_st_atime: 683 gottime = 1; 684 secs = st->st_atime; 685 #if HAVE_STRUCT_STAT_ST_MTIMENSEC 686 nsecs = st->st_atimensec; 687 #endif 688 /* FALLTHROUGH */ 689 case SHOW_st_mtime: 690 if (!gottime) { 691 gottime = 1; 692 secs = st->st_mtime; 693 #if HAVE_STRUCT_STAT_ST_MTIMENSEC 694 nsecs = st->st_mtimensec; 695 #endif 696 } 697 /* FALLTHROUGH */ 698 case SHOW_st_ctime: 699 if (!gottime) { 700 gottime = 1; 701 secs = st->st_ctime; 702 #if HAVE_STRUCT_STAT_ST_MTIMENSEC 703 nsecs = st->st_ctimensec; 704 #endif 705 } 706 /* FALLTHROUGH */ 707 #if HAVE_STRUCT_STAT_ST_BIRTHTIME 708 case SHOW_st_btime: 709 if (!gottime) { 710 gottime = 1; 711 secs = st->st_birthtime; 712 #if HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC 713 nsecs = st->st_birthtimensec; 714 #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC */ 715 } 716 #endif /* HAVE_STRUCT_STAT_ST_BIRTHTIME */ 717 small = (sizeof(secs) == 4); 718 data = secs; 719 small = 1; 720 tm = localtime(&secs); 721 (void)strftime(path, sizeof(path), timefmt, tm); 722 sdata = path; 723 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX | 724 FMTF_FLOAT | FMTF_STRING; 725 if (ofmt == 0) 726 ofmt = FMTF_DECIMAL; 727 break; 728 case SHOW_st_size: 729 small = (sizeof(st->st_size) == 4); 730 data = st->st_size; 731 sdata = NULL; 732 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX; 733 if (ofmt == 0) 734 ofmt = FMTF_UNSIGNED; 735 switch (hilo) { 736 case HIGH_PIECE: 737 shift = 30; /* gigabytes */ 738 hilo = 0; 739 break; 740 case MIDDLE_PIECE: 741 shift = 20; /* megabytes */ 742 hilo = 0; 743 break; 744 case LOW_PIECE: 745 shift = 10; /* kilobytes */ 746 hilo = 0; 747 break; 748 } 749 break; 750 case SHOW_st_blocks: 751 small = (sizeof(st->st_blocks) == 4); 752 data = st->st_blocks; 753 sdata = NULL; 754 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX; 755 if (ofmt == 0) 756 ofmt = FMTF_UNSIGNED; 757 break; 758 case SHOW_st_blksize: 759 small = (sizeof(st->st_blksize) == 4); 760 data = st->st_blksize; 761 sdata = NULL; 762 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX; 763 if (ofmt == 0) 764 ofmt = FMTF_UNSIGNED; 765 break; 766 #if HAVE_STRUCT_STAT_ST_FLAGS 767 case SHOW_st_flags: 768 small = (sizeof(st->st_flags) == 4); 769 data = st->st_flags; 770 sdata = NULL; 771 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX; 772 if (ofmt == 0) 773 ofmt = FMTF_UNSIGNED; 774 break; 775 #endif /* HAVE_STRUCT_STAT_ST_FLAGS */ 776 #if HAVE_STRUCT_STAT_ST_GEN 777 case SHOW_st_gen: 778 small = (sizeof(st->st_gen) == 4); 779 data = st->st_gen; 780 sdata = NULL; 781 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX; 782 if (ofmt == 0) 783 ofmt = FMTF_UNSIGNED; 784 break; 785 #endif /* HAVE_STRUCT_STAT_ST_GEN */ 786 case SHOW_realpath: 787 small = 0; 788 data = 0; 789 if (file == NULL) { 790 (void)strncpy(path, "(stdin)", sizeof(path)); 791 sdata = path; 792 } else { 793 snprintf(path, sizeof(path), " -> "); 794 if (realpath(file, path + 4) == NULL) { 795 linkfail = 1; 796 l = 0; 797 path[0] = '\0'; 798 } 799 sdata = path + (ofmt == FMTF_STRING ? 0 : 4); 800 } 801 802 formats = FMTF_STRING; 803 if (ofmt == 0) 804 ofmt = FMTF_STRING; 805 break; 806 case SHOW_symlink: 807 small = 0; 808 data = 0; 809 if (S_ISLNK(st->st_mode)) { 810 snprintf(path, sizeof(path), " -> "); 811 l = readlink(file, path + 4, sizeof(path) - 4 - 1); 812 if (l == -1) { 813 linkfail = 1; 814 l = 0; 815 path[0] = '\0'; 816 } 817 path[l + 4] = '\0'; 818 sdata = path + (ofmt == FMTF_STRING ? 0 : 4); 819 } 820 else { 821 linkfail = 1; 822 sdata = ""; 823 } 824 formats = FMTF_STRING; 825 if (ofmt == 0) 826 ofmt = FMTF_STRING; 827 break; 828 case SHOW_filetype: 829 small = 0; 830 data = 0; 831 sdata = smode; 832 sdata[0] = '\0'; 833 if (hilo == 0 || hilo == LOW_PIECE) { 834 switch (st->st_mode & S_IFMT) { 835 case S_IFIFO: (void)strcat(sdata, "|"); break; 836 case S_IFDIR: (void)strcat(sdata, "/"); break; 837 case S_IFREG: 838 if (st->st_mode & 839 (S_IXUSR | S_IXGRP | S_IXOTH)) 840 (void)strcat(sdata, "*"); 841 break; 842 case S_IFLNK: (void)strcat(sdata, "@"); break; 843 #ifdef S_IFSOCK 844 case S_IFSOCK: (void)strcat(sdata, "="); break; 845 #endif 846 #ifdef S_IFWHT 847 case S_IFWHT: (void)strcat(sdata, "%"); break; 848 #endif /* S_IFWHT */ 849 #ifdef S_IFDOOR 850 case S_IFDOOR: (void)strcat(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)strncpy(path, "(stdin)", sizeof(path)); 885 if (hilo == HIGH_PIECE || hilo == LOW_PIECE) 886 hilo = 0; 887 } 888 else if (hilo == 0) 889 (void)strncpy(path, file, sizeof(path)); 890 else { 891 char *s; 892 (void)strncpy(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)strncpy(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)strncpy(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, "d"); 992 return (snprintf(buf, blen, lfmt, secs)); 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 (void)strcat(lfmt, "d"); 1016 1017 /* 1018 * The stuff after the decimal point always needs zero 1019 * filling. 1020 */ 1021 (void)strcat(lfmt, ".%0"); 1022 1023 /* 1024 * We can "print" at most nine digits of precision. The 1025 * rest we will pad on at the end. 1026 */ 1027 (void)snprintf(tmp, sizeof(tmp), "%dd", prec > 9 ? 9 : prec); 1028 (void)strcat(lfmt, tmp); 1029 1030 /* 1031 * For precision of less that nine digits, trim off the 1032 * less significant figures. 1033 */ 1034 for (; prec < 9; prec++) 1035 nsecs /= 10; 1036 1037 /* 1038 * Use the format, and then tack on any zeroes that 1039 * might be required to make up the requested precision. 1040 */ 1041 l = snprintf(buf, blen, lfmt, secs, nsecs); 1042 for (; prec > 9 && l < blen; prec--, l++) 1043 (void)strcat(buf, "0"); 1044 return (l); 1045 } 1046 1047 /* 1048 * Add on size and precision, if specified, to the format. 1049 */ 1050 if (size != -1) { 1051 (void)snprintf(tmp, sizeof(tmp), "%d", size); 1052 (void)strcat(lfmt, tmp); 1053 } 1054 if (prec != -1) { 1055 (void)snprintf(tmp, sizeof(tmp), ".%d", prec); 1056 (void)strcat(lfmt, tmp); 1057 } 1058 1059 /* 1060 * String output uses the temporary sdata. 1061 */ 1062 if (ofmt == FMTF_STRING) { 1063 if (sdata == NULL) 1064 errx(1, "%.*s: bad format", (int)flen, fmt); 1065 (void)strcat(lfmt, "s"); 1066 return (snprintf(buf, blen, lfmt, sdata)); 1067 } 1068 1069 /* 1070 * Ensure that sign extension does not cause bad looking output 1071 * for some forms. 1072 */ 1073 if (small && ofmt != FMTF_DECIMAL) 1074 data = (u_int32_t)data; 1075 1076 /* 1077 * The four "numeric" output forms. 1078 */ 1079 (void)strcat(lfmt, "ll"); 1080 switch (ofmt) { 1081 case FMTF_DECIMAL: (void)strcat(lfmt, "d"); break; 1082 case FMTF_OCTAL: (void)strcat(lfmt, "o"); break; 1083 case FMTF_UNSIGNED: (void)strcat(lfmt, "u"); break; 1084 case FMTF_HEX: (void)strcat(lfmt, "x"); break; 1085 } 1086 1087 /* 1088 * shift and round to nearest for kilobytes, megabytes, 1089 * gigabytes. 1090 */ 1091 if (shift > 0) { 1092 data >>= (shift - 1); 1093 data++; 1094 data >>= 1; 1095 } 1096 1097 return (snprintf(buf, blen, lfmt, data)); 1098 } 1099