I am having trouble having redirection working with the ability to hit enter and remain in the same shell.
My problem is if user types: ./a.out < foo.txt (where foo.txt contains a command like date)
I want to execute that command and exit the shell.However,if the user compiles the code without using redirection: ./a.out, I want to keep the user in the shell even if he presses enter.
e.g.
shell>> (pressed Enter)
shell>> (pressed Enter)
shell>> ls
The problem arises because both enter and end of file are read in as '\0' using gets. Is there a way, such that both functionality can be achieved. Any help is greatly appreciated.
Snippet of my code:
int main(int argc, char *argv[])
{
int nbytes = 100;
char my_string[nbytes];
string arg_list[20];
while(1){
printf("shell>> ");
my_string[0] = '\0';
gets( my_string );
//this should happen when end of file is reached
if(my_string[0] == '\0'){
exit(0);
}
// this should happen when user presses enter
while(strlen(my_string)==0){
printf("shell>>");
gets(my_string);
}