xref: /freebsd/stand/libsa/geli/pwgets.c (revision 3e15b01d6914c927e37d1699645783acf286655c)
1*62bd02ceSWarner Losh /*	$NetBSD: gets.c,v 1.6 1995/10/11 21:16:57 pk Exp $	*/
2*62bd02ceSWarner Losh 
3*62bd02ceSWarner Losh /*-
4*62bd02ceSWarner Losh  * Copyright (c) 1993
5*62bd02ceSWarner Losh  *	The Regents of the University of California.  All rights reserved.
6*62bd02ceSWarner Losh  *
7*62bd02ceSWarner Losh  * Redistribution and use in source and binary forms, with or without
8*62bd02ceSWarner Losh  * modification, are permitted provided that the following conditions
9*62bd02ceSWarner Losh  * are met:
10*62bd02ceSWarner Losh  * 1. Redistributions of source code must retain the above copyright
11*62bd02ceSWarner Losh  *    notice, this list of conditions and the following disclaimer.
12*62bd02ceSWarner Losh  * 2. Redistributions in binary form must reproduce the above copyright
13*62bd02ceSWarner Losh  *    notice, this list of conditions and the following disclaimer in the
14*62bd02ceSWarner Losh  *    documentation and/or other materials provided with the distribution.
15*62bd02ceSWarner Losh  * 3. Neither the name of the University nor the names of its contributors
16*62bd02ceSWarner Losh  *    may be used to endorse or promote products derived from this software
17*62bd02ceSWarner Losh  *    without specific prior written permission.
18*62bd02ceSWarner Losh  *
19*62bd02ceSWarner Losh  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20*62bd02ceSWarner Losh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21*62bd02ceSWarner Losh  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22*62bd02ceSWarner Losh  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23*62bd02ceSWarner Losh  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24*62bd02ceSWarner Losh  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25*62bd02ceSWarner Losh  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26*62bd02ceSWarner Losh  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27*62bd02ceSWarner Losh  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28*62bd02ceSWarner Losh  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29*62bd02ceSWarner Losh  * SUCH DAMAGE.
30*62bd02ceSWarner Losh  */
31*62bd02ceSWarner Losh 
32*62bd02ceSWarner Losh #include "stand.h"
33*62bd02ceSWarner Losh 
34*62bd02ceSWarner Losh /* gets() with constrained input length, for passwords */
35*62bd02ceSWarner Losh 
36*62bd02ceSWarner Losh void
pwgets(char * buf,int n,int hide)37*62bd02ceSWarner Losh pwgets(char *buf, int n, int hide)
38*62bd02ceSWarner Losh {
39*62bd02ceSWarner Losh     int c;
40*62bd02ceSWarner Losh     char *lp;
41*62bd02ceSWarner Losh 
42*62bd02ceSWarner Losh     for (lp = buf;;)
43*62bd02ceSWarner Losh 	switch (c = getchar() & 0177) {
44*62bd02ceSWarner Losh 	case '\n':
45*62bd02ceSWarner Losh 	case '\r':
46*62bd02ceSWarner Losh 	    *lp = '\0';
47*62bd02ceSWarner Losh 	    putchar('\n');
48*62bd02ceSWarner Losh 	    return;
49*62bd02ceSWarner Losh 	case '\b':
50*62bd02ceSWarner Losh 	case '\177':
51*62bd02ceSWarner Losh 	    if (lp > buf) {
52*62bd02ceSWarner Losh 		lp--;
53*62bd02ceSWarner Losh 		if (hide == 0) {
54*62bd02ceSWarner Losh 			putchar('\b');
55*62bd02ceSWarner Losh 			putchar(' ');
56*62bd02ceSWarner Losh 			putchar('\b');
57*62bd02ceSWarner Losh 		}
58*62bd02ceSWarner Losh 	    }
59*62bd02ceSWarner Losh 	    break;
60*62bd02ceSWarner Losh 	case 'u'&037:
61*62bd02ceSWarner Losh 	case 'w'&037:
62*62bd02ceSWarner Losh 	    lp = buf;
63*62bd02ceSWarner Losh 	    putchar('\n');
64*62bd02ceSWarner Losh 	    break;
65*62bd02ceSWarner Losh 	default:
66*62bd02ceSWarner Losh 	    if ((n < 1) || ((lp - buf) < n - 1)) {
67*62bd02ceSWarner Losh 		*lp++ = c;
68*62bd02ceSWarner Losh 		if (hide == 0) {
69*62bd02ceSWarner Losh 			putchar('*');
70*62bd02ceSWarner Losh 		}
71*62bd02ceSWarner Losh 	    }
72*62bd02ceSWarner Losh 	}
73*62bd02ceSWarner Losh     /*NOTREACHED*/
74*62bd02ceSWarner Losh }
75