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 * @(#)gets.c 8.1 (Berkeley) 6/11/93 32*62bd02ceSWarner Losh */ 33*62bd02ceSWarner Losh 34*62bd02ceSWarner Losh #include <sys/cdefs.h> 35*62bd02ceSWarner Losh __FBSDID("$FreeBSD$"); 36*62bd02ceSWarner Losh 37*62bd02ceSWarner Losh #include "stand.h" 38*62bd02ceSWarner Losh 39*62bd02ceSWarner Losh /* gets() with constrained input length, for passwords */ 40*62bd02ceSWarner Losh 41*62bd02ceSWarner Losh void 42*62bd02ceSWarner Losh pwgets(char *buf, int n, int hide) 43*62bd02ceSWarner Losh { 44*62bd02ceSWarner Losh int c; 45*62bd02ceSWarner Losh char *lp; 46*62bd02ceSWarner Losh 47*62bd02ceSWarner Losh for (lp = buf;;) 48*62bd02ceSWarner Losh switch (c = getchar() & 0177) { 49*62bd02ceSWarner Losh case '\n': 50*62bd02ceSWarner Losh case '\r': 51*62bd02ceSWarner Losh *lp = '\0'; 52*62bd02ceSWarner Losh putchar('\n'); 53*62bd02ceSWarner Losh return; 54*62bd02ceSWarner Losh case '\b': 55*62bd02ceSWarner Losh case '\177': 56*62bd02ceSWarner Losh if (lp > buf) { 57*62bd02ceSWarner Losh lp--; 58*62bd02ceSWarner Losh if (hide == 0) { 59*62bd02ceSWarner Losh putchar('\b'); 60*62bd02ceSWarner Losh putchar(' '); 61*62bd02ceSWarner Losh putchar('\b'); 62*62bd02ceSWarner Losh } 63*62bd02ceSWarner Losh } 64*62bd02ceSWarner Losh break; 65*62bd02ceSWarner Losh case 'u'&037: 66*62bd02ceSWarner Losh case 'w'&037: 67*62bd02ceSWarner Losh lp = buf; 68*62bd02ceSWarner Losh putchar('\n'); 69*62bd02ceSWarner Losh break; 70*62bd02ceSWarner Losh default: 71*62bd02ceSWarner Losh if ((n < 1) || ((lp - buf) < n - 1)) { 72*62bd02ceSWarner Losh *lp++ = c; 73*62bd02ceSWarner Losh if (hide == 0) { 74*62bd02ceSWarner Losh putchar('*'); 75*62bd02ceSWarner Losh } 76*62bd02ceSWarner Losh } 77*62bd02ceSWarner Losh } 78*62bd02ceSWarner Losh /*NOTREACHED*/ 79*62bd02ceSWarner Losh } 80