Browse Source

Add detecttion of OS with preprocessor

T. Meissner 6 months ago
parent
commit
6e6e07fff7
1 changed files with 26 additions and 0 deletions
  1. 26
    0
      chapter_04/prompt.c

+ 26
- 0
chapter_04/prompt.c View File

@@ -1,7 +1,33 @@
1 1
 #include <stdio.h>
2 2
 #include <stdlib.h>
3 3
 
4
+
5
+/* If we are on Windows compile these functions */
6
+#ifdef _WIN32
7
+#include <string.h>
8
+#include <assert.h>
9
+
10
+static char buffer[2048];
11
+
12
+/* Fake readline function */
13
+char* readline(char* prompt) {
14
+    fputs(prompt, stdout);
15
+    fgets(buffer, 2048, stdin);
16
+    char* cpy = malloc(strlen(buffer)+1);
17
+    assert(cpy != NULL)
18
+    strcpy(cpy, buffer);
19
+    cpy[strlen(cpy)-1] = '\0';
20
+    return cpy;
21
+}
22
+
23
+/* Fake add_history function */
24
+void add_history(char* unused) {}
25
+
26
+/* Otherwise include the editline headers
27
+   could use __APPLE__ for detection of OSX */
28
+#else
4 29
 #include <editline/readline.h>
30
+#endif
5 31
 
6 32
 
7 33
 int main(int argc, char const *argv[])