diff options
author | Michael Hunteman <michael@huntm.net> | 2023-07-04 17:03:53 -0500 |
---|---|---|
committer | Michael Hunteman <michael@huntm.net> | 2023-07-06 17:23:45 -0500 |
commit | bfce8f0d0d828209ec0bec71371ee94a7ad62d3e (patch) | |
tree | bdf49ca788ca1ca030d5b1cccfd0c9dffeb3f69f /four/getch.c |
Initial commit
Diffstat (limited to 'four/getch.c')
-rw-r--r-- | four/getch.c | 38 |
1 files changed, 38 insertions, 0 deletions
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 <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]); + } +} |