Skip to content
Snippets Groups Projects
Commit e7ac3308 authored by Dana-Maria CĂRUNTU's avatar Dana-Maria CĂRUNTU
Browse files

solve test 18 uknown command error (58p)

parent 483a355f
No related branches found
No related tags found
No related merge requests found
Pipeline #98918 passed
......@@ -34,27 +34,29 @@ static void clean_resources(int count, ...);
/**
* Internal change-directory command.
*/
static bool shell_cd(word_t *dir)
{
/* TODO: Execute cd. */
char *path = NULL;
if (dir) {
path = get_word(dir);
}
if (path == NULL) {
path = getenv("HOME");
}
static bool shell_cd(word_t *dir) {
char *path = NULL;
bool dynamically_allocated = false;
if (dir) {
path = get_word(dir);
dynamically_allocated = true;
} else {
path = getenv("HOME");
}
if (chdir(path) == -1) {
fprintf(stderr, "cd: %s: %s\n", path, strerror(errno));
free (path);
return false;
}
if (chdir(path) == -1) {
fprintf(stderr, "cd: %s: %s\n", path, strerror(errno));
if (dynamically_allocated) {
free(path);
}
return false;
}
free(path);
return true;
if (dynamically_allocated) {
free(path);
}
return true;
}
/**
......@@ -99,7 +101,7 @@ static void handle_output_redirection(word_t *out, int io_flags, word_t *err)
clean_resources(1, fd, output_file);
if (error_file != output_file) {
free(error_file);
free(error_file);
}
return;
}
......@@ -135,6 +137,12 @@ static void handle_redirections(simple_command_t *s)
handle_output_redirection(s->out, s->io_flags, s->err);
}
/**
* Parse a simple command (internal, environment variable assignment,
* external command).
*/
void handle_builtin_output(word_t * path) {
if (path) {
char *resolved_path = get_word(path);
......@@ -147,10 +155,6 @@ void handle_builtin_output(word_t * path) {
}
/**
* Parse a simple command (internal, environment variable assignment,
* external command).
*/
static int parse_simple(simple_command_t *s, int level, command_t *father)
{
/* TODO: Sanity checks. */
......@@ -161,21 +165,24 @@ static int parse_simple(simple_command_t *s, int level, command_t *father)
/* TODO: If builtin command, execute the command. */
char *command = get_word(s->verb);
if (strcmp(command, "cd") == 0) {
free(command);
handle_output(s->out);
handle_output(s->err);
free(command);
handle_builtin_output(s->out);
handle_builtin_output(s->err);
if (shell_cd(s->params)) {
return 0;
} else {
return -1;
}
}
if (strcmp(command, "exit") == 0 || strcmp(command, "quit") == 0) {
handle_redirections(s);
free(command);
return shell_exit();
}
/* TODO: If variable assignment, execute the assignment and return
* the exit status.
*/
......@@ -213,7 +220,8 @@ static int parse_simple(simple_command_t *s, int level, command_t *father)
char **argv = get_argv(s, &argc);
execvp(command, argv);
perror("Command execution failed");
// perror("Command execution failed");
fprintf(stderr, "Execution failed for '%s'\n", command);
//free resources and exit child process
for (int i = 0; i < argc; i++) {
......@@ -245,8 +253,38 @@ static bool run_in_parallel(command_t *cmd1, command_t *cmd2, int level,
command_t *father)
{
/* TODO: Execute cmd1 and cmd2 simultaneously. */
pid_t pid1, pid2;
// Fork the first command
pid1 = fork();
DIE(pid1 < 0, "Failed to fork for cmd1");
if (pid1 == 0) {
// Child process for cmd1
int status = parse_command(cmd1, level + 1, father);
exit(status);
}
// Fork the second command
pid2 = fork();
DIE(pid2 < 0, "Failed to fork for cmd2");
if (pid2 == 0) {
// Child process for cmd2
int status = parse_command(cmd2, level + 1, father);
exit(status);
}
// Parent process waits for both children
int status1, status2;
return true; /* TODO: Replace with actual exit status. */
waitpid(pid1, &status1, 0);
waitpid(pid2, &status2, 0);
// Return true if both commands succeed
return WIFEXITED(status1) && WIFEXITED(status2) && WEXITSTATUS(status1) == 0 && WEXITSTATUS(status2) == 0;
// return true; /* TODO: Replace with actual exit status. */
}
/**
......@@ -256,8 +294,9 @@ static bool run_on_pipe(command_t *cmd1, command_t *cmd2, int level,
command_t *father)
{
/* TODO: Redirect the output of cmd1 to the input of cmd2. */
return true; /* TODO: Replace with actual exit status. */
// return true; /* TODO: Replace with actual exit status. */
}
/**
......@@ -283,7 +322,8 @@ int parse_command(command_t *c, int level, command_t *father)
/* TODO: Execute the commands one after the other. */
case OP_PARALLEL:
/* TODO: Execute the commands simultaneously. */
break;
return run_in_parallel(c->cmd1, c->cmd2, level, c);
// break;
case OP_CONDITIONAL_NZERO:
/* TODO: Execute the second command only if the first one
......@@ -362,4 +402,4 @@ static bool sanity_check_command(command_t *c)
}
return true;
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment