You need to specify the descriptor of the input file of your shell program in the input file. In c, this is achieved by calling int dup2(int oldfd, int newfd); whose task is newfd to be a copy of oldfd, closing newfd if necessary.
On Unix / Linux, each process has its own file descriptors, which are stored as follows:
0 - Standard input (stdin) 1 - Standard output (standard output) 2 - Standard error (stderr)
So, you must point the stdin handle to the input file you want to use. Here is how I wrote a few months before:
void ioredirection(int type,char *addr) { // output append redirection using ">>" if (type == 2) { re_file = open(addr, O_APPEND | O_RDWR, S_IREAD | S_IWRITE); type--; } // output redirection using ">" else if (type==1) re_file = open(addr, O_TRUNC | O_RDWR, S_IREAD | S_IWRITE); // input redirection using "<" or "<<" else re_file = open(addr, O_CREAT | O_RDWR, S_IREAD | S_IWRITE); old_stdio = dup(type); dup2(re_file, type); close(re_file); }
source share