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