From 43ed358e3a74b9e34b95d736771fa3d811059be1 Mon Sep 17 00:00:00 2001 From: bigsketti Date: Sun, 10 Nov 2024 16:52:02 -0500 Subject: [PATCH] stopped memory leaking --- main | Bin 16472 -> 16472 bytes main.c | 15 +++++++-------- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/main b/main index 022b6675caa189032b6ad62c7601ee03adb386e8..9d84fe4f8fed9bfb51e4171b77bd6da5bd4ce9b3 100755 GIT binary patch delta 309 zcmcc7z<8s9aRUbx+*-iIazc?9cpizdV}XY>pRe<=mtY z!X$i>2`K8wC(y>^%*)1ngo%fpV{@TWJon@SOk9&M=vpwAZ06K6X8}@iK<60s|q&*14w8iQ0TRYM{jEaR1N2!|Nmbq{rmra!b=$txdY@X zkM7VPDIVG)sv1B}3F`}PpstgL89wvN{qksj!{O0;$i0iO2%_fKET8^`GT$mW5i}oJ#!Wyk#YUzpT_kZjN+3kZS*-K Q#6b2jFhp$LXtSId0I|V$#{d8T diff --git a/main.c b/main.c index 8edad8d..a369bcd 100644 --- a/main.c +++ b/main.c @@ -31,7 +31,7 @@ void parse_input(char *input, char **args) { args[i] = NULL; } -char* getCWD() { +char* getCWD() { // current working directory char *cwd = malloc(1024); if (cwd == NULL) { @@ -52,10 +52,9 @@ int main() { char input[INPUT_SIZE]; char *args[100]; char user[100] = "defaultusr"; + char *cwd = getCWD(); while (1) { - char *cwd = getCWD(); // current working directory - if (cwd != NULL) { printf("[cShell-%s] %s $> ", user, cwd); } else { @@ -66,7 +65,7 @@ int main() { break; } - input[strcspn(input, "\n")] = '\0'; // Remove trailing newline + input[strcspn(input, "\n")] = '\0'; // built in command checks if (strcmp("exit", input) == 0) { @@ -81,7 +80,7 @@ int main() { parse_input(input, args); - // Handle 'cd' built-in command + // handle 'cd' command if (strcmp("cd", args[0]) == 0) { if (args[1] == NULL) { fprintf(stderr, "Missing argument for command \"cd\"\n"); @@ -89,13 +88,13 @@ int main() { if (chdir(args[1]) != 0) { perror("chdir failed"); } + char *cwd = getCWD(); } continue; } - - // If not a built-in command, execute the external command + // execute the external command execute(args); } - + free(cwd); return 0; }