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