file.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  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.181 2019/03/28 20:54:03 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. (void)setlocale(LC_CTYPE, "");
  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 < __arraycount(nv); i++)
  222. if (strcmp(nv[i].name, optarg) == 0)
  223. break;
  224. if (i == __arraycount(nv))
  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 = CAST(size_t, optind); j < CAST(size_t, argc);
  368. j++) {
  369. nw = file_mbswidth(argv[j]);
  370. if (nw > wid)
  371. wid = nw;
  372. }
  373. /*
  374. * If bflag is only set twice, set it depending on
  375. * number of files [this is undocumented, and subject to change]
  376. */
  377. if (bflag == 2) {
  378. bflag = optind >= argc - 1;
  379. }
  380. for (; optind < argc; optind++)
  381. e |= process(magic, argv[optind], wid);
  382. }
  383. out:
  384. if (magic)
  385. magic_close(magic);
  386. return e;
  387. }
  388. private void
  389. applyparam(magic_t magic)
  390. {
  391. size_t i;
  392. for (i = 0; i < __arraycount(pm); i++) {
  393. if (!pm[i].set)
  394. continue;
  395. if (magic_setparam(magic, pm[i].tag, &pm[i].value) == -1)
  396. file_err(EXIT_FAILURE, "Can't set %s", pm[i].name);
  397. }
  398. }
  399. private void
  400. setparam(const char *p)
  401. {
  402. size_t i;
  403. char *s;
  404. if ((s = strchr(p, '=')) == NULL)
  405. goto badparm;
  406. for (i = 0; i < __arraycount(pm); i++) {
  407. if (strncmp(p, pm[i].name, s - p) != 0)
  408. continue;
  409. pm[i].value = atoi(s + 1);
  410. pm[i].set = 1;
  411. return;
  412. }
  413. badparm:
  414. file_errx(EXIT_FAILURE, "Unknown param %s", p);
  415. }
  416. private struct magic_set *
  417. /*ARGSUSED*/
  418. load(const char *magicfile, int flags)
  419. {
  420. struct magic_set *magic = magic_open(flags);
  421. const char *e;
  422. if (magic == NULL) {
  423. file_warn("Can't create magic");
  424. return NULL;
  425. }
  426. if (magic_load(magic, magicfile) == -1) {
  427. file_warn("%s", magic_error(magic));
  428. magic_close(magic);
  429. return NULL;
  430. }
  431. if ((e = magic_error(magic)) != NULL)
  432. file_warn("%s", e);
  433. return magic;
  434. }
  435. /*
  436. * unwrap -- read a file of filenames, do each one.
  437. */
  438. private int
  439. unwrap(struct magic_set *ms, const char *fn)
  440. {
  441. FILE *f;
  442. ssize_t len;
  443. char *line = NULL;
  444. size_t llen = 0;
  445. int wid = 0, cwid;
  446. int e = 0;
  447. if (strcmp("-", fn) == 0) {
  448. f = stdin;
  449. wid = 1;
  450. } else {
  451. if ((f = fopen(fn, "r")) == NULL) {
  452. file_warn("Cannot open `%s'", fn);
  453. return 1;
  454. }
  455. while ((len = getline(&line, &llen, f)) > 0) {
  456. if (line[len - 1] == '\n')
  457. line[len - 1] = '\0';
  458. cwid = file_mbswidth(line);
  459. if (cwid > wid)
  460. wid = cwid;
  461. }
  462. rewind(f);
  463. }
  464. while ((len = getline(&line, &llen, f)) > 0) {
  465. if (line[len - 1] == '\n')
  466. line[len - 1] = '\0';
  467. e |= process(ms, line, wid);
  468. if(nobuffer)
  469. (void)fflush(stdout);
  470. }
  471. free(line);
  472. (void)fclose(f);
  473. return e;
  474. }
  475. /*
  476. * Called for each input file on the command line (or in a list of files)
  477. */
  478. private int
  479. process(struct magic_set *ms, const char *inname, int wid)
  480. {
  481. const char *type, c = nulsep > 1 ? '\0' : '\n';
  482. int std_in = strcmp(inname, "-") == 0;
  483. if (wid > 0 && !bflag) {
  484. (void)printf("%s", std_in ? "/dev/stdin" : inname);
  485. if (nulsep)
  486. (void)putc('\0', stdout);
  487. if (nulsep < 2) {
  488. (void)printf("%s", separator);
  489. (void)printf("%*s ", CAST(int, nopad ? 0
  490. : (wid - file_mbswidth(inname))), "");
  491. }
  492. }
  493. type = magic_file(ms, std_in ? NULL : inname);
  494. if (type == NULL) {
  495. (void)printf("ERROR: %s%c", magic_error(ms), c);
  496. return 1;
  497. } else {
  498. (void)printf("%s%c", type, c);
  499. return 0;
  500. }
  501. }
  502. protected size_t
  503. file_mbswidth(const char *s)
  504. {
  505. #if defined(HAVE_WCHAR_H) && defined(HAVE_MBRTOWC) && defined(HAVE_WCWIDTH)
  506. size_t bytesconsumed, old_n, n, width = 0;
  507. mbstate_t state;
  508. wchar_t nextchar;
  509. (void)memset(&state, 0, sizeof(mbstate_t));
  510. old_n = n = strlen(s);
  511. while (n > 0) {
  512. bytesconsumed = mbrtowc(&nextchar, s, n, &state);
  513. if (bytesconsumed == CAST(size_t, -1) ||
  514. bytesconsumed == CAST(size_t, -2)) {
  515. /* Something went wrong, return something reasonable */
  516. return old_n;
  517. }
  518. if (s[0] == '\n') {
  519. /*
  520. * do what strlen() would do, so that caller
  521. * is always right
  522. */
  523. width++;
  524. } else {
  525. int w = wcwidth(nextchar);
  526. if (w > 0)
  527. width += w;
  528. }
  529. s += bytesconsumed, n -= bytesconsumed;
  530. }
  531. return width;
  532. #else
  533. return strlen(s);
  534. #endif
  535. }
  536. private void
  537. usage(void)
  538. {
  539. const char *pn = file_getprogname();
  540. (void)fprintf(stderr, USAGE, pn, pn, pn);
  541. exit(EXIT_FAILURE);
  542. }
  543. private void
  544. defprint(int def)
  545. {
  546. if (!def)
  547. return;
  548. if (((def & 1) && posixly) || ((def & 2) && !posixly))
  549. fprintf(stdout, " (default)");
  550. fputc('\n', stdout);
  551. }
  552. private void
  553. docprint(const char *opts, int def)
  554. {
  555. size_t i;
  556. int comma;
  557. char *sp, *p;
  558. p = strstr(opts, "%o");
  559. if (p == NULL) {
  560. fprintf(stdout, "%s", opts);
  561. defprint(def);
  562. return;
  563. }
  564. for (sp = p - 1; sp > opts && *sp == ' '; sp--)
  565. continue;
  566. fprintf(stdout, "%.*s", CAST(int, p - opts), opts);
  567. comma = 0;
  568. for (i = 0; i < __arraycount(nv); i++) {
  569. fprintf(stdout, "%s%s", comma++ ? ", " : "", nv[i].name);
  570. if (i && i % 5 == 0 && i != __arraycount(nv) - 1) {
  571. fprintf(stdout, ",\n%*s", CAST(int, p - sp - 1), "");
  572. comma = 0;
  573. }
  574. }
  575. fprintf(stdout, "%s", opts + (p - opts) + 2);
  576. }
  577. private void
  578. help(void)
  579. {
  580. (void)fputs(
  581. "Usage: file [OPTION...] [FILE...]\n"
  582. "Determine type of FILEs.\n"
  583. "\n", stdout);
  584. #define OPT(shortname, longname, opt, def, doc) \
  585. fprintf(stdout, " -%c, --" longname, shortname), \
  586. docprint(doc, def);
  587. #define OPT_LONGONLY(longname, opt, def, doc, id) \
  588. fprintf(stdout, " --" longname), \
  589. docprint(doc, def);
  590. #include "file_opts.h"
  591. #undef OPT
  592. #undef OPT_LONGONLY
  593. fprintf(stdout, "\nReport bugs to https://bugs.astron.com/\n");
  594. exit(EXIT_SUCCESS);
  595. }
  596. private const char *file_progname;
  597. protected void
  598. file_setprogname(const char *progname)
  599. {
  600. file_progname = progname;
  601. }
  602. protected const char *
  603. file_getprogname(void)
  604. {
  605. return file_progname;
  606. }
  607. protected void
  608. file_err(int e, const char *fmt, ...)
  609. {
  610. va_list ap;
  611. int se = errno;
  612. va_start(ap, fmt);
  613. fprintf(stderr, "%s: ", file_progname);
  614. vfprintf(stderr, fmt, ap);
  615. va_end(ap);
  616. fprintf(stderr, " (%s)\n", strerror(se));
  617. exit(e);
  618. }
  619. protected void
  620. file_errx(int e, const char *fmt, ...)
  621. {
  622. va_list ap;
  623. va_start(ap, fmt);
  624. fprintf(stderr, "%s: ", file_progname);
  625. vfprintf(stderr, fmt, ap);
  626. va_end(ap);
  627. fprintf(stderr, "\n");
  628. exit(e);
  629. }
  630. protected void
  631. file_warn(const char *fmt, ...)
  632. {
  633. va_list ap;
  634. int se = errno;
  635. va_start(ap, fmt);
  636. fprintf(stderr, "%s: ", file_progname);
  637. vfprintf(stderr, fmt, ap);
  638. va_end(ap);
  639. fprintf(stderr, " (%s)\n", strerror(se));
  640. errno = se;
  641. }
  642. protected void
  643. file_warnx(const char *fmt, ...)
  644. {
  645. va_list ap;
  646. int se = errno;
  647. va_start(ap, fmt);
  648. fprintf(stderr, "%s: ", file_progname);
  649. vfprintf(stderr, fmt, ap);
  650. va_end(ap);
  651. fprintf(stderr, "\n");
  652. errno = se;
  653. }