199767f8919635c4928607450d9e0abb932109ceToomas Soome/* $NetBSD: gets.c,v 1.6 1995/10/11 21:16:57 pk Exp $ */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*-
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 1993
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The Regents of the University of California. All rights reserved.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Redistribution and use in source and binary forms, with or without
199767f8919635c4928607450d9e0abb932109ceToomas Soome * modification, are permitted provided that the following conditions
199767f8919635c4928607450d9e0abb932109ceToomas Soome * are met:
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 1. Redistributions of source code must retain the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 2. Redistributions in binary form must reproduce the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer in the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * documentation and/or other materials provided with the distribution.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 4. Neither the name of the University nor the names of its contributors
199767f8919635c4928607450d9e0abb932109ceToomas Soome * may be used to endorse or promote products derived from this software
199767f8919635c4928607450d9e0abb932109ceToomas Soome * without specific prior written permission.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
199767f8919635c4928607450d9e0abb932109ceToomas Soome * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
199767f8919635c4928607450d9e0abb932109ceToomas Soome * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
199767f8919635c4928607450d9e0abb932109ceToomas Soome * SUCH DAMAGE.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * @(#)gets.c 8.1 (Berkeley) 6/11/93
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/cdefs.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome__FBSDID("$FreeBSD$");
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "stand.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* gets() with constrained input length */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid
199767f8919635c4928607450d9e0abb932109ceToomas Soomengets(char *buf, int n)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int c;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *lp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (lp = buf;;)
199767f8919635c4928607450d9e0abb932109ceToomas Soome switch (c = getchar() & 0177) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome case '\n':
199767f8919635c4928607450d9e0abb932109ceToomas Soome case '\r':
199767f8919635c4928607450d9e0abb932109ceToomas Soome *lp = '\0';
199767f8919635c4928607450d9e0abb932109ceToomas Soome putchar('\n');
199767f8919635c4928607450d9e0abb932109ceToomas Soome return;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case '\b':
199767f8919635c4928607450d9e0abb932109ceToomas Soome case '\177':
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (lp > buf) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome lp--;
199767f8919635c4928607450d9e0abb932109ceToomas Soome putchar('\b');
199767f8919635c4928607450d9e0abb932109ceToomas Soome putchar(' ');
199767f8919635c4928607450d9e0abb932109ceToomas Soome putchar('\b');
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case 'r'&037: {
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *p;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome putchar('\n');
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (p = buf; p < lp; ++p)
199767f8919635c4928607450d9e0abb932109ceToomas Soome putchar(*p);
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome case 'u'&037:
199767f8919635c4928607450d9e0abb932109ceToomas Soome case 'w'&037:
199767f8919635c4928607450d9e0abb932109ceToomas Soome lp = buf;
199767f8919635c4928607450d9e0abb932109ceToomas Soome putchar('\n');
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome default:
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((n < 1) || ((lp - buf) < n - 1)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome *lp++ = c;
199767f8919635c4928607450d9e0abb932109ceToomas Soome putchar(c);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*NOTREACHED*/
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soomefgetstr(char *buf, int size, int fd)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome char c;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int err, len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome size--; /* leave space for terminator */
199767f8919635c4928607450d9e0abb932109ceToomas Soome len = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (size != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome err = read(fd, &c, sizeof(c));
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (err < 0) /* read error */
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (err == 0) { /* EOF */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (len == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(-1); /* nothing to read */
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((c == '\r') || /* line terminators */
199767f8919635c4928607450d9e0abb932109ceToomas Soome (c == '\n'))
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome *buf++ = c; /* keep char */
199767f8919635c4928607450d9e0abb932109ceToomas Soome size--;
199767f8919635c4928607450d9e0abb932109ceToomas Soome len++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome *buf = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome