From dd4f3ec7551fddb88c5d6b96ed1aff3521c937ca Mon Sep 17 00:00:00 2001 From: Stefan Zabka Date: Thu, 21 May 2020 12:37:46 +0200 Subject: Added warning and cleaned them up --- 02_exercise/CMakeLists.txt | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 02_exercise/prompt_utils.c | 10 +++++----- 02_exercise/prompt_utils.h | 6 +++--- 02_exercise/shell.c | 2 +- 4 files changed, 55 insertions(+), 9 deletions(-) create mode 100644 02_exercise/CMakeLists.txt diff --git a/02_exercise/CMakeLists.txt b/02_exercise/CMakeLists.txt new file mode 100644 index 0000000..c2fa8d5 --- /dev/null +++ b/02_exercise/CMakeLists.txt @@ -0,0 +1,46 @@ +cmake_minimum_required(VERSION 3.5) + +project(shell) + +add_executable(shell shell.c) +target_link_libraries(shell PRIVATE array prompt_utils) +add_compile_definitions(_GNU_SOURCE) + +add_library(array array.c) + +add_library(prompt_utils prompt_utils.c) +target_link_libraries(prompt_utils PRIVATE array) + +set(CMAKE_C_STANDARD gnu11) +set(CMAKE_C_STANDARD_REQUIRED True) +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 + -Woverloaded-virtual # warn if you overload (not override) a virtual + # function + -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 + -Wuseless-cast # warn if you perform a cast to the same type + ) +set(PROJECT_WARNINGS ${CLANG_WARNINGS}) +target_compile_options(shell INTERFACE ${PROJECT_WARNINGS}) +target_compile_options(prompt_utils INTERFACE ${PROJECT_WARNINGS}) \ No newline at end of file diff --git a/02_exercise/prompt_utils.c b/02_exercise/prompt_utils.c index 4f86811..b7c836b 100644 --- a/02_exercise/prompt_utils.c +++ b/02_exercise/prompt_utils.c @@ -6,11 +6,11 @@ #include #include -char const *const relative_path(char const *const from_dir, char const *const to_dir) { +char const *relative_path(char const *const from_dir, char const *const to_dir) { + // easiest cases first { - int result = strcmp(from_dir, to_dir); - if (result == 0) { + if (strcmp(from_dir, to_dir) == 0) { char *return_value = malloc(2); strcpy(return_value, "."); return return_value; @@ -35,7 +35,7 @@ char const *const relative_path(char const *const from_dir, char const *const to size_t array_len = from_dir_len < to_dir_len ? from_dir_len : to_dir_len; size_t i = 0; size_t common_position = 0; - for (; i < array_len - 1; i++) { + for (; i < array_len - 1; ++i) { if (from_dir_indeces[i + 1] != to_dir_indeces[i + 1]) { break; } @@ -61,7 +61,7 @@ char const *const relative_path(char const *const from_dir, char const *const to strlen(go_up) * (levels_up - 1); return_value = malloc(length * sizeof(char)); strcpy(return_value, ".."); - for (size_t j = 0; j < levels_up - 1; j++) { + for (size_t j = 0; j < levels_up - 1; ++j) { strcat(return_value, go_up); } if (strcmp("/", to_dir) != 0) { diff --git a/02_exercise/prompt_utils.h b/02_exercise/prompt_utils.h index 908019c..a3a9d9e 100644 --- a/02_exercise/prompt_utils.h +++ b/02_exercise/prompt_utils.h @@ -4,8 +4,8 @@ #include // Returns the relative path to get from the `from_dir` to the `to_dir` -char const *const relative_path(char const *const from_dir, char const *const to_dir); +char const *relative_path(char const *from_dir, char const *to_dir); // Returns the position of each occurence of the seperator -size_t *get_seperator_indeces(char const *const text, char seperator); -#endif // PROMPT_UTILS_H_INCLUDED \ No newline at end of file +size_t *get_seperator_indeces(char const *text, char seperator); +#endif // PROMPT_UTILS_H_INCLUDED diff --git a/02_exercise/shell.c b/02_exercise/shell.c index d155a58..46b169a 100644 --- a/02_exercise/shell.c +++ b/02_exercise/shell.c @@ -27,7 +27,7 @@ int main(void) { while (true) { char *command = NULL; size_t cap = 0; - size_t length = 0; + __ssize_t length = 0; printf("%s > ", prompt); if ((length = getline(&command, &cap, stdin)) < 0) { -- cgit v1.2.3-54-g00ecf