diff options
Diffstat (limited to '02_exercise')
-rw-r--r-- | 02_exercise/.gitignore | 1 | ||||
-rw-r--r-- | 02_exercise/CMakeLists.txt | 35 | ||||
-rw-r--r-- | 02_exercise/Makefile | 2 | ||||
-rw-r--r-- | 02_exercise/process.c | 16 | ||||
-rw-r--r-- | 02_exercise/process_test.c | 26 | ||||
-rw-r--r-- | 02_exercise/prompt_utils_test.c | 4 | ||||
-rw-r--r-- | 02_exercise/shell.c | 3 |
7 files changed, 46 insertions, 41 deletions
diff --git a/02_exercise/.gitignore b/02_exercise/.gitignore index 5aacf99..819a501 100644 --- a/02_exercise/.gitignore +++ b/02_exercise/.gitignore @@ -1,2 +1,3 @@ prog shell +build
\ No newline at end of file diff --git a/02_exercise/CMakeLists.txt b/02_exercise/CMakeLists.txt index ca67d1a..9cae089 100644 --- a/02_exercise/CMakeLists.txt +++ b/02_exercise/CMakeLists.txt @@ -6,9 +6,13 @@ set(CMAKE_C_COMPILER gcc) set(CMAKE_C_STANDARD 11) set(CMAKE_C_STANDARD_REQUIRED True) +find_package(Sanitizers) + add_executable(prog prog.c) add_executable(shell shell.c) target_link_libraries(shell PRIVATE array prompt_utils process) + +add_sanitizers(shell) add_compile_definitions(_GNU_SOURCE) add_library(array array.c) @@ -25,33 +29,6 @@ target_link_libraries(process PRIVATE array) add_executable(process_test process_test.c) target_link_libraries(process_test PRIVATE process) - - -set(CLANG_WARNINGS - -Wall - -Wextra # reasonable and standard - -Wshadow # warn the user if a variable declaration shadows one from a - # parent context - -Wcast-align # warn for potential performance problem casts - -Wunused # warn on anything being unused - -Wpedantic # warn if non-standard C++ is used - -Wconversion # warn on type conversions that may lose data - -Wsign-conversion # warn on sign conversions - -Wnull-dereference # warn if a null dereference is detected - -Wdouble-promotion # warn if float is implicit promoted to double - -Wformat=2 # warn on security issues around functions that format output - # (ie printf) - -Werror - ) -set(GCC_WARNINGS - ${CLANG_WARNINGS} - -Wmisleading-indentation # warn if indentation implies blocks where blocks - # do not exist - -Wduplicated-cond # warn if if / else chain has duplicated conditions - -Wduplicated-branches # warn if if / else branches have duplicated code - -Wlogical-op # warn about logical operations being used where bitwise were - # probably wanted - ) -set(PROJECT_WARNINGS ${GCC_WARNINGS}) target_compile_options(shell INTERFACE ${PROJECT_WARNINGS}) -target_compile_options(prompt_utils INTERFACE ${PROJECT_WARNINGS})
\ No newline at end of file +target_compile_options(prompt_utils INTERFACE ${PROJECT_WARNINGS}) +target_compile_options(process INTERFACE ${PROJECT_WARNINGS}) diff --git a/02_exercise/Makefile b/02_exercise/Makefile index 5d1b360..277b597 100644 --- a/02_exercise/Makefile +++ b/02_exercise/Makefile @@ -19,7 +19,7 @@ all: $(TAR) prog: prog.o $(CC) -o $@ $^ -shell: $(filter-out prog.o,$(OBJ)) +shell: $(filter-out prog.o prompt_utils_test.o process_test.o, $(OBJ)) $(CC) -o $@ $^ run: all diff --git a/02_exercise/process.c b/02_exercise/process.c index 8f6589d..fb55236 100644 --- a/02_exercise/process.c +++ b/02_exercise/process.c @@ -1,8 +1,8 @@ -#include <wait.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> +#include <wait.h> #include "array.h" #include "process.h" @@ -11,29 +11,33 @@ int parse_command(char const *line, char const *end, process *p) { char *part; char **local_parts; - if (arrayInit(part) != 0 || arrayInit(local_parts) != 0) { fprintf(stderr, "Failed to prepare new part / parts array whilst parsing line"); return -1; } for (size_t i = 0; line + i < end; ++i) { - char c = line[i]; - if (c == '\t' || c == ' ' || c == '\0') { + char c; + switch (c = line[i]) { + default: + arrayPush(part) = c; + break; + case ' ': + case '\t': + case '\0': if (arrayLen(part) == 0) continue; arrayPush(part) = '\0'; arrayPush(local_parts) = part; arrayInit(part); - } else { - arrayPush(part) = c; } } if (arrayLen(part) == 0) { arrayRelease(part); } else { + arrayPush(part) = '\0'; arrayPush(local_parts) = part; } diff --git a/02_exercise/process_test.c b/02_exercise/process_test.c index 591a06f..d7e59a4 100644 --- a/02_exercise/process_test.c +++ b/02_exercise/process_test.c @@ -53,11 +53,33 @@ void test_ls(){ free_processes(&processes); } -/* +void test_multiparse() { + Process *processes = NULL; + parse_line("echo abcdefg", &processes); + assert(arrayLen(processes) == 1); + Process p = processes[0]; + assert(arrayLen(p.argv)==3); + assert(p.argc == 2); + assert(strcmp(p.argv[0], "echo") == 0); + assert(strcmp(p.argv[1], "abcdefg") == 0); + assert(p.argc[p.argv] == NULL); + free_processes(&processes); + parse_line("echo abc", &processes); + assert(arrayLen(processes) == 1); + p = processes[0]; + assert(arrayLen(p.argv)==3); + assert(p.argc == 2); + assert(strcmp(p.argv[0], "echo") == 0); + assert(strcmp(p.argv[1], "abc") == 0); + assert(p.argc[p.argv] == NULL); +} + + int main() { test_ls(); test_simple_case(); test_detached(); test_pipe(); + test_multiparse(); return 0; -}*/ +} diff --git a/02_exercise/prompt_utils_test.c b/02_exercise/prompt_utils_test.c index 8bc29de..1be6162 100644 --- a/02_exercise/prompt_utils_test.c +++ b/02_exercise/prompt_utils_test.c @@ -36,10 +36,10 @@ void test_relative_path() { test_paths("/B/C", "/", "../.."); } -/* + int main(void) { test_relative_path(); test_get_separator_indices(); return 0; } -*/ + diff --git a/02_exercise/shell.c b/02_exercise/shell.c index d1e52ec..ea1b13f 100644 --- a/02_exercise/shell.c +++ b/02_exercise/shell.c @@ -31,7 +31,6 @@ int main(void) { // skip empty lines - empty being just spaces or tabs continue; } - line[length - 1] = '\0'; // cut the line feed process *processes = NULL; @@ -68,6 +67,8 @@ int main(void) { clean: free((void *)line); + line = NULL; + cap = 0; free_processes(&processes); } |