xref: /titanic_50/usr/src/cmd/devprop/devprop.c (revision 843e19887f64dde75055cf8842fc4db2171eff45)
1*843e1988Sjohnlev /*
2*843e1988Sjohnlev  * CDDL HEADER START
3*843e1988Sjohnlev  *
4*843e1988Sjohnlev  * The contents of this file are subject to the terms of the
5*843e1988Sjohnlev  * Common Development and Distribution License (the "License").
6*843e1988Sjohnlev  * You may not use this file except in compliance with the License.
7*843e1988Sjohnlev  *
8*843e1988Sjohnlev  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*843e1988Sjohnlev  * or http://www.opensolaris.org/os/licensing.
10*843e1988Sjohnlev  * See the License for the specific language governing permissions
11*843e1988Sjohnlev  * and limitations under the License.
12*843e1988Sjohnlev  *
13*843e1988Sjohnlev  * When distributing Covered Code, include this CDDL HEADER in each
14*843e1988Sjohnlev  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*843e1988Sjohnlev  * If applicable, add the following below this CDDL HEADER, with the
16*843e1988Sjohnlev  * fields enclosed by brackets "[]" replaced with your own identifying
17*843e1988Sjohnlev  * information: Portions Copyright [yyyy] [name of copyright owner]
18*843e1988Sjohnlev  *
19*843e1988Sjohnlev  * CDDL HEADER END
20*843e1988Sjohnlev  */
21*843e1988Sjohnlev /*
22*843e1988Sjohnlev  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23*843e1988Sjohnlev  * Use is subject to license terms.
24*843e1988Sjohnlev  */
25*843e1988Sjohnlev 
26*843e1988Sjohnlev #pragma ident	"%Z%%M%	%I%	%E% SMI"
27*843e1988Sjohnlev 
28*843e1988Sjohnlev #include <stdio.h>
29*843e1988Sjohnlev #include <unistd.h>
30*843e1988Sjohnlev #include <strings.h>
31*843e1988Sjohnlev #include <libdevinfo.h>
32*843e1988Sjohnlev 
33*843e1988Sjohnlev static void
usage(void)34*843e1988Sjohnlev usage(void)
35*843e1988Sjohnlev {
36*843e1988Sjohnlev 	(void) fprintf(stderr,
37*843e1988Sjohnlev 	    "Usage: devprop [-n device-path] "
38*843e1988Sjohnlev 	    "[-vq] [-{b|i|l|s}] [property [...]]\n");
39*843e1988Sjohnlev }
40*843e1988Sjohnlev 
41*843e1988Sjohnlev int
main(int argc,char * argv[])42*843e1988Sjohnlev main(int argc, char *argv[])
43*843e1988Sjohnlev {
44*843e1988Sjohnlev 	int c;
45*843e1988Sjohnlev 	boolean_t verbose = B_FALSE, quote = B_FALSE,
46*843e1988Sjohnlev 	    error = B_FALSE;
47*843e1988Sjohnlev 	int type = DI_PROP_TYPE_UNKNOWN;
48*843e1988Sjohnlev 	char *path = "/";
49*843e1988Sjohnlev 	di_node_t dn;
50*843e1988Sjohnlev 	uchar_t *val_b;
51*843e1988Sjohnlev 	int *val_i;
52*843e1988Sjohnlev 	int64_t *val_l;
53*843e1988Sjohnlev 	char *val_s;
54*843e1988Sjohnlev 	int n;
55*843e1988Sjohnlev 
56*843e1988Sjohnlev 	extern char *optarg;
57*843e1988Sjohnlev 	extern int optind;
58*843e1988Sjohnlev 
59*843e1988Sjohnlev #define	BOOL(ch, var)				\
60*843e1988Sjohnlev case ch:					\
61*843e1988Sjohnlev 	var = B_TRUE;				\
62*843e1988Sjohnlev 	break
63*843e1988Sjohnlev 
64*843e1988Sjohnlev #define	PER_OPT(ch, typ)			\
65*843e1988Sjohnlev case ch:					\
66*843e1988Sjohnlev 	if (type != DI_PROP_TYPE_UNKNOWN) {	\
67*843e1988Sjohnlev 		usage();			\
68*843e1988Sjohnlev 		return (1);			\
69*843e1988Sjohnlev 	}					\
70*843e1988Sjohnlev 	type = (typ);				\
71*843e1988Sjohnlev 	break
72*843e1988Sjohnlev 
73*843e1988Sjohnlev 	while ((c = getopt(argc, argv, ":n:vqbils")) != -1) {
74*843e1988Sjohnlev 		switch (c) {
75*843e1988Sjohnlev 		case 'n':
76*843e1988Sjohnlev 			path = optarg;
77*843e1988Sjohnlev 			break;
78*843e1988Sjohnlev 		case ':':
79*843e1988Sjohnlev 			usage();
80*843e1988Sjohnlev 			return (1);
81*843e1988Sjohnlev 
82*843e1988Sjohnlev 		BOOL('v', verbose);
83*843e1988Sjohnlev 		BOOL('q', quote);
84*843e1988Sjohnlev 		BOOL('?', error);
85*843e1988Sjohnlev 
86*843e1988Sjohnlev 		PER_OPT('b', DI_PROP_TYPE_BYTE);
87*843e1988Sjohnlev 		PER_OPT('i', DI_PROP_TYPE_INT);
88*843e1988Sjohnlev 		PER_OPT('l', DI_PROP_TYPE_INT64);
89*843e1988Sjohnlev 		PER_OPT('s', DI_PROP_TYPE_STRING);
90*843e1988Sjohnlev 		}
91*843e1988Sjohnlev 	}
92*843e1988Sjohnlev 
93*843e1988Sjohnlev #undef	BOOL
94*843e1988Sjohnlev #undef	PER_OPT
95*843e1988Sjohnlev 
96*843e1988Sjohnlev 	if (error) {
97*843e1988Sjohnlev 		usage();
98*843e1988Sjohnlev 		return (1);
99*843e1988Sjohnlev 	}
100*843e1988Sjohnlev 
101*843e1988Sjohnlev 	/* default to strings */
102*843e1988Sjohnlev 	if (type == DI_PROP_TYPE_UNKNOWN)
103*843e1988Sjohnlev 		type = DI_PROP_TYPE_STRING;
104*843e1988Sjohnlev 
105*843e1988Sjohnlev 	/*
106*843e1988Sjohnlev 	 * It's convenient to use the filesystem as a source of device
107*843e1988Sjohnlev 	 * node paths.  In that case, the path will be prefixed with
108*843e1988Sjohnlev 	 * "/devices", which we strip off here as di_init() expects
109*843e1988Sjohnlev 	 * just the path to the node.
110*843e1988Sjohnlev 	 */
111*843e1988Sjohnlev 	if (strncmp("/devices/", path, strlen("/devices/")) == 0)
112*843e1988Sjohnlev 		path += strlen("/devices");
113*843e1988Sjohnlev 
114*843e1988Sjohnlev 	if ((dn = di_init(path, DINFOPROP)) == DI_NODE_NIL) {
115*843e1988Sjohnlev 		perror("di_init");
116*843e1988Sjohnlev 		return (1);
117*843e1988Sjohnlev 	}
118*843e1988Sjohnlev 
119*843e1988Sjohnlev 	/* Careful with that axe, Eugene... */
120*843e1988Sjohnlev #define	PER_TYPE(typ, func, val, incr, form, pv, sep)	\
121*843e1988Sjohnlev case (typ):						\
122*843e1988Sjohnlev 	n = func(DDI_DEV_T_ANY,				\
123*843e1988Sjohnlev 	    dn, argv[optind], &(val));			\
124*843e1988Sjohnlev 	while (n > 0) {					\
125*843e1988Sjohnlev 		(void) printf((form), pv);		\
126*843e1988Sjohnlev 		incr;					\
127*843e1988Sjohnlev 		n--;					\
128*843e1988Sjohnlev 		if (n > 0)				\
129*843e1988Sjohnlev 			(void) printf(sep);		\
130*843e1988Sjohnlev 	}						\
131*843e1988Sjohnlev 	(void) printf("\n");				\
132*843e1988Sjohnlev 	break
133*843e1988Sjohnlev 
134*843e1988Sjohnlev 	while (optind < argc) {
135*843e1988Sjohnlev 		if (verbose)
136*843e1988Sjohnlev 			(void) printf("%s=", argv[optind]);
137*843e1988Sjohnlev 
138*843e1988Sjohnlev 		switch (type) {
139*843e1988Sjohnlev 		PER_TYPE(DI_PROP_TYPE_BYTE, di_prop_lookup_bytes,
140*843e1988Sjohnlev 		    val_b, val_b++, "%2.2x", *val_b, ".");
141*843e1988Sjohnlev 		PER_TYPE(DI_PROP_TYPE_INT, di_prop_lookup_ints,
142*843e1988Sjohnlev 		    val_i, val_i++, "%8.8x", *val_i, ".");
143*843e1988Sjohnlev 		PER_TYPE(DI_PROP_TYPE_INT64, di_prop_lookup_int64,
144*843e1988Sjohnlev 		    val_l, val_l++, "%16.16llx", *val_l, ".");
145*843e1988Sjohnlev 		PER_TYPE(DI_PROP_TYPE_STRING, di_prop_lookup_strings,
146*843e1988Sjohnlev 		    val_s, val_s += strlen(val_s) + 1,
147*843e1988Sjohnlev 		    (quote ? "\"%s\"" : "%s"), val_s, " + ");
148*843e1988Sjohnlev 		}
149*843e1988Sjohnlev 
150*843e1988Sjohnlev 		optind++;
151*843e1988Sjohnlev 	}
152*843e1988Sjohnlev 
153*843e1988Sjohnlev #undef	PER_TYPE
154*843e1988Sjohnlev 
155*843e1988Sjohnlev 	di_fini(dn);
156*843e1988Sjohnlev 
157*843e1988Sjohnlev 	return (0);
158*843e1988Sjohnlev }
159