Blame


1 6509b181 2022-12-30 thomas /*
2 96b8c570 2022-12-30 thomas * Copyright (c) 2022 Omar Polo <op@openbsd.org>
3 6509b181 2022-12-30 thomas *
4 6509b181 2022-12-30 thomas * Permission to use, copy, modify, and distribute this software for any
5 6509b181 2022-12-30 thomas * purpose with or without fee is hereby granted, provided that the above
6 6509b181 2022-12-30 thomas * copyright notice and this permission notice appear in all copies.
7 6509b181 2022-12-30 thomas *
8 6509b181 2022-12-30 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 6509b181 2022-12-30 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 6509b181 2022-12-30 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 6509b181 2022-12-30 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 6509b181 2022-12-30 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 6509b181 2022-12-30 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 6509b181 2022-12-30 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 6509b181 2022-12-30 thomas */
16 6509b181 2022-12-30 thomas
17 6509b181 2022-12-30 thomas #include <err.h>
18 6509b181 2022-12-30 thomas #include <stdio.h>
19 6509b181 2022-12-30 thomas #include <stdlib.h>
20 6509b181 2022-12-30 thomas #include <unistd.h>
21 6509b181 2022-12-30 thomas
22 cb11302c 2022-12-30 thomas #include "got_compat.h"
23 cb11302c 2022-12-30 thomas
24 6509b181 2022-12-30 thomas int parse(FILE *, const char *);
25 6509b181 2022-12-30 thomas
26 6509b181 2022-12-30 thomas int nodebug;
27 6509b181 2022-12-30 thomas
28 6509b181 2022-12-30 thomas static void __dead
29 6509b181 2022-12-30 thomas usage(void)
30 6509b181 2022-12-30 thomas {
31 6509b181 2022-12-30 thomas fprintf(stderr, "usage: %s [file...]\n",
32 6509b181 2022-12-30 thomas getprogname());
33 6509b181 2022-12-30 thomas exit(1);
34 6509b181 2022-12-30 thomas }
35 6509b181 2022-12-30 thomas
36 6509b181 2022-12-30 thomas int
37 6509b181 2022-12-30 thomas main(int argc, char **argv)
38 6509b181 2022-12-30 thomas {
39 6509b181 2022-12-30 thomas FILE *fp = stdout;
40 6509b181 2022-12-30 thomas const char *out = NULL;
41 6509b181 2022-12-30 thomas int ch, i;
42 6509b181 2022-12-30 thomas
43 6509b181 2022-12-30 thomas while ((ch = getopt(argc, argv, "Go:")) != -1) {
44 6509b181 2022-12-30 thomas switch (ch) {
45 6509b181 2022-12-30 thomas case 'G':
46 6509b181 2022-12-30 thomas nodebug = 1;
47 6509b181 2022-12-30 thomas break;
48 6509b181 2022-12-30 thomas case 'o':
49 6509b181 2022-12-30 thomas out = optarg;
50 6509b181 2022-12-30 thomas break;
51 6509b181 2022-12-30 thomas default:
52 6509b181 2022-12-30 thomas usage();
53 6509b181 2022-12-30 thomas }
54 6509b181 2022-12-30 thomas }
55 6509b181 2022-12-30 thomas argc -= optind;
56 6509b181 2022-12-30 thomas argv += optind;
57 6509b181 2022-12-30 thomas
58 6509b181 2022-12-30 thomas if (out && (fp = fopen(out, "w")) == NULL)
59 6509b181 2022-12-30 thomas err(1, "can't open %s", out);
60 6509b181 2022-12-30 thomas
61 6509b181 2022-12-30 thomas if (out && unveil(out, "wc") == -1)
62 6509b181 2022-12-30 thomas err(1, "unveil %s", out);
63 6509b181 2022-12-30 thomas if (unveil("/", "r") == -1)
64 6509b181 2022-12-30 thomas err(1, "unveil /");
65 6509b181 2022-12-30 thomas if (pledge(out ? "stdio rpath cpath" : "stdio rpath", NULL) == -1)
66 6509b181 2022-12-30 thomas err(1, "pledge");
67 6509b181 2022-12-30 thomas
68 6509b181 2022-12-30 thomas if (argc == 0) {
69 6509b181 2022-12-30 thomas nodebug = 1;
70 6509b181 2022-12-30 thomas if (parse(fp, "/dev/stdin") == -1)
71 6509b181 2022-12-30 thomas goto err;
72 6509b181 2022-12-30 thomas } else {
73 6509b181 2022-12-30 thomas for (i = 0; i < argc; ++i)
74 6509b181 2022-12-30 thomas if (parse(fp, argv[i]) == -1)
75 6509b181 2022-12-30 thomas goto err;
76 6509b181 2022-12-30 thomas }
77 6509b181 2022-12-30 thomas
78 6509b181 2022-12-30 thomas if (ferror(fp))
79 6509b181 2022-12-30 thomas goto err;
80 6509b181 2022-12-30 thomas
81 6509b181 2022-12-30 thomas if (fclose(fp) == -1) {
82 6509b181 2022-12-30 thomas fp = NULL;
83 6509b181 2022-12-30 thomas goto err;
84 6509b181 2022-12-30 thomas }
85 6509b181 2022-12-30 thomas
86 6509b181 2022-12-30 thomas return (0);
87 6509b181 2022-12-30 thomas
88 6509b181 2022-12-30 thomas err:
89 6509b181 2022-12-30 thomas if (fp)
90 6509b181 2022-12-30 thomas fclose(fp);
91 6509b181 2022-12-30 thomas if (out && unlink(out) == -1)
92 6509b181 2022-12-30 thomas err(1, "unlink %s", out);
93 6509b181 2022-12-30 thomas return (1);
94 6509b181 2022-12-30 thomas }