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