fix waitpid

This commit is contained in:
Night Kaly 2024-02-08 12:31:57 +00:00
parent d9f5d29dda
commit 3cc6fb0d5a
No known key found for this signature in database
GPG key ID: 8E829D3381CFEBBE

View file

@ -158,15 +158,21 @@ int execute(char **args) {
return 1; return 1;
} }
// prioritize builtin commands
for (int i = 0; i < num_builtins(); i++) { for (int i = 0; i < num_builtins(); i++) {
if (strcmp(args[0], builtin_cmds[i]) == 0) { if (strcmp(args[0], builtin_cmds[i]) == 0) {
return (*builtin_func[i])(args); return (*builtin_func[i])(args);
} }
} }
/*
while (*args) {
}
*/
pid_t pid;
pid_t pid = fork();
int status; int status;
if (pid == 0) { if ((pid = fork()) == 0) {
// Child process // Child process
if (execvp(args[0], args) == -1) { if (execvp(args[0], args) == -1) {
if (errno == ENOENT) { if (errno == ENOENT) {
@ -178,9 +184,9 @@ int execute(char **args) {
perror("fork failed"); perror("fork failed");
} else { } else {
// Parent process // Parent process
while (!WIFEXITED(status) && !WIFSIGNALED(status)) { do {
waitpid(pid, &status, WUNTRACED); // wait child to be exited to return to prompt waitpid(pid, &status, WUNTRACED); // wait child to be exited to return to prompt
} } while (!WIFEXITED(status) && !WIFSIGNALED(status));
} }
return 1; return 1;