why does dash save, dup, and restore redirected descriptor in the parent, rather than redirect in the child?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hello,

a dash script like: 

date > foobar.txt 
date 

is (as an SSCCE) handled like this: 


int fd; 
int saved; 

fd = open64("foobar.txt", O_WRONLY|O_CREAT); 
saved = fcntl(1, F_DUPFD, 10); 
dup2(fd, 1); 
if (!fork()) { 
    execl("/bin/date", "date", (char *)NULL); 
} 

dup2(saved, 1); 
if (!fork()) { 
    execl("/bin/date", "date", (char *)NULL); 
} 



This is strange. Why save, dup and dup again to restore, descriptors in the parent, when it would be much simpler to just dup in the child, and not have to save and restore. This is simpler and I checked it works the same: 


int fd; 

if (!fork()) { 
    fd = open64("foobar.txt", O_WRONLY|O_CREAT); 
    dup2(fd, 1); 
    execl("/bin/date", "date", (char *)NULL); 
} 

if (!fork()) { 
    execl("/bin/date", "date", (char *)NULL); 
} 



I am sure there must be a good reason and I am not understanding something deeper. What is it?


Thank you,

Mark
--
To unsubscribe from this list: send the line "unsubscribe dash" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html



[Index of Archives]     [LARTC]     [Bugtraq]     [Yosemite Forum]     [Photo]

  Powered by Linux