feature_tests.h: always use largefile interfaces
[unleashed.git] / usr / src / lib / libadm / common / ckuid.c
blob668e93718dcaa5437c7a9bbd91ab3be07d6db128
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 <sys/types.h>
34 #include <stdio.h>
35 #include <string.h>
36 #include <ctype.h>
37 #include <pwd.h>
38 #include <stdlib.h>
39 #include <limits.h>
40 #include "libadm.h"
42 #define PROMPT "Enter the login name of an existing user"
43 #define MESG "Please enter the login name of an existing user."
44 #define ALTMESG "Please enter one of the following login names:\\n\\t"
45 #define MALSIZ 64
47 #define DELIM1 '/'
48 #define BLANK ' '
50 static char *
51 setmsg(int disp)
53 struct passwd
54 *pwdptr;
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 = (char *) calloc(m, sizeof (char));
65 (void) strcpy(msg, ALTMESG);
67 setpwent();
68 count = 0;
69 while (pwdptr = getpwent()) {
70 n += strlen(pwdptr->pw_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, pwdptr->pw_name);
79 endpwent();
80 return (msg);
83 int
84 ckuid_dsp(void)
86 struct passwd *pwdptr;
88 /* if display flag is set, then list out passwd file */
89 if (ckpwdfile() == 1)
90 return (1);
91 setpwent();
92 while (pwdptr = getpwent())
93 (void) printf("%s\n", pwdptr->pw_name);
94 endpwent();
95 return (0);
98 int
99 ckuid_val(char *usrnm)
101 int valid;
103 setpwent();
104 valid = (getpwnam(usrnm) ? 0 : 1);
105 endpwent();
106 return (valid);
110 ckpwdfile(void) /* check to see if passwd file there */
112 struct passwd *pwdptr;
114 setpwent();
115 pwdptr = getpwent();
116 if (!pwdptr) {
117 endpwent();
118 return (1);
120 endpwent();
121 return (0);
124 void
125 ckuid_err(short disp, char *error)
127 char *msg;
129 msg = setmsg(disp);
130 puterror(stdout, msg, error);
131 if (disp)
132 free(msg);
135 void
136 ckuid_hlp(int disp, char *help)
138 char *msg;
140 msg = setmsg(disp);
141 puthelp(stdout, msg, help);
142 if (disp)
143 free(msg);
147 ckuid(char *uid, 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(uid, 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 (ckuid_val(input)) {
186 if (!defmesg)
187 defmesg = setmsg(disp);
188 puterror(stderr, defmesg, error);
189 goto start;
191 (void) strcpy(uid, input);
192 if (disp)
193 free(defmesg);
194 return (0);