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