file_in file_out
This commit is contained in:
parent
21dcfc371c
commit
ef3aefe876
19
minishell.c
19
minishell.c
|
@ -6,6 +6,7 @@
|
||||||
#include <sys/wait.h>
|
#include <sys/wait.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
|
#include <fcntl.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <setjmp.h>
|
#include <setjmp.h>
|
||||||
#include "readcmd.h"
|
#include "readcmd.h"
|
||||||
|
@ -24,6 +25,8 @@ jmp_buf goto_prompt;
|
||||||
|
|
||||||
char initcd[256], currentcd[256];
|
char initcd[256], currentcd[256];
|
||||||
|
|
||||||
|
int file_in, file_out;
|
||||||
|
|
||||||
void handler_sigchld(int sig_num)
|
void handler_sigchld(int sig_num)
|
||||||
{
|
{
|
||||||
do
|
do
|
||||||
|
@ -227,8 +230,20 @@ int main(int argc, char *argv[])
|
||||||
sigaction(SIGTSTP, &action, NULL); // on igonre SIGTSTP
|
sigaction(SIGTSTP, &action, NULL); // on igonre SIGTSTP
|
||||||
sigaction(SIGINT, &action, NULL); // on ignore SIGINT
|
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]);
|
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
|
else
|
||||||
{ // instructions du père
|
{ // instructions du père
|
||||||
|
@ -249,7 +264,7 @@ int main(int argc, char *argv[])
|
||||||
|
|
||||||
if (wait_code)
|
if (wait_code)
|
||||||
{ // execvp fail ?
|
{ // 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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue