diff --git a/minishell b/minishell new file mode 100755 index 0000000..839a771 Binary files /dev/null and b/minishell differ diff --git a/minishell.c b/minishell.c index 49c8400..bc06a03 100644 --- a/minishell.c +++ b/minishell.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include "readcmd.h" @@ -24,6 +25,8 @@ jmp_buf goto_prompt; char initcd[256], currentcd[256]; +int file_in, file_out; + void handler_sigchld(int sig_num) { do @@ -227,8 +230,20 @@ int main(int argc, char *argv[]) sigaction(SIGTSTP, &action, NULL); // on igonre SIGTSTP sigaction(SIGINT, &action, NULL); // on ignore SIGINT + if (cmd->in) + { + file_in = open(cmd->in, O_RDONLY); + dup2(file_in, 0); + } + if (cmd->out) + { + file_out = open(cmd->out, O_CREAT | O_TRUNC | O_WRONLY, 0640); + dup2(file_out, 1); + } + execvp(cmd->seq[0][0], cmd->seq[0]); - exit(errno); // si execlp échoue on exit avec une erreur + fprintf(stderr, "ERROR: execvp failed, (%d) %s\n", errno, strerror(errno)); + exit(errno); // si execvp échoue on exit avec une erreur } else { // instructions du père @@ -249,7 +264,7 @@ int main(int argc, char *argv[]) if (wait_code) { // execvp fail ? - fprintf(stderr, "ERROR: command failed, (%d) %s\n", wait_code, strerror(wait_code)); + fprintf(stderr, "ERROR: child failed, (%d) %s\n", wait_code, strerror(wait_code)); } } }