Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

shell - question 4 - fopen ou open #22

Open
Thothore opened this issue Apr 20, 2018 · 0 comments
Open

shell - question 4 - fopen ou open #22

Thothore opened this issue Apr 20, 2018 · 0 comments

Comments

@Thothore
Copy link

`int pid = fork(); if(pid==-1){return -1;}
if(pid==0)
{
//if(dup2(fileno(fopen(output_path,"w")),STDOUT_FILENO)==-1) //test1
if(dup2(open(output_path,O_RDWR|O_CREAT|O_TRUNC),STDOUT_FILENO)==-1) //test2
{
return -1;
}
execve(path,argv,NULL);
}
else
{
int exitcode;
int p = waitpid(pid,&exitcode,0);
if(p==-1){return -1;}

if(!WIFEXITED(exitcode)) //pid didn't end correctly
{
    return -1;
}
else
{
    return WEXITSTATUS(exitcode);
}

}`
avec la ligne test1, ca passe, avec test2, l'on obtient le message d'erreur suivant :

Running suite(s): Shell
75%: Checks: 4, Failures: 1, Errors: 0
shell_test.c:85:F:Core:test_redirect:0: Assertion 'fd >= 0' failed
test_redirect s’est terminé après le message suivant : Exécution de "echo 'Hello World' > file_a"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant