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 <stdio.h>
18 6509b181 2022-12-30 thomas #include <stdlib.h>
19 6509b181 2022-12-30 thomas #include <unistd.h>
20 6509b181 2022-12-30 thomas
21 cb11302c 2022-12-30 thomas #include "got_compat.h"
22 cb11302c 2022-12-30 thomas
23 6509b181 2022-12-30 thomas int parse(FILE *, const char *);
24 6509b181 2022-12-30 thomas
25 6509b181 2022-12-30 thomas int nodebug;
26 6509b181 2022-12-30 thomas
27 6509b181 2022-12-30 thomas static void __dead
28 6509b181 2022-12-30 thomas usage(void)
29 6509b181 2022-12-30 thomas {
30 6509b181 2022-12-30 thomas fprintf(stderr, "usage: %s [file...]\n",
31 6509b181 2022-12-30 thomas getprogname());
32 6509b181 2022-12-30 thomas exit(1);
33 6509b181 2022-12-30 thomas }
34 6509b181 2022-12-30 thomas
35 6509b181 2022-12-30 thomas int
36 6509b181 2022-12-30 thomas main(int argc, char **argv)
37 6509b181 2022-12-30 thomas {
38 6509b181 2022-12-30 thomas FILE *fp = stdout;
39 6509b181 2022-12-30 thomas const char *out = NULL;
40 6509b181 2022-12-30 thomas int ch, i;
41 6509b181 2022-12-30 thomas
42 6509b181 2022-12-30 thomas while ((ch = getopt(argc, argv, "Go:")) != -1) {
43 6509b181 2022-12-30 thomas switch (ch) {
44 6509b181 2022-12-30 thomas case 'G':
45 6509b181 2022-12-30 thomas nodebug = 1;
46 6509b181 2022-12-30 thomas break;
47 6509b181 2022-12-30 thomas case 'o':
48 6509b181 2022-12-30 thomas out = optarg;
49 6509b181 2022-12-30 thomas break;
50 6509b181 2022-12-30 thomas default:
51 6509b181 2022-12-30 thomas usage();
52 6509b181 2022-12-30 thomas }
53 6509b181 2022-12-30 thomas }
54 6509b181 2022-12-30 thomas argc -= optind;
55 6509b181 2022-12-30 thomas argv += optind;
56 6509b181 2022-12-30 thomas
57 6509b181 2022-12-30 thomas if (out && (fp = fopen(out, "w")) == NULL)
58 6509b181 2022-12-30 thomas err(1, "can't open %s", out);
59 6509b181 2022-12-30 thomas
60 6509b181 2022-12-30 thomas if (out && unveil(out, "wc") == -1)
61 6509b181 2022-12-30 thomas err(1, "unveil %s", out);
62 6509b181 2022-12-30 thomas if (unveil("/", "r") == -1)
63 6509b181 2022-12-30 thomas err(1, "unveil /");
64 6509b181 2022-12-30 thomas if (pledge(out ? "stdio rpath cpath" : "stdio rpath", NULL) == -1)
65 6509b181 2022-12-30 thomas err(1, "pledge");
66 6509b181 2022-12-30 thomas
67 6509b181 2022-12-30 thomas if (argc == 0) {
68 6509b181 2022-12-30 thomas nodebug = 1;
69 6509b181 2022-12-30 thomas if (parse(fp, "/dev/stdin") == -1)
70 6509b181 2022-12-30 thomas goto err;
71 6509b181 2022-12-30 thomas } else {
72 6509b181 2022-12-30 thomas for (i = 0; i < argc; ++i)
73 6509b181 2022-12-30 thomas if (parse(fp, argv[i]) == -1)
74 6509b181 2022-12-30 thomas goto err;
75 6509b181 2022-12-30 thomas }
76 6509b181 2022-12-30 thomas
77 6509b181 2022-12-30 thomas if (ferror(fp))
78 6509b181 2022-12-30 thomas goto err;
79 6509b181 2022-12-30 thomas
80 6509b181 2022-12-30 thomas if (fclose(fp) == -1) {
81 6509b181 2022-12-30 thomas fp = NULL;
82 6509b181 2022-12-30 thomas goto err;
83 6509b181 2022-12-30 thomas }
84 6509b181 2022-12-30 thomas
85 6509b181 2022-12-30 thomas return (0);
86 6509b181 2022-12-30 thomas
87 6509b181 2022-12-30 thomas err:
88 6509b181 2022-12-30 thomas if (fp)
89 6509b181 2022-12-30 thomas fclose(fp);
90 6509b181 2022-12-30 thomas if (out && unlink(out) == -1)
91 6509b181 2022-12-30 thomas err(1, "unlink %s", out);
92 6509b181 2022-12-30 thomas return (1);
93 6509b181 2022-12-30 thomas }