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