mirror of
https://github.com/bigsketti/cshell.git
synced 2025-06-07 05:14:47 +00:00
stopped memory leaking
This commit is contained in:
parent
649deafef2
commit
43ed358e3a
15
main.c
15
main.c
@ -31,7 +31,7 @@ void parse_input(char *input, char **args) {
|
|||||||
args[i] = NULL;
|
args[i] = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
char* getCWD() {
|
char* getCWD() { // current working directory
|
||||||
char *cwd = malloc(1024);
|
char *cwd = malloc(1024);
|
||||||
|
|
||||||
if (cwd == NULL) {
|
if (cwd == NULL) {
|
||||||
@ -52,10 +52,9 @@ int main() {
|
|||||||
char input[INPUT_SIZE];
|
char input[INPUT_SIZE];
|
||||||
char *args[100];
|
char *args[100];
|
||||||
char user[100] = "defaultusr";
|
char user[100] = "defaultusr";
|
||||||
|
char *cwd = getCWD();
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
char *cwd = getCWD(); // current working directory
|
|
||||||
|
|
||||||
if (cwd != NULL) {
|
if (cwd != NULL) {
|
||||||
printf("[cShell-%s] %s $> ", user, cwd);
|
printf("[cShell-%s] %s $> ", user, cwd);
|
||||||
} else {
|
} else {
|
||||||
@ -66,7 +65,7 @@ int main() {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
input[strcspn(input, "\n")] = '\0'; // Remove trailing newline
|
input[strcspn(input, "\n")] = '\0';
|
||||||
|
|
||||||
// built in command checks
|
// built in command checks
|
||||||
if (strcmp("exit", input) == 0) {
|
if (strcmp("exit", input) == 0) {
|
||||||
@ -81,7 +80,7 @@ int main() {
|
|||||||
|
|
||||||
parse_input(input, args);
|
parse_input(input, args);
|
||||||
|
|
||||||
// Handle 'cd' built-in command
|
// handle 'cd' command
|
||||||
if (strcmp("cd", args[0]) == 0) {
|
if (strcmp("cd", args[0]) == 0) {
|
||||||
if (args[1] == NULL) {
|
if (args[1] == NULL) {
|
||||||
fprintf(stderr, "Missing argument for command \"cd\"\n");
|
fprintf(stderr, "Missing argument for command \"cd\"\n");
|
||||||
@ -89,13 +88,13 @@ int main() {
|
|||||||
if (chdir(args[1]) != 0) {
|
if (chdir(args[1]) != 0) {
|
||||||
perror("chdir failed");
|
perror("chdir failed");
|
||||||
}
|
}
|
||||||
|
char *cwd = getCWD();
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
// execute the external command
|
||||||
// If not a built-in command, execute the external command
|
|
||||||
execute(args);
|
execute(args);
|
||||||
}
|
}
|
||||||
|
free(cwd);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user