file.c 16 KB

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