www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/2002/01/12/17:27:51

X-Authentication-Warning: delorie.com: mailnull set sender to djgpp-workers-bounces using -f
From: sandmann AT clio DOT rice DOT edu (Charles Sandmann)
Message-Id: <10201122227.AA12170@clio.rice.edu>
Subject: frlist.c proposed patch [Updated]
To: djgpp-workers AT delorie DOT com
Date: Sat, 12 Jan 2002 16:27:50 -0600 (CST)
In-Reply-To: <7680-Sat12Jan2002095605+0200-eliz@is.elta.co.il> from "Eli Zaretskii" at Jan 12, 2002 09:56:05 AM
X-Mailer: ELM [version 2.5 PL2]
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

> However, I'm not sure we indeed should open those handles, just make
> them invalid, so any code that uses them will fail.  Some program
> could actually close those handles to gain more handles, for example;
> IMHO we shouldn't defeat such code unless we absolutely have to.

I agree.  I don't think this has been an issue in the 7 years of DJGPP
V2 - so letting it fail seems OK if the parent closed them.

However, I did consider that the child might want to freopen the 
handles, so the following is my latest thoughts on how to do this.
It fixes the flush issue and allows you to freopen.  The FILE structs
are always added, but the handle numbers are set to -1.  This prevents
I/O which would be bad to an incorrect handle, and also fixes the 
flush problem.

*** frlist.c_	Sat Jan 12 10:54:56 2002
--- frlist.c	Sat Jan 12 10:55:28 2002
***************
*** 2,5 ****
--- 2,7 ----
  /* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
  #include <stdio.h>
+ #include <io.h>
+ #include <libc/file.h>
  #include <libc/local.h>
  
*************** __setup_file_rec_list(void)
*** 22,24 ****
--- 24,50 ----
    __initial_file_rec.files[3] = stdprn;	/* in reverse order! (history) */
    __initial_file_rec.files[4] = stdaux;
+ 
+   /* A parent program may have closed one or more of the standard 0..4 DOS 
+      handles.  If so, we disable the FILE structure so I/O will fail.
+      The exit handler may close this structure, but since the handle is
+      impossible it will not cause flushing problems with a new file which
+      may have been opened on that handle.  If the FILE structure is
+      freopened it will flush OK on exit.
+      
+      Note: fileno() is a macro, so can be on the LHS of assignment. */
+ 
+   if(_get_dev_info(fileno(stdin)) == -1)
+     fileno(stdin) = -1;
+ 
+   if(_get_dev_info(fileno(stdout)) == -1)
+     fileno(stdout) = -1;
+ 
+   if(_get_dev_info(fileno(stderr)) == -1)
+     fileno(stderr) = -1;
+ 
+   if(_get_dev_info(fileno(stdprn)) == -1)
+     fileno(stdprn) = -1;
+ 
+   if(_get_dev_info(fileno(stdaux)) == -1)
+     fileno(stdaux) = -1;
  }


*** freopen.bak	Mon Sep 17 23:39:54 2001
--- freopen.c	Sat Jan 12 16:20:42 2002
*************** freopen(const char *file, const char *mo
*** 53,57 ****
      return NULL;
  
!   if(fd != fdo) {	/* This should rarely happen, but if it does for */
      dup2(fd, fdo);	/* stdin/stdout/stderr handles, we must fix it or */
      _close(fd);		/* child processes won't popen properly. */
--- 53,57 ----
      return NULL;
  
!   if(fd != fdo && fdo >= 0) {   /* Might rarely happen, but if it does for */
      dup2(fd, fdo);	/* stdin/stdout/stderr handles, we must fix it or */
      _close(fd);		/* child processes won't popen properly. */

- Raw text -


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