www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1995/02/07/19:37:00

Date: Tue, 7 Feb 1995 17:35:01 -0500
From: davis AT amy DOT tch DOT harvard DOT edu ("John E. Davis")
To: djgpp AT sun DOT soe DOT clarkson DOT edu
Subject: Example of the coff2exe bug

Hi,

Here is a demo that illustrates a problem with coff2exe that I mentioned
earlier.  Perhaps I am doing something wrong here which is also good because
then I will also learn something.  Anyway, here is what you do:

   1.  Put the following in a file called t.c
   2.  Compile it:  gcc -Wall t.c
   3.  coff2exe -s C:/djgpp/bin/go32.exe  a.out
   
         (Use correct path above for go32)
	 
   4.  Run the program like this:
   
          a.exe gcc -Wall t.c
       
       This essentually spawns gcc to recompile the program to produce
       a.out.  As a side effect, all stdout/stderr messages will go to a
       file called `test.out'.
       
       There should be no problems here which hopefully indicates that the
       program works properly.  
       
    5. Now do:
    
          a.exe coff2exe -s C:/djgpp/bin/go32.exe a.out
	  
       Did your system crash?  Do you get a hard drive seek error?  What is
       in test.out?

The file t.c follows below.  It redirects stdout/stderr to a file and spawns
the program on the command line.  It compiles under both gcc and bcc.


#include <stdio.h>
#include <io.h>
# include <fcntl.h>
# include <sys/types.h>
# include <sys/stat.h>

#ifdef __GO32__
# include <sys/file.h>
#endif

# include <process.h>
# include <errno.h>

#define msg_error(x) fprintf(stderr, "%s\n", (x))

static int execute_the_command (char **argv, char *file)
{
   int ret;
   int fd1 = -1, fd2 = -1;
   int fd_out = -1;
 
   
   /* save stdout file handle */
   fd1 = dup (fileno (stdout));
   if (fd1 == -1) 
     {
	msg_error ("Unable to dup stdout");
	return -1;
     }
   
   fd_out = open (file,
		  O_CREAT | O_TRUNC | O_TEXT | O_WRONLY | O_APPEND,
		  S_IREAD | S_IWRITE);
	
   if ((fd_out == -1) || (-1 == dup2 (fd_out, fileno (stdout))))
     {
	msg_error ("Unable to redirect stdout!");
	return -1;
     }
   
   /* save stderr file handle */
   fd2 = dup (fileno (stderr));
	
   if (fd2 == -1) 
     {
	msg_error ("Unable to dup stderr");
	return -1;
     }
   
   if (-1 == dup2 (fd_out, fileno (stderr)))
     {
	msg_error ("Unable to redirect stderr!");
	return -1;
     }

   close (fd_out);
   
   if (-1 == (ret = spawnvp(P_WAIT, argv[0], (void *) argv)))
     {
	msg_error ("Spawn failed.");
     }
   
   if (-1 == dup2 (fd1, fileno (stdout)))
     {
	msg_error ("Unable to reattach stdout");
     }
   close (fd1);
   
   if (-1 == dup2 (fd2, fileno (stderr)))
     {
	msg_error ("Unable to reattach stderr");
     }
   close (fd2);
   return ret;
}

int main (int argc, char **argv)
{
   char *file = "test.out";
   char *my_argv[10], *arg;
   int i;
   
   for (i = 1; i < argc; i++)
     {
	arg = malloc (strlen (argv[i]) + 1);
	if (arg == NULL) return -1;
	strcpy (arg, argv[i]);
	my_argv[i - 1] = arg;
	fprintf (stderr, "my_argv[%d] = %s\n", i - 1, my_argv[i - 1]);
     }
   my_argv[i - 1] = NULL;
   
   if (argc > 1) execute_the_command (my_argv, file);

   fprintf (stdout, "Writing to stdout...\n");
   fprintf (stderr, "Writing to stderr...\n");
   return 0;
}

- Raw text -


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