file.c 15 KB

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