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