aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--7.4/Makefile11
-rw-r--r--7.4/main.c15
-rw-r--r--7.4/mscanf.c34
-rw-r--r--7.4/mscanf.h1
4 files changed, 61 insertions, 0 deletions
diff --git a/7.4/Makefile b/7.4/Makefile
new file mode 100644
index 0000000..968458a
--- /dev/null
+++ b/7.4/Makefile
@@ -0,0 +1,11 @@
+OBJECTS = main.o mscanf.o
+CC = gcc
+CFLAGS = -Wvla -Wall -Wextra -Wstrict-prototypes -Wmissing-prototypes -fsanitize=address
+
+scan : $(OBJECTS)
+ $(CC) $(CFLAGS) -o scan $(OBJECTS)
+
+.PHONY : clean
+
+clean :
+ rm -f scan $(OBJECTS)
diff --git a/7.4/main.c b/7.4/main.c
new file mode 100644
index 0000000..097ca6e
--- /dev/null
+++ b/7.4/main.c
@@ -0,0 +1,15 @@
+#include <stdio.h>
+#include "mscanf.h"
+
+#define MAXLEN 100
+
+int main(void)
+{
+ int i;
+ char str[MAXLEN];
+
+ mscanf("%d %s", &i, &str);
+ printf("i: %d, s: %s\n", i, str);
+
+ return 0;
+}
diff --git a/7.4/mscanf.c b/7.4/mscanf.c
new file mode 100644
index 0000000..8f09f35
--- /dev/null
+++ b/7.4/mscanf.c
@@ -0,0 +1,34 @@
+#include <stdio.h>
+#include <stdarg.h>
+#include <ctype.h>
+#include "mscanf.h"
+
+#define MAXFMT 100
+
+void mscanf(char *fmt, ...)
+{
+ va_list ap;
+ int i;
+ char lfmt[MAXFMT];
+
+ va_start(ap, fmt);
+ for (; *fmt; fmt++) {
+ if (isblank(*fmt)) {
+ continue;
+ } else if (*fmt != '%') {
+ if (*fmt == getchar())
+ continue;
+ else
+ break;
+ }
+
+ i = 0;
+ while (i < MAXFMT && !isalpha(lfmt[i++] = *fmt++))
+ ;
+ lfmt[i] = '\0';
+ scanf(lfmt, va_arg(ap, void *));
+
+ fmt--;
+ }
+ va_end(ap);
+}
diff --git a/7.4/mscanf.h b/7.4/mscanf.h
new file mode 100644
index 0000000..0a7e67f
--- /dev/null
+++ b/7.4/mscanf.h
@@ -0,0 +1 @@
+void mscanf(char *fmt, ...);