samedi 6 octobre 2018

When I dup2 SDTOUT it takes all its contents

I am making a shell (works well). However, right now I am trying in implement output redirection. cat test1.txt > text2.txt. If I run commands without redirection, it works perfectly. So what am I missing in my redirection output code?

Text1.txt

This is some dummy text


It I ran my shell right now, it would be as follows

$shell cat test1.txt > text2.txt
Executing cat
Everything went well!
$shell

Now, if I open text2.txt. This is what it contains

Executing cat
This is some dummy text

Everything went well!


My redirection code

char **mycommand = {"cat", "text1.txt", ">", "text2.txt"};
if (strcmp(mycommand[2], ">") == 0) {
   int fd = open(mycommand[3], O_RDWR | O_CREAT, S_IWUSR | S_IRUSR);
   dup2(fd, STDOUT);
   mycommand[2] = '\0';
   break;
}
// Then it does all the execution stuff on mycommand

Aucun commentaire:

Enregistrer un commentaire