From bfce8f0d0d828209ec0bec71371ee94a7ad62d3e Mon Sep 17 00:00:00 2001 From: Michael Hunteman Date: Tue, 4 Jul 2023 17:03:53 -0500 Subject: Initial commit --- four/getch.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 four/getch.c (limited to 'four/getch.c') diff --git a/four/getch.c b/four/getch.c new file mode 100644 index 0000000..84582ce --- /dev/null +++ b/four/getch.c @@ -0,0 +1,38 @@ +#include +#include + +#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]); + } +} -- cgit v1.2.3