From 98c04215a6e83f1de6424433ca35f97e68885b14 Mon Sep 17 00:00:00 2001 From: Pin Date: Sun, 5 Mar 2023 20:09:08 -0500 Subject: [PATCH] restruct & mem fixes --- cmd/dfs.c | 105 ++++++++++++------------------------------------- include/util.h | 1 + src/util.c | 68 ++++++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+), 80 deletions(-) diff --git a/cmd/dfs.c b/cmd/dfs.c index 3716d7b..1935be7 100644 --- a/cmd/dfs.c +++ b/cmd/dfs.c @@ -11,20 +11,13 @@ #include #include "dfs.h" +#include "util.h" #define clear() printf("\033[H\033[J") -static char *prompt = NULL; +char *prompt = NULL; static size_t commandRun = 0; -union pipe { - int fileDesc[2]; - struct { - int read; - int write; - }; -}; - static char *builtinFunctions[] = { "exit", "author", @@ -45,7 +38,6 @@ int builtinExit(char **args) { #else clear_history(); #endif - free(args); exit(EXIT_SUCCESS); } @@ -56,7 +48,6 @@ int builtinAuthor(char **args) { "Ask for the source code!\n" "\n"; printf("%s", message); - free(args); return 0; } @@ -102,12 +93,10 @@ int builtinCD(char **dir) { } free(changeDir); changeDir = NULL; - free(dir); return status; } int builtinNMN(char **args) { - free(args); return 0; } @@ -142,46 +131,55 @@ int runCommand(char *command) { return status; } -char **splitCMD(const char *line) { +char **splitCMD(const char *line, size_t *length) { size_t size = 8; size_t pos = 0; char **args = malloc(size * sizeof(char*)); char *arg = NULL; - char *lineTest = NULL; - lineTest = strdup(line); - if(line == NULL) { *args=NULL; return args; } + char *lineTest = strdup(line); + arg = strtok(lineTest, " \t\r\n\a"); while (arg != NULL) { - args[pos] = arg; + if (pos >= size) { + size *= 2; + args = realloc(args, size * sizeof(char*)); + } + + *length += 1; + args[pos] = strdup(arg); pos++; - if (pos >= size) { - size += 1; - args = realloc(args, size * sizeof(char*)); - - } arg = strtok(NULL, " \t\r\n\a"); } - free(arg); free(lineTest); - args[pos] = NULL; return args; } int execCommand(char *command) { - char **commandARGS = splitCMD(command); + size_t commandLen = 0; + char **commandARGS = splitCMD(command, &commandLen); for (size_t i = 0; i < (sizeof(builtinFunctions) / sizeof(char *)); i++) { if (strcmp(commandARGS[0], builtinFunctions[i]) == 0) { - return (*builtinFunc[i])(commandARGS); + (*builtinFunc[i])(commandARGS); + for (size_t i = 0; i < commandLen; i++) { + free(commandARGS[i]); + } + free(commandARGS); + return 0; } } + + for (size_t i = 0; i < commandLen; i++) { + free(commandARGS[i]); + } + free(commandARGS); return runCommand(command); } @@ -215,59 +213,6 @@ void initShell() { return; } -char *getPrompt() { - pid_t pid; - union pipe input, output; - FILE *outputFileD; - size_t len = 0; - int status = 0; - - static size_t promptCap = 0; - - pipe(input.fileDesc); - pipe(output.fileDesc); - - pid = fork(); - if (pid == 0) { // Child process - close(input.write); - close(output.read); - - dup2(input.read, STDIN_FILENO); - dup2(output.write, STDOUT_FILENO); - close(STDERR_FILENO); // Do not print errors to the screen - - execlp("bash", "bash", "-i", "-c", "\ - ver=$(bash --version | head -n 1 | awk \'{print $4}\' | grep -o \".\\..\");\ - check_ver=$(echo -e \"${ver}\n4.3\" | sort -V | head -n 1);\ - if [[ \"${ver}\" == \"${check_ver}\" ]]; then\ - echo \"[${USER}@${HOSTNAME} ${PWD##*/}]$ \";\ - else\ - echo \"${PS1@P}\";\ - fi\ - ", NULL); - } else if (pid < 0) { // Error forking - printf("Error forking\n"); - } else { // Parent process - close(input.read); - close(output.write); - - outputFileD = fdopen(output.read, "r"); - - len = getline(&prompt, &promptCap, outputFileD); - - if (prompt[len - 1] == '\n') { - prompt[len - 1] = '\0'; - } - - fclose(outputFileD); - do { - waitpid(pid, &status, WUNTRACED); - } while(!WIFEXITED(status) && !WIFSIGNALED(status)); - } - - return prompt; -} - int randomNum(int limit) { int num = 0; if (limit != 0) { diff --git a/include/util.h b/include/util.h index e69de29..2ea3bc0 100644 --- a/include/util.h +++ b/include/util.h @@ -0,0 +1 @@ +char *getPrompt(); diff --git a/src/util.c b/src/util.c index e69de29..0cdc935 100644 --- a/src/util.c +++ b/src/util.c @@ -0,0 +1,68 @@ +#include +#include +#include +#include + +static char *prompt = NULL; + +union pipe { + int fileDesc[2]; + struct { + int read; + int write; + }; +}; + +char *getPrompt() { + pid_t pid; + union pipe input, output; + FILE *outputFileD; + size_t len = 0; + int status = 0; + + static size_t promptCap = 0; + + pipe(input.fileDesc); + pipe(output.fileDesc); + + pid = fork(); + if (pid == 0) { // Child process + close(input.write); + close(output.read); + + dup2(input.read, STDIN_FILENO); + dup2(output.write, STDOUT_FILENO); + close(STDERR_FILENO); // Do not print errors to the screen + + execlp("bash", "bash", "-i", "-c", "\ + ver=$(bash --version | head -n 1 | awk \'{print $4}\' | grep -o \".\\..\");\ + check_ver=$(echo -e \"${ver}\n4.3\" | sort -V | head -n 1);\ + if [[ \"${ver}\" == \"${check_ver}\" ]]; then\ + echo \"[${USER}@${HOSTNAME} ${PWD##*/}]$ \";\ + else\ + echo \"${PS1@P}\";\ + fi\ + ", NULL); + } else if (pid < 0) { // Error forking + printf("Error forking\n"); + } else { // Parent process + close(input.read); + close(output.write); + + outputFileD = fdopen(output.read, "r"); + + len = getline(&prompt, &promptCap, outputFileD); + + if (prompt[len - 1] == '\n') { + prompt[len - 1] = '\0'; + } + + fclose(outputFileD); + do { + waitpid(pid, &status, WUNTRACED); + } while(!WIFEXITED(status) && !WIFSIGNALED(status)); + } + + return prompt; +} +