file.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  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.184 2019/08/03 11:51:59 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. #define FILE_FLAGS "bcCdE" IFLNK_h "ik" IFLNK_L "lNnprsSvzZ0"
  73. #define OPTSTRING "bcCde:Ef:F:hiklLm:nNpP:rsSvzZ0"
  74. # define USAGE \
  75. "Usage: %s [-" FILE_FLAGS "] [--apple] [--extension] [--mime-encoding]\n" \
  76. " [--mime-type] [-e <testname>] [-F <separator>] " \
  77. " [-f <namefile>]\n" \
  78. " [-m <magicfiles>] [-P <parameter=value>] <file> ...\n" \
  79. " %s -C [-m <magicfiles>]\n" \
  80. " %s [--help]\n"
  81. private int /* Global command-line options */
  82. bflag = 0, /* brief output format */
  83. nopad = 0, /* Don't pad output */
  84. nobuffer = 0, /* Do not buffer stdout */
  85. nulsep = 0; /* Append '\0' to the separator */
  86. private const char *separator = ":"; /* Default field separator */
  87. private const struct option long_options[] = {
  88. #define OPT_HELP 1
  89. #define OPT_APPLE 2
  90. #define OPT_EXTENSIONS 3
  91. #define OPT_MIME_TYPE 4
  92. #define OPT_MIME_ENCODING 5
  93. #define OPT(shortname, longname, opt, def, doc) \
  94. {longname, opt, NULL, shortname},
  95. #define OPT_LONGONLY(longname, opt, def, doc, id) \
  96. {longname, opt, NULL, id},
  97. #include "file_opts.h"
  98. #undef OPT
  99. #undef OPT_LONGONLY
  100. {0, 0, NULL, 0}
  101. };
  102. private const struct {
  103. const char *name;
  104. int value;
  105. } nv[] = {
  106. { "apptype", MAGIC_NO_CHECK_APPTYPE },
  107. { "ascii", MAGIC_NO_CHECK_ASCII },
  108. { "cdf", MAGIC_NO_CHECK_CDF },
  109. { "compress", MAGIC_NO_CHECK_COMPRESS },
  110. { "csv", MAGIC_NO_CHECK_CSV },
  111. { "elf", MAGIC_NO_CHECK_ELF },
  112. { "encoding", MAGIC_NO_CHECK_ENCODING },
  113. { "soft", MAGIC_NO_CHECK_SOFT },
  114. { "tar", MAGIC_NO_CHECK_TAR },
  115. { "json", MAGIC_NO_CHECK_JSON },
  116. { "text", MAGIC_NO_CHECK_TEXT }, /* synonym for ascii */
  117. { "tokens", MAGIC_NO_CHECK_TOKENS }, /* OBSOLETE: ignored for backwards compatibility */
  118. };
  119. private struct {
  120. const char *name;
  121. int tag;
  122. size_t value;
  123. int set;
  124. } pm[] = {
  125. { "indir", MAGIC_PARAM_INDIR_MAX, 0, 0 },
  126. { "name", MAGIC_PARAM_NAME_MAX, 0, 0 },
  127. { "elf_phnum", MAGIC_PARAM_ELF_PHNUM_MAX, 0, 0 },
  128. { "elf_shnum", MAGIC_PARAM_ELF_SHNUM_MAX, 0, 0 },
  129. { "elf_notes", MAGIC_PARAM_ELF_NOTES_MAX, 0, 0 },
  130. { "regex", MAGIC_PARAM_REGEX_MAX, 0, 0 },
  131. { "bytes", MAGIC_PARAM_BYTES_MAX, 0, 0 },
  132. };
  133. private int posixly;
  134. #ifdef __dead
  135. __dead
  136. #endif
  137. private void usage(void);
  138. private void docprint(const char *, int);
  139. #ifdef __dead
  140. __dead
  141. #endif
  142. private void help(void);
  143. private int unwrap(struct magic_set *, const char *);
  144. private int process(struct magic_set *ms, const char *, int);
  145. private struct magic_set *load(const char *, int);
  146. private void setparam(const char *);
  147. private void applyparam(magic_t);
  148. /*
  149. * main - parse arguments and handle options
  150. */
  151. int
  152. main(int argc, char *argv[])
  153. {
  154. int c;
  155. size_t i;
  156. int action = 0, didsomefiles = 0, errflg = 0;
  157. int flags = 0, e = 0;
  158. #ifdef HAVE_LIBSECCOMP
  159. int sandbox = 1;
  160. #endif
  161. struct magic_set *magic = NULL;
  162. int longindex;
  163. const char *magicfile = NULL; /* where the magic is */
  164. char *progname;
  165. /* makes islower etc work for other langs */
  166. (void)setlocale(LC_CTYPE, "");
  167. #ifdef __EMX__
  168. /* sh-like wildcard expansion! Shouldn't hurt at least ... */
  169. _wildcard(&argc, &argv);
  170. #endif
  171. if ((progname = strrchr(argv[0], '/')) != NULL)
  172. progname++;
  173. else
  174. progname = argv[0];
  175. file_setprogname(progname);
  176. #ifdef S_IFLNK
  177. posixly = getenv("POSIXLY_CORRECT") != NULL;
  178. flags |= posixly ? MAGIC_SYMLINK : 0;
  179. #endif
  180. while ((c = getopt_long(argc, argv, OPTSTRING, long_options,
  181. &longindex)) != -1)
  182. switch (c) {
  183. case OPT_HELP:
  184. help();
  185. break;
  186. case OPT_APPLE:
  187. flags |= MAGIC_APPLE;
  188. break;
  189. case OPT_EXTENSIONS:
  190. flags |= MAGIC_EXTENSION;
  191. break;
  192. case OPT_MIME_TYPE:
  193. flags |= MAGIC_MIME_TYPE;
  194. break;
  195. case OPT_MIME_ENCODING:
  196. flags |= MAGIC_MIME_ENCODING;
  197. break;
  198. case '0':
  199. nulsep++;
  200. break;
  201. case 'b':
  202. bflag++;
  203. break;
  204. case 'c':
  205. action = FILE_CHECK;
  206. break;
  207. case 'C':
  208. action = FILE_COMPILE;
  209. break;
  210. case 'd':
  211. flags |= MAGIC_DEBUG|MAGIC_CHECK;
  212. break;
  213. case 'E':
  214. flags |= MAGIC_ERROR;
  215. break;
  216. case 'e':
  217. for (i = 0; i < __arraycount(nv); i++)
  218. if (strcmp(nv[i].name, optarg) == 0)
  219. break;
  220. if (i == __arraycount(nv))
  221. errflg++;
  222. else
  223. flags |= nv[i].value;
  224. break;
  225. case 'f':
  226. if(action)
  227. usage();
  228. if (magic == NULL)
  229. if ((magic = load(magicfile, flags)) == NULL)
  230. return 1;
  231. applyparam(magic);
  232. e |= unwrap(magic, optarg);
  233. ++didsomefiles;
  234. break;
  235. case 'F':
  236. separator = optarg;
  237. break;
  238. case 'i':
  239. flags |= MAGIC_MIME;
  240. break;
  241. case 'k':
  242. flags |= MAGIC_CONTINUE;
  243. break;
  244. case 'l':
  245. action = FILE_LIST;
  246. break;
  247. case 'm':
  248. magicfile = optarg;
  249. break;
  250. case 'n':
  251. ++nobuffer;
  252. break;
  253. case 'N':
  254. ++nopad;
  255. break;
  256. #if defined(HAVE_UTIME) || defined(HAVE_UTIMES)
  257. case 'p':
  258. flags |= MAGIC_PRESERVE_ATIME;
  259. break;
  260. #endif
  261. case 'P':
  262. setparam(optarg);
  263. break;
  264. case 'r':
  265. flags |= MAGIC_RAW;
  266. break;
  267. case 's':
  268. flags |= MAGIC_DEVICES;
  269. break;
  270. case 'S':
  271. #ifdef HAVE_LIBSECCOMP
  272. sandbox = 0;
  273. #endif
  274. break;
  275. case 'v':
  276. if (magicfile == NULL)
  277. magicfile = magic_getpath(magicfile, action);
  278. (void)fprintf(stdout, "%s-%s\n", file_getprogname(),
  279. VERSION);
  280. (void)fprintf(stdout, "magic file from %s\n",
  281. magicfile);
  282. #ifdef HAVE_LIBSECCOMP
  283. (void)fprintf(stdout, "seccomp support included\n");
  284. #endif
  285. return 0;
  286. case 'z':
  287. flags |= MAGIC_COMPRESS;
  288. break;
  289. case 'Z':
  290. flags |= MAGIC_COMPRESS|MAGIC_COMPRESS_TRANSP;
  291. break;
  292. #ifdef S_IFLNK
  293. case 'L':
  294. flags |= MAGIC_SYMLINK;
  295. break;
  296. case 'h':
  297. flags &= ~MAGIC_SYMLINK;
  298. break;
  299. #endif
  300. case '?':
  301. default:
  302. errflg++;
  303. break;
  304. }
  305. if (errflg) {
  306. usage();
  307. }
  308. if (e)
  309. return e;
  310. #ifdef HAVE_LIBSECCOMP
  311. #if 0
  312. if (sandbox && enable_sandbox_basic() == -1)
  313. #else
  314. if (sandbox && enable_sandbox_full() == -1)
  315. #endif
  316. file_err(EXIT_FAILURE, "SECCOMP initialisation failed");
  317. #endif /* HAVE_LIBSECCOMP */
  318. if (MAGIC_VERSION != magic_version())
  319. file_warnx("Compiled magic version [%d] "
  320. "does not match with shared library magic version [%d]\n",
  321. MAGIC_VERSION, magic_version());
  322. switch(action) {
  323. case FILE_CHECK:
  324. case FILE_COMPILE:
  325. case FILE_LIST:
  326. /*
  327. * Don't try to check/compile ~/.magic unless we explicitly
  328. * ask for it.
  329. */
  330. magic = magic_open(flags|MAGIC_CHECK);
  331. if (magic == NULL) {
  332. file_warn("Can't create magic");
  333. return 1;
  334. }
  335. switch(action) {
  336. case FILE_CHECK:
  337. c = magic_check(magic, magicfile);
  338. break;
  339. case FILE_COMPILE:
  340. c = magic_compile(magic, magicfile);
  341. break;
  342. case FILE_LIST:
  343. c = magic_list(magic, magicfile);
  344. break;
  345. default:
  346. abort();
  347. }
  348. if (c == -1) {
  349. file_warnx("%s", magic_error(magic));
  350. e = 1;
  351. goto out;
  352. }
  353. goto out;
  354. default:
  355. if (magic == NULL)
  356. if ((magic = load(magicfile, flags)) == NULL)
  357. return 1;
  358. applyparam(magic);
  359. }
  360. if (optind == argc) {
  361. if (!didsomefiles)
  362. usage();
  363. }
  364. else {
  365. size_t j, wid, nw;
  366. for (wid = 0, j = CAST(size_t, optind); j < CAST(size_t, argc);
  367. 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].set)
  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. pm[i].set = 1;
  410. return;
  411. }
  412. badparm:
  413. file_errx(EXIT_FAILURE, "Unknown param %s", p);
  414. }
  415. private struct magic_set *
  416. /*ARGSUSED*/
  417. load(const char *magicfile, int flags)
  418. {
  419. struct magic_set *magic = magic_open(flags);
  420. const char *e;
  421. if (magic == NULL) {
  422. file_warn("Can't create magic");
  423. return NULL;
  424. }
  425. if (magic_load(magic, magicfile) == -1) {
  426. file_warn("%s", magic_error(magic));
  427. magic_close(magic);
  428. return NULL;
  429. }
  430. if ((e = magic_error(magic)) != NULL)
  431. file_warn("%s", e);
  432. return magic;
  433. }
  434. /*
  435. * unwrap -- read a file of filenames, do each one.
  436. */
  437. private int
  438. unwrap(struct magic_set *ms, const char *fn)
  439. {
  440. FILE *f;
  441. ssize_t len;
  442. char *line = NULL;
  443. size_t llen = 0;
  444. int wid = 0, cwid;
  445. int e = 0;
  446. if (strcmp("-", fn) == 0) {
  447. f = stdin;
  448. wid = 1;
  449. } else {
  450. if ((f = fopen(fn, "r")) == NULL) {
  451. file_warn("Cannot open `%s'", fn);
  452. return 1;
  453. }
  454. while ((len = getline(&line, &llen, f)) > 0) {
  455. if (line[len - 1] == '\n')
  456. line[len - 1] = '\0';
  457. cwid = file_mbswidth(line);
  458. if (cwid > wid)
  459. wid = cwid;
  460. }
  461. rewind(f);
  462. }
  463. while ((len = getline(&line, &llen, f)) > 0) {
  464. if (line[len - 1] == '\n')
  465. line[len - 1] = '\0';
  466. e |= process(ms, line, wid);
  467. if(nobuffer)
  468. (void)fflush(stdout);
  469. }
  470. free(line);
  471. (void)fclose(f);
  472. return e;
  473. }
  474. /*
  475. * Called for each input file on the command line (or in a list of files)
  476. */
  477. private int
  478. process(struct magic_set *ms, const char *inname, int wid)
  479. {
  480. const char *type, c = nulsep > 1 ? '\0' : '\n';
  481. int std_in = strcmp(inname, "-") == 0;
  482. if (wid > 0 && !bflag) {
  483. (void)printf("%s", std_in ? "/dev/stdin" : inname);
  484. if (nulsep)
  485. (void)putc('\0', stdout);
  486. if (nulsep < 2) {
  487. (void)printf("%s", separator);
  488. (void)printf("%*s ", CAST(int, nopad ? 0
  489. : (wid - file_mbswidth(inname))), "");
  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 == CAST(size_t, -1) ||
  513. bytesconsumed == CAST(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", CAST(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 && i != __arraycount(nv) - 1) {
  570. fprintf(stdout, ",\n%*s", CAST(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 https://bugs.astron.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. }