diff options
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]); + } +} |