Blob


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