1 /* $OpenBSD: stat.c,v 1.13 2008/06/26 05:42:21 ray Exp $ */ 2 /* $NetBSD: stat.c,v 1.19 2004/06/20 22:20:16 jmc Exp $ */ 3 4 /* 5 * Copyright (c) 2002 The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by Andrew Brown. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 #ifndef lint 34 static const char rccs_id[] = 35 "$OpenBSD: stat.c,v 1.13 2008/06/26 05:42:21 ray Exp $"; 36 #endif 37 38 #include <sys/types.h> 39 #include <sys/stat.h> 40 41 #include <ctype.h> 42 #include <err.h> 43 #include <errno.h> 44 #include <grp.h> 45 #include <limits.h> 46 #include <pwd.h> 47 #include <stdio.h> 48 #include <stdlib.h> 49 #include <string.h> 50 #include <time.h> 51 #include <unistd.h> 52 53 #define DEF_FORMAT \ 54 "%d %i %Sp %l %Su %Sg %r %z \"%Sa\" \"%Sm\" \"%Sc\" " \ 55 "%k %b %#Xf %N" 56 #define RAW_FORMAT "%d %i %#p %l %u %g %r %z %a %m %c " \ 57 "%k %b %f %N" 58 #define LS_FORMAT "%Sp %l %Su %Sg %Z %Sm %N%SY" 59 #define LSF_FORMAT "%Sp %l %Su %Sg %Z %Sm %N%T%SY" 60 #define SHELL_FORMAT \ 61 "st_dev=%d st_ino=%i st_mode=%#p st_nlink=%l " \ 62 "st_uid=%u st_gid=%g st_rdev=%r st_size=%z " \ 63 "st_atime=%a st_mtime=%m st_ctime=%c " \ 64 "st_blksize=%k st_blocks=%b st_flags=%f" 65 #define LINUX_FORMAT \ 66 " File: \"%N\"%n" \ 67 " Size: %-11z FileType: %HT%n" \ 68 " Mode: (%04OLp/%.10Sp) Uid: (%5u/%8Su) Gid: (%5g/%8Sg)%n" \ 69 "Device: %Hd,%Ld Inode: %i Links: %l%n" \ 70 "Access: %Sa%n" \ 71 "Modify: %Sm%n" \ 72 "Change: %Sc" 73 74 #define TIME_FORMAT "%b %e %T %Y" 75 76 #define FLAG_POUND 0x01 77 #define FLAG_SPACE 0x02 78 #define FLAG_PLUS 0x04 79 #define FLAG_ZERO 0x08 80 #define FLAG_MINUS 0x10 81 82 /* 83 * These format characters must all be unique, except the magic one. 84 */ 85 #define FMT_MAGIC '%' 86 #define FMT_DOT '.' 87 88 #define SIMPLE_NEWLINE 'n' 89 #define SIMPLE_TAB 't' 90 #define SIMPLE_PERCENT '%' 91 #define SIMPLE_NUMBER '@' 92 93 #define FMT_POUND '#' 94 #define FMT_SPACE ' ' 95 #define FMT_PLUS '+' 96 #define FMT_ZERO '0' 97 #define FMT_MINUS '-' 98 99 #define FMT_DECIMAL 'D' 100 #define FMT_OCTAL 'O' 101 #define FMT_UNSIGNED 'U' 102 #define FMT_HEX 'X' 103 #define FMT_FLOAT 'F' 104 #define FMT_STRING 'S' 105 106 #define FMTF_DECIMAL 0x01 107 #define FMTF_OCTAL 0x02 108 #define FMTF_UNSIGNED 0x04 109 #define FMTF_HEX 0x08 110 #define FMTF_FLOAT 0x10 111 #define FMTF_STRING 0x20 112 113 #define HIGH_PIECE 'H' 114 #define MIDDLE_PIECE 'M' 115 #define LOW_PIECE 'L' 116 117 #define SHOW_st_dev 'd' 118 #define SHOW_st_ino 'i' 119 #define SHOW_st_mode 'p' 120 #define SHOW_st_nlink 'l' 121 #define SHOW_st_uid 'u' 122 #define SHOW_st_gid 'g' 123 #define SHOW_st_rdev 'r' 124 #define SHOW_st_atime 'a' 125 #define SHOW_st_mtime 'm' 126 #define SHOW_st_ctime 'c' 127 #define SHOW_st_btime 'B' 128 #define SHOW_st_size 'z' 129 #define SHOW_st_blocks 'b' 130 #define SHOW_st_blksize 'k' 131 #define SHOW_st_flags 'f' 132 #define SHOW_st_gen 'v' 133 #define SHOW_symlink 'Y' 134 #define SHOW_filetype 'T' 135 #define SHOW_filename 'N' 136 #define SHOW_sizerdev 'Z' 137 138 void usage(const char *); 139 void output(const struct stat *, const char *, 140 const char *, int, int); 141 int format1(const struct stat *, /* stat info */ 142 const char *, /* the file name */ 143 const char *, int, /* the format string itself */ 144 char *, size_t, /* a place to put the output */ 145 int, int, int, int, /* the parsed format */ 146 int, int); 147 148 char *timefmt; 149 int linkfail; 150 151 #define addchar(s, c, nl) \ 152 do { \ 153 (void)fputc((c), (s)); \ 154 (*nl) = ((c) == '\n'); \ 155 } while (0/*CONSTCOND*/) 156 157 extern char *__progname; 158 159 int 160 main(int argc, char *argv[]) 161 { 162 struct stat st; 163 int ch, rc, errs; 164 int lsF, fmtchar, usestat, fn, nonl, quiet; 165 char *statfmt, *options, *synopsis; 166 167 lsF = 0; 168 fmtchar = '\0'; 169 usestat = 0; 170 nonl = 0; 171 quiet = 0; 172 linkfail = 0; 173 statfmt = NULL; 174 timefmt = NULL; 175 176 options = "f:FlLnqrst:x"; 177 synopsis = "[-FlLnqrsx] [-f format] [-t timefmt] [file ...]"; 178 179 while ((ch = getopt(argc, argv, options)) != -1) 180 switch (ch) { 181 case 'F': 182 lsF = 1; 183 break; 184 case 'L': 185 usestat = 1; 186 break; 187 case 'n': 188 nonl = 1; 189 break; 190 case 'q': 191 quiet = 1; 192 break; 193 case 'f': 194 statfmt = optarg; 195 /* FALLTHROUGH */ 196 case 'l': 197 case 'r': 198 case 's': 199 case 'x': 200 if (fmtchar != 0) 201 errx(1, "can't use format '%c' with '%c'", 202 fmtchar, ch); 203 fmtchar = ch; 204 break; 205 case 't': 206 timefmt = optarg; 207 break; 208 default: 209 usage(synopsis); 210 } 211 212 argc -= optind; 213 argv += optind; 214 fn = 1; 215 216 if (fmtchar == '\0') { 217 if (lsF) 218 fmtchar = 'l'; 219 else { 220 fmtchar = 'f'; 221 statfmt = DEF_FORMAT; 222 } 223 } 224 225 if (lsF && fmtchar != 'l') 226 errx(1, "can't use format '%c' with -F", fmtchar); 227 228 switch (fmtchar) { 229 case 'f': 230 /* statfmt already set */ 231 break; 232 case 'l': 233 statfmt = lsF ? LSF_FORMAT : LS_FORMAT; 234 break; 235 case 'r': 236 statfmt = RAW_FORMAT; 237 break; 238 case 's': 239 statfmt = SHELL_FORMAT; 240 break; 241 case 'x': 242 statfmt = LINUX_FORMAT; 243 if (timefmt == NULL) 244 timefmt = "%c"; 245 break; 246 default: 247 usage(synopsis); 248 /*NOTREACHED*/ 249 } 250 251 if (timefmt == NULL) 252 timefmt = TIME_FORMAT; 253 254 errs = 0; 255 do { 256 if (argc == 0) 257 rc = fstat(STDIN_FILENO, &st); 258 else if (usestat) { 259 /* 260 * Try stat() and if it fails, fall back to 261 * lstat() just in case we're examining a 262 * broken symlink. 263 */ 264 if ((rc = stat(argv[0], &st)) == -1 && 265 errno == ENOENT && 266 (rc = lstat(argv[0], &st)) == -1) 267 errno = ENOENT; 268 } else 269 rc = lstat(argv[0], &st); 270 271 if (rc == -1) { 272 errs = 1; 273 linkfail = 1; 274 if (!quiet) 275 warn("%s", 276 argc == 0 ? "(stdin)" : argv[0]); 277 } else 278 output(&st, argv[0], statfmt, fn, nonl); 279 280 argv++; 281 argc--; 282 fn++; 283 } while (argc > 0); 284 285 return (errs); 286 } 287 288 void 289 usage(const char *synopsis) 290 { 291 292 (void)fprintf(stderr, "usage: %s %s\n", __progname, synopsis); 293 exit(1); 294 } 295 296 /* 297 * Parses a format string. 298 */ 299 void 300 output(const struct stat *st, const char *file, 301 const char *statfmt, int fn, int nonl) 302 { 303 int flags, size, prec, ofmt, hilo, what; 304 char buf[PATH_MAX + 4 + 1]; 305 const char *subfmt; 306 int nl, t, i; 307 308 nl = 1; 309 while (*statfmt != '\0') { 310 311 /* 312 * Non-format characters go straight out. 313 */ 314 if (*statfmt != FMT_MAGIC) { 315 addchar(stdout, *statfmt, &nl); 316 statfmt++; 317 continue; 318 } 319 320 /* 321 * The current format "substring" starts here, 322 * and then we skip the magic. 323 */ 324 subfmt = statfmt; 325 statfmt++; 326 327 /* 328 * Some simple one-character "formats". 329 */ 330 switch (*statfmt) { 331 case SIMPLE_NEWLINE: 332 addchar(stdout, '\n', &nl); 333 statfmt++; 334 continue; 335 case SIMPLE_TAB: 336 addchar(stdout, '\t', &nl); 337 statfmt++; 338 continue; 339 case SIMPLE_PERCENT: 340 addchar(stdout, '%', &nl); 341 statfmt++; 342 continue; 343 case SIMPLE_NUMBER: { 344 char num[12], *p; 345 346 snprintf(num, sizeof(num), "%d", fn); 347 for (p = &num[0]; *p; p++) 348 addchar(stdout, *p, &nl); 349 statfmt++; 350 continue; 351 } 352 } 353 354 /* 355 * This must be an actual format string. Format strings are 356 * similar to printf(3) formats up to a point, and are of 357 * the form: 358 * 359 * % required start of format 360 * [-# +0] opt. format characters 361 * size opt. field width 362 * . opt. decimal separator, followed by 363 * prec opt. precision 364 * fmt opt. output specifier (string, numeric, etc.) 365 * sub opt. sub field specifier (high, middle, low) 366 * datum required field specifier (size, mode, etc) 367 * 368 * Only the % and the datum selector are required. All data 369 * have reasonable default output forms. The "sub" specifier 370 * only applies to certain data (mode, dev, rdev, filetype). 371 * The symlink output defaults to STRING, yet will only emit 372 * the leading " -> " if STRING is explicitly specified. The 373 * sizerdev datum will generate rdev output for character or 374 * block devices, and size output for all others. 375 */ 376 flags = 0; 377 do { 378 if (*statfmt == FMT_POUND) 379 flags |= FLAG_POUND; 380 else if (*statfmt == FMT_SPACE) 381 flags |= FLAG_SPACE; 382 else if (*statfmt == FMT_PLUS) 383 flags |= FLAG_PLUS; 384 else if (*statfmt == FMT_ZERO) 385 flags |= FLAG_ZERO; 386 else if (*statfmt == FMT_MINUS) 387 flags |= FLAG_MINUS; 388 else 389 break; 390 statfmt++; 391 } while (1/*CONSTCOND*/); 392 393 size = -1; 394 if (isdigit((unsigned)*statfmt)) { 395 size = 0; 396 while (isdigit((unsigned)*statfmt)) { 397 size = (size * 10) + (*statfmt - '0'); 398 statfmt++; 399 if (size < 0) 400 goto badfmt; 401 } 402 } 403 404 prec = -1; 405 if (*statfmt == FMT_DOT) { 406 statfmt++; 407 408 prec = 0; 409 while (isdigit((unsigned)*statfmt)) { 410 prec = (prec * 10) + (*statfmt - '0'); 411 statfmt++; 412 if (prec < 0) 413 goto badfmt; 414 } 415 } 416 417 #define fmtcase(x, y) case (y): (x) = (y); statfmt++; break 418 #define fmtcasef(x, y, z) case (y): (x) = (z); statfmt++; break 419 switch (*statfmt) { 420 fmtcasef(ofmt, FMT_DECIMAL, FMTF_DECIMAL); 421 fmtcasef(ofmt, FMT_OCTAL, FMTF_OCTAL); 422 fmtcasef(ofmt, FMT_UNSIGNED, FMTF_UNSIGNED); 423 fmtcasef(ofmt, FMT_HEX, FMTF_HEX); 424 fmtcasef(ofmt, FMT_FLOAT, FMTF_FLOAT); 425 fmtcasef(ofmt, FMT_STRING, FMTF_STRING); 426 default: 427 ofmt = 0; 428 break; 429 } 430 431 switch (*statfmt) { 432 fmtcase(hilo, HIGH_PIECE); 433 fmtcase(hilo, MIDDLE_PIECE); 434 fmtcase(hilo, LOW_PIECE); 435 default: 436 hilo = 0; 437 break; 438 } 439 440 switch (*statfmt) { 441 fmtcase(what, SHOW_st_dev); 442 fmtcase(what, SHOW_st_ino); 443 fmtcase(what, SHOW_st_mode); 444 fmtcase(what, SHOW_st_nlink); 445 fmtcase(what, SHOW_st_uid); 446 fmtcase(what, SHOW_st_gid); 447 fmtcase(what, SHOW_st_rdev); 448 fmtcase(what, SHOW_st_atime); 449 fmtcase(what, SHOW_st_mtime); 450 fmtcase(what, SHOW_st_ctime); 451 fmtcase(what, SHOW_st_btime); 452 fmtcase(what, SHOW_st_size); 453 fmtcase(what, SHOW_st_blocks); 454 fmtcase(what, SHOW_st_blksize); 455 fmtcase(what, SHOW_st_flags); 456 fmtcase(what, SHOW_st_gen); 457 fmtcase(what, SHOW_symlink); 458 fmtcase(what, SHOW_filetype); 459 fmtcase(what, SHOW_filename); 460 fmtcase(what, SHOW_sizerdev); 461 default: 462 goto badfmt; 463 } 464 #undef fmtcasef 465 #undef fmtcase 466 467 t = format1(st, file, subfmt, statfmt - subfmt, buf, 468 sizeof(buf), flags, size, prec, ofmt, hilo, what); 469 470 for (i = 0; i < t && i < sizeof(buf) - 1; i++) 471 addchar(stdout, buf[i], &nl); 472 473 continue; 474 475 badfmt: 476 errx(1, "%.*s: bad format", 477 (int)(statfmt - subfmt + 1), subfmt); 478 } 479 480 if (!nl && !nonl) 481 (void)fputc('\n', stdout); 482 (void)fflush(stdout); 483 } 484 485 /* 486 * Arranges output according to a single parsed format substring. 487 */ 488 int 489 format1(const struct stat *st, 490 const char *file, 491 const char *fmt, int flen, 492 char *buf, size_t blen, 493 int flags, int size, int prec, int ofmt, 494 int hilo, int what) 495 { 496 u_int64_t data; 497 char *sdata, lfmt[24], tmp[20]; 498 char smode[12], sid[12], path[PATH_MAX + 4]; 499 struct passwd *pw; 500 struct group *gr; 501 struct tm *tm; 502 time_t secs; 503 long nsecs; 504 int l, small, formats, gottime, n; 505 506 formats = 0; 507 small = 0; 508 gottime = 0; 509 secs = 0; 510 nsecs = 0; 511 512 /* 513 * First, pick out the data and tweak it based on hilo or 514 * specified output format (symlink output only). 515 */ 516 switch (what) { 517 case SHOW_st_dev: 518 case SHOW_st_rdev: 519 small = (sizeof(st->st_dev) == 4); 520 data = (what == SHOW_st_dev) ? st->st_dev : st->st_rdev; 521 sdata = (what == SHOW_st_dev) ? 522 devname(st->st_dev, S_IFBLK) : 523 devname(st->st_rdev, 524 S_ISCHR(st->st_mode) ? S_IFCHR : 525 S_ISBLK(st->st_mode) ? S_IFBLK : 526 0U); 527 if (sdata == NULL) 528 sdata = "???"; 529 if (hilo == HIGH_PIECE) { 530 data = major(data); 531 hilo = 0; 532 } else if (hilo == LOW_PIECE) { 533 data = minor((unsigned)data); 534 hilo = 0; 535 } 536 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX | 537 FMTF_STRING; 538 if (ofmt == 0) 539 ofmt = FMTF_UNSIGNED; 540 break; 541 case SHOW_st_ino: 542 small = (sizeof(st->st_ino) == 4); 543 data = st->st_ino; 544 sdata = NULL; 545 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX; 546 if (ofmt == 0) 547 ofmt = FMTF_UNSIGNED; 548 break; 549 case SHOW_st_mode: 550 small = (sizeof(st->st_mode) == 4); 551 data = st->st_mode; 552 strmode(st->st_mode, smode); 553 sdata = smode; 554 l = strlen(sdata); 555 if (sdata[l - 1] == ' ') 556 sdata[--l] = '\0'; 557 if (hilo == HIGH_PIECE) { 558 data >>= 12; 559 sdata += 1; 560 sdata[3] = '\0'; 561 hilo = 0; 562 } else if (hilo == MIDDLE_PIECE) { 563 data = (data >> 9) & 07; 564 sdata += 4; 565 sdata[3] = '\0'; 566 hilo = 0; 567 } else if (hilo == LOW_PIECE) { 568 data &= 0777; 569 sdata += 7; 570 sdata[3] = '\0'; 571 hilo = 0; 572 } 573 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX | 574 FMTF_STRING; 575 if (ofmt == 0) 576 ofmt = FMTF_OCTAL; 577 break; 578 case SHOW_st_nlink: 579 small = (sizeof(st->st_dev) == 4); 580 data = st->st_nlink; 581 sdata = NULL; 582 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX; 583 if (ofmt == 0) 584 ofmt = FMTF_UNSIGNED; 585 break; 586 case SHOW_st_uid: 587 small = (sizeof(st->st_uid) == 4); 588 data = st->st_uid; 589 if ((pw = getpwuid(st->st_uid)) != NULL) 590 sdata = pw->pw_name; 591 else { 592 snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_uid); 593 sdata = sid; 594 } 595 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX | 596 FMTF_STRING; 597 if (ofmt == 0) 598 ofmt = FMTF_UNSIGNED; 599 break; 600 case SHOW_st_gid: 601 small = (sizeof(st->st_gid) == 4); 602 data = st->st_gid; 603 if ((gr = getgrgid(st->st_gid)) != NULL) 604 sdata = gr->gr_name; 605 else { 606 snprintf(sid, sizeof(sid), "(%ld)", (long)st->st_gid); 607 sdata = sid; 608 } 609 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX | 610 FMTF_STRING; 611 if (ofmt == 0) 612 ofmt = FMTF_UNSIGNED; 613 break; 614 case SHOW_st_atime: 615 gottime = 1; 616 secs = st->st_atime; 617 nsecs = st->st_atimensec; 618 /* FALLTHROUGH */ 619 case SHOW_st_mtime: 620 if (!gottime) { 621 gottime = 1; 622 secs = st->st_mtime; 623 nsecs = st->st_mtimensec; 624 } 625 /* FALLTHROUGH */ 626 case SHOW_st_ctime: 627 if (!gottime) { 628 gottime = 1; 629 secs = st->st_ctime; 630 nsecs = st->st_ctimensec; 631 } 632 /* FALLTHROUGH */ 633 case SHOW_st_btime: 634 if (!gottime) { 635 gottime = 1; 636 secs = st->__st_birthtimespec.tv_sec; 637 nsecs = st->__st_birthtimespec.tv_nsec; 638 } 639 small = (sizeof(secs) == 4); 640 data = secs; 641 small = 1; 642 tm = localtime(&secs); 643 (void)strftime(path, sizeof(path), timefmt, tm); 644 sdata = path; 645 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX | 646 FMTF_FLOAT | FMTF_STRING; 647 if (ofmt == 0) 648 ofmt = FMTF_DECIMAL; 649 break; 650 case SHOW_st_size: 651 small = (sizeof(st->st_size) == 4); 652 data = st->st_size; 653 sdata = NULL; 654 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX; 655 if (ofmt == 0) 656 ofmt = FMTF_UNSIGNED; 657 break; 658 case SHOW_st_blocks: 659 small = (sizeof(st->st_blocks) == 4); 660 data = st->st_blocks; 661 sdata = NULL; 662 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX; 663 if (ofmt == 0) 664 ofmt = FMTF_UNSIGNED; 665 break; 666 case SHOW_st_blksize: 667 small = (sizeof(st->st_blksize) == 4); 668 data = st->st_blksize; 669 sdata = NULL; 670 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX; 671 if (ofmt == 0) 672 ofmt = FMTF_UNSIGNED; 673 break; 674 case SHOW_st_flags: 675 small = (sizeof(st->st_flags) == 4); 676 data = st->st_flags; 677 sdata = NULL; 678 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX; 679 if (ofmt == 0) 680 ofmt = FMTF_UNSIGNED; 681 break; 682 case SHOW_st_gen: 683 small = (sizeof(st->st_gen) == 4); 684 data = st->st_gen; 685 sdata = NULL; 686 formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX; 687 if (ofmt == 0) 688 ofmt = FMTF_UNSIGNED; 689 break; 690 case SHOW_symlink: 691 small = 0; 692 data = 0; 693 if (S_ISLNK(st->st_mode)) { 694 snprintf(path, sizeof(path), " -> "); 695 l = readlink(file, path + 4, sizeof(path) - 4 - 1); 696 if (l == -1) { 697 linkfail = 1; 698 l = 0; 699 path[0] = '\0'; 700 } 701 path[l + 4] = '\0'; 702 sdata = path + (ofmt == FMTF_STRING ? 0 : 4); 703 } else { 704 linkfail = 1; 705 sdata = ""; 706 } 707 formats = FMTF_STRING; 708 if (ofmt == 0) 709 ofmt = FMTF_STRING; 710 break; 711 case SHOW_filetype: 712 small = 0; 713 data = 0; 714 sdata = smode; 715 sdata[0] = '\0'; 716 if (hilo == 0 || hilo == LOW_PIECE) { 717 switch (st->st_mode & S_IFMT) { 718 case S_IFIFO: 719 (void)strlcat(sdata, "|", sizeof(smode)); 720 break; 721 case S_IFDIR: 722 (void)strlcat(sdata, "/", sizeof(smode)); 723 break; 724 case S_IFREG: 725 if (st->st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) 726 (void)strlcat(sdata, "*", 727 sizeof(smode)); 728 break; 729 case S_IFLNK: 730 (void)strlcat(sdata, "@", sizeof(smode)); 731 break; 732 case S_IFSOCK: 733 (void)strlcat(sdata, "=", sizeof(smode)); 734 break; 735 } 736 hilo = 0; 737 } else if (hilo == HIGH_PIECE) { 738 switch (st->st_mode & S_IFMT) { 739 case S_IFIFO: sdata = "Fifo File"; break; 740 case S_IFCHR: sdata = "Character Device"; break; 741 case S_IFDIR: sdata = "Directory"; break; 742 case S_IFBLK: sdata = "Block Device"; break; 743 case S_IFREG: sdata = "Regular File"; break; 744 case S_IFLNK: sdata = "Symbolic Link"; break; 745 case S_IFSOCK: sdata = "Socket"; break; 746 default: sdata = "???"; break; 747 } 748 hilo = 0; 749 } 750 formats = FMTF_STRING; 751 if (ofmt == 0) 752 ofmt = FMTF_STRING; 753 break; 754 case SHOW_filename: 755 small = 0; 756 data = 0; 757 if (file == NULL) 758 (void)strlcpy(path, "(stdin)", sizeof(path)); 759 else 760 (void)strlcpy(path, file, sizeof(path)); 761 sdata = path; 762 formats = FMTF_STRING; 763 if (ofmt == 0) 764 ofmt = FMTF_STRING; 765 break; 766 case SHOW_sizerdev: 767 if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) { 768 char majdev[20], mindev[20]; 769 int l1, l2; 770 771 l1 = format1(st, file, fmt, flen, 772 majdev, sizeof(majdev), flags, size, prec, 773 ofmt, HIGH_PIECE, SHOW_st_rdev); 774 l2 = format1(st, file, fmt, flen, 775 mindev, sizeof(mindev), flags, size, prec, 776 ofmt, LOW_PIECE, SHOW_st_rdev); 777 n = snprintf(buf, blen, "%.*s,%.*s", 778 l1, majdev, l2, mindev); 779 return (n >= blen ? blen : n); 780 } else { 781 return (format1(st, file, fmt, flen, buf, blen, 782 flags, size, prec, ofmt, 0, SHOW_st_size)); 783 } 784 /*NOTREACHED*/ 785 default: 786 errx(1, "%.*s: bad format", (int)flen, fmt); 787 } 788 789 /* 790 * If a subdatum was specified but not supported, or an output 791 * format was selected that is not supported, that's an error. 792 */ 793 if (hilo != 0 || (ofmt & formats) == 0) 794 errx(1, "%.*s: bad format", (int)flen, fmt); 795 796 /* 797 * Assemble the format string for passing to printf(3). 798 */ 799 lfmt[0] = '\0'; 800 (void)strlcat(lfmt, "%", sizeof(lfmt)); 801 if (flags & FLAG_POUND) 802 (void)strlcat(lfmt, "#", sizeof(lfmt)); 803 if (flags & FLAG_SPACE) 804 (void)strlcat(lfmt, " ", sizeof(lfmt)); 805 if (flags & FLAG_PLUS) 806 (void)strlcat(lfmt, "+", sizeof(lfmt)); 807 if (flags & FLAG_MINUS) 808 (void)strlcat(lfmt, "-", sizeof(lfmt)); 809 if (flags & FLAG_ZERO) 810 (void)strlcat(lfmt, "0", sizeof(lfmt)); 811 812 /* 813 * Only the timespecs support the FLOAT output format, and that 814 * requires work that differs from the other formats. 815 */ 816 if (ofmt == FMTF_FLOAT) { 817 /* 818 * Nothing after the decimal point, so just print seconds. 819 */ 820 if (prec == 0) { 821 if (size != -1) { 822 (void)snprintf(tmp, sizeof(tmp), "%d", size); 823 (void)strlcat(lfmt, tmp, sizeof(lfmt)); 824 } 825 (void)strlcat(lfmt, "d", sizeof(lfmt)); 826 n = snprintf(buf, blen, lfmt, secs); 827 return (n >= blen ? blen : n); 828 } 829 830 /* 831 * Unspecified precision gets all the precision we have: 832 * 9 digits. 833 */ 834 if (prec == -1) 835 prec = 9; 836 837 /* 838 * Adjust the size for the decimal point and the digits 839 * that will follow. 840 */ 841 size -= prec + 1; 842 843 /* 844 * Any leftover size that's legitimate will be used. 845 */ 846 if (size > 0) { 847 (void)snprintf(tmp, sizeof(tmp), "%d", size); 848 (void)strlcat(lfmt, tmp, sizeof(lfmt)); 849 } 850 (void)strlcat(lfmt, "d", sizeof(lfmt)); 851 852 /* 853 * The stuff after the decimal point always needs zero 854 * filling. 855 */ 856 (void)strlcat(lfmt, ".%0", sizeof(lfmt)); 857 858 /* 859 * We can "print" at most nine digits of precision. The 860 * rest we will pad on at the end. 861 */ 862 (void)snprintf(tmp, sizeof(tmp), "%dd", prec > 9 ? 9 : prec); 863 (void)strlcat(lfmt, tmp, sizeof(lfmt)); 864 865 /* 866 * For precision of less that nine digits, trim off the 867 * less significant figures. 868 */ 869 for (; prec < 9; prec++) 870 nsecs /= 10; 871 872 /* 873 * Use the format, and then tack on any zeroes that 874 * might be required to make up the requested precision. 875 */ 876 l = snprintf(buf, blen, lfmt, secs, nsecs); 877 if (l >= blen) 878 return (l); 879 for (; prec > 9 && l < blen; prec--, l++) 880 (void)strlcat(buf, "0", blen); 881 return (l); 882 } 883 884 /* 885 * Add on size and precision, if specified, to the format. 886 */ 887 if (size != -1) { 888 (void)snprintf(tmp, sizeof(tmp), "%d", size); 889 (void)strlcat(lfmt, tmp, sizeof(lfmt)); 890 } 891 if (prec != -1) { 892 (void)snprintf(tmp, sizeof(tmp), ".%d", prec); 893 (void)strlcat(lfmt, tmp, sizeof(lfmt)); 894 } 895 896 /* 897 * String output uses the temporary sdata. 898 */ 899 if (ofmt == FMTF_STRING) { 900 if (sdata == NULL) 901 errx(1, "%.*s: bad format", (int)flen, fmt); 902 (void)strlcat(lfmt, "s", sizeof(lfmt)); 903 n = snprintf(buf, blen, lfmt, sdata); 904 return (n >= blen ? blen : n); 905 } 906 907 /* 908 * Ensure that sign extension does not cause bad looking output 909 * for some forms. 910 */ 911 if (small && ofmt != FMTF_DECIMAL) 912 data = (u_int32_t)data; 913 914 /* 915 * The four "numeric" output forms. 916 */ 917 (void)strlcat(lfmt, "ll", sizeof(lfmt)); 918 switch (ofmt) { 919 case FMTF_DECIMAL: (void)strlcat(lfmt, "d", sizeof(lfmt)); break; 920 case FMTF_OCTAL: (void)strlcat(lfmt, "o", sizeof(lfmt)); break; 921 case FMTF_UNSIGNED: (void)strlcat(lfmt, "u", sizeof(lfmt)); break; 922 case FMTF_HEX: (void)strlcat(lfmt, "x", sizeof(lfmt)); break; 923 } 924 925 n = snprintf(buf, blen, lfmt, data); 926 return (n >= blen ? blen : n); 927 } 928