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