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