gets.c revision 1.5 1 1.5 dholland /* $NetBSD: gets.c,v 1.5 2016/06/11 06:49:46 dholland Exp $ */
2 1.1 fredette
3 1.1 fredette /*-
4 1.1 fredette * Copyright (c) 1993
5 1.1 fredette * The Regents of the University of California. All rights reserved.
6 1.1 fredette *
7 1.1 fredette * Redistribution and use in source and binary forms, with or without
8 1.1 fredette * modification, are permitted provided that the following conditions
9 1.1 fredette * are met:
10 1.1 fredette * 1. Redistributions of source code must retain the above copyright
11 1.1 fredette * notice, this list of conditions and the following disclaimer.
12 1.1 fredette * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 fredette * notice, this list of conditions and the following disclaimer in the
14 1.1 fredette * documentation and/or other materials provided with the distribution.
15 1.2 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 fredette * may be used to endorse or promote products derived from this software
17 1.1 fredette * without specific prior written permission.
18 1.1 fredette *
19 1.1 fredette * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 fredette * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 fredette * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 fredette * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 fredette * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 fredette * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 fredette * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 fredette * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 fredette * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 fredette * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 fredette * SUCH DAMAGE.
30 1.1 fredette *
31 1.1 fredette * @(#)gets.c 8.1 (Berkeley) 6/11/93
32 1.1 fredette */
33 1.1 fredette
34 1.1 fredette #include "stand.h"
35 1.1 fredette
36 1.1 fredette /*
37 1.1 fredette * This implementation assumes that getchar() does echo, because
38 1.1 fredette * on some machines, it is hard to keep echo from being done.
39 1.1 fredette * Those that need it can do echo in their getchar() function.
40 1.1 fredette *
41 1.1 fredette * Yes, the code below will echo CR, DEL, and other control chars,
42 1.1 fredette * but sending CR or DEL here is harmless. All the other editing
43 1.1 fredette * characters will be followed by a newline, so it doesn't matter.
44 1.1 fredette * (Most terminals will not show them anyway.)
45 1.1 fredette */
46 1.1 fredette
47 1.3 chs void
48 1.5 dholland kgets(char *buf, size_t size)
49 1.1 fredette {
50 1.3 chs int c;
51 1.3 chs char *lp;
52 1.1 fredette
53 1.1 fredette top:
54 1.1 fredette lp = buf;
55 1.1 fredette
56 1.1 fredette for (;;) {
57 1.5 dholland if (lp - buf == size) {
58 1.5 dholland lp--;
59 1.5 dholland *lp = '\0';
60 1.5 dholland return;
61 1.5 dholland }
62 1.1 fredette c = getchar() & 0177;
63 1.1 fredette
64 1.1 fredette #ifdef GETS_MUST_ECHO /* Preserved in case someone wants it... */
65 1.1 fredette putchar(c);
66 1.1 fredette #endif
67 1.1 fredette
68 1.1 fredette switch (c) {
69 1.1 fredette
70 1.1 fredette default:
71 1.1 fredette *lp++ = c;
72 1.1 fredette continue;
73 1.1 fredette
74 1.1 fredette case '\177':
75 1.1 fredette putchar('\b');
76 1.1 fredette /* fall through */
77 1.1 fredette case '\b':
78 1.1 fredette putchar(' ');
79 1.1 fredette putchar('\b');
80 1.1 fredette /* fall through */
81 1.1 fredette case '#':
82 1.1 fredette if (lp > buf)
83 1.1 fredette lp--;
84 1.1 fredette continue;
85 1.1 fredette
86 1.1 fredette #ifdef GETS_REPRINT
87 1.1 fredette /*
88 1.1 fredette * This is not very useful in a boot program.
89 1.1 fredette * (It costs you 52 bytes on m68k, gcc -O3).
90 1.1 fredette */
91 1.1 fredette case 'r'&037: {
92 1.3 chs char *p;
93 1.3 chs
94 1.1 fredette putchar('\n');
95 1.1 fredette for (p = buf; p < lp; ++p)
96 1.1 fredette putchar(*p);
97 1.1 fredette continue;
98 1.1 fredette }
99 1.1 fredette #endif
100 1.1 fredette
101 1.1 fredette case '@':
102 1.1 fredette case 'u'&037:
103 1.1 fredette case 'w'&037:
104 1.1 fredette putchar('\n');
105 1.1 fredette goto top;
106 1.1 fredette
107 1.1 fredette case '\r':
108 1.1 fredette putchar('\n');
109 1.1 fredette /* fall through */
110 1.1 fredette case '\n':
111 1.1 fredette *lp = '\0';
112 1.1 fredette return;
113 1.1 fredette
114 1.1 fredette } /* switch */
115 1.1 fredette }
116 1.1 fredette /*NOTREACHED*/
117 1.1 fredette }
118