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