1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*0Sstevel@tonic-gate 
25*0Sstevel@tonic-gate 
26*0Sstevel@tonic-gate #ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.5	*/
27*0Sstevel@tonic-gate /*
28*0Sstevel@tonic-gate  * Streams Command strconf:	display the configuration of the
29*0Sstevel@tonic-gate  *				stream associated with stdin.
30*0Sstevel@tonic-gate  *
31*0Sstevel@tonic-gate  * USAGE:	strconf
32*0Sstevel@tonic-gate  *    or:	strconf -m module
33*0Sstevel@tonic-gate  *    or:	strconf -t
34*0Sstevel@tonic-gate  *
35*0Sstevel@tonic-gate  * strconf with no options lists the modules on the stream.
36*0Sstevel@tonic-gate  * -m module	echos "yes" and returns 0 if the module is on the stream.
37*0Sstevel@tonic-gate  *		echos "no" and returns 2 if not.
38*0Sstevel@tonic-gate  * -t		lists only the topmost module.  returns 0 if there is a
39*0Sstevel@tonic-gate  *		module, 2 if not.
40*0Sstevel@tonic-gate  *
41*0Sstevel@tonic-gate  * RETURNS:
42*0Sstevel@tonic-gate  *	0	SUCCESS		it works
43*0Sstevel@tonic-gate  *	1	ERR_USAGE	bad invocation
44*0Sstevel@tonic-gate  *	2	ERR_MODULE	module not there
45*0Sstevel@tonic-gate  *	3	ERR_STDIN	an ioctl on the stdin stream failed
46*0Sstevel@tonic-gate  *	4	ERR_MEM		couldn't allocate memory
47*0Sstevel@tonic-gate  */
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate #include	<stdio.h>
50*0Sstevel@tonic-gate #include	<sys/stropts.h>
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate #define	TRUE		 1
53*0Sstevel@tonic-gate #define FALSE		 0
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate #define	OPTLIST		"m:t"
56*0Sstevel@tonic-gate #define USAGE		"USAGE: %s [ -m module | -t ]\n"
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate #define SUCCESS		0
59*0Sstevel@tonic-gate #define FAILURE		1
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate #define	ERR_USAGE	1	/* bad invocation			*/
62*0Sstevel@tonic-gate #define	ERR_MODULE	2	/* module not there			*/
63*0Sstevel@tonic-gate #define	ERR_STDIN	3	/* an ioctl on the stdin stream failed	*/
64*0Sstevel@tonic-gate #define	ERR_MEM		4	/* couldn't allocate memory		*/
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate #define	NMODULES	16	/* "reasonable" # of modules on a stream  */
67*0Sstevel@tonic-gate 				/* 	(there can be more)		  */
68*0Sstevel@tonic-gate #define	MAXMODULES	2048	/* max # of modules			  */
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate #define	STDIN		0
71*0Sstevel@tonic-gate #define	SAME		0	/* return from str[n]cmp if match	*/
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate static char		 *Cmd_namep;		/* how was it invoked?	*/
75*0Sstevel@tonic-gate 
76*0Sstevel@tonic-gate 
77*0Sstevel@tonic-gate extern char	*strcpy();
78*0Sstevel@tonic-gate extern int	getopt();
79*0Sstevel@tonic-gate extern int	ioctl();
80*0Sstevel@tonic-gate extern int	strncmp();
81*0Sstevel@tonic-gate 
82*0Sstevel@tonic-gate static int	more_modules();	/* increase size of mod list		*/
83*0Sstevel@tonic-gate 
84*0Sstevel@tonic-gate main( argc, argv)
85*0Sstevel@tonic-gate int	argc;
86*0Sstevel@tonic-gate char	*argv[];
87*0Sstevel@tonic-gate {
88*0Sstevel@tonic-gate 	char		*modp;		/* ptr to module name		*/
89*0Sstevel@tonic-gate 	register int	i;		/* loop var & junk (what else?)	*/
90*0Sstevel@tonic-gate 	short		mod_present;	/* TRUE if -m module		*/
91*0Sstevel@tonic-gate 	short		topmost;	/* TRUE if -t			*/
92*0Sstevel@tonic-gate 	struct str_mlist
93*0Sstevel@tonic-gate 			mlist[NMODULES];/* modlist for strlist		*/
94*0Sstevel@tonic-gate 	struct str_list	strlist;	/* mods on stream		*/
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate 	extern char	*optarg;	/* for getopt()			*/
97*0Sstevel@tonic-gate 	extern int	optind;		/* for getopt()			*/
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate 	/*
100*0Sstevel@tonic-gate 	 *	init
101*0Sstevel@tonic-gate 	 */
102*0Sstevel@tonic-gate 	Cmd_namep = argv[0];
103*0Sstevel@tonic-gate 	mod_present = topmost = FALSE;
104*0Sstevel@tonic-gate 	strlist.sl_nmods = NMODULES;
105*0Sstevel@tonic-gate 	strlist.sl_modlist = mlist;
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate 	/*
108*0Sstevel@tonic-gate 	 *	parse args
109*0Sstevel@tonic-gate 	 */
110*0Sstevel@tonic-gate 	if ( argc > 1) {
111*0Sstevel@tonic-gate 		while ( (i = getopt( argc, argv, OPTLIST)) != -1 ) {
112*0Sstevel@tonic-gate 			switch( i) {
113*0Sstevel@tonic-gate 				case 'm':	/* module present ? */
114*0Sstevel@tonic-gate 					modp = optarg;
115*0Sstevel@tonic-gate 					mod_present = TRUE;
116*0Sstevel@tonic-gate 					break;
117*0Sstevel@tonic-gate 
118*0Sstevel@tonic-gate 				case 't':	/* list topmost	*/
119*0Sstevel@tonic-gate 					topmost = TRUE;
120*0Sstevel@tonic-gate 					break;
121*0Sstevel@tonic-gate 
122*0Sstevel@tonic-gate 				default:
123*0Sstevel@tonic-gate 					(void) fprintf(stderr, USAGE, Cmd_namep);
124*0Sstevel@tonic-gate 					return(ERR_USAGE);
125*0Sstevel@tonic-gate 			}
126*0Sstevel@tonic-gate 		}
127*0Sstevel@tonic-gate 
128*0Sstevel@tonic-gate 		if ( optind < argc ) {
129*0Sstevel@tonic-gate 			(void) fprintf(stderr, USAGE, Cmd_namep);
130*0Sstevel@tonic-gate 			return(ERR_USAGE);
131*0Sstevel@tonic-gate 		}
132*0Sstevel@tonic-gate 	}
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate 	if (topmost && mod_present) {
135*0Sstevel@tonic-gate 		(void) fprintf(stderr,
136*0Sstevel@tonic-gate 		"%s: [-t] and [-m] options cannot be used together\n", Cmd_namep);
137*0Sstevel@tonic-gate 		(void) fprintf(stderr, USAGE, Cmd_namep);
138*0Sstevel@tonic-gate 		return(ERR_USAGE);
139*0Sstevel@tonic-gate 	}
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate 	/*
142*0Sstevel@tonic-gate 	 * get number of modules on stream
143*0Sstevel@tonic-gate 	 * allocate more room if needed
144*0Sstevel@tonic-gate 	 */
145*0Sstevel@tonic-gate 	if ( (i =  ioctl(STDIN, I_LIST, (struct str_list *)NULL))
146*0Sstevel@tonic-gate 	 < 0 ) {
147*0Sstevel@tonic-gate 		perror("I_LIST");
148*0Sstevel@tonic-gate 		(void) fprintf(stderr,
149*0Sstevel@tonic-gate 			"%s: I_LIST ioctl failed\n", Cmd_namep);
150*0Sstevel@tonic-gate 		return(ERR_STDIN);
151*0Sstevel@tonic-gate 	}
152*0Sstevel@tonic-gate 	if ( i > strlist.sl_nmods )
153*0Sstevel@tonic-gate 		if ( more_modules(&strlist, i) != SUCCESS )
154*0Sstevel@tonic-gate 			return(ERR_MEM);
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate 	/*
157*0Sstevel@tonic-gate 	 *	get list of modules on stream
158*0Sstevel@tonic-gate 	 */
159*0Sstevel@tonic-gate 	strlist.sl_nmods = i;
160*0Sstevel@tonic-gate 	if ( ioctl (0, I_LIST, &strlist) < 0) {
161*0Sstevel@tonic-gate 		perror("I_LIST");
162*0Sstevel@tonic-gate 		(void) fprintf (stderr, "%s: I_LIST ioctl failed\n", Cmd_namep);
163*0Sstevel@tonic-gate 		return(ERR_STDIN);
164*0Sstevel@tonic-gate 	}
165*0Sstevel@tonic-gate 
166*0Sstevel@tonic-gate 	/*
167*0Sstevel@tonic-gate 	 *	list topmost module
168*0Sstevel@tonic-gate 	 */
169*0Sstevel@tonic-gate 	if ( topmost ) {
170*0Sstevel@tonic-gate 		if ( strlist.sl_nmods >= 2 ) {
171*0Sstevel@tonic-gate 			(void) printf("%s\n", strlist.sl_modlist[0].l_name);
172*0Sstevel@tonic-gate 			return(SUCCESS);
173*0Sstevel@tonic-gate 		}
174*0Sstevel@tonic-gate 		return(ERR_MODULE);
175*0Sstevel@tonic-gate 	}
176*0Sstevel@tonic-gate 
177*0Sstevel@tonic-gate 	/*
178*0Sstevel@tonic-gate 	 *	check if module is present
179*0Sstevel@tonic-gate 	 */
180*0Sstevel@tonic-gate 	if ( mod_present ) {
181*0Sstevel@tonic-gate 		for ( i = 0; i < strlist.sl_nmods; i++ ) {
182*0Sstevel@tonic-gate 			if ( strncmp(modp, strlist.sl_modlist[i].l_name,
183*0Sstevel@tonic-gate 							FMNAMESZ) == SAME ) {
184*0Sstevel@tonic-gate 				(void) printf("yes\n");
185*0Sstevel@tonic-gate 				return(SUCCESS);
186*0Sstevel@tonic-gate 			}
187*0Sstevel@tonic-gate 		}
188*0Sstevel@tonic-gate 		(void) printf("no\n");
189*0Sstevel@tonic-gate 		return(ERR_MODULE);
190*0Sstevel@tonic-gate 	}
191*0Sstevel@tonic-gate 
192*0Sstevel@tonic-gate 	/*
193*0Sstevel@tonic-gate 	 *	print names of all modules and topmost driver on stream
194*0Sstevel@tonic-gate 	 */
195*0Sstevel@tonic-gate 	for ( i = 0; i < strlist.sl_nmods; i++ )
196*0Sstevel@tonic-gate 		(void) printf("%s\n", strlist.sl_modlist[i].l_name);
197*0Sstevel@tonic-gate 	return(SUCCESS);
198*0Sstevel@tonic-gate }
199*0Sstevel@tonic-gate 
200*0Sstevel@tonic-gate /*
201*0Sstevel@tonic-gate  * more_modules(listp, n)	allocate space for 'n' modules in 'listp'
202*0Sstevel@tonic-gate  *
203*0Sstevel@tonic-gate  * returns:	SUCCESS or FAILURE
204*0Sstevel@tonic-gate  */
205*0Sstevel@tonic-gate 
206*0Sstevel@tonic-gate static int
207*0Sstevel@tonic-gate more_modules(listp, n)
208*0Sstevel@tonic-gate struct str_list	*listp;		/* streams module list	*/
209*0Sstevel@tonic-gate int		n;		/* # of modules		*/
210*0Sstevel@tonic-gate {
211*0Sstevel@tonic-gate 	register int			i;
212*0Sstevel@tonic-gate 	register struct str_mlist	*modp;
213*0Sstevel@tonic-gate 
214*0Sstevel@tonic-gate 	extern char	*calloc();
215*0Sstevel@tonic-gate 
216*0Sstevel@tonic-gate 	if ( n > MAXMODULES ) {
217*0Sstevel@tonic-gate 		(void) fprintf(stderr,
218*0Sstevel@tonic-gate 			"%s: too many modules (%d) -- max is %d\n",
219*0Sstevel@tonic-gate 			Cmd_namep, n, MAXMODULES);
220*0Sstevel@tonic-gate 		return(FAILURE);
221*0Sstevel@tonic-gate 	}
222*0Sstevel@tonic-gate 
223*0Sstevel@tonic-gate 	if ( (modp = (struct str_mlist *)calloc((unsigned)n,
224*0Sstevel@tonic-gate 	(unsigned)sizeof(struct str_mlist))) == (struct str_mlist *)NULL ) {
225*0Sstevel@tonic-gate 		perror("calloc");
226*0Sstevel@tonic-gate 		(void) fprintf(stderr,
227*0Sstevel@tonic-gate 			"%s: failed to allocate space for module list\n",
228*0Sstevel@tonic-gate 			Cmd_namep);
229*0Sstevel@tonic-gate 		return(FAILURE);
230*0Sstevel@tonic-gate 	}
231*0Sstevel@tonic-gate 
232*0Sstevel@tonic-gate 	for ( i = 0; i < listp->sl_nmods; ++i )
233*0Sstevel@tonic-gate 		(void) strncpy(modp[i].l_name, listp->sl_modlist[i].l_name,
234*0Sstevel@tonic-gate 			FMNAMESZ);
235*0Sstevel@tonic-gate 	listp->sl_nmods = n;
236*0Sstevel@tonic-gate 	listp->sl_modlist = modp;
237*0Sstevel@tonic-gate 	return(SUCCESS);
238*0Sstevel@tonic-gate }
239