dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / lib / libadm / common / ckgid.c
blob26a36bdf655e1d789c0ab07a436222688a1c9cca
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
22 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
23 /* All Rights Reserved */
27 * Copyright (c) 1997, by Sun Microsystems, Inc.
28 * All rights reserved.
31 /*LINTLIBRARY*/
33 #include <stdio.h>
34 #include <string.h>
35 #include <ctype.h>
36 #include <grp.h>
37 #include <sys/types.h>
38 #include "libadm.h"
39 #include <stdlib.h>
40 #include <limits.h>
42 #define PROMPT "Enter the name of an existing group"
43 #define MESG "Please enter the name of an existing group."
44 #define ALTMESG "Please enter one of the following group names:\\n\\t"
45 #define MALSIZ 64
47 #define DELIM1 '/'
48 #define BLANK ' '
50 static char *
51 setmsg(int disp)
53 struct group
54 *grpptr;
55 int count;
56 size_t n, m;
57 char *msg;
59 if (disp == 0)
60 return (MESG);
62 m = MALSIZ;
63 n = sizeof (ALTMESG);
64 msg = calloc(m, sizeof (char));
65 (void) strcpy(msg, ALTMESG);
67 setgrent();
68 count = 0;
69 while ((grpptr = getgrent()) != NULL) {
70 n += strlen(grpptr->gr_name) + 2;
71 while (n >= m) {
72 m += MALSIZ;
73 msg = reallocarray(msg, m, sizeof (char));
75 if (count++)
76 (void) strcat(msg, ", ");
77 (void) strcat(msg, grpptr->gr_name);
79 endgrent();
80 return (msg);
83 int
84 ckgid_dsp(void)
86 struct group *grpptr;
88 /* if display flag is set, then list out group file */
89 if (ckgrpfile() == 1)
90 return (1);
91 setgrent();
92 while (grpptr = getgrent())
93 (void) printf("%s\n", grpptr->gr_name);
94 endgrent();
95 return (0);
98 int
99 ckgid_val(char *grpnm)
101 int valid;
103 setgrent();
104 valid = (getgrnam(grpnm) ? 0 : 1);
105 endgrent();
106 return (valid);
110 ckgrpfile(void) /* check to see if group file there */
112 struct group *grpptr;
114 setgrent();
115 grpptr = getgrent();
116 if (!grpptr) {
117 endgrent();
118 return (1);
120 endgrent();
121 return (0);
124 void
125 ckgid_err(int disp, char *error)
127 char *msg;
129 msg = setmsg(disp);
130 puterror(stdout, msg, error);
131 if (disp)
132 free(msg);
135 void
136 ckgid_hlp(int disp, char *help)
138 char *msg;
140 msg = setmsg(disp);
141 puthelp(stdout, msg, help);
142 if (disp)
143 free(msg);
147 ckgid(char *gid, short disp, char *defstr, char *error, char *help,
148 char *prompt)
150 char *defmesg,
151 input[MAX_INPUT];
153 defmesg = NULL;
154 if (!prompt)
155 prompt = PROMPT;
157 start:
158 putprmpt(stderr, prompt, NULL, defstr);
159 if (getinput(input)) {
160 if (disp)
161 free(defmesg);
162 return (1);
165 if (!strlen(input)) {
166 if (defstr) {
167 if (disp)
168 free(defmesg);
169 (void) strcpy(gid, defstr);
170 return (0);
172 if (!defmesg)
173 defmesg = setmsg(disp);
174 puterror(stderr, defmesg, error);
175 goto start;
176 } else if (strcmp(input, "?") == 0) {
177 if (!defmesg)
178 defmesg = setmsg(disp);
179 puthelp(stderr, defmesg, help);
180 goto start;
181 } else if (ckquit && (strcmp(input, "q") == 0)) {
182 if (disp)
183 free(defmesg);
184 return (3);
185 } else if (ckgid_val(input)) {
186 if (!defmesg)
187 defmesg = setmsg(disp);
188 puterror(stderr, defmesg, error);
189 goto start;
191 (void) strcpy(gid, input);
192 if (disp)
193 free(defmesg);
194 return (0);