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