1255253Ssjg/*	$NetBSD: gets.c,v 1.6 1995/10/11 21:16:57 pk Exp $	*/
2236769Sobrien
3236769Sobrien/*-
4236769Sobrien * Copyright (c) 1993
5236769Sobrien *	The Regents of the University of California.  All rights reserved.
6236769Sobrien *
7236769Sobrien * Redistribution and use in source and binary forms, with or without
8236769Sobrien * modification, are permitted provided that the following conditions
9236769Sobrien * are met:
10236769Sobrien * 1. Redistributions of source code must retain the above copyright
11236769Sobrien *    notice, this list of conditions and the following disclaimer.
12236769Sobrien * 2. Redistributions in binary form must reproduce the above copyright
13236769Sobrien *    notice, this list of conditions and the following disclaimer in the
14236769Sobrien *    documentation and/or other materials provided with the distribution.
15236769Sobrien * 4. Neither the name of the University nor the names of its contributors
16236769Sobrien *    may be used to endorse or promote products derived from this software
17236769Sobrien *    without specific prior written permission.
18236769Sobrien *
19236769Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20236769Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21236769Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22236769Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23236769Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24236769Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25236769Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26236769Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27236769Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28236769Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29236769Sobrien * SUCH DAMAGE.
30236769Sobrien *
31236769Sobrien *	@(#)gets.c	8.1 (Berkeley) 6/11/93
32236769Sobrien */
33236769Sobrien
34236769Sobrien#include <sys/cdefs.h>
35236769Sobrien__FBSDID("$FreeBSD$");
36236769Sobrien
37236769Sobrien#include "stand.h"
38236769Sobrien
39236769Sobrien/* gets() with constrained input length */
40236769Sobrien
41236769Sobrienvoid
42236769Sobrienngets(char *buf, int n)
43236769Sobrien{
44236769Sobrien    int c;
45236769Sobrien    char *lp;
46236769Sobrien
47236769Sobrien    for (lp = buf;;)
48236769Sobrien	switch (c = getchar() & 0177) {
49236769Sobrien	case '\n':
50236769Sobrien	case '\r':
51236769Sobrien	    *lp = '\0';
52236769Sobrien	    putchar('\n');
53236769Sobrien	    return;
54236769Sobrien	case '\b':
55236769Sobrien	case '\177':
56236769Sobrien	    if (lp > buf) {
57236769Sobrien		lp--;
58236769Sobrien		putchar('\b');
59236769Sobrien		putchar(' ');
60236769Sobrien		putchar('\b');
61236769Sobrien	    }
62236769Sobrien	    break;
63236769Sobrien	case 'r'&037: {
64236769Sobrien	    char *p;
65236769Sobrien
66236769Sobrien	    putchar('\n');
67236769Sobrien	    for (p = buf; p < lp; ++p)
68236769Sobrien		putchar(*p);
69236769Sobrien	    break;
70236769Sobrien	}
71236769Sobrien	case 'u'&037:
72236769Sobrien	case 'w'&037:
73236769Sobrien	    lp = buf;
74236769Sobrien	    putchar('\n');
75236769Sobrien	    break;
76236769Sobrien	default:
77236769Sobrien	    if ((n < 1) || ((lp - buf) < n - 1)) {
78236769Sobrien		*lp++ = c;
79236769Sobrien		putchar(c);
80236769Sobrien	    }
81236769Sobrien	}
82236769Sobrien    /*NOTREACHED*/
83236769Sobrien}
84236769Sobrien
85236769Sobrienint
86236769Sobrienfgetstr(char *buf, int size, int fd)
87236769Sobrien{
88236769Sobrien    char	c;
89236769Sobrien    int		err, len;
90236769Sobrien
91236769Sobrien    size--;	/* leave space for terminator */
92236769Sobrien    len = 0;
93236769Sobrien    while (size != 0) {
94236769Sobrien	err = read(fd, &c, sizeof(c));
95236769Sobrien	if (err < 0)		/* read error */
96236769Sobrien	    return(-1);
97236769Sobrien	if (err == 0) {		/* EOF */
98236769Sobrien	    if (len == 0)
99236769Sobrien		return(-1);	/* nothing to read */
100236769Sobrien	    break;
101236769Sobrien	}
102237578Sobrien	if ((c == '\r') ||	/* line terminators */
103236769Sobrien	    (c == '\n'))
104236769Sobrien	    break;
105236769Sobrien	*buf++ = c;		/* keep char */
106249033Ssjg	size--;
107236769Sobrien	len++;
108236769Sobrien    }
109237578Sobrien    *buf = 0;
110237578Sobrien    return(len);
111236769Sobrien}
112237578Sobrien
113236769Sobrien