| 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 );
}
|