int execute_command(char *args[]) {
if (is_builtin_command(args[0])) {
execute_builtin_command(args);
return 0; // Indicate success for built-in commands
}
pid_t pid;
int status;
pid = fork();
if (pid == 0) {
// Child process
int input_fd = 0; // Default to stdin
int output_fd = 1; // Default to stdout
handle_redirection(args, &input_fd, &output_fd);
if (input_fd != 0) {
dup2(input_fd, 0);
close(input_fd);
}
if (output_fd != 1) {
dup2(output_fd, 1);
close(output_fd);
}
execvp(args[0], args);
// If execvp fails
print_error("command not found");
} else if (pid < 0) {
// Forking error
print_error("forking error");
} else {
// Parent process
waitpid(pid, &status, 0);
return WEXITSTATUS(status);
}
// Add a return statement here
return -1;
}
void handle_redirection(char *args[], int *input_fd, int *output_fd) {
int i = 0;
while (args[i] != NULL) {
if (strcmp(args[i], "<") == 0) {
*input_fd = open(args[i + 1], O_RDONLY);
if (*input_fd == -1) {
print_error("input redirection error");
}
args[i] = NULL;
} else if (strcmp(args[i], ">") == 0) {
*output_fd = open(args[i + 1], O_WRONLY | O_CREAT | O_TRUNC, 0640);
if (*output_fd == -1) {
print_error("output redirection error");
}
args[i] = NULL;
}
i++;
}
}
void handle_pipes(char *args1[], char *args2[]) {
int pipe_fd[2];
int status;
if (pipe(pipe_fd) == -1) {
print_error("pipe creation error");
}
pid_t pid1, pid2;
pid1 = fork();
if (pid1 == 0) {
// Child process 1
close(pipe_fd[0]); // Close unused read end
// Redirect stdout to the write end of the pipe
dup2(pipe_fd[1], 1);
close(pipe_fd[1]);
execvp(args1[0], args1);
// If execvp fails
print_error("command not found");
} else if (pid1 < 0) {
// Forking error
print_error("forking error");
}
pid2 = fork();
if (pid2 == 0) {
// Child process 2
close(pipe_fd[1]); // Close unused write end
// Redirect stdin to the read end of the pipe
dup2(pipe_fd[0], 0);
close(pipe_fd[0]);
execvp(args2[0], args2);
// If execvp fails
print_error("command not found");
} else if (pid2 < 0) {
// Forking error
print_error("forking error");
}
// Close both ends of the pipe in the parent process
close(pipe_fd[0]);
close(pipe_fd[1]);
// Wait for both child processes to finish
waitpid(pid1, &status, 0);
waitpid(pid2, &status, 0);
the code above cannot currently handle echo commands. for example, if i run the program with a sh file that has three lines of echo commands, it will instead output three blank lines instead of three strings. other commands such as pwd, which, ls, and cd work fine so what is the issue
The issue is with how the execute_builtin_command() function is handling the echo command. Currently, the echo command prints each argument followed by a space, including the last argument. This is causing an extra space to be printed at the end of the output.
To fix this issue, you can modify the execute_builtin_command() function to only print a space between arguments, excluding the last argument. Here's an updated version of the execute_builtin_command() function that handles the echo command correctly:
void execute_builtin_command(char *args[]) {
if (strcmp(args[0], "cd") == 0) {
if (args[1] != NULL) {
if (chdir(args[1]) == -1) {
print_error("cd error");
}
} else {
fprintf(stderr, "cd: missing argument\n");
}
} else if (strcmp(args[0], "pwd") == 0) {
char cwd[MAX_PATH_SIZE];
if (getcwd(cwd, sizeof(cwd)) != NULL) {
printf("%s\n", cwd);
} else {
print_error("pwd error");
}
} else if (strcmp(args[0], "which") == 0) {
if (args[1] != NULL) {
char *path = NULL;
// Check if the command is a built-in command
if (is_builtin_command(args[1])) {
printf("%s: shell built-in command\n", args[1]);
} else {
// Search for the program in the specified directories
char *directories[] = {"/usr/local/bin", "/usr/bin", "/bin"};
for (int i = 0; i < (int)(sizeof(directories) / sizeof(directories[0])); i++) {
char command_path[MAX_PATH_SIZE];
snprintf(command_path, MAX_PATH_SIZE, "%s/%s", directories[i], args[1]);
if (access(command_path, X_OK) == 0) {
path = strdup(command_path);
break;
}
}
if (path != NULL) {
printf("%s\n", path);
free(path);
} else {
fprintf(stderr, "%s: command not found\n", args[1]);
}
}
} else {
fprintf(stderr, "which: missing argument\n");
}
} else if (strcmp(args[0], "echo") == 0) {
// Handle echo command
int i = 1;
while (args[i] != NULL) {
printf("%s", args[i]);
if (args[i + 1] != NULL) {
printf(" ");
}
i++;
}
printf("\n");
}
}
With this updated function, the echo command will output the correct strings without any extra spaces.