forked from Heatwave/The-C-Programming-Language-2nd-Edition
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetch.c
More file actions
49 lines (41 loc) · 630 Bytes
/
getch.c
File metadata and controls
49 lines (41 loc) · 630 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <stdio.h>
#include <string.h>
#define BUFSIZE 100
int buf[BUFSIZE];
int bufp = 0;
int value = EOF;
int getch(void)
{
return (bufp > 0) ? buf[--bufp] : getchar();
}
void ungetch(int c)
{
if (bufp >= BUFSIZE)
printf("ungetch: too many characters\n");
else
buf[bufp++] = c;
}
void ungetchs(char s[])
{
int len = strlen(s);
while (len > 0) {
ungetch(s[--len]);
}
}
int getchOne(void)
{
if (value != EOF) {
int temp = value;
value = EOF;
return temp;
}
else
return getchar();
}
void ungetchOne(int c)
{
if (value != EOF)
printf("ungetchOne: cannot ungetch more char\n");
else
value = c;
}