xref: /illumos-gate/usr/src/cmd/smbsrv/nvlprint/nvlprint.c (revision 8d94f651a44d41a7147253bb5dad1a53941e8f50)
1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright 2017 Nexenta Systems, Inc.  All rights reserved.
14  */
15 
16 /*
17  * Print a packed nvlist from a file.
18  */
19 
20 #include <stdio.h>
21 #include <fcntl.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include "libnvpair.h"
25 
26 char buf[65536];
27 
28 void
dumpit(FILE * fp)29 dumpit(FILE *fp)
30 {
31 	struct stat st;
32 	size_t flen;
33 	int rlen;
34 	nvlist_t *nvl = NULL;
35 	int err;
36 
37 	if (fstat(fileno(fp), &st) < 0) {
38 		perror("fstat");
39 		return;
40 	}
41 	flen = (size_t)st.st_size;
42 	if (flen > sizeof (buf)) {
43 		(void) printf("File too large\n");
44 		return;
45 	}
46 	rlen = fread(buf, 1, flen, fp);
47 	if (rlen <= 0) {
48 		perror("fread");
49 		return;
50 	}
51 	if (rlen != flen) {
52 		(void) printf("Short read %d %d \n", rlen, flen);
53 		return;
54 	}
55 
56 	err = nvlist_unpack(buf, flen, &nvl, 0);
57 	if (err != 0) {
58 		(void) printf("nvlist_unpack, err=%d\n", err);
59 		return;
60 	}
61 
62 	nvlist_print(stdout, nvl);
63 	nvlist_free(nvl);
64 }
65 
66 int
main(int argc,char ** argv)67 main(int argc, char **argv)
68 {
69 	FILE *fp;
70 	int i;
71 
72 	if (argc < 2) {
73 		(void) fprintf(stderr, "usage: %s {filename} [filename2...]\n",
74 		    argv[0]);
75 		return (1);
76 	}
77 	for (i = 1; i < argc; i++) {
78 		fp = fopen(argv[i], "r");
79 		if (fp == NULL) {
80 			perror(argv[i]);
81 			return (1);
82 		}
83 		(void) printf("%s:\n", argv[i]);
84 		dumpit(fp);
85 		(void) fclose(fp);
86 	}
87 	return (0);
88 }
89