From f481f43596073d7f5cee231da901fb43d6af8366 Mon Sep 17 00:00:00 2001 From: sinanmohd Date: Thu, 11 May 2023 16:09:33 +0530 Subject: 7.2: initial commit --- 7.2/Makefile | 11 +++++++++++ 7.2/getch.c | 23 +++++++++++++++++++++++ 7.2/getch.h | 2 ++ 7.2/main.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 86 insertions(+) create mode 100644 7.2/Makefile create mode 100644 7.2/getch.c create mode 100644 7.2/getch.h create mode 100644 7.2/main.c 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 +#include +#include +#include +#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 +#include +#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; + } +} -- cgit v1.2.3