1 /* $NetBSD: file.c,v 1.19 2026/06/10 20:54:16 christos Exp $ */ 2 3 /* 4 * Copyright (c) Ian F. Darwin 1986-1995. 5 * Software written by Ian F. Darwin and others; 6 * maintained 1995-present by Christos Zoulas and others. 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 immediately at the beginning of the file, without modification, 13 * this list of conditions, and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 22 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 */ 30 /* 31 * file - find type of a file or files - main program. 32 */ 33 34 #include "file.h" 35 36 #ifndef lint 37 #if 0 38 FILE_RCSID("@(#)$File: file.c,v 1.222 2026/05/14 18:54:27 christos Exp $") 39 #else 40 __RCSID("$NetBSD: file.c,v 1.19 2026/06/10 20:54:16 christos Exp $"); 41 #endif 42 #endif /* lint */ 43 44 #include "magic.h" 45 46 #include <stdlib.h> 47 #include <unistd.h> 48 #include <string.h> 49 #ifdef RESTORE_TIME 50 # if (__COHERENT__ >= 0x420) 51 # include <sys/utime.h> 52 # else 53 # ifdef USE_UTIMES 54 # include <sys/time.h> 55 # else 56 # include <utime.h> 57 # endif 58 # endif 59 #endif 60 #ifdef HAVE_UNISTD_H 61 #include <unistd.h> /* for read() */ 62 #endif 63 #ifdef HAVE_WCHAR_H 64 #include <wchar.h> 65 #endif 66 #ifdef HAVE_WCTYPE_H 67 #include <wctype.h> 68 #endif 69 #if defined(HAVE_WCHAR_H) && defined(HAVE_MBRTOWC) && defined(HAVE_WCWIDTH) && \ 70 defined(HAVE_WCTYPE_H) 71 #define FILE_WIDE_SUPPORT 72 #else 73 #include <ctype.h> 74 #endif 75 76 #if defined(HAVE_GETOPT_H) && defined(HAVE_STRUCT_OPTION) 77 # include <getopt.h> 78 # ifndef HAVE_GETOPT_LONG 79 int getopt_long(int, char * const *, const char *, 80 const struct option *, int *); 81 # endif 82 # else 83 # include "mygetopt.h" 84 #endif 85 86 #ifdef S_IFLNK 87 # define IFLNK_h "h" 88 # define IFLNK_L "L" 89 #else 90 # define IFLNK_h "" 91 # define IFLNK_L "" 92 #endif 93 94 #define FILE_FLAGS "bcCdE" IFLNK_h "ik" IFLNK_L "lNnprsSvzZ0" 95 #define OPTSTRING "bcCde:Ef:F:hiklLm:nNpP:rsSvzZ0" 96 97 # define USAGE \ 98 "Usage: %s [-" FILE_FLAGS "] [--apple] [--extension] [--mime-encoding]\n" \ 99 " [--mime-type] [-e <testname>] [-F <separator>] " \ 100 " [-f <namefile>]\n" \ 101 " [-m <magicfiles>] [-P <parameter=value>] [--exclude-quiet]\n" \ 102 " <file> ...\n" \ 103 " %s -C [-m <magicfiles>]\n" \ 104 " %s [--help]\n" 105 106 file_private int /* Global command-line options */ 107 bflag = 0, /* brief output format */ 108 nopad = 0, /* Don't pad output */ 109 nobuffer = 0, /* Do not buffer stdout */ 110 nulsep = 0; /* Append '\0' to the separator */ 111 112 file_private const char *separator = ":"; /* Default field separator */ 113 file_private const struct option long_options[] = { 114 #define OPT_HELP 1 115 #define OPT_APPLE 2 116 #define OPT_EXTENSIONS 3 117 #define OPT_MIME_TYPE 4 118 #define OPT_MIME_ENCODING 5 119 #define OPT_EXCLUDE_QUIET 6 120 #define OPT(shortname, longname, opt, def, doc) \ 121 {longname, opt, NULL, shortname}, 122 #define OPT_LONGONLY(longname, opt, def, doc, id) \ 123 {longname, opt, NULL, id}, 124 #include "file_opts.h" 125 #undef OPT 126 #undef OPT_LONGONLY 127 {0, 0, NULL, 0} 128 }; 129 130 file_private const struct { 131 const char *name; 132 int value; 133 } nv[] = { 134 { "apptype", MAGIC_NO_CHECK_APPTYPE }, 135 { "ascii", MAGIC_NO_CHECK_ASCII }, 136 { "cdf", MAGIC_NO_CHECK_CDF }, 137 { "compress", MAGIC_NO_CHECK_COMPRESS }, 138 { "csv", MAGIC_NO_CHECK_CSV }, 139 { "elf", MAGIC_NO_CHECK_ELF }, 140 { "encoding", MAGIC_NO_CHECK_ENCODING }, 141 { "soft", MAGIC_NO_CHECK_SOFT }, 142 { "tar", MAGIC_NO_CHECK_TAR }, 143 { "json", MAGIC_NO_CHECK_JSON }, 144 { "simh", MAGIC_NO_CHECK_SIMH }, 145 { "text", MAGIC_NO_CHECK_TEXT }, /* synonym for ascii */ 146 { "tokens", MAGIC_NO_CHECK_TOKENS }, /* OBSOLETE: ignored for backwards compatibility */ 147 }; 148 149 file_private struct { 150 const char *name; 151 size_t value; 152 size_t def; 153 const char *desc; 154 int tag; 155 int set; 156 } pm[] = { 157 { "bytes", 0, FILE_BYTES_MAX, "max bytes to look inside file", 158 MAGIC_PARAM_BYTES_MAX, 0 }, 159 { "elf_notes", 0, FILE_ELF_NOTES_MAX, "max ELF notes processed", 160 MAGIC_PARAM_ELF_NOTES_MAX, 0 }, 161 { "elf_phnum", 0, FILE_ELF_PHNUM_MAX, "max ELF prog sections processed", 162 MAGIC_PARAM_ELF_PHNUM_MAX, 0 }, 163 { "elf_shnum", 0, FILE_ELF_SHNUM_MAX, "max ELF sections processed", 164 MAGIC_PARAM_ELF_SHNUM_MAX, 0 }, 165 { "elf_shsize", 0, FILE_ELF_SHSIZE_MAX, "max ELF section size", 166 MAGIC_PARAM_ELF_SHSIZE_MAX, 0 }, 167 { "encoding", 0, FILE_ENCODING_MAX, "max bytes to scan for encoding", 168 MAGIC_PARAM_ENCODING_MAX, 0 }, 169 { "indir", 0, FILE_INDIR_MAX, "recursion limit for indirection", 170 MAGIC_PARAM_INDIR_MAX, 0 }, 171 { "name", 0, FILE_NAME_MAX, "use limit for name/use magic", 172 MAGIC_PARAM_NAME_MAX, 0 }, 173 { "regex", 0, FILE_REGEX_MAX, "length limit for REGEX searches", 174 MAGIC_PARAM_REGEX_MAX, 0 }, 175 { "magwarn", 0, FILE_MAGWARN_MAX, "maximum number of magic warnings", 176 MAGIC_PARAM_MAGWARN_MAX, 0 }, 177 }; 178 179 file_private int posixly; 180 181 #ifdef __dead 182 __dead 183 #endif 184 file_private void usage(void); 185 file_private void docprint(const char *, int); 186 #ifdef __dead 187 __dead 188 #endif 189 file_private void help(void); 190 191 file_private int unwrap(struct magic_set *, const char *); 192 file_private int process(struct magic_set *ms, const char *, int); 193 file_private struct magic_set *load(const char *, int); 194 file_private void setparam(const char *); 195 file_private void applyparam(magic_t); 196 197 198 /* 199 * main - parse arguments and handle options 200 */ 201 int 202 main(int argc, char *argv[]) 203 { 204 int c; 205 size_t i, j, wid, nw; 206 int action = 0, didsomefiles = 0, errflg = 0; 207 int flags = 0, e = 0; 208 #if defined(HAVE_LIBSECCOMP) || defined(HAVE_LINUX_LANDLOCK_H) 209 int sandbox = 1; 210 #endif 211 struct magic_set *magic = NULL; 212 int longindex; 213 const char *magicfile = NULL; /* where the magic is */ 214 char *progname; 215 216 /* makes islower etc work for other langs */ 217 (void)setlocale(LC_CTYPE, ""); 218 219 #ifdef __EMX__ 220 /* sh-like wildcard expansion! Shouldn't hurt at least ... */ 221 _wildcard(&argc, &argv); 222 #endif 223 224 if ((progname = strrchr(argv[0], '/')) != NULL) 225 progname++; 226 else 227 progname = argv[0]; 228 229 file_setprogname(progname); 230 231 232 #ifdef S_IFLNK 233 posixly = getenv("POSIXLY_CORRECT") != NULL; 234 flags |= posixly ? MAGIC_SYMLINK : 0; 235 #endif 236 while ((c = getopt_long(argc, argv, OPTSTRING, long_options, 237 &longindex)) != -1) 238 switch (c) { 239 case OPT_HELP: 240 help(); 241 break; 242 case OPT_APPLE: 243 flags |= MAGIC_APPLE; 244 break; 245 case OPT_EXTENSIONS: 246 flags |= MAGIC_EXTENSION; 247 break; 248 case OPT_MIME_TYPE: 249 flags |= MAGIC_MIME_TYPE; 250 break; 251 case OPT_MIME_ENCODING: 252 flags |= MAGIC_MIME_ENCODING; 253 break; 254 case '0': 255 nulsep++; 256 break; 257 case 'b': 258 bflag++; 259 break; 260 case 'c': 261 action = FILE_CHECK; 262 break; 263 case 'C': 264 action = FILE_COMPILE; 265 break; 266 case 'd': 267 flags |= MAGIC_DEBUG|MAGIC_CHECK; 268 break; 269 case 'E': 270 flags |= MAGIC_ERROR; 271 break; 272 case 'e': 273 case OPT_EXCLUDE_QUIET: 274 for (i = 0; i < __arraycount(nv); i++) 275 if (strcmp(nv[i].name, optarg) == 0) 276 break; 277 278 if (i == __arraycount(nv)) { 279 if (c != OPT_EXCLUDE_QUIET) 280 errflg++; 281 } else 282 flags |= nv[i].value; 283 break; 284 285 case 'f': 286 if(action) 287 usage(); 288 if (magic == NULL) 289 if ((magic = load(magicfile, flags)) == NULL) 290 return 1; 291 applyparam(magic); 292 e |= unwrap(magic, optarg); 293 ++didsomefiles; 294 break; 295 case 'F': 296 separator = optarg; 297 break; 298 case 'i': 299 flags |= MAGIC_MIME; 300 break; 301 case 'k': 302 flags |= MAGIC_CONTINUE; 303 break; 304 case 'l': 305 action = FILE_LIST; 306 break; 307 case 'm': 308 magicfile = optarg; 309 break; 310 case 'n': 311 ++nobuffer; 312 break; 313 case 'N': 314 ++nopad; 315 break; 316 #if defined(HAVE_UTIME) || defined(HAVE_UTIMES) 317 case 'p': 318 flags |= MAGIC_PRESERVE_ATIME; 319 break; 320 #endif 321 case 'P': 322 setparam(optarg); 323 break; 324 case 'r': 325 flags |= MAGIC_RAW; 326 break; 327 case 's': 328 flags |= MAGIC_DEVICES; 329 break; 330 case 'S': 331 #ifdef HAVE_LIBSECCOMP 332 sandbox = 0; 333 #endif 334 break; 335 case 'v': 336 if (magicfile == NULL) 337 magicfile = magic_getpath(magicfile, action); 338 (void)fprintf(stdout, "%s-%s\n", file_getprogname(), 339 VERSION); 340 (void)fprintf(stdout, "magic file from %s\n", 341 magicfile); 342 #ifdef HAVE_LIBSECCOMP 343 (void)fprintf(stdout, "seccomp support included\n"); 344 #endif 345 #ifdef HAVE_LINUX_LANDLOCK_H 346 (void)fprintf(stdout, "Landlock support included\n"); 347 #endif 348 return 0; 349 case 'z': 350 flags |= MAGIC_COMPRESS; 351 break; 352 353 case 'Z': 354 flags |= MAGIC_COMPRESS|MAGIC_COMPRESS_TRANSP; 355 break; 356 #ifdef S_IFLNK 357 case 'L': 358 flags |= MAGIC_SYMLINK; 359 break; 360 case 'h': 361 flags &= ~MAGIC_SYMLINK; 362 break; 363 #endif 364 case '?': 365 default: 366 errflg++; 367 break; 368 } 369 370 if (errflg) { 371 usage(); 372 } 373 if (e) 374 return e; 375 376 #ifdef HAVE_LINUX_LANDLOCK_H 377 if (sandbox && enable_landlock(flags, action) == -1) 378 file_err(EXIT_FAILURE, "Landlock initialisation failed"); 379 #endif /* HAVE_LINUX_LANDLOCK_H */ 380 381 #ifdef HAVE_LIBSECCOMP 382 if (sandbox && enable_sandbox(flags, action) == -1) 383 file_err(EXIT_FAILURE, "SECCOMP initialisation failed"); 384 if (sandbox) 385 flags |= MAGIC_NO_COMPRESS_FORK; 386 #endif /* HAVE_LIBSECCOMP */ 387 388 if (MAGIC_VERSION != magic_version()) 389 file_warnx("Compiled magic version [%d] " 390 "does not match with shared library magic version [%d]\n", 391 MAGIC_VERSION, magic_version()); 392 393 switch(action) { 394 case FILE_CHECK: 395 case FILE_COMPILE: 396 case FILE_LIST: 397 /* 398 * Don't try to check/compile ~/.magic unless we explicitly 399 * ask for it. 400 */ 401 magic = magic_open(flags|MAGIC_CHECK); 402 if (magic == NULL) { 403 file_warn("Can't create magic"); 404 return 1; 405 } 406 407 408 switch(action) { 409 case FILE_CHECK: 410 c = magic_check(magic, magicfile); 411 break; 412 case FILE_COMPILE: 413 c = magic_compile(magic, magicfile); 414 break; 415 case FILE_LIST: 416 c = magic_list(magic, magicfile); 417 break; 418 default: 419 abort(); 420 } 421 if (c == -1) { 422 file_warnx("%s", magic_error(magic)); 423 e = 1; 424 goto out; 425 } 426 goto out; 427 default: 428 if (magic == NULL) 429 if ((magic = load(magicfile, flags)) == NULL) 430 return 1; 431 applyparam(magic); 432 } 433 434 if (optind == argc) { 435 if (!didsomefiles) 436 usage(); 437 goto out; 438 } 439 440 for (wid = 0, j = CAST(size_t, optind); j < CAST(size_t, argc); 441 j++) { 442 nw = file_mbswidth(magic, argv[j]); 443 if (nw > wid) 444 wid = nw; 445 } 446 447 /* 448 * If bflag is only set twice, set it depending on 449 * number of files [this is undocumented, and subject to change] 450 */ 451 if (bflag == 2) { 452 bflag = optind >= argc - 1; 453 } 454 for (; optind < argc; optind++) 455 e |= process(magic, argv[optind], wid); 456 457 out: 458 if (!nobuffer) 459 e |= fflush(stdout) != 0; 460 461 if (magic) 462 magic_close(magic); 463 return e; 464 } 465 466 file_private void 467 applyparam(magic_t magic) 468 { 469 size_t i; 470 471 for (i = 0; i < __arraycount(pm); i++) { 472 if (!pm[i].set) 473 continue; 474 if (magic_setparam(magic, pm[i].tag, &pm[i].value) == -1) 475 file_err(EXIT_FAILURE, "Can't set %s", pm[i].name); 476 } 477 } 478 479 file_private void 480 setparam(const char *p) 481 { 482 size_t i; 483 ssize_t mpar; 484 int par; 485 char *s; 486 487 if ((s = CCAST(char *, strchr(p, '='))) == NULL) 488 goto badparm; 489 490 for (i = 0; i < __arraycount(pm); i++) { 491 if (strncmp(p, pm[i].name, s - p) != 0) 492 continue; 493 par = atoi(s + 1); 494 mpar = magic_getmaxparam(pm[i].tag); 495 if (par < 0 || par > mpar) 496 file_err(EXIT_FAILURE, "Out of bounds value %d for %s", 497 par, pm[i].name); 498 pm[i].value = par; 499 pm[i].set = 1; 500 return; 501 } 502 badparm: 503 file_errx(EXIT_FAILURE, "Unknown param %s", p); 504 } 505 506 file_private struct magic_set * 507 /*ARGSUSED*/ 508 load(const char *magicfile, int flags) 509 { 510 struct magic_set *magic = magic_open(flags); 511 const char *e; 512 513 if (magic == NULL) { 514 file_warn("Can't create magic"); 515 return NULL; 516 } 517 if (magic_load(magic, magicfile) == -1) { 518 file_warn("%s", magic_error(magic)); 519 magic_close(magic); 520 return NULL; 521 } 522 if ((e = magic_error(magic)) != NULL) 523 file_warn("%s", e); 524 return magic; 525 } 526 527 /* 528 * unwrap -- read a file of filenames, do each one. 529 */ 530 file_private int 531 unwrap(struct magic_set *ms, const char *fn) 532 { 533 FILE *f; 534 ssize_t len; 535 char *line = NULL; 536 size_t llen = 0; 537 int wid = 0, cwid; 538 int e = 0; 539 size_t fi = 0, fimax = 0; 540 char **flist = NULL; 541 542 if (strcmp("-", fn) == 0) 543 f = stdin; 544 else { 545 if ((f = fopen(fn, "r")) == NULL) { 546 file_warn("Cannot open `%s'", fn); 547 return 1; 548 } 549 } 550 551 while ((len = getline(&line, &llen, f)) > 0) { 552 if (line[len - 1] == '\n') 553 line[len - 1] = '\0'; 554 cwid = file_mbswidth(ms, line); 555 if (nobuffer) { 556 e |= process(ms, line, cwid); 557 free(line); 558 line = NULL; 559 llen = 0; 560 continue; 561 } 562 if (cwid > wid) 563 wid = cwid; 564 if (fi >= fimax) { 565 fimax += 100; 566 char **nf = CAST(char **, 567 realloc(flist, fimax * sizeof(*flist))); 568 if (nf == NULL) { 569 file_err(EXIT_FAILURE, 570 "Cannot allocate memory for file list"); 571 } 572 flist = nf; 573 } 574 flist[fi++] = line; 575 line = NULL; 576 llen = 0; 577 } 578 579 if (!nobuffer) { 580 fimax = fi; 581 for (fi = 0; fi < fimax; fi++) { 582 e |= process(ms, flist[fi], wid); 583 free(flist[fi]); 584 } 585 } 586 free(flist); 587 588 if (f != stdin) 589 (void)fclose(f); 590 return e; 591 } 592 593 file_private void 594 file_octal(unsigned char c) 595 { 596 (void)putc('\\', stdout); 597 (void)putc(((c >> 6) & 7) + '0', stdout); 598 (void)putc(((c >> 3) & 7) + '0', stdout); 599 (void)putc(((c >> 0) & 7) + '0', stdout); 600 } 601 602 file_private void 603 fname_print(const char *inname) 604 { 605 size_t n = strlen(inname); 606 #ifdef FILE_WIDE_SUPPORT 607 mbstate_t state; 608 wchar_t nextchar; 609 size_t bytesconsumed; 610 611 612 (void)memset(&state, 0, sizeof(state)); 613 while (n > 0) { 614 bytesconsumed = mbrtowc(&nextchar, inname, n, &state); 615 if (bytesconsumed == CAST(size_t, -1) || 616 bytesconsumed == CAST(size_t, -2)) { 617 nextchar = *inname++; 618 n--; 619 (void)memset(&state, 0, sizeof(state)); 620 file_octal(CAST(unsigned char, nextchar)); 621 continue; 622 } 623 inname += bytesconsumed; 624 n -= bytesconsumed; 625 if (iswprint(nextchar)) { 626 printf("%lc", (wint_t)nextchar); 627 continue; 628 } 629 /* XXX: What if it is > 255? */ 630 file_octal(CAST(unsigned char, nextchar)); 631 } 632 #else 633 size_t i; 634 for (i = 0; i < n; i++) { 635 unsigned char c = CAST(unsigned char, inname[i]); 636 if (isprint(c)) { 637 (void)putc(c, stdout); 638 continue; 639 } 640 file_octal(c); 641 } 642 #endif 643 } 644 645 /* 646 * Called for each input file on the command line (or in a list of files) 647 */ 648 file_private int 649 process(struct magic_set *ms, const char *inname, int wid) 650 { 651 const char *type, c = nulsep > 1 ? '\0' : '\n'; 652 int std_in = strcmp(inname, "-") == 0; 653 int haderror = 0; 654 655 if (wid > 0 && !bflag) { 656 const char *pname = std_in ? "/dev/stdin" : inname; 657 if ((ms->flags & MAGIC_RAW) == 0) 658 fname_print(pname); 659 else 660 (void)printf("%s", pname); 661 if (nulsep) 662 (void)putc('\0', stdout); 663 if (nulsep < 2) { 664 (void)printf("%s", separator); 665 (void)printf("%*s ", CAST(int, nopad ? 0 666 : (wid - file_mbswidth(ms, inname))), ""); 667 } 668 } 669 670 type = magic_file(ms, std_in ? NULL : inname); 671 672 if (type == NULL) { 673 haderror |= printf("ERROR: %s%c", magic_error(ms), c); 674 } else { 675 haderror |= printf("%s%c", type, c) < 0; 676 } 677 if (nobuffer) 678 haderror |= fflush(stdout) != 0; 679 return haderror || type == NULL; 680 } 681 682 file_protected size_t 683 file_mbswidth(struct magic_set *ms, const char *s) 684 { 685 size_t width = 0; 686 #ifdef FILE_WIDE_SUPPORT 687 size_t bytesconsumed, n; 688 mbstate_t state; 689 wchar_t nextchar; 690 691 (void)memset(&state, 0, sizeof(state)); 692 n = strlen(s); 693 694 while (n > 0) { 695 bytesconsumed = mbrtowc(&nextchar, s, n, &state); 696 if (bytesconsumed == CAST(size_t, -1) || 697 bytesconsumed == CAST(size_t, -2)) { 698 nextchar = *s; 699 bytesconsumed = 1; 700 (void)memset(&state, 0, sizeof(state)); 701 width += 4; 702 } else { 703 int w = wcwidth(nextchar); 704 width += ((ms->flags & MAGIC_RAW) != 0 705 || iswprint(nextchar)) ? (w > 0 ? w : 1) : 4; 706 } 707 708 s += bytesconsumed, n -= bytesconsumed; 709 } 710 #else 711 for (; *s; s++) { 712 width += (ms->flags & MAGIC_RAW) != 0 713 || isprint(CAST(unsigned char, *s)) ? 1 : 4; 714 } 715 #endif 716 return width; 717 } 718 719 file_private void 720 usage(void) 721 { 722 const char *pn = file_getprogname(); 723 (void)fprintf(stderr, USAGE, pn, pn, pn); 724 exit(EXIT_FAILURE); 725 } 726 727 file_private void 728 defprint(int def) 729 { 730 if (!def) 731 return; 732 if (((def & 1) && posixly) || ((def & 2) && !posixly)) 733 (void)fprintf(stdout, " (default)"); 734 (void)putc('\n', stdout); 735 } 736 737 file_private void 738 docprint(const char *opts, int def) 739 { 740 size_t i; 741 int comma, pad; 742 char *sp, *p; 743 744 p = CCAST(char *, strchr(opts, '%')); 745 if (p == NULL) { 746 (void)fprintf(stdout, "%s", opts); 747 defprint(def); 748 return; 749 } 750 751 for (sp = p - 1; sp > opts && *sp == ' '; sp--) 752 continue; 753 754 (void)printf("%.*s", CAST(int, p - opts), opts); 755 pad = (int)CAST(int, p - sp - 1); 756 757 switch (*++p) { 758 case 'e': 759 comma = 0; 760 for (i = 0; i < __arraycount(nv); i++) { 761 (void)printf("%s%s", comma++ ? ", " : "", nv[i].name); 762 if (i && i % 5 == 0 && i != __arraycount(nv) - 1) { 763 (void)printf(",\n%*s", pad, ""); 764 comma = 0; 765 } 766 } 767 break; 768 case 'P': 769 for (i = 0; i < __arraycount(pm); i++) { 770 (void)printf("%9s %7zu %s", pm[i].name, pm[i].def, 771 pm[i].desc); 772 if (i != __arraycount(pm) - 1) 773 (void)printf("\n%*s", pad, ""); 774 } 775 break; 776 default: 777 file_errx(EXIT_FAILURE, "Unknown escape `%c' in long options", 778 *p); 779 break; 780 } 781 (void)printf("%s", opts + (p - opts) + 1); 782 783 } 784 785 file_private void 786 help(void) 787 { 788 (void)fputs( 789 "Usage: file [OPTION...] [FILE...]\n" 790 "Determine type of FILEs.\n" 791 "\n", stdout); 792 #define OPT(shortname, longname, opt, def, doc) \ 793 (void)printf(" -%c, --" longname, shortname), \ 794 docprint(doc, def); 795 #define OPT_LONGONLY(longname, opt, def, doc, id) \ 796 (void)printf(" --" longname), \ 797 docprint(doc, def); 798 #include "file_opts.h" 799 #undef OPT 800 #undef OPT_LONGONLY 801 (void)printf("\nReport bugs to https://bugs.astron.com/\n"); 802 exit(EXIT_SUCCESS); 803 } 804 805 file_private const char *file_progname; 806 807 file_protected void 808 file_setprogname(const char *progname) 809 { 810 file_progname = progname; 811 } 812 813 file_protected const char * 814 file_getprogname(void) 815 { 816 return file_progname; 817 } 818 819 file_protected void 820 file_err(int e, const char *fmt, ...) 821 { 822 va_list ap; 823 int se = errno; 824 825 va_start(ap, fmt); 826 (void)fprintf(stderr, "%s: ", file_progname); 827 (void)vfprintf(stderr, fmt, ap); 828 va_end(ap); 829 if (se) 830 (void)fprintf(stderr, " (%s)\n", strerror(se)); 831 else 832 fputc('\n', stderr); 833 exit(e); 834 } 835 836 file_protected void 837 file_errx(int e, const char *fmt, ...) 838 { 839 va_list ap; 840 841 va_start(ap, fmt); 842 (void)fprintf(stderr, "%s: ", file_progname); 843 (void)vfprintf(stderr, fmt, ap); 844 va_end(ap); 845 (void)fprintf(stderr, "\n"); 846 exit(e); 847 } 848 849 file_protected void 850 file_warn(const char *fmt, ...) 851 { 852 va_list ap; 853 int se = errno; 854 855 va_start(ap, fmt); 856 (void)fprintf(stderr, "%s: ", file_progname); 857 (void)vfprintf(stderr, fmt, ap); 858 va_end(ap); 859 if (se) 860 (void)fprintf(stderr, " (%s)\n", strerror(se)); 861 else 862 fputc('\n', stderr); 863 errno = se; 864 } 865 866 file_protected void 867 file_warnx(const char *fmt, ...) 868 { 869 va_list ap; 870 int se = errno; 871 872 va_start(ap, fmt); 873 (void)fprintf(stderr, "%s: ", file_progname); 874 (void)vfprintf(stderr, fmt, ap); 875 va_end(ap); 876 (void)fprintf(stderr, "\n"); 877 errno = se; 878 } 879