dash.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Mark Galeck <mark_galeck@pacbell.net>
To: "dash@vger.kernel.org" <dash@vger.kernel.org>
Subject: why does dash save, dup, and restore redirected descriptor in the parent, rather than redirect in the child?
Date: Tue, 31 Jan 2017 20:00:34 +0000 (UTC)	[thread overview]
Message-ID: <2132582962.805179.1485892834849@mail.yahoo.com> (raw)
In-Reply-To: <2132582962.805179.1485892834849.ref@mail.yahoo.com>


a dash script like: 

date > foobar.txt 

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,


       reply	other threads:[~2017-01-31 20:00 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <2132582962.805179.1485892834849.ref@mail.yahoo.com>
2017-01-31 20:00 ` Mark Galeck [this message]
2017-01-31 20:30   ` Harald van Dijk
2017-02-01  2:27   ` Parke
2017-02-03 22:37     ` Jilles Tjoelker
2017-02-02 18:17   ` Mark Galeck

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=2132582962.805179.1485892834849@mail.yahoo.com \
    --to=mark_galeck@pacbell.net \
    --cc=dash@vger.kernel.org \
    --subject='Re: why does dash save, dup, and restore redirected descriptor in the parent, rather than redirect in the child?' \


* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).