[Search for users] [Overall Top Noters] [List of all Conferences] [Download this site]

Conference 7.286::atarist

Title:Atari ST, TT, & Falcon
Notice:Please read note 1.0 and its replies before posting!
Moderator:FUNYET::ANDERSON
Created:Mon Apr 04 1988
Last Modified:Tue May 06 1997
Last Successful Update:Fri Jun 06 1997
Number of topics:1433
Total number of notes:10312

1201.0. "I/O redirection and the way back ?" by HLFS00::SCHIPPER_T (Theo Schipper CS Rotterdam) Fri Nov 01 1991 05:37

    To redirect I/O to the RS232 port I use Fdup and Fforce to bend the
    handle for the output. My problem is how do I bend the handle back to
    the original I/O channel.( I use GFA Basic V3 and gemdos calls )
    Theo.
T.RTitleUserPersonal
Name
DateLines
1201.1Here is an exampleYNOTME::WALLACEMon Nov 04 1991 13:5362
Attached is an example of redirecting ("bending") STDIN to a file which is
open for reading, and then redirecting STDIN back to where it was originaly
(presumably the keyboard unless redirection was done in the process of
starting up this program).

In this code I use the standard C library functions dup() and dup2() so that I
could test it on my Ultrix system here at work. But as indicated in the
comments, Fdup() and Fforce() provide the same functionality. 

If you need more explanation of what's happening in the program, just ask.

	Ray


#include	<stdio.h>
#include	<sys/file.h>

#define	STDIN	0

int	Dest_desc;

main( )
{
  int	src_desc, stdin_desc;

  Dest_desc = open( "temp.tmp", O_CREAT | O_WRONLY, '\022' );
  src_desc = open( ".login", O_RDONLY, 0 );


  /* Copy some data from STDIN (keyboard right now) to a file */
  copy_data( );


  /* Remember what STDIN refered to (ie: the keyboard) */
  stdin_desc = dup( STDIN );		/* Similar to Fdup() */

  /* Redirect STDIN to a file */
  dup2( src_desc, STDIN );		/* Similar to Fforce() */

  /* Copy some data from STDIN (a file right now) to a file */
  copy_data( );


  /* Redirect STDIN back to the keyboard */
  dup2( stdin_desc, STDIN );		/* Similar to Fforce() */

  /* Copy some data from STDIN (keyboard again) to a file */
  copy_data( );

}

copy_data( )
{
  int	bytes;
  char	data[128];

  /* Read from the keyboard */
  bytes = read( STDIN, data, 128 );

  /* Write it out */
  write( Dest_desc, data, bytes );
}