aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--6.1/Makefile11
-rw-r--r--6.1/getch.c23
-rw-r--r--6.1/getch.h2
-rw-r--r--6.1/key.c22
-rw-r--r--6.1/key.h13
-rw-r--r--6.1/main.c70
-rw-r--r--6.1/word.c63
-rw-r--r--6.1/word.h1
8 files changed, 205 insertions, 0 deletions
diff --git a/6.1/Makefile b/6.1/Makefile
new file mode 100644
index 0000000..eaee48d
--- /dev/null
+++ b/6.1/Makefile
@@ -0,0 +1,11 @@
+objects = main.o getch.o word.o key.o
+
+keys : $(objects)
+ cc -o keys $(objects)
+
+main.o : word.h key.h
+word.o : getch.h
+
+.PHONY : clean
+clean :
+ rm keys $(objects)
diff --git a/6.1/getch.c b/6.1/getch.c
new file mode 100644
index 0000000..2b86efe
--- /dev/null
+++ b/6.1/getch.c
@@ -0,0 +1,23 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <error.h>
+#include <errno.h>
+#include "getch.h"
+
+#define MAXBUFF 100
+
+char buff[MAXBUFF];
+static size_t top;
+
+void ungetch(char c)
+{
+ if (top < MAXBUFF)
+ buff[top++] = c;
+ else
+ error(EXIT_FAILURE, ENOBUFS , "getch");
+}
+
+char getch(void)
+{
+ return (top > 0) ? buff[--top] : getchar();
+}
diff --git a/6.1/getch.h b/6.1/getch.h
new file mode 100644
index 0000000..cbfe8ad
--- /dev/null
+++ b/6.1/getch.h
@@ -0,0 +1,2 @@
+void ungetch(char c);
+char getch(void);
diff --git a/6.1/key.c b/6.1/key.c
new file mode 100644
index 0000000..33757ee
--- /dev/null
+++ b/6.1/key.c
@@ -0,0 +1,22 @@
+#include <string.h>
+#include "key.h"
+
+int bsearch(char *word, struct key tab[], int n)
+{
+ int cond;
+ int low, high, mid;
+
+ low = 0;
+ high = n - 1;
+ while (low <= high) {
+ mid = (low+high)/2;
+ if ((cond = strcmp(word, tab[mid].word)) < 0)
+ high = mid - 1;
+ else if (cond > 0)
+ low = mid + 1;
+ else
+ return mid;
+ }
+
+ return -1;
+}
diff --git a/6.1/key.h b/6.1/key.h
new file mode 100644
index 0000000..1cba74e
--- /dev/null
+++ b/6.1/key.h
@@ -0,0 +1,13 @@
+#ifndef KEY_H
+
+#define NKEYS (sizeof(keytab) / sizeof(keytab[0]))
+
+struct key {
+ const char *word;
+ int count;
+};
+
+#define KEY_H
+#endif
+
+int bsearch(char *word, struct key tab[], int n);
diff --git a/6.1/main.c b/6.1/main.c
new file mode 100644
index 0000000..ce53539
--- /dev/null
+++ b/6.1/main.c
@@ -0,0 +1,70 @@
+#include <stdio.h>
+#include <ctype.h>
+#include "word.h"
+#include "key.h"
+
+#define MAXWORD 100
+
+struct key keytab[] = {
+ "#define", 0,
+ "#elif", 0,
+ "#else", 0,
+ "#endif", 0,
+ "#error", 0,
+ "#if", 0,
+ "#ifdef", 0,
+ "#ifndef", 0,
+ "#include", 0,
+ "#pragma", 0,
+ "#undef", 0,
+ "auto", 0,
+ "break", 0,
+ "case", 0,
+ "char", 0,
+ "const", 0,
+ "continue", 0,
+ "default", 0,
+ "do", 0,
+ "double", 0,
+ "else", 0,
+ "enum", 0,
+ "extern", 0,
+ "float", 0,
+ "for", 0,
+ "goto", 0,
+ "if", 0,
+ "int", 0,
+ "long", 0,
+ "register", 0,
+ "return", 0,
+ "short", 0,
+ "signed", 0,
+ "sizeof", 0,
+ "static", 0,
+ "struct", 0,
+ "switch", 0,
+ "typedef", 0,
+ "union", 0,
+ "unsigned", 0,
+ "void", 0,
+ "volatile", 0,
+ "while", 0,
+};
+
+int main(void)
+{
+ int n;
+ char word[MAXWORD];
+
+ while (getword(word, MAXWORD) != EOF)
+ if (isalpha(word[0]) || word[0] == '#')
+ if ((n = bsearch(word, keytab, NKEYS)) >= 0)
+ keytab[n].count++;
+
+ for (n = 0; n < NKEYS; ++n)
+ if(keytab[n].count > 0)
+ printf("%4d %s\n",
+ keytab[n].count, keytab[n].word);
+
+ return 0;
+}
diff --git a/6.1/word.c b/6.1/word.c
new file mode 100644
index 0000000..538198f
--- /dev/null
+++ b/6.1/word.c
@@ -0,0 +1,63 @@
+#include <stdio.h>
+#include <ctype.h>
+#include "getch.h"
+#include "word.h"
+
+char scomment();
+
+int getword(char *word, int lim)
+{
+ int instr = 0;
+ char *w = word;
+
+ while (isblank(*w = getch()) || *w == '"' || *w == '/') {
+ if (*w == '"') {
+ while ((*w = getch()) != '"' && *w != EOF)
+ ;
+ if (*w == EOF)
+ ungetch(*w);
+ } else if (*w == '/' && !scomment()) {
+ break;
+ }
+ }
+
+ if (isalpha(*w)) {
+ while ((isalnum(*w) || *w == '_') && --lim > 0)
+ *++w = getch();
+ ungetch(*w--);
+ } else if (*w == '#' && --lim > 0) {
+ do {
+ *++w = getch();
+ } while (isalpha(*w) && --lim > 0);
+ ungetch(*w--);
+ }
+
+ *++w = '\0';
+ return word[0];
+}
+
+char scomment()
+{
+ char c;
+
+ switch (c = getch()) {
+ case '/' :
+ while ((c = getch()) != EOF && c != '\n')
+ ;
+ if (c == EOF)
+ ungetch(c);
+
+ return '/';
+ case '*' :
+ while ((c = getch()) != EOF && !(c == '*' && (c = getch()) == '/'))
+ if (c == '*')
+ ungetch(c);
+ if (c == EOF)
+ ungetch(c);
+
+ return '*';
+ default:
+ ungetch(c);
+ return 0;
+ }
+}
diff --git a/6.1/word.h b/6.1/word.h
new file mode 100644
index 0000000..5714c50
--- /dev/null
+++ b/6.1/word.h
@@ -0,0 +1 @@
+int getword(char *word, int len);