diff options
-rw-r--r-- | 7.2/Makefile | 11 | ||||
-rw-r--r-- | 7.2/getch.c | 23 | ||||
-rw-r--r-- | 7.2/getch.h | 2 | ||||
-rw-r--r-- | 7.2/main.c | 50 |
4 files changed, 86 insertions, 0 deletions
diff --git a/7.2/Makefile b/7.2/Makefile new file mode 100644 index 0000000..982c7a2 --- /dev/null +++ b/7.2/Makefile @@ -0,0 +1,11 @@ +objects = main.o getch.o +CC = gcc +CFLAGS = -Wvla -Wall -Wextra -Wstrict-prototypes -Wmissing-prototypes -fsanitize=address + +fmt : $(objects) + $(CC) $(CFLAGS) -o fmt $(objects) +main.o: getch.h + +.PHONY : clean +clean: + rm fmt $(objects) diff --git a/7.2/getch.c b/7.2/getch.c new file mode 100644 index 0000000..a473190 --- /dev/null +++ b/7.2/getch.c @@ -0,0 +1,23 @@ +#include <stdio.h> +#include <stdlib.h> +#include <error.h> +#include <errno.h> +#include "getch.h" + +#define MAXBUFF 100 + +int gtop; +char buff[MAXBUFF]; + +char getch(void) +{ + return (gtop > 0) ? buff[--gtop] : getchar(); +} + +void ungetch(char c) +{ + if (gtop < MAXBUFF) + buff[gtop++] = c; + else + error(EXIT_FAILURE, ENOBUFS, "getch"); +} diff --git a/7.2/getch.h b/7.2/getch.h new file mode 100644 index 0000000..b0e086b --- /dev/null +++ b/7.2/getch.h @@ -0,0 +1,2 @@ +char getch(void); +void ungetch(char c); diff --git a/7.2/main.c b/7.2/main.c new file mode 100644 index 0000000..f11256e --- /dev/null +++ b/7.2/main.c @@ -0,0 +1,50 @@ +#include <stdio.h> +#include <ctype.h> +#include "getch.h" + +#define MAXLINE 80 +#define OCTLEN 6 + +int inc(int col, int n); + +int main(void) +{ + char c, col, next; + + col = 0; + while ((c = getch()) != EOF) { + if (iscntrl(c) || isblank(c)) { + col = inc(col, OCTLEN); + printf(" \\%03o ", c); + + if (c == '\n') { + col = 0; + putchar(c); + } + } else { + col = inc(col, 1); + /* ac- + * k */ + ungetch(next = getch()); + if (col == MAXLINE && isalpha(c) && + isalpha(next)) { + ungetch(c); + c = '-'; + } + + putchar(c); + } + } + + return 0; +} + +int inc(int col, int n) +{ + if (col + n > MAXLINE) { + putchar('\n'); + return n; + } else { + return col + n; + } +} |