From b5d017c6241ac356915b178d0a9588653d18d460 Mon Sep 17 00:00:00 2001 From: Paul Smith Date: Tue, 16 Apr 2013 00:35:48 -0400 Subject: Create an open_tmpfd() function to return temp files by FD. Use it. --- job.c | 63 ++++++++------------------------------------------------------- 1 file changed, 8 insertions(+), 55 deletions(-) (limited to 'job.c') diff --git a/job.c b/job.c index 2c76be3..df1991b 100644 --- a/job.c +++ b/job.c @@ -558,7 +558,6 @@ static int assign_child_tempfiles (struct child *c, int combined) { FILE *outstrm = NULL, *errstrm = NULL; - int o_ok, e_ok; const char *suppressed = "output-sync suppressed: "; char *failmode = NULL; @@ -566,70 +565,24 @@ assign_child_tempfiles (struct child *c, int combined) if (c->outfd != -1 && c->errfd != -1) return 1; - /* Check status of stdout and stderr before hooking up temp files. */ - o_ok = STREAM_OK (stdout); - e_ok = STREAM_OK (stderr); - - /* The tmpfile() function returns a FILE pointer but those can be in - limited supply, so we immediately dup its file descriptor and keep - only that, closing the FILE pointer. */ - - if (combined) - { - if (!(outstrm = tmpfile ())) - failmode = "tmpfile()"; - else - errstrm = outstrm; - } - else if (o_ok && e_ok) + if (STREAM_OK (stdout)) { - if (!(outstrm = tmpfile ()) || !(errstrm = tmpfile ())) - failmode = "tmpfile()"; + c->outfd = open_tmpfd (); + CLOSE_ON_EXEC (c->outfd); } - else if (o_ok) - { - if (!(outstrm = tmpfile ())) - failmode = "tmpfile()"; - } - else if (e_ok) - { - if (!(errstrm = tmpfile ())) - failmode = "tmpfile()"; - } - else - failmode = "stdout"; - if (!failmode && outstrm) + if (STREAM_OK (stderr)) { - if ((c->outfd = dup (fileno (outstrm))) == -1) - failmode = "dup2()"; - else - CLOSE_ON_EXEC (c->outfd); - } - - if (!failmode && errstrm) - { - if (combined) + if (c->outfd >= 0 && combined) c->errfd = c->outfd; else { - if ((c->errfd = dup (fileno (errstrm))) == -1) - failmode = "dup2()"; - else - CLOSE_ON_EXEC (c->errfd); + c->errfd = open_tmpfd (); + CLOSE_ON_EXEC (c->errfd); } } - if (failmode) - perror_with_name (suppressed, failmode); - - if (outstrm) - (void) fclose (outstrm); - - if (errstrm && errstrm != outstrm) - (void) fclose (errstrm); - - return failmode == NULL; + return 1; } /* Support routine for sync_output() */ -- cgit v1.2.3