diff --git a/exercises/process/makefile b/exercises/process/makefile new file mode 100644 index 0000000000000000000000000000000000000000..5ef480d6381a3c6c1f5099b10feb17019c8c6eff --- /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 0000000000000000000000000000000000000000..1d8c8ea6a3c84b240d8c09afa761cbcbe9c7febc --- /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; +}