file.c 16 KB

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