138451Smsmith/*	$NetBSD: gets.c,v 1.6 1995/10/11 21:16:57 pk Exp $	*/
238451Smsmith
338451Smsmith/*-
438451Smsmith * Copyright (c) 1993
538451Smsmith *	The Regents of the University of California.  All rights reserved.
638451Smsmith *
738451Smsmith * Redistribution and use in source and binary forms, with or without
838451Smsmith * modification, are permitted provided that the following conditions
938451Smsmith * are met:
1038451Smsmith * 1. Redistributions of source code must retain the above copyright
1138451Smsmith *    notice, this list of conditions and the following disclaimer.
1238451Smsmith * 2. Redistributions in binary form must reproduce the above copyright
1338451Smsmith *    notice, this list of conditions and the following disclaimer in the
1438451Smsmith *    documentation and/or other materials provided with the distribution.
1538451Smsmith * 4. Neither the name of the University nor the names of its contributors
1638451Smsmith *    may be used to endorse or promote products derived from this software
1738451Smsmith *    without specific prior written permission.
1838451Smsmith *
1938451Smsmith * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2038451Smsmith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2138451Smsmith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2238451Smsmith * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2338451Smsmith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2438451Smsmith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2538451Smsmith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2638451Smsmith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2738451Smsmith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2838451Smsmith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2938451Smsmith * SUCH DAMAGE.
3038451Smsmith *
3138451Smsmith *	@(#)gets.c	8.1 (Berkeley) 6/11/93
3238451Smsmith */
3338451Smsmith
3484221Sdillon#include <sys/cdefs.h>
3584221Sdillon__FBSDID("$FreeBSD$");
3684221Sdillon
3738451Smsmith#include "stand.h"
3838451Smsmith
3938451Smsmith/* gets() with constrained input length */
4038451Smsmith
4138451Smsmithvoid
4238451Smsmithngets(char *buf, int n)
4338451Smsmith{
4492913Sobrien    int c;
4592913Sobrien    char *lp;
4638451Smsmith
4738451Smsmith    for (lp = buf;;)
4838451Smsmith	switch (c = getchar() & 0177) {
4938451Smsmith	case '\n':
5038451Smsmith	case '\r':
5138451Smsmith	    *lp = '\0';
5238451Smsmith	    putchar('\n');
5338451Smsmith	    return;
5438451Smsmith	case '\b':
5538451Smsmith	case '\177':
5638451Smsmith	    if (lp > buf) {
5738451Smsmith		lp--;
5838451Smsmith		putchar('\b');
5938451Smsmith		putchar(' ');
6038451Smsmith		putchar('\b');
6138451Smsmith	    }
6238451Smsmith	    break;
6338451Smsmith	case 'r'&037: {
6492913Sobrien	    char *p;
6538451Smsmith
6638451Smsmith	    putchar('\n');
6738451Smsmith	    for (p = buf; p < lp; ++p)
6838451Smsmith		putchar(*p);
6938451Smsmith	    break;
7038451Smsmith	}
7138451Smsmith	case 'u'&037:
7238451Smsmith	case 'w'&037:
7338451Smsmith	    lp = buf;
7438451Smsmith	    putchar('\n');
7538451Smsmith	    break;
7638451Smsmith	default:
77190593Sjhb	    if ((n < 1) || ((lp - buf) < n - 1)) {
7838451Smsmith		*lp++ = c;
7938451Smsmith		putchar(c);
8038451Smsmith	    }
8138451Smsmith	}
8238451Smsmith    /*NOTREACHED*/
8338451Smsmith}
8438451Smsmith
8538451Smsmithint
8638451Smsmithfgetstr(char *buf, int size, int fd)
8738451Smsmith{
8838451Smsmith    char	c;
8938451Smsmith    int		err, len;
9038451Smsmith
9138451Smsmith    size--;	/* leave space for terminator */
9238451Smsmith    len = 0;
9338451Smsmith    while (size != 0) {
9438451Smsmith	err = read(fd, &c, sizeof(c));
9538451Smsmith	if (err < 0)		/* read error */
9638451Smsmith	    return(-1);
9740774Smsmith	if (err == 0) {		/* EOF */
9840774Smsmith	    if (len == 0)
9940774Smsmith		return(-1);	/* nothing to read */
10038451Smsmith	    break;
10140774Smsmith	}
10238451Smsmith	if ((c == '\r') ||	/* line terminators */
10338451Smsmith	    (c == '\n'))
10438451Smsmith	    break;
10538451Smsmith	*buf++ = c;		/* keep char */
10638451Smsmith	size--;
10738451Smsmith	len++;
10838451Smsmith    }
10938451Smsmith    *buf = 0;
11038451Smsmith    return(len);
11138451Smsmith}
11238451Smsmith
113