From cbdcb1c53bb6522c1cd8d38d91280500f439e4fb Mon Sep 17 00:00:00 2001 From: nasrlol Date: Thu, 13 Feb 2025 00:01:32 +0100 Subject: fixed some basic program problems, but at the moment im having issue with a segfault somehow somewhere --- .clang-tidy | 147 +++++++++++++++++++++ .../v1/reply/index-2025-02-12T21-48-20-0243.json | 108 --------------- .../v1/reply/index-2025-02-12T22-07-31-0839.json | 108 +++++++++++++++ folder/file | 0 main | Bin 15728 -> 15784 bytes main.c | 87 +++--------- 6 files changed, 277 insertions(+), 173 deletions(-) create mode 100644 .clang-tidy delete mode 100644 cmake-build-debug/.cmake/api/v1/reply/index-2025-02-12T21-48-20-0243.json create mode 100644 cmake-build-debug/.cmake/api/v1/reply/index-2025-02-12T22-07-31-0839.json create mode 100644 folder/file diff --git a/.clang-tidy b/.clang-tidy new file mode 100644 index 0000000..fd8c681 --- /dev/null +++ b/.clang-tidy @@ -0,0 +1,147 @@ +# Generated from CLion Inspection settings +--- +Checks: '-*, +bugprone-argument-comment, +bugprone-assert-side-effect, +bugprone-bad-signal-to-kill-thread, +bugprone-branch-clone, +bugprone-copy-constructor-init, +bugprone-dangling-handle, +bugprone-dynamic-static-initializers, +bugprone-fold-init-type, +bugprone-forward-declaration-namespace, +bugprone-forwarding-reference-overload, +bugprone-inaccurate-erase, +bugprone-incorrect-roundings, +bugprone-integer-division, +bugprone-lambda-function-name, +bugprone-macro-parentheses, +bugprone-macro-repeated-side-effects, +bugprone-misplaced-operator-in-strlen-in-alloc, +bugprone-misplaced-pointer-arithmetic-in-alloc, +bugprone-misplaced-widening-cast, +bugprone-move-forwarding-reference, +bugprone-multiple-statement-macro, +bugprone-no-escape, +bugprone-parent-virtual-call, +bugprone-posix-return, +bugprone-reserved-identifier, +bugprone-sizeof-container, +bugprone-sizeof-expression, +bugprone-spuriously-wake-up-functions, +bugprone-string-constructor, +bugprone-string-integer-assignment, +bugprone-string-literal-with-embedded-nul, +bugprone-suspicious-enum-usage, +bugprone-suspicious-include, +bugprone-suspicious-memset-usage, +bugprone-suspicious-missing-comma, +bugprone-suspicious-semicolon, +bugprone-suspicious-string-compare, +bugprone-suspicious-memory-comparison, +bugprone-suspicious-realloc-usage, +bugprone-swapped-arguments, +bugprone-terminating-continue, +bugprone-throw-keyword-missing, +bugprone-too-small-loop-variable, +bugprone-undefined-memory-manipulation, +bugprone-undelegated-constructor, +bugprone-unhandled-self-assignment, +bugprone-unused-raii, +bugprone-unused-return-value, +bugprone-use-after-move, +bugprone-virtual-near-miss, +cert-dcl21-cpp, +cert-dcl58-cpp, +cert-err34-c, +cert-err52-cpp, +cert-err60-cpp, +cert-flp30-c, +cert-msc50-cpp, +cert-msc51-cpp, +cert-str34-c, +cppcoreguidelines-interfaces-global-init, +cppcoreguidelines-narrowing-conversions, +cppcoreguidelines-pro-type-member-init, +cppcoreguidelines-pro-type-static-cast-downcast, +cppcoreguidelines-slicing, +google-default-arguments, +google-explicit-constructor, +google-runtime-operator, +hicpp-exception-baseclass, +hicpp-multiway-paths-covered, +misc-misplaced-const, +misc-new-delete-overloads, +misc-no-recursion, +misc-non-copyable-objects, +misc-throw-by-value-catch-by-reference, +misc-unconventional-assign-operator, +misc-uniqueptr-reset-release, +modernize-avoid-bind, +modernize-concat-nested-namespaces, +modernize-deprecated-headers, +modernize-deprecated-ios-base-aliases, +modernize-loop-convert, +modernize-make-shared, +modernize-make-unique, +modernize-pass-by-value, +modernize-raw-string-literal, +modernize-redundant-void-arg, +modernize-replace-auto-ptr, +modernize-replace-disallow-copy-and-assign-macro, +modernize-replace-random-shuffle, +modernize-return-braced-init-list, +modernize-shrink-to-fit, +modernize-unary-static-assert, +modernize-use-auto, +modernize-use-bool-literals, +modernize-use-emplace, +modernize-use-equals-default, +modernize-use-equals-delete, +modernize-use-nodiscard, +modernize-use-noexcept, +modernize-use-nullptr, +modernize-use-override, +modernize-use-transparent-functors, +modernize-use-uncaught-exceptions, +mpi-buffer-deref, +mpi-type-mismatch, +openmp-use-default-none, +performance-faster-string-find, +performance-for-range-copy, +performance-implicit-conversion-in-loop, +performance-inefficient-algorithm, +performance-inefficient-string-concatenation, +performance-inefficient-vector-operation, +performance-move-const-arg, +performance-move-constructor-init, +performance-no-automatic-move, +performance-noexcept-move-constructor, +performance-trivially-destructible, +performance-type-promotion-in-math-fn, +performance-unnecessary-copy-initialization, +performance-unnecessary-value-param, +portability-simd-intrinsics, +readability-avoid-const-params-in-decls, +readability-const-return-type, +readability-container-size-empty, +readability-convert-member-functions-to-static, +readability-delete-null-pointer, +readability-deleted-default, +readability-inconsistent-declaration-parameter-name, +readability-make-member-function-const, +readability-misleading-indentation, +readability-misplaced-array-index, +readability-non-const-parameter, +readability-redundant-control-flow, +readability-redundant-declaration, +readability-redundant-function-ptr-dereference, +readability-redundant-smartptr-get, +readability-redundant-string-cstr, +readability-redundant-string-init, +readability-simplify-subscript-expr, +readability-static-accessed-through-instance, +readability-static-definition-in-anonymous-namespace, +readability-string-compare, +readability-uniqueptr-delete-release, +readability-use-anyofallof' \ No newline at end of file diff --git a/cmake-build-debug/.cmake/api/v1/reply/index-2025-02-12T21-48-20-0243.json b/cmake-build-debug/.cmake/api/v1/reply/index-2025-02-12T21-48-20-0243.json deleted file mode 100644 index d64b556..0000000 --- a/cmake-build-debug/.cmake/api/v1/reply/index-2025-02-12T21-48-20-0243.json +++ /dev/null @@ -1,108 +0,0 @@ -{ - "cmake" : - { - "generator" : - { - "multiConfig" : false, - "name" : "Ninja" - }, - "paths" : - { - "cmake" : "/usr/bin/clion-2024.3.2/bin/cmake/linux/x64/bin/cmake", - "cpack" : "/usr/bin/clion-2024.3.2/bin/cmake/linux/x64/bin/cpack", - "ctest" : "/usr/bin/clion-2024.3.2/bin/cmake/linux/x64/bin/ctest", - "root" : "/usr/bin/clion-2024.3.2/bin/cmake/linux/x64/share/cmake-3.30" - }, - "version" : - { - "isDirty" : false, - "major" : 3, - "minor" : 30, - "patch" : 5, - "string" : "3.30.5", - "suffix" : "" - } - }, - "objects" : - [ - { - "jsonFile" : "codemodel-v2-99875c5b2244f821ca13.json", - "kind" : "codemodel", - "version" : - { - "major" : 2, - "minor" : 7 - } - }, - { - "jsonFile" : "cache-v2-4f865ba5d5c6118bca9f.json", - "kind" : "cache", - "version" : - { - "major" : 2, - "minor" : 0 - } - }, - { - "jsonFile" : "cmakeFiles-v1-aea6057ee7f821d67024.json", - "kind" : "cmakeFiles", - "version" : - { - "major" : 1, - "minor" : 1 - } - }, - { - "jsonFile" : "toolchains-v1-142e64fff77130cba846.json", - "kind" : "toolchains", - "version" : - { - "major" : 1, - "minor" : 0 - } - } - ], - "reply" : - { - "cache-v2" : - { - "jsonFile" : "cache-v2-4f865ba5d5c6118bca9f.json", - "kind" : "cache", - "version" : - { - "major" : 2, - "minor" : 0 - } - }, - "cmakeFiles-v1" : - { - "jsonFile" : "cmakeFiles-v1-aea6057ee7f821d67024.json", - "kind" : "cmakeFiles", - "version" : - { - "major" : 1, - "minor" : 1 - } - }, - "codemodel-v2" : - { - "jsonFile" : "codemodel-v2-99875c5b2244f821ca13.json", - "kind" : "codemodel", - "version" : - { - "major" : 2, - "minor" : 7 - } - }, - "toolchains-v1" : - { - "jsonFile" : "toolchains-v1-142e64fff77130cba846.json", - "kind" : "toolchains", - "version" : - { - "major" : 1, - "minor" : 0 - } - } - } -} diff --git a/cmake-build-debug/.cmake/api/v1/reply/index-2025-02-12T22-07-31-0839.json b/cmake-build-debug/.cmake/api/v1/reply/index-2025-02-12T22-07-31-0839.json new file mode 100644 index 0000000..d64b556 --- /dev/null +++ b/cmake-build-debug/.cmake/api/v1/reply/index-2025-02-12T22-07-31-0839.json @@ -0,0 +1,108 @@ +{ + "cmake" : + { + "generator" : + { + "multiConfig" : false, + "name" : "Ninja" + }, + "paths" : + { + "cmake" : "/usr/bin/clion-2024.3.2/bin/cmake/linux/x64/bin/cmake", + "cpack" : "/usr/bin/clion-2024.3.2/bin/cmake/linux/x64/bin/cpack", + "ctest" : "/usr/bin/clion-2024.3.2/bin/cmake/linux/x64/bin/ctest", + "root" : "/usr/bin/clion-2024.3.2/bin/cmake/linux/x64/share/cmake-3.30" + }, + "version" : + { + "isDirty" : false, + "major" : 3, + "minor" : 30, + "patch" : 5, + "string" : "3.30.5", + "suffix" : "" + } + }, + "objects" : + [ + { + "jsonFile" : "codemodel-v2-99875c5b2244f821ca13.json", + "kind" : "codemodel", + "version" : + { + "major" : 2, + "minor" : 7 + } + }, + { + "jsonFile" : "cache-v2-4f865ba5d5c6118bca9f.json", + "kind" : "cache", + "version" : + { + "major" : 2, + "minor" : 0 + } + }, + { + "jsonFile" : "cmakeFiles-v1-aea6057ee7f821d67024.json", + "kind" : "cmakeFiles", + "version" : + { + "major" : 1, + "minor" : 1 + } + }, + { + "jsonFile" : "toolchains-v1-142e64fff77130cba846.json", + "kind" : "toolchains", + "version" : + { + "major" : 1, + "minor" : 0 + } + } + ], + "reply" : + { + "cache-v2" : + { + "jsonFile" : "cache-v2-4f865ba5d5c6118bca9f.json", + "kind" : "cache", + "version" : + { + "major" : 2, + "minor" : 0 + } + }, + "cmakeFiles-v1" : + { + "jsonFile" : "cmakeFiles-v1-aea6057ee7f821d67024.json", + "kind" : "cmakeFiles", + "version" : + { + "major" : 1, + "minor" : 1 + } + }, + "codemodel-v2" : + { + "jsonFile" : "codemodel-v2-99875c5b2244f821ca13.json", + "kind" : "codemodel", + "version" : + { + "major" : 2, + "minor" : 7 + } + }, + "toolchains-v1" : + { + "jsonFile" : "toolchains-v1-142e64fff77130cba846.json", + "kind" : "toolchains", + "version" : + { + "major" : 1, + "minor" : 0 + } + } + } +} diff --git a/folder/file b/folder/file new file mode 100644 index 0000000..e69de29 diff --git a/main b/main index cd6a581..73fc58a 100755 Binary files a/main and b/main differ diff --git a/main.c b/main.c index 901b2c4..0be7c07 100644 --- a/main.c +++ b/main.c @@ -1,27 +1,19 @@ #include #include #include - -#include #include #include -#include -#include #define MAX_HISTORY 100 #define MAX_COMMAND_LENGTH 256 -// static int CURRENT_COMMAND_INDEX = 0; -// static int CURRENT_HISTORY_INDEX = 0; -// static char *COMMAND_HISTORY[MAX_HISTORY] = { NULL}; - struct com_struct { char *com; char *arg; }; void test() { - printf("print to output"); + printf("\nprint to output"); } // exit() command @@ -43,26 +35,14 @@ void ls(const char *path) { // print the folder|directory name while ((entry = readdir(dP)) != NULL) { - printf("s\n", entry->d_name); + printf("%s", entry->d_name); } closedir(dP); } -// pwd command -char *pwd() { - char *current_working_directory = malloc(sizeof(char) * 100); - - if (getcwd(current_working_directory, sizeof(current_working_directory) != 0)) { - return current_working_directory; - } else { - perror("getcwd() error"); - return "ERROR"; - } -} - // echo command -char *echo(char *command) { - char *argv = (char *) malloc(sizeof(char) * 4); +char *echo(const char *command) { + const char *argv = (char *) malloc(sizeof(char) * 4); char *command_argument = (char *) malloc(sizeof(char) * 100); int separation_index = 0; @@ -76,7 +56,6 @@ char *echo(char *command) { } } if (strcmp(argv, "echo") == 0) { - for (int i = separation_index + 1; i < (sizeof(command) - separation_index); i++) { if (strcmp(&command[i], "\0") != 0) { break; @@ -89,60 +68,38 @@ char *echo(char *command) { } struct com_struct split_command(char *input) { - struct com_struct NEW_COMMAND; - char *command = (char *) malloc(sizeof(char) * 4); - char *argument = (char *) malloc(sizeof(char) * 100); + const char *command = strtok(input, ""); + const char *argument = strtok(NULL, "\n"); - if (command == NULL || argument == NULL) { - printf("found a null pointer when dynamically assigning memory to the command or variable argument"); - exit(1); - } - - int space_position = 0; - for (int i = 0; i < sizeof(command); i++) { - if (strcmp(&input[i], " ") == 0) { - space_position = i; - - for (int j = 0; j < space_position; j++) { - command = &input[j]; - } - for (int x = space_position; x < sizeof(input) - space_position; x++) { - argument = &input[x]; - } - } - - } - NEW_COMMAND.com = command; - NEW_COMMAND.arg = argument; + NEW_COMMAND.com = command ? strdup(command) : NULL; + NEW_COMMAND.arg = command ? strdup(argument) : NULL; return NEW_COMMAND; - } int main(void) { - char *input = malloc(sizeof(char *) * MAX_COMMAND_LENGTH); + while (1) { + char *input = malloc(MAX_COMMAND_LENGTH); - do { - if (getchar()) { - printf("\nSHELL> "); - scanf("%s", input); + // print the shell sign + printf("\n $ "); - struct com_struct new_input = split_command(input); + // get the user input, store it and split it up + fgets(input, MAX_COMMAND_LENGTH, stdin); + const struct com_struct new_input = split_command(input); if (strcmp(new_input.com, "ls") == 0) { - char *path; - path = (char *) malloc(sizeof(char) * 100); - - strcpy(path, new_input.arg); - ls(path); - } else if (strcmp(new_input.com, "test") == 0){ + ls(new_input.arg); + } else if (strcmp(new_input.com, "test") == 0) { test(); - } + } else if (strcmp(new_input.com, "exit") == 0) { + free(input); + free(new_input.com); + free(new_input.arg); + exit_(); } } - while (true); - return 0; } -- cgit v1.2.3-70-g09d2