8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / valtools / ckgid.c
blob8305c8f1592f159702e8f4d6cf92e344851b9a87
1 /*
2 * CDDL HEADER START
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.
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.
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]
20 * CDDL HEADER END
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
31 #pragma ident "%Z%%M% %I% %E% SMI"
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"
43 #define BADPID (-2)
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;
50 static const char eusage[] = "mWe";
51 static const char husage[] = "mWh";
53 static void
54 usage(void)
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;
64 case 'd':
65 (void) fprintf(stderr,
66 gettext("usage: %s\n"), prog);
67 break;
69 case 'v':
70 (void) fprintf(stderr,
71 gettext("usage: %s input\n"), prog);
72 break;
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;
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;
90 exit(1);
94 * Given argv[0], return a pointer to the basename of the program.
96 static char *
97 prog_name(char *arg0)
99 char *str;
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);
111 main(int argc, char **argv)
113 int c, n;
114 char *gid;
115 size_t len;
117 (void) setlocale(LC_ALL, "");
119 #if !defined(TEXT_DOMAIN)
120 #define TEXT_DOMAIN "SYS_TEST"
121 #endif
122 (void) textdomain(TEXT_DOMAIN);
124 prog = prog_name(argv[0]);
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();
135 switch (c) {
136 case 'Q':
137 ckquit = 0;
138 break;
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);
148 break;
150 case 'm':
151 disp = 1;
152 break;
154 case 'd':
155 deflt = optarg;
156 break;
158 case 'p':
159 prompt = optarg;
160 break;
162 case 'e':
163 error = optarg;
164 break;
166 case 'h':
167 help = optarg;
168 break;
170 case 'k':
171 kpid = atoi(optarg);
172 break;
174 case 's':
175 signo = atoi(optarg);
176 break;
178 default:
179 usage();
183 if (signo) {
184 if (kpid == BADPID)
185 usage();
186 } else
187 signo = SIGTERM;
189 if (*prog == 'v') {
190 if (argc != (optind+1))
191 usage();
192 exit(ckgid_val(argv[optind]));
195 if (argc != optind)
196 usage();
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());
211 if (deflt) {
212 len = strlen(deflt) + 1;
213 if (len < MAX_INPUT)
214 len = MAX_INPUT;
215 } else {
216 len = MAX_INPUT;
218 gid = (char *)malloc(len);
219 if (!gid) {
220 (void) fprintf(stderr,
221 gettext("Not enough memory\n"));
222 exit(1);
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);