'simulating '<' operator in a c programme

I am recreating a complete shell. For that I must simulate <. To do this, I have to use the function dup2().

I made this but it didn't work and I donc't understand why :

void simple_redirection_left(char *buffer, int index, global *glob)
{
    char *file_name = &buffer[index + 1];
    int fd = 0;

    file_name = clean_file_name(file_name);
    if (file_name[0] == '\0') {
        my_putstr(1, "Missing name for redirect.\n");
        return;
    }

    if ((fd = open(file_name, O_RDONLY)) == -1) {
        printf("Error: file not found.\n");
        return;
    }

    dup2(fd, STDIN_FILENO);
}

I first execute the binary on the left of the < in a fork() and then call the function above.

Did someone know why it didn't work ?

Thanks in advance for your answers.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source