D
DyslexicAnaboko
Hello,
I have a module that is part of larger project that is giving me
trouble, so I setup an example.
Brief
=====
I simply want to open a text file and make the contents avaliable
through stdin that way a bourne shell script I wrote will execute
properly. The shell script is expecting input from stdin.
This program will be executing on a UNIX machine and communicating
with the shell.
Code
=====START======
#include <stdio.h>
#include <stdlib.h>
const char cmd[] = "./proj4.sh" ; /* Shell Script */
const char FILE_NAME[] = "tf.txt" ; /* File to open */
int main()
{
FILE *fp ;
/* Open File For Standard Input */
if( ( fp = fopen( FILE_NAME, "r" ) ) == NULL )
{
printf( "Cannot Open File: %s", FILE_NAME ) ;
exit( 1 ) ;
}
if( fp != 0 ) /* If fp isn't stdin */
{
dup2( fp, 0 ); /* Make fp stdin */
close( fp ) ; /* Close original fp */
}
printf( "\n\nRunning Line Now...\n\n" ) ;
execl( "/bin/sh", "sh", "-c", cmd, (char *)0 ) ;
}
=====STOP=======
Essentially the effect I am looking for here is:
../proj4.sh < tf.txt
I cannot get this to execute period. Either I get a compiler error, or
if I change fopen() for open() and make fp an int, the program
executes, but my shell script does nothing, meaning it didn't recieve
anything from stdin.
I would appreciate the help if anyone is will to work with me.
Thank You for your time,
Eli Hayon
I have a module that is part of larger project that is giving me
trouble, so I setup an example.
Brief
=====
I simply want to open a text file and make the contents avaliable
through stdin that way a bourne shell script I wrote will execute
properly. The shell script is expecting input from stdin.
This program will be executing on a UNIX machine and communicating
with the shell.
Code
=====START======
#include <stdio.h>
#include <stdlib.h>
const char cmd[] = "./proj4.sh" ; /* Shell Script */
const char FILE_NAME[] = "tf.txt" ; /* File to open */
int main()
{
FILE *fp ;
/* Open File For Standard Input */
if( ( fp = fopen( FILE_NAME, "r" ) ) == NULL )
{
printf( "Cannot Open File: %s", FILE_NAME ) ;
exit( 1 ) ;
}
if( fp != 0 ) /* If fp isn't stdin */
{
dup2( fp, 0 ); /* Make fp stdin */
close( fp ) ; /* Close original fp */
}
printf( "\n\nRunning Line Now...\n\n" ) ;
execl( "/bin/sh", "sh", "-c", cmd, (char *)0 ) ;
}
=====STOP=======
Essentially the effect I am looking for here is:
../proj4.sh < tf.txt
I cannot get this to execute period. Either I get a compiler error, or
if I change fopen() for open() and make fp an int, the program
executes, but my shell script does nothing, meaning it didn't recieve
anything from stdin.
I would appreciate the help if anyone is will to work with me.
Thank You for your time,
Eli Hayon