hey guys, I have made a program using c language. The program consists to create my own shell. So, I did a function to change the standard input(stdin) in the terminal. Terminal receives a text file as input. To do this, I used fopen(), dup(), fileno() and close() for closing the file descriptor of my choice and after, use dup() to duplicate the file descriptor(code below)

The terminal receives correctly all data from text file, but the problem is when I want to return to my main function. The code enters in a infinite loop printing one line from my main function.
So, I would like to know how can I RESTORE ALL FILE DESCRIPTORS to use them as they were before I change it. I think this would solve my problem.

Please guys!! This is very important!! =\
Waiting for answers! HUGSS!!!

PS: my function where I change file descriptors is post here:

FILE *retorno;
int x;
int caracter;
retorno = fopen("/input.txt","r");

if (retorno == NULL)

x = fileno(retorno); // fileno(FILE *stream);

if( x < 0){
return 1;

if(dup(x) != 0)
return 1;

while (feof(retorno) == 0) // ok. Print all text file data
caracter = getc(retorno);


//printf("This will be printed to saida.txt\n");

// here: How can I restore all file descriptors to have the same configuration before run this code?
return 0; // return to main and enters in a loop