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