about summary refs log tree commit diff stats
path: root/dev/c/src/basic
diff options
context:
space:
mode:
authorpunk <punk@libernaut>2021-04-21 15:26:19 +0100
committerpunk <punk@libernaut>2021-04-21 15:26:19 +0100
commiteac48b5a8d709135a95abcc2243b369095f074f4 (patch)
treea2e34d995cef5ac8068ec7047e93b1125c80d175 /dev/c/src/basic
parent3bd43803fc8cb7a39a87394cb7c491ddc151e06b (diff)
parent452477a2635d85ecf772a5242ce97d9479503bb3 (diff)
downloaddoc-eac48b5a8d709135a95abcc2243b369095f074f4.tar.gz
release 0.7.0
Diffstat (limited to 'dev/c/src/basic')
-rw-r--r--dev/c/src/basic/Makefile4
-rw-r--r--dev/c/src/basic/shell.c57
2 files changed, 59 insertions, 2 deletions
diff --git a/dev/c/src/basic/Makefile b/dev/c/src/basic/Makefile
index f165c15..88f7890 100644
--- a/dev/c/src/basic/Makefile
+++ b/dev/c/src/basic/Makefile
@@ -1,7 +1,7 @@
 CC=gcc
 CFLAGS=-Wall
 
-basic-c: main.o basic.o
+shell: shell.c
 
 clean:
-	rm -f *.o basic-c
+	rm -f *.o shell
diff --git a/dev/c/src/basic/shell.c b/dev/c/src/basic/shell.c
new file mode 100644
index 0000000..addc8a7
--- /dev/null
+++ b/dev/c/src/basic/shell.c
@@ -0,0 +1,57 @@
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <errno.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sysexits.h>
+#include <unistd.h>
+
+char *getinput(char *buffer, size_t buflen) {
+	printf("$$ ");
+	return fgets(buffer, buflen, stdin);
+}
+
+void sig_int(int signo) {
+	printf("\nCaught SIGINT (Signal #%d)!\n$$ ", signo);
+	(void)fflush(stdout);
+}
+
+int main(int argc, char **argv) {
+	char buf[BUFSIZ];
+	pid_t pid;
+	int status;
+
+	/* cast to void to silence compiler warnings */
+	(void)argc;
+	(void)argv;
+
+	if (signal(SIGINT, sig_int) == SIG_ERR) {
+		fprintf(stderr, "signal error: %s\n", strerror(errno));
+		exit(1);
+	}
+
+	while (getinput(buf, sizeof(buf))) {
+		buf[strlen(buf) - 1] = '\0';
+
+		if((pid=fork()) == -1) {
+			fprintf(stderr, "shell: can't fork: %s\n",
+					strerror(errno));
+			continue;
+		} else if (pid == 0) {   /* child */
+			execlp(buf, buf, (char *)0);
+			fprintf(stderr, "shell: couldn't exec %s: %s\n", buf,
+					strerror(errno));
+			exit(EX_UNAVAILABLE);
+		}
+
+		/* parent waits */
+		if ((pid=waitpid(pid, &status, 0)) < 0) {
+			fprintf(stderr, "shell: waitpid error: %s\n",
+					strerror(errno));
+		}
+	}
+
+	exit(EX_OK);
+}