2 b2b17923 2022-12-17 op * Copyright (c) 2022 Omar Polo <op@openbsd.org>
4 2c02675e 2022-12-14 op * Permission to use, copy, modify, and distribute this software for any
5 2c02675e 2022-12-14 op * purpose with or without fee is hereby granted, provided that the above
6 2c02675e 2022-12-14 op * copyright notice and this permission notice appear in all copies.
8 2c02675e 2022-12-14 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 2c02675e 2022-12-14 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 2c02675e 2022-12-14 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 2c02675e 2022-12-14 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 2c02675e 2022-12-14 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 2c02675e 2022-12-14 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 2c02675e 2022-12-14 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 2c02675e 2022-12-14 op #include <err.h>
18 2c02675e 2022-12-14 op #include <stdio.h>
19 2c02675e 2022-12-14 op #include <stdlib.h>
20 2c02675e 2022-12-14 op #include <unistd.h>
22 2c02675e 2022-12-14 op int parse(FILE *, const char *);
26 2c02675e 2022-12-14 op static void __dead
29 754c4343 2023-03-04 op fprintf(stderr, "usage: %s [-G] [-o out] [file ...]\n", getprogname());
34 2c02675e 2022-12-14 op main(int argc, char **argv)
36 2c02675e 2022-12-14 op FILE *fp = stdout;
37 2c02675e 2022-12-14 op const char *out = NULL;
40 2c02675e 2022-12-14 op while ((ch = getopt(argc, argv, "Go:")) != -1) {
52 2c02675e 2022-12-14 op argc -= optind;
53 2c02675e 2022-12-14 op argv += optind;
55 2c02675e 2022-12-14 op if (out && (fp = fopen(out, "w")) == NULL)
56 2c02675e 2022-12-14 op err(1, "can't open %s", out);
58 2c02675e 2022-12-14 op if (out && unveil(out, "wc") == -1)
59 2c02675e 2022-12-14 op err(1, "unveil %s", out);
60 2c02675e 2022-12-14 op if (unveil("/", "r") == -1)
61 2c02675e 2022-12-14 op err(1, "unveil /");
62 2c02675e 2022-12-14 op if (pledge(out ? "stdio rpath cpath" : "stdio rpath", NULL) == -1)
63 2c02675e 2022-12-14 op err(1, "pledge");
65 2c02675e 2022-12-14 op if (argc == 0) {
67 2c02675e 2022-12-14 op if (parse(fp, "/dev/stdin") == -1)
70 2c02675e 2022-12-14 op for (i = 0; i < argc; ++i)
71 2c02675e 2022-12-14 op if (parse(fp, argv[i]) == -1)
75 2c02675e 2022-12-14 op if (ferror(fp))
78 2c02675e 2022-12-14 op if (fclose(fp) == -1) {
88 2c02675e 2022-12-14 op if (out && unlink(out) == -1)
89 2c02675e 2022-12-14 op err(1, "unlink %s", out);