file.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  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. #include "file.h"
  32. #ifndef lint
  33. FILE_RCSID("@(#)$File: file.c,v 1.178 2018/10/01 18:50:31 christos Exp $")
  34. #endif /* lint */
  35. #include "magic.h"
  36. #include <stdlib.h>
  37. #include <unistd.h>
  38. #include <string.h>
  39. #ifdef RESTORE_TIME
  40. # if (__COHERENT__ >= 0x420)
  41. # include <sys/utime.h>
  42. # else
  43. # ifdef USE_UTIMES
  44. # include <sys/time.h>
  45. # else
  46. # include <utime.h>
  47. # endif
  48. # endif
  49. #endif
  50. #ifdef HAVE_UNISTD_H
  51. #include <unistd.h> /* for read() */
  52. #endif
  53. #ifdef HAVE_WCHAR_H
  54. #include <wchar.h>
  55. #endif
  56. #if defined(HAVE_GETOPT_H) && defined(HAVE_STRUCT_OPTION)
  57. # include <getopt.h>
  58. # ifndef HAVE_GETOPT_LONG
  59. int getopt_long(int, char * const *, const char *,
  60. const struct option *, int *);
  61. # endif
  62. # else
  63. # include "mygetopt.h"
  64. #endif
  65. #ifdef S_IFLNK
  66. # define IFLNK_h "h"
  67. # define IFLNK_L "L"
  68. #else
  69. # define IFLNK_h ""
  70. # define IFLNK_L ""
  71. #endif
  72. #ifdef HAVE_LIBSECCOMP
  73. # define SECCOMP_S "S"
  74. #else
  75. # define SECCOMP_S ""
  76. #endif
  77. #define FILE_FLAGS "bcCdE" IFLNK_h "ik" IFLNK_L "lNnprs" SECCOMP_S "vzZ0"
  78. #define OPTSTRING "bcCde:Ef:F:hiklLm:nNpP:rsSvzZ0"
  79. # define USAGE \
  80. "Usage: %s [-" FILE_FLAGS "] [--apple] [--extension] [--mime-encoding]\n" \
  81. " [--mime-type] [-e <testname>] [-F <separator>] " \
  82. " [-f <namefile>]\n" \
  83. " [-m <magicfiles>] [-P <parameter=value>] <file> ...\n" \
  84. " %s -C [-m <magicfiles>]\n" \
  85. " %s [--help]\n"
  86. private int /* Global command-line options */
  87. bflag = 0, /* brief output format */
  88. nopad = 0, /* Don't pad output */
  89. nobuffer = 0, /* Do not buffer stdout */
  90. nulsep = 0; /* Append '\0' to the separator */
  91. private const char *separator = ":"; /* Default field separator */
  92. private const struct option long_options[] = {
  93. #define OPT_HELP 1
  94. #define OPT_APPLE 2
  95. #define OPT_EXTENSIONS 3
  96. #define OPT_MIME_TYPE 4
  97. #define OPT_MIME_ENCODING 5
  98. #define OPT(shortname, longname, opt, def, doc) \
  99. {longname, opt, NULL, shortname},
  100. #define OPT_LONGONLY(longname, opt, def, doc, id) \
  101. {longname, opt, NULL, id},
  102. #include "file_opts.h"
  103. #undef OPT
  104. #undef OPT_LONGONLY
  105. {0, 0, NULL, 0}
  106. };
  107. private const struct {
  108. const char *name;
  109. int value;
  110. } nv[] = {
  111. { "apptype", MAGIC_NO_CHECK_APPTYPE },
  112. { "ascii", MAGIC_NO_CHECK_ASCII },
  113. { "cdf", MAGIC_NO_CHECK_CDF },
  114. { "compress", MAGIC_NO_CHECK_COMPRESS },
  115. { "elf", MAGIC_NO_CHECK_ELF },
  116. { "encoding", MAGIC_NO_CHECK_ENCODING },
  117. { "soft", MAGIC_NO_CHECK_SOFT },
  118. { "tar", MAGIC_NO_CHECK_TAR },
  119. { "json", MAGIC_NO_CHECK_JSON },
  120. { "text", MAGIC_NO_CHECK_TEXT }, /* synonym for ascii */
  121. { "tokens", MAGIC_NO_CHECK_TOKENS }, /* OBSOLETE: ignored for backwards compatibility */
  122. };
  123. private struct {
  124. const char *name;
  125. int tag;
  126. size_t value;
  127. int set;
  128. } pm[] = {
  129. { "indir", MAGIC_PARAM_INDIR_MAX, 0, 0 },
  130. { "name", MAGIC_PARAM_NAME_MAX, 0, 0 },
  131. { "elf_phnum", MAGIC_PARAM_ELF_PHNUM_MAX, 0, 0 },
  132. { "elf_shnum", MAGIC_PARAM_ELF_SHNUM_MAX, 0, 0 },
  133. { "elf_notes", MAGIC_PARAM_ELF_NOTES_MAX, 0, 0 },
  134. { "regex", MAGIC_PARAM_REGEX_MAX, 0, 0 },
  135. { "bytes", MAGIC_PARAM_BYTES_MAX, 0, 0 },
  136. };
  137. private int posixly;
  138. #ifdef __dead
  139. __dead
  140. #endif
  141. private void usage(void);
  142. private void docprint(const char *, int);
  143. #ifdef __dead
  144. __dead
  145. #endif
  146. private void help(void);
  147. private int unwrap(struct magic_set *, const char *);
  148. private int process(struct magic_set *ms, const char *, int);
  149. private struct magic_set *load(const char *, int);
  150. private void setparam(const char *);
  151. private void applyparam(magic_t);
  152. /*
  153. * main - parse arguments and handle options
  154. */
  155. int
  156. main(int argc, char *argv[])
  157. {
  158. int c;
  159. size_t i;
  160. int action = 0, didsomefiles = 0, errflg = 0;
  161. int flags = 0, e = 0;
  162. #ifdef HAVE_LIBSECCOMP
  163. int sandbox = 1;
  164. #endif
  165. struct magic_set *magic = NULL;
  166. int longindex;
  167. const char *magicfile = NULL; /* where the magic is */
  168. char *progname;
  169. /* makes islower etc work for other langs */
  170. #ifdef HAVE_SETLOCALE
  171. (void)setlocale(LC_CTYPE, "");
  172. #endif
  173. #ifdef __EMX__
  174. /* sh-like wildcard expansion! Shouldn't hurt at least ... */
  175. _wildcard(&argc, &argv);
  176. #endif
  177. if ((progname = strrchr(argv[0], '/')) != NULL)
  178. progname++;
  179. else
  180. progname = argv[0];
  181. file_setprogname(progname);
  182. #ifdef S_IFLNK
  183. posixly = getenv("POSIXLY_CORRECT") != NULL;
  184. flags |= posixly ? MAGIC_SYMLINK : 0;
  185. #endif
  186. while ((c = getopt_long(argc, argv, OPTSTRING, long_options,
  187. &longindex)) != -1)
  188. switch (c) {
  189. case OPT_HELP:
  190. help();
  191. break;
  192. case OPT_APPLE:
  193. flags |= MAGIC_APPLE;
  194. break;
  195. case OPT_EXTENSIONS:
  196. flags |= MAGIC_EXTENSION;
  197. break;
  198. case OPT_MIME_TYPE:
  199. flags |= MAGIC_MIME_TYPE;
  200. break;
  201. case OPT_MIME_ENCODING:
  202. flags |= MAGIC_MIME_ENCODING;
  203. break;
  204. case '0':
  205. nulsep++;
  206. break;
  207. case 'b':
  208. bflag++;
  209. break;
  210. case 'c':
  211. action = FILE_CHECK;
  212. break;
  213. case 'C':
  214. action = FILE_COMPILE;
  215. break;
  216. case 'd':
  217. flags |= MAGIC_DEBUG|MAGIC_CHECK;
  218. break;
  219. case 'E':
  220. flags |= MAGIC_ERROR;
  221. break;
  222. case 'e':
  223. for (i = 0; i < sizeof(nv) / sizeof(nv[0]); i++)
  224. if (strcmp(nv[i].name, optarg) == 0)
  225. break;
  226. if (i == sizeof(nv) / sizeof(nv[0]))
  227. errflg++;
  228. else
  229. flags |= nv[i].value;
  230. break;
  231. case 'f':
  232. if(action)
  233. usage();
  234. if (magic == NULL)
  235. if ((magic = load(magicfile, flags)) == NULL)
  236. return 1;
  237. applyparam(magic);
  238. e |= unwrap(magic, optarg);
  239. ++didsomefiles;
  240. break;
  241. case 'F':
  242. separator = optarg;
  243. break;
  244. case 'i':
  245. flags |= MAGIC_MIME;
  246. break;
  247. case 'k':
  248. flags |= MAGIC_CONTINUE;
  249. break;
  250. case 'l':
  251. action = FILE_LIST;
  252. break;
  253. case 'm':
  254. magicfile = optarg;
  255. break;
  256. case 'n':
  257. ++nobuffer;
  258. break;
  259. case 'N':
  260. ++nopad;
  261. break;
  262. #if defined(HAVE_UTIME) || defined(HAVE_UTIMES)
  263. case 'p':
  264. flags |= MAGIC_PRESERVE_ATIME;
  265. break;
  266. #endif
  267. case 'P':
  268. setparam(optarg);
  269. break;
  270. case 'r':
  271. flags |= MAGIC_RAW;
  272. break;
  273. case 's':
  274. flags |= MAGIC_DEVICES;
  275. break;
  276. #ifdef HAVE_LIBSECCOMP
  277. case 'S':
  278. sandbox = 0;
  279. break;
  280. #endif
  281. case 'v':
  282. if (magicfile == NULL)
  283. magicfile = magic_getpath(magicfile, action);
  284. (void)fprintf(stdout, "%s-%s\n", file_getprogname(),
  285. VERSION);
  286. (void)fprintf(stdout, "magic file from %s\n",
  287. magicfile);
  288. return 0;
  289. case 'z':
  290. flags |= MAGIC_COMPRESS;
  291. break;
  292. case 'Z':
  293. flags |= MAGIC_COMPRESS|MAGIC_COMPRESS_TRANSP;
  294. break;
  295. #ifdef S_IFLNK
  296. case 'L':
  297. flags |= MAGIC_SYMLINK;
  298. break;
  299. case 'h':
  300. flags &= ~MAGIC_SYMLINK;
  301. break;
  302. #endif
  303. case '?':
  304. default:
  305. errflg++;
  306. break;
  307. }
  308. if (errflg) {
  309. usage();
  310. }
  311. if (e)
  312. return e;
  313. #ifdef HAVE_LIBSECCOMP
  314. #if 0
  315. if (sandbox && enable_sandbox_basic() == -1)
  316. #else
  317. if (sandbox && enable_sandbox_full() == -1)
  318. #endif
  319. file_err(EXIT_FAILURE, "SECCOMP initialisation failed");
  320. #endif /* HAVE_LIBSECCOMP */
  321. if (MAGIC_VERSION != magic_version())
  322. file_warnx("Compiled magic version [%d] "
  323. "does not match with shared library magic version [%d]\n",
  324. MAGIC_VERSION, magic_version());
  325. switch(action) {
  326. case FILE_CHECK:
  327. case FILE_COMPILE:
  328. case FILE_LIST:
  329. /*
  330. * Don't try to check/compile ~/.magic unless we explicitly
  331. * ask for it.
  332. */
  333. magic = magic_open(flags|MAGIC_CHECK);
  334. if (magic == NULL) {
  335. file_warn("Can't create magic");
  336. return 1;
  337. }
  338. switch(action) {
  339. case FILE_CHECK:
  340. c = magic_check(magic, magicfile);
  341. break;
  342. case FILE_COMPILE:
  343. c = magic_compile(magic, magicfile);
  344. break;
  345. case FILE_LIST:
  346. c = magic_list(magic, magicfile);
  347. break;
  348. default:
  349. abort();
  350. }
  351. if (c == -1) {
  352. file_warnx("%s", magic_error(magic));
  353. e = 1;
  354. goto out;
  355. }
  356. goto out;
  357. default:
  358. if (magic == NULL)
  359. if ((magic = load(magicfile, flags)) == NULL)
  360. return 1;
  361. applyparam(magic);
  362. }
  363. if (optind == argc) {
  364. if (!didsomefiles)
  365. usage();
  366. }
  367. else {
  368. size_t j, wid, nw;
  369. for (wid = 0, j = (size_t)optind; j < (size_t)argc; j++) {
  370. nw = file_mbswidth(argv[j]);
  371. if (nw > wid)
  372. wid = nw;
  373. }
  374. /*
  375. * If bflag is only set twice, set it depending on
  376. * number of files [this is undocumented, and subject to change]
  377. */
  378. if (bflag == 2) {
  379. bflag = optind >= argc - 1;
  380. }
  381. for (; optind < argc; optind++)
  382. e |= process(magic, argv[optind], wid);
  383. }
  384. out:
  385. if (magic)
  386. magic_close(magic);
  387. return e;
  388. }
  389. private void
  390. applyparam(magic_t magic)
  391. {
  392. size_t i;
  393. for (i = 0; i < __arraycount(pm); i++) {
  394. if (!pm[i].set)
  395. continue;
  396. if (magic_setparam(magic, pm[i].tag, &pm[i].value) == -1)
  397. file_err(EXIT_FAILURE, "Can't set %s", pm[i].name);
  398. }
  399. }
  400. private void
  401. setparam(const char *p)
  402. {
  403. size_t i;
  404. char *s;
  405. if ((s = strchr(p, '=')) == NULL)
  406. goto badparm;
  407. for (i = 0; i < __arraycount(pm); i++) {
  408. if (strncmp(p, pm[i].name, s - p) != 0)
  409. continue;
  410. pm[i].value = atoi(s + 1);
  411. pm[i].set = 1;
  412. return;
  413. }
  414. badparm:
  415. file_errx(EXIT_FAILURE, "Unknown param %s", p);
  416. }
  417. private struct magic_set *
  418. /*ARGSUSED*/
  419. load(const char *magicfile, int flags)
  420. {
  421. struct magic_set *magic = magic_open(flags);
  422. const char *e;
  423. if (magic == NULL) {
  424. file_warn("Can't create magic");
  425. return NULL;
  426. }
  427. if (magic_load(magic, magicfile) == -1) {
  428. file_warn("%s", magic_error(magic));
  429. magic_close(magic);
  430. return NULL;
  431. }
  432. if ((e = magic_error(magic)) != NULL)
  433. file_warn("%s", e);
  434. return magic;
  435. }
  436. /*
  437. * unwrap -- read a file of filenames, do each one.
  438. */
  439. private int
  440. unwrap(struct magic_set *ms, const char *fn)
  441. {
  442. FILE *f;
  443. ssize_t len;
  444. char *line = NULL;
  445. size_t llen = 0;
  446. int wid = 0, cwid;
  447. int e = 0;
  448. if (strcmp("-", fn) == 0) {
  449. f = stdin;
  450. wid = 1;
  451. } else {
  452. if ((f = fopen(fn, "r")) == NULL) {
  453. file_warn("Cannot open `%s'", fn);
  454. return 1;
  455. }
  456. while ((len = getline(&line, &llen, f)) > 0) {
  457. if (line[len - 1] == '\n')
  458. line[len - 1] = '\0';
  459. cwid = file_mbswidth(line);
  460. if (cwid > wid)
  461. wid = cwid;
  462. }
  463. rewind(f);
  464. }
  465. while ((len = getline(&line, &llen, f)) > 0) {
  466. if (line[len - 1] == '\n')
  467. line[len - 1] = '\0';
  468. e |= process(ms, line, wid);
  469. if(nobuffer)
  470. (void)fflush(stdout);
  471. }
  472. free(line);
  473. (void)fclose(f);
  474. return e;
  475. }
  476. /*
  477. * Called for each input file on the command line (or in a list of files)
  478. */
  479. private int
  480. process(struct magic_set *ms, const char *inname, int wid)
  481. {
  482. const char *type, c = nulsep > 1 ? '\0' : '\n';
  483. int std_in = strcmp(inname, "-") == 0;
  484. if (wid > 0 && !bflag) {
  485. (void)printf("%s", std_in ? "/dev/stdin" : inname);
  486. if (nulsep)
  487. (void)putc('\0', stdout);
  488. if (nulsep < 2) {
  489. (void)printf("%s", separator);
  490. (void)printf("%*s ",
  491. (int) (nopad ? 0 : (wid - file_mbswidth(inname))),
  492. "");
  493. }
  494. }
  495. type = magic_file(ms, std_in ? NULL : inname);
  496. if (type == NULL) {
  497. (void)printf("ERROR: %s%c", magic_error(ms), c);
  498. return 1;
  499. } else {
  500. (void)printf("%s%c", type, c);
  501. return 0;
  502. }
  503. }
  504. protected size_t
  505. file_mbswidth(const char *s)
  506. {
  507. #if defined(HAVE_WCHAR_H) && defined(HAVE_MBRTOWC) && defined(HAVE_WCWIDTH)
  508. size_t bytesconsumed, old_n, n, width = 0;
  509. mbstate_t state;
  510. wchar_t nextchar;
  511. (void)memset(&state, 0, sizeof(mbstate_t));
  512. old_n = n = strlen(s);
  513. while (n > 0) {
  514. bytesconsumed = mbrtowc(&nextchar, s, n, &state);
  515. if (bytesconsumed == (size_t)(-1) ||
  516. bytesconsumed == (size_t)(-2)) {
  517. /* Something went wrong, return something reasonable */
  518. return old_n;
  519. }
  520. if (s[0] == '\n') {
  521. /*
  522. * do what strlen() would do, so that caller
  523. * is always right
  524. */
  525. width++;
  526. } else {
  527. int w = wcwidth(nextchar);
  528. if (w > 0)
  529. width += w;
  530. }
  531. s += bytesconsumed, n -= bytesconsumed;
  532. }
  533. return width;
  534. #else
  535. return strlen(s);
  536. #endif
  537. }
  538. private void
  539. usage(void)
  540. {
  541. const char *pn = file_getprogname();
  542. (void)fprintf(stderr, USAGE, pn, pn, pn);
  543. exit(EXIT_FAILURE);
  544. }
  545. private void
  546. defprint(int def)
  547. {
  548. if (!def)
  549. return;
  550. if (((def & 1) && posixly) || ((def & 2) && !posixly))
  551. fprintf(stdout, " (default)");
  552. fputc('\n', stdout);
  553. }
  554. private void
  555. docprint(const char *opts, int def)
  556. {
  557. size_t i;
  558. int comma;
  559. char *sp, *p;
  560. p = strstr(opts, "%o");
  561. if (p == NULL) {
  562. fprintf(stdout, "%s", opts);
  563. defprint(def);
  564. return;
  565. }
  566. for (sp = p - 1; sp > opts && *sp == ' '; sp--)
  567. continue;
  568. fprintf(stdout, "%.*s", (int)(p - opts), opts);
  569. comma = 0;
  570. for (i = 0; i < __arraycount(nv); i++) {
  571. fprintf(stdout, "%s%s", comma++ ? ", " : "", nv[i].name);
  572. if (i && i % 5 == 0 && i != __arraycount(nv) - 1) {
  573. fprintf(stdout, ",\n%*s", (int)(p - sp - 1), "");
  574. comma = 0;
  575. }
  576. }
  577. fprintf(stdout, "%s", opts + (p - opts) + 2);
  578. }
  579. private void
  580. help(void)
  581. {
  582. (void)fputs(
  583. "Usage: file [OPTION...] [FILE...]\n"
  584. "Determine type of FILEs.\n"
  585. "\n", stdout);
  586. #define OPT(shortname, longname, opt, def, doc) \
  587. fprintf(stdout, " -%c, --" longname, shortname), \
  588. docprint(doc, def);
  589. #define OPT_LONGONLY(longname, opt, def, doc, id) \
  590. fprintf(stdout, " --" longname), \
  591. docprint(doc, def);
  592. #include "file_opts.h"
  593. #undef OPT
  594. #undef OPT_LONGONLY
  595. fprintf(stdout, "\nReport bugs to https://bugs.astron.com/\n");
  596. exit(EXIT_SUCCESS);
  597. }
  598. private const char *file_progname;
  599. protected void
  600. file_setprogname(const char *progname)
  601. {
  602. file_progname = progname;
  603. }
  604. protected const char *
  605. file_getprogname(void)
  606. {
  607. return file_progname;
  608. }
  609. protected void
  610. file_err(int e, const char *fmt, ...)
  611. {
  612. va_list ap;
  613. int se = errno;
  614. va_start(ap, fmt);
  615. fprintf(stderr, "%s: ", file_progname);
  616. vfprintf(stderr, fmt, ap);
  617. va_end(ap);
  618. fprintf(stderr, " (%s)\n", strerror(se));
  619. exit(e);
  620. }
  621. protected void
  622. file_errx(int e, const char *fmt, ...)
  623. {
  624. va_list ap;
  625. va_start(ap, fmt);
  626. fprintf(stderr, "%s: ", file_progname);
  627. vfprintf(stderr, fmt, ap);
  628. va_end(ap);
  629. fprintf(stderr, "\n");
  630. exit(e);
  631. }
  632. protected void
  633. file_warn(const char *fmt, ...)
  634. {
  635. va_list ap;
  636. int se = errno;
  637. va_start(ap, fmt);
  638. fprintf(stderr, "%s: ", file_progname);
  639. vfprintf(stderr, fmt, ap);
  640. va_end(ap);
  641. fprintf(stderr, " (%s)\n", strerror(se));
  642. errno = se;
  643. }
  644. protected void
  645. file_warnx(const char *fmt, ...)
  646. {
  647. va_list ap;
  648. int se = errno;
  649. va_start(ap, fmt);
  650. fprintf(stderr, "%s: ", file_progname);
  651. vfprintf(stderr, fmt, ap);
  652. va_end(ap);
  653. fprintf(stderr, "\n");
  654. errno = se;
  655. }