How to handle inputs in a C shell program during exec

Posted by hits_lucky on Stack Overflow See other posts from Stack Overflow or by hits_lucky
Published on 2010-06-08T18:24:40Z Indexed on 2010/06/08 18:42 UTC
Read the original article Hit count: 177

Filed under:
|
|
|

I am currently writing my own shell program. This simple shell can just execute commands.

When executing commands like vi or calc which require input from the terminal , the command is getting executed and is waiting for the input from the user. But I am unable to give any input on the screen.

How should the input be handled during the fork and exec.

Here is the piece of code which is executing commands:

    if((pid = fork()) < 0)
    {
            perror("Fork failed");
            exit(errno);
    }
    if(pid == 0)
    {
            // Child process
            if(execvp(arguments[0], arguments) == -1)
            {
                    child_status = errno;
                    switch(child_status)
                    {
                            case ENOENT:
                                    printf(" command not found \n");
                                    break;
                    }
                    exit(errno);
            }
    }
    else
    {
            // parent process
            int wait_stat;
            if(waitpid(pid , &wait_stat, WNOHANG) == -1)
            {
                    printf(" waitpid failed \n");
                    return;
            }
    }

} ~

Thanks,

© Stack Overflow or respective owner

Related posts about c

    Related posts about unix