From f0d3ef8a48d7112057f100fc1001d2859f99d528 Mon Sep 17 00:00:00 2001
From: Mactavish <maczhanchao@yahoo.com>
Date: Tue, 2 May 2023 23:56:47 +0200
Subject: [PATCH] add process exercise

---
 exercises/process/makefile   | 20 ++++++++++++
 exercises/process/mysystem.c | 61 ++++++++++++++++++++++++++++++++++++
 2 files changed, 81 insertions(+)
 create mode 100644 exercises/process/makefile
 create mode 100644 exercises/process/mysystem.c

diff --git a/exercises/process/makefile b/exercises/process/makefile
new file mode 100644
index 0000000..5ef480d
--- /dev/null
+++ b/exercises/process/makefile
@@ -0,0 +1,20 @@
+# Define required macros here
+PROG = mysystem
+OBJS = mysystem.o
+CFLAG = -Wall -g
+CC = gcc
+INCLUDE = -I.
+
+# binary target
+$(PROG): $(OBJS)
+	$(CC) $(CFLAGS) $(INCLUDES) -o $@ $(OBJS)
+
+# implicit rule for building .o from .c
+%.o: %.c
+	$(CC) -c -o $@ $< $(CFLAGS) $(INCLUDES)
+
+.PHONY: clean
+# clean all the .o and executable files
+clean:
+	rm -rf *.o $(PROG)
+
diff --git a/exercises/process/mysystem.c b/exercises/process/mysystem.c
new file mode 100644
index 0000000..1d8c8ea
--- /dev/null
+++ b/exercises/process/mysystem.c
@@ -0,0 +1,61 @@
+/**
+ * File: mysystem.c
+ * ----------------
+ * A program containing an implementation of a working shell that can
+ * execute entered text commands.  It relies on our own implementation
+ * of system(), called mysystem(), that creates a process to execute
+ * a given shell command.
+ */
+
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/wait.h>
+#include <unistd.h>
+#include <errno.h>
+
+// The max-length shell command the user can enter
+static const size_t COMMAND_MAX_LEN = 2048;
+
+/* Function: mysystem
+ * ------------------
+ * Parameters:
+ *     command - the shell command to execute in another process
+ * Returns: the exit status of the command execution
+ *
+ * This function executes the given shell command using /bin/sh.
+ * It executes it in another process, and waits for that process
+ * to complete before returning.  This function returns its exit status.
+ *
+ * Hint: use fork(), exec() family of functions, and waitpid()
+ * use "/bin/sh" and "-c" als the first two arguments to exec() family of
+ * Further information: https://www.baeldung.com/linux/exec-functions
+ */
+// Uncomment when implemented
+static int mysystem(char *command) {
+  // TODO: implement this function
+}
+
+int main(int argc, char *argv[]) {
+  char command[COMMAND_MAX_LEN];
+  while (true) {
+    printf("> ");
+    fgets(command, sizeof(command), stdin);
+
+    // If the user entered Ctl-d, stop
+    if (feof(stdin)) {
+      break;
+    }
+
+    // Remove the \n that fgets puts at the end
+    command[strlen(command) - 1] = '\0';
+
+    // TODO: use mysystem instead of system
+    int commandReturnCode = system(command);
+    printf("return code = %d\n", commandReturnCode);
+  }
+
+  printf("\n");
+  return 0;
+}
-- 
GitLab