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