xref: /titanic_44/usr/src/cmd/valtools/ckuid.c (revision 80e2ca8596e3435bc3b76f3c597833ea0a87f85e)
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, *prompt = NULL, *error = NULL, *help = NULL;
47 static int	kpid = BADPID;
48 static int	signo;
49 
50 static const char	husage[] = "mWh";
51 static const char	eusage[] = "mWe";
52 
53 static void
54 usage(void)
55 {
56 	switch (*prog) {
57 	default:
58 		(void) fprintf(stderr,
59 			gettext("usage: %s [options] [-m]\n"),
60 			prog);
61 		(void) fprintf(stderr, gettext(OPTMESG));
62 		(void) fprintf(stderr, gettext(STDOPTS));
63 		break;
64 
65 	case 'd':
66 		(void) fprintf(stderr,
67 			gettext("usage: %s\n"), prog);
68 		break;
69 
70 	case 'v':
71 		(void) fprintf(stderr,
72 			gettext("usage: %s input\n"), prog);
73 		break;
74 
75 	case 'h':
76 		(void) fprintf(stderr,
77 			gettext("usage: %s [options] [-m]\n"),
78 			prog);
79 		(void) fprintf(stderr, gettext(OPTMESG));
80 		(void) fprintf(stderr,
81 			gettext("\t-W width\n\t-h help\n"));
82 		break;
83 
84 	case 'e':
85 		(void) fprintf(stderr,
86 			gettext("usage: %s [options] [-m]\n"),
87 			prog);
88 		(void) fprintf(stderr, gettext(OPTMESG));
89 		(void) fprintf(stderr,
90 			gettext("\t-W width\n\t-e error\n"));
91 		break;
92 	}
93 	exit(1);
94 }
95 
96 /*
97  * Given argv[0], return a pointer to the basename of the program.
98  */
99 static char *
100 prog_name(char *arg0)
101 {
102 	char *str;
103 
104 	/* first strip trailing '/' characters (exec() allows these!) */
105 	str = arg0 + strlen(arg0);
106 	while (str > arg0 && *--str == '/')
107 		*str = '\0';
108 	if ((str = strrchr(arg0, '/')) != NULL)
109 		return (str + 1);
110 	return (arg0);
111 }
112 
113 int
114 main(int argc, char **argv)
115 {
116 	int	c, n;
117 	char	*strval;
118 	size_t	len;
119 	int	disp = 0;	/* display set to no-display */
120 
121 	(void) setlocale(LC_ALL, "");
122 
123 #if	!defined(TEXT_DOMAIN)
124 #define	TEXT_DOMAIN	"SYS_TEST"
125 #endif
126 	(void) textdomain(TEXT_DOMAIN);
127 
128 	prog = prog_name(argv[0]);
129 
130 	while ((c = getopt(argc, argv, "md:p:e:h:k:s:QW:?")) != EOF) {
131 		/* check for invalid option */
132 		if ((*prog == 'v') || (*prog == 'd'))
133 			usage();
134 		if ((*prog == 'e') && !strchr(eusage, c))
135 			usage();
136 		if ((*prog == 'h') && !strchr(husage, c))
137 			usage();
138 
139 		switch (c) {
140 		case 'Q':
141 			ckquit = 0;
142 			break;
143 
144 		case 'W':
145 			ckwidth = atoi(optarg);
146 			if (ckwidth < 0) {
147 				(void) fprintf(stderr,
148 		gettext("%s: ERROR: negative display width specified\n"),
149 					prog);
150 				exit(1);
151 			}
152 			break;
153 
154 		case 'm':
155 			disp = 1;
156 			break;
157 
158 		case 'd':
159 			deflt = optarg;
160 			break;
161 
162 		case 'p':
163 			prompt = optarg;
164 			break;
165 
166 		case 'e':
167 			error = optarg;
168 			break;
169 
170 		case 'h':
171 			help = optarg;
172 			break;
173 
174 		case 'k':
175 			kpid = atoi(optarg);
176 			break;
177 
178 		case 's':
179 			signo = atoi(optarg);
180 			break;
181 
182 		default:
183 			usage();
184 		}
185 	}
186 
187 	if (signo) {
188 		if (kpid == BADPID)
189 			usage();
190 	} else
191 		signo = SIGTERM;
192 
193 	if (*prog == 'v') {
194 		if (argc != (optind+1))
195 			usage();
196 		exit(ckuid_val(argv[optind]));
197 	}
198 
199 	if (optind != argc)
200 		usage();
201 
202 	if (*prog == 'e') {
203 		ckindent = 0;
204 		ckuid_err(disp, error);
205 		exit(0);
206 	} else if (*prog == 'h') {
207 		ckindent = 0;
208 		ckuid_hlp(disp, help);
209 		exit(0);
210 	} else if (*prog == 'd') {
211 		exit(ckuid_dsp());
212 	}
213 
214 	if (deflt) {
215 		len = strlen(deflt) + 1;
216 		if (len < MAX_INPUT)
217 			len = MAX_INPUT;
218 	} else {
219 		len = MAX_INPUT;
220 	}
221 	strval = (char *)malloc(len);
222 	if (!strval) {
223 		(void) fprintf(stderr,
224 			gettext("Not enough memory\n"));
225 		exit(1);
226 	}
227 	n = ckuid(strval, disp, deflt, error, help, prompt);
228 	if (n == 3) {
229 		if (kpid > -2)
230 			(void) kill(kpid, signo);
231 		(void) puts("q");
232 	} else if (n == 0)
233 		(void) fputs(strval, stdout);
234 	return (n);
235 }
236