summaryrefslogtreecommitdiff
path: root/four/getch.c
blob: 84582cea0751cae4a6131bcb1b38f4c910530892 (plain)
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
#include <stdio.h>
#include <string.h>

#define BUFSIZE 101

char buf[BUFSIZE];
int bufp = 0;

/*
 * deliver the next character to be considered
 * reading from the buffer if it contains a character
 * and calling getchar if the buffer is empty
 */
int
getch(void)
{
	return (bufp > 0) ? buf[--bufp] : getchar();
}

/* remember the characters put back on the input */
void
ungetch(int c)
{
	if (bufp >= BUFSIZE) {
		printf("ungetch: too many characters\n");
	} else {
		buf[bufp++] = c;
	}
}

void
ungets(char s[])
{
	int l = strlen(s);
	while (l) {
		ungetch(s[--l]);
	}
}