2 96b8c570 2022-12-30 thomas * Copyright (c) 2022 Omar Polo <op@openbsd.org>
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.
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.
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>
21 cb11302c 2022-12-30 thomas #include "got_compat.h"
23 6509b181 2022-12-30 thomas int parse(FILE *, const char *);
25 6509b181 2022-12-30 thomas int nodebug;
27 6509b181 2022-12-30 thomas static void __dead
28 6509b181 2022-12-30 thomas usage(void)
30 8879c44a 2023-03-05 thomas fprintf(stderr, "usage: %s [-G] [-o out] [file ...]\n", getprogname());
35 6509b181 2022-12-30 thomas main(int argc, char **argv)
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;
41 6509b181 2022-12-30 thomas while ((ch = getopt(argc, argv, "Go:")) != -1) {
42 6509b181 2022-12-30 thomas switch (ch) {
44 6509b181 2022-12-30 thomas nodebug = 1;
47 6509b181 2022-12-30 thomas out = optarg;
53 6509b181 2022-12-30 thomas argc -= optind;
54 6509b181 2022-12-30 thomas argv += optind;
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);
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");
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)
71 6509b181 2022-12-30 thomas for (i = 0; i < argc; ++i)
72 6509b181 2022-12-30 thomas if (parse(fp, argv[i]) == -1)
76 6509b181 2022-12-30 thomas if (ferror(fp))
79 6509b181 2022-12-30 thomas if (fclose(fp) == -1) {
80 6509b181 2022-12-30 thomas fp = NULL;
84 6509b181 2022-12-30 thomas return (0);
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);