xref: /freebsd/contrib/libder/derdump/derdump.c (revision 35c0a8c449fd2b7f75029ebed5e10852240f0865)
1 /*-
2  * Copyright (c) 2024 Kyle Evans <kevans@FreeBSD.org>
3  *
4  * SPDX-License-Identifier: BSD-2-Clause
5  */
6 
7 #include <err.h>
8 #include <stdio.h>
9 
10 #include <libder.h>
11 
12 int
main(int argc,char * argv[])13 main(int argc, char *argv[])
14 {
15 	FILE *fp;
16 	struct libder_ctx *ctx;
17 	struct libder_object *root;
18 	size_t rootsz;
19 	bool first = true;
20 
21 	if (argc < 2) {
22 		fprintf(stderr, "usage: %s file [file...]\n", argv[0]);
23 		return (1);
24 	}
25 
26 	ctx = libder_open();
27 	libder_set_verbose(ctx, 2);
28 	for (int i = 1; i < argc; i++) {
29 		fp = fopen(argv[i], "rb");
30 		if (fp == NULL) {
31 			warn("%s", argv[i]);
32 			continue;
33 		}
34 
35 		if (!first)
36 			fprintf(stderr, "\n");
37 		fprintf(stdout, "[%s]\n", argv[i]);
38 		root = libder_read_file(ctx, fp, &rootsz);
39 		if (root != NULL) {
40 			libder_obj_dump(root, stdout);
41 			libder_obj_free(root);
42 			root = NULL;
43 		}
44 
45 		first = false;
46 		fclose(fp);
47 	}
48 
49 	libder_close(ctx);
50 
51 	return (0);
52 }
53