gets.c revision 1.1.6.2 1 1.1.6.2 simonb /* $NetBSD: gets.c,v 1.1.6.2 2006/04/22 11:37:38 simonb Exp $ */
2 1.1.6.2 simonb
3 1.1.6.2 simonb /*-
4 1.1.6.2 simonb * Copyright (c) 1993
5 1.1.6.2 simonb * The Regents of the University of California. All rights reserved.
6 1.1.6.2 simonb *
7 1.1.6.2 simonb * Redistribution and use in source and binary forms, with or without
8 1.1.6.2 simonb * modification, are permitted provided that the following conditions
9 1.1.6.2 simonb * are met:
10 1.1.6.2 simonb * 1. Redistributions of source code must retain the above copyright
11 1.1.6.2 simonb * notice, this list of conditions and the following disclaimer.
12 1.1.6.2 simonb * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.6.2 simonb * notice, this list of conditions and the following disclaimer in the
14 1.1.6.2 simonb * documentation and/or other materials provided with the distribution.
15 1.1.6.2 simonb * 3. All advertising materials mentioning features or use of this software
16 1.1.6.2 simonb * must display the following acknowledgement:
17 1.1.6.2 simonb * This product includes software developed by the University of
18 1.1.6.2 simonb * California, Berkeley and its contributors.
19 1.1.6.2 simonb * 4. Neither the name of the University nor the names of its contributors
20 1.1.6.2 simonb * may be used to endorse or promote products derived from this software
21 1.1.6.2 simonb * without specific prior written permission.
22 1.1.6.2 simonb *
23 1.1.6.2 simonb * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1.6.2 simonb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1.6.2 simonb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1.6.2 simonb * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1.6.2 simonb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1.6.2 simonb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1.6.2 simonb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1.6.2 simonb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1.6.2 simonb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1.6.2 simonb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1.6.2 simonb * SUCH DAMAGE.
34 1.1.6.2 simonb *
35 1.1.6.2 simonb * @(#)gets.c 8.1 (Berkeley) 6/11/93
36 1.1.6.2 simonb */
37 1.1.6.2 simonb
38 1.1.6.2 simonb #include <sys/cdefs.h>
39 1.1.6.2 simonb
40 1.1.6.2 simonb #include "bootstrap.h"
41 1.1.6.2 simonb
42 1.1.6.2 simonb /* gets() with constrained input length */
43 1.1.6.2 simonb
44 1.1.6.2 simonb void
45 1.1.6.2 simonb ngets(char *buf, int n)
46 1.1.6.2 simonb {
47 1.1.6.2 simonb int c;
48 1.1.6.2 simonb char *lp;
49 1.1.6.2 simonb
50 1.1.6.2 simonb for (lp = buf;;)
51 1.1.6.2 simonb switch (c = getchar() & 0177) {
52 1.1.6.2 simonb case '\n':
53 1.1.6.2 simonb case '\r':
54 1.1.6.2 simonb *lp = '\0';
55 1.1.6.2 simonb putchar('\n');
56 1.1.6.2 simonb return;
57 1.1.6.2 simonb case '\b':
58 1.1.6.2 simonb case '\177':
59 1.1.6.2 simonb if (lp > buf) {
60 1.1.6.2 simonb lp--;
61 1.1.6.2 simonb putchar('\b');
62 1.1.6.2 simonb putchar(' ');
63 1.1.6.2 simonb putchar('\b');
64 1.1.6.2 simonb }
65 1.1.6.2 simonb break;
66 1.1.6.2 simonb case 'r'&037: {
67 1.1.6.2 simonb char *p;
68 1.1.6.2 simonb
69 1.1.6.2 simonb putchar('\n');
70 1.1.6.2 simonb for (p = buf; p < lp; ++p)
71 1.1.6.2 simonb putchar(*p);
72 1.1.6.2 simonb break;
73 1.1.6.2 simonb }
74 1.1.6.2 simonb case 'u'&037:
75 1.1.6.2 simonb case 'w'&037:
76 1.1.6.2 simonb lp = buf;
77 1.1.6.2 simonb putchar('\n');
78 1.1.6.2 simonb break;
79 1.1.6.2 simonb default:
80 1.1.6.2 simonb if ((n < 1) || ((lp - buf) < n)) {
81 1.1.6.2 simonb *lp++ = c;
82 1.1.6.2 simonb putchar(c);
83 1.1.6.2 simonb }
84 1.1.6.2 simonb }
85 1.1.6.2 simonb /*NOTREACHED*/
86 1.1.6.2 simonb }
87 1.1.6.2 simonb
88 1.1.6.2 simonb int
89 1.1.6.2 simonb fgetstr(char *buf, int size, int fd)
90 1.1.6.2 simonb {
91 1.1.6.2 simonb char c;
92 1.1.6.2 simonb int err, len;
93 1.1.6.2 simonb
94 1.1.6.2 simonb size--; /* leave space for terminator */
95 1.1.6.2 simonb len = 0;
96 1.1.6.2 simonb while (size != 0) {
97 1.1.6.2 simonb err = read(fd, &c, sizeof(c));
98 1.1.6.2 simonb if (err < 0) /* read error */
99 1.1.6.2 simonb return(-1);
100 1.1.6.2 simonb if (err == 0) { /* EOF */
101 1.1.6.2 simonb if (len == 0)
102 1.1.6.2 simonb return(-1); /* nothing to read */
103 1.1.6.2 simonb break;
104 1.1.6.2 simonb }
105 1.1.6.2 simonb if ((c == '\r') || /* line terminators */
106 1.1.6.2 simonb (c == '\n'))
107 1.1.6.2 simonb break;
108 1.1.6.2 simonb *buf++ = c; /* keep char */
109 1.1.6.2 simonb size--;
110 1.1.6.2 simonb len++;
111 1.1.6.2 simonb }
112 1.1.6.2 simonb *buf = 0;
113 1.1.6.2 simonb return(len);
114 1.1.6.2 simonb }
115