2 * Copyright (c) 1989 - 1993, Julianne Frances Haugh
3 * Copyright (c) 1996 - 2000, Marek Michałkiewicz
4 * Copyright (c) 2003 - 2005, Tomasz Kłoczko
5 * Copyright (c) 2008 - 2009, Nicolas François
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the copyright holders or contributors may not be used to
17 * endorse or promote products derived from this software without
18 * specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #ident "$Id: loginprompt.c 2787 2009-04-24 22:46:06Z nekral-guest $"
41 #include "prototypes.h"
45 static void login_exit (unused int sig)
51 * login_prompt - prompt the user for their login name
53 * login_prompt() displays the standard login prompt. If ISSUE_FILE
54 * is set in login.defs, this file is displayed before the prompt.
57 void login_prompt (const char *prompt, char *name, int namesize)
68 RETSIGTYPE (*sigquit) (int);
70 RETSIGTYPE (*sigtstp) (int);
74 * There is a small chance that a QUIT character will be part of
75 * some random noise during a prompt. Deal with this by exiting
76 * instead of core dumping. If SIGTSTP is defined, do the same
77 * thing for that signal.
80 sigquit = signal (SIGQUIT, login_exit);
82 sigtstp = signal (SIGTSTP, login_exit);
86 * See if the user has configured the issue file to
87 * be displayed and display it before the prompt.
91 cp = getdef_str ("ISSUE_FILE");
95 while ((i = getc (fp)) != EOF) {
96 (void) putc (i, stdout);
102 gethostname (buf, sizeof buf);
103 printf (prompt, buf);
104 (void) fflush (stdout);
108 * Read the user's response. The trailing newline will be
112 memzero (buf, sizeof buf);
113 if (fgets (buf, (int) sizeof buf, stdin) != buf) {
117 cp = strchr (buf, '\n');
121 *cp = '\0'; /* remove \n [ must be there ] */
124 * Skip leading whitespace. This makes " username" work right.
125 * Then copy the rest (up to the end or the first "non-graphic"
126 * character into the username.
129 for (cp = buf; *cp == ' ' || *cp == '\t'; cp++);
131 for (i = 0; i < namesize - 1 && isgraph (*cp); name[i++] = *cp++);
132 while (isgraph (*cp)) {
143 * This is a disaster, at best. The user may have entered extra
144 * environmental variables at the prompt. There are several ways
145 * to do this, and I just take the easy way out.
148 if ('\0' != *cp) { /* process new variables */
152 for (envc = 0; envc < MAX_ENV; envc++) {
153 nvar = strtok ((0 != envc) ? (char *) 0 : cp, " \t,");
157 if (strchr (nvar, '=') != NULL) {
160 size_t len = strlen (nvar) + 32;
162 envp[envc] = xmalloc (len);
163 wlen = snprintf (envp[envc], len, "L%d=%s", count++, nvar);
164 assert (wlen == (int) len -1);
167 set_env (envc, envp);
171 * Set the SIGQUIT handler back to its original value
174 (void) signal (SIGQUIT, sigquit);
176 (void) signal (SIGTSTP, sigtstp);