xref: /titanic_51/usr/src/cmd/valtools/ckgid.c (revision 1e49577a7fcde812700ded04431b49d67cc57d6d)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"
32 
33 #include <stdio.h>
34 #include <string.h>
35 #include <signal.h>
36 #include <stdlib.h>
37 #include <locale.h>
38 #include <libintl.h>
39 #include <limits.h>
40 #include "usage.h"
41 #include "libadm.h"
42 
43 #define	BADPID	(-2)
44 
45 static char	*prog;
46 static char	*deflt = NULL, *error = NULL, *help = NULL, *prompt = NULL;
47 static int	kpid = BADPID;
48 static int	signo, disp;
49 
50 static const char	eusage[] = "mWe";
51 static const char	husage[] = "mWh";
52 
53 static void
54 usage(void)
55 {
56 	switch (*prog) {
57 	default:
58 		(void) fprintf(stderr,
59 			gettext("usage: %s [options] [-m]\n"), prog);
60 		(void) fprintf(stderr, gettext(OPTMESG));
61 		(void) fprintf(stderr, gettext(STDOPTS));
62 		break;
63 
64 	case 'd':
65 		(void) fprintf(stderr,
66 			gettext("usage: %s\n"), prog);
67 		break;
68 
69 	case 'v':
70 		(void) fprintf(stderr,
71 			gettext("usage: %s input\n"), prog);
72 		break;
73 
74 	case 'h':
75 		(void) fprintf(stderr,
76 			gettext("usage: %s [options] [-m]\n"), prog);
77 		(void) fprintf(stderr, gettext(OPTMESG));
78 		(void) fprintf(stderr,
79 			gettext("\t-W width\n\t-h help\n"));
80 		break;
81 
82 	case 'e':
83 		(void) fprintf(stderr,
84 			gettext("usage: %s [options] [-m]\n"), prog);
85 		(void) fprintf(stderr, gettext(OPTMESG));
86 		(void) fprintf(stderr,
87 			gettext("\t-W width\n\t-e error\n"));
88 		break;
89 	}
90 	exit(1);
91 }
92 
93 /*
94  * Given argv[0], return a pointer to the basename of the program.
95  */
96 static char *
97 prog_name(char *arg0)
98 {
99 	char *str;
100 
101 	/* first strip trailing '/' characters (exec() allows these!) */
102 	str = arg0 + strlen(arg0);
103 	while (str > arg0 && *--str == '/')
104 		*str = '\0';
105 	if ((str = strrchr(arg0, '/')) != NULL)
106 		return (str + 1);
107 	return (arg0);
108 }
109 
110 int
111 main(int argc, char **argv)
112 {
113 	int	c, n;
114 	char	*gid;
115 	size_t	len;
116 
117 	(void) setlocale(LC_ALL, "");
118 
119 #if	!defined(TEXT_DOMAIN)
120 #define	TEXT_DOMAIN	"SYS_TEST"
121 #endif
122 	(void) textdomain(TEXT_DOMAIN);
123 
124 	prog = prog_name(argv[0]);
125 
126 	while ((c = getopt(argc, argv, "md:p:e:h:k:s:QW:?")) != EOF) {
127 		/* check for invalid option */
128 		if ((*prog == 'v') || (*prog == 'd'))
129 			usage(); /* no valid options */
130 		if ((*prog == 'e') && !strchr(eusage, c))
131 			usage();
132 		if ((*prog == 'h') && !strchr(husage, c))
133 			usage();
134 
135 		switch (c) {
136 		case 'Q':
137 			ckquit = 0;
138 			break;
139 
140 		case 'W':
141 			ckwidth = atoi(optarg);
142 			if (ckwidth < 0) {
143 				(void) fprintf(stderr,
144 		gettext("%s: ERROR: negative display width specified\n"),
145 					prog);
146 				exit(1);
147 			}
148 			break;
149 
150 		case 'm':
151 			disp = 1;
152 			break;
153 
154 		case 'd':
155 			deflt = optarg;
156 			break;
157 
158 		case 'p':
159 			prompt = optarg;
160 			break;
161 
162 		case 'e':
163 			error = optarg;
164 			break;
165 
166 		case 'h':
167 			help = optarg;
168 			break;
169 
170 		case 'k':
171 			kpid = atoi(optarg);
172 			break;
173 
174 		case 's':
175 			signo = atoi(optarg);
176 			break;
177 
178 		default:
179 			usage();
180 		}
181 	}
182 
183 	if (signo) {
184 		if (kpid == BADPID)
185 			usage();
186 	} else
187 		signo = SIGTERM;
188 
189 	if (*prog == 'v') {
190 		if (argc != (optind+1))
191 			usage();
192 		exit(ckgid_val(argv[optind]));
193 	}
194 
195 	if (argc != optind)
196 		usage();
197 
198 
199 	if (*prog == 'e') {
200 		ckindent = 0;
201 		ckgid_err(disp, error);
202 		exit(0);
203 	} else if (*prog == 'h') {
204 		ckindent = 0;
205 		ckgid_hlp(disp, help);
206 		exit(0);
207 	} else if (*prog == 'd') {
208 		exit(ckgid_dsp());
209 	}
210 
211 	if (deflt) {
212 		len = strlen(deflt) + 1;
213 		if (len < MAX_INPUT)
214 			len = MAX_INPUT;
215 	} else {
216 		len = MAX_INPUT;
217 	}
218 	gid = (char *)malloc(len);
219 	if (!gid) {
220 		(void) fprintf(stderr,
221 			gettext("Not enough memory\n"));
222 		exit(1);
223 	}
224 	n = ckgid(gid, disp, deflt, error, help, prompt);
225 	if (n == 3) {
226 		if (kpid > -2)
227 			(void) kill(kpid, signo);
228 		(void) puts("q");
229 	} else if (n == 0)
230 		(void) fputs(gid, stdout);
231 	return (n);
232 }
233