Redirect Output in the Middle of a Program??

didsgn root at didsgn.UUCP
Tue Apr 26 06:09:08 AEST 1988


In article <2841 at cvl.umd.edu>, liuqing at cvl.umd.edu (Larry Liuqing Huang) writes:
> Now more people are dialing into a system via telephone lines. It is
> not unusual that the connection died out in the middle of a big C
> program which you hate to start running from the beginning again.
> 
> Is it possible for the C program or Shell to detect the line problem?
> 
> If so, is it possible to redirect all standard output and error messages
> to a designated file from the POINT where the problem is detected and 
> continue running?
> 
> Any hints highly appreciated.

There is a way....If your system can pass you a SIGHUP signal telling you that the controlling terminal of your process has 'hung up' then you can handle() the signal (see: signal() etc..). At this point and in the 'handling' subroutine you can fopen() one or more files and replace stdout, and stderr.  WATCH OUT: as in many unix systems stdXXX are defined as &iob[X], you will need to to copy the FILE structure associated to your newly opened files to &iob[X].  Once this is done, you can keep on with your p





rogram execution.

Now this can be fairly easy for stderr and stdout but tricky for stdin as you cannot really tell when the event occurs and where is your input stream at...But
this is another story.

Here is a sort of an illustration of what I tried to explain (I am french so my english constructions are not perfect):

void myhandlerofsighup();

{
static	FILE	*emergencyout,*emergencyerr;

if ( (emergencyout = fopen(EMERGENCYOUT,"a")) == (FILE *)0 )
	{
	/* THIS IS REALLY BAD */
	.
	.
	}
if ( (emergencyerr = fopen(EMERGENCYERR,"a")) == (FILE *)0 )
	{
	/* THIS IS REALLY REALLY BAD */
	.
	.
	}

	(void) memcpy(stdout,emergencyout,sizeof(FILE));
	(void) memcpy(stderr,emergencyerr,sizeof(FILE));
	
	Go on ....
}

I am not finished...
What you could do is write a small process that does this work for any process that is passed as and argv[] and fexeced().

Have fun...
jlc

Jean-Luc Chatelain
404 447 0274

...!gatech!rebel!didsgn!jlc



More information about the Comp.unix.wizards mailing list