www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/2004/11/26/05:25:39

X-Authentication-Warning: delorie.com: mail set sender to djgpp-workers-bounces using -f
Date: Fri, 26 Nov 2004 12:25:34 +0200 (EET)
From: Esa A E Peuha <peuha AT cc DOT helsinki DOT fi>
Sender: peuha AT sirppi DOT helsinki DOT fi
To: djgpp-workers AT delorie DOT com
Subject: Re: A fix for popen().
In-Reply-To: <01c4d33b$Blat.v2.2.2$77027f00@zahav.net.il>
Message-ID: <Pine.OSF.4.58.0411261217570.10977@sirppi.helsinki.fi>
References: <41A5BE9F DOT 15534 DOT 2346D29 AT localhost> <Pine DOT OSF DOT 4 DOT 58 DOT 0411251228180 DOT 11858 AT sirppi DOT helsinki DOT fi>
<01c4d33b$Blat.v2.2.2$77027f00 AT zahav DOT net DOT il>
MIME-Version: 1.0
Reply-To: djgpp-workers AT delorie DOT com
Errors-To: nobody AT delorie DOT com
X-Mailing-List: djgpp-workers AT delorie DOT com
X-Unsubscribes-To: listserv AT delorie DOT com

On Fri, 26 Nov 2004, Eli Zaretskii wrote:

> If you remove the fclose, please put an fflush there, in case there's
> something buffered left.  (I know it shouldn't happen, but better be
> safe than sorry.)

Yes.  Here's the patch; I changed the l1->fp to stdout/stdin to avoid
confusion.  BTW, does setting _IORMONCL around system() have any real
effect?  Is there any real chance that the program could crash inside
system (by the child program or otherwise) and yes be able to remove
the temproary file?

Index: popen.c
===================================================================
RCS file: /cvs/djgpp/djgpp/src/libc/posix/stdio/popen.c,v
retrieving revision 1.6
diff -u -r1.6 popen.c
--- popen.c	1 Jun 2004 07:59:03 -0000	1.6
+++ popen.c	26 Nov 2004 10:16:39 -0000
@@ -102,22 +102,22 @@
       goto error;

     /* redirect stdout */
-    if (!(l1->fp = freopen(temp_name, "wb", stdout)))
+    if (!freopen(temp_name, "wb", stdout))
       goto error;

     /* make sure file is removed on abnormal exit */
-    l1->fp->_flag |= _IORMONCL;
-    l1->fp->_name_to_remove = temp_name;
+    stdout->_flag |= _IORMONCL;
+    stdout->_name_to_remove = temp_name;

     /* execute command */
     l1->exit_status = system(cm);

-    /* don't remove file while closing */
-    l1->fp->_flag &= ~_IORMONCL;
-    l1->fp->_name_to_remove = NULL;
+    /* don't remove file */
+    stdout->_flag &= ~_IORMONCL;
+    stdout->_name_to_remove = NULL;

-    /* close file */
-    fclose(l1->fp);
+    /* flush file just in case */
+    fflush(stdout);

     /* reopen real stdout */
     if (dup2(fd, fileno(stdout)) == -1)
@@ -205,17 +205,25 @@
       goto exit;

     /* redirect stdin */
-    if (!(l1->fp = freopen(temp_name, "rb", stdin)))
+    if (!freopen(temp_name, "rb", stdin))
       goto exit;

     /* make sure file is removed on abnormal exit */
-    l1->fp->_flag |= _IORMONCL;
-    l1->fp->_name_to_remove = temp_name;
+    stdin->_flag |= _IORMONCL;
+    stdin->_name_to_remove = temp_name;

     /* execute command */
     retval = system(l1->command);

-    /* reopen stdin */
+    /* don't remove file */
+    stdin->_flag &= ~_IORMONCL;
+    stdin->_name_to_remove = NULL;
+
+    /* close and remove file */
+    close(fileno(stdin));
+    remove(temp_name);
+
+    /* reopen real stdin */
     if (dup2(fd, fileno(stdin)) == -1)
     {
       retval = -1;
@@ -227,16 +235,18 @@
   }
   /* if pipe was opened to read, return the exit status we saved */
   else if (l1->mode[0] == 'r')
+  {
     retval = l1->exit_status;
+
+    /* close and remove file */
+    fclose(l1->fp);
+  }
   else
     /* invalid mode */
     retval = -1;

  exit:

-  /* close and remove file */
-  fclose(l1->fp);
-
   if (l1->command)
     free(l1->command);



-- 
Esa Peuha
student of mathematics at the University of Helsinki
http://www.helsinki.fi/~peuha/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019