Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I/O redirection in a C shell. How do implement I/O redirection in a fork-exec() type system call?(any info on I/O redirection helps) if(command)//command holds the
I/O redirection in a C shell.
How do implement I/O redirection in a fork-exec() type system call?(any info on I/O redirection helps)
if(command)//command holds the system call string { int status; pid_t childpid; switch(childpid = fork()) { case -1: //error printf("Forking error"); return -1; case 0: //child process //pretty sure this is where I/O redirection would occur execl("/bin/sh", "sh", "-c", command, (char*)NULL); default: //parent process waitpid(childpid, &status, WUNTRACED);/ot sure exactly what WUNTRACED IS }//end switch
}//end if
here is more info on what im doing if this helps
The line is typed in the command line
programname argl arg2 outputfile will execute the program programname with arguments argl and arg2 , the stdin FILE stream replaced by inputfile and the stdout FILE stream replaced by outputfile . For more cases on how redirection I/O redirection should be handled, please visit the textbook website: http://www.tldp.org/LDP/intro- linux/html/sect_05_01 html With output redirection, if the redirection character is > then the outputfile is created if it does not exist and truncated if it does. If the redirection token is >> then outputfile is created if it does not exist and appended to if it does. Note: you can assume that the redirection symbols, & >> will be delimited from other command line arguments by white space - one or more spaces and/or tabs. This condition and the meanings for the redirection symbols outlined above and in the project differ from that for the standard shell IVO redirection is accomplished in the child process immediately after the fork and before the exec command. At this point, the child has inherited all the filehandles of its parent and still has access to a copy of the parent memory. Thus, it will know if redirection is to be performed, and, if it does change the stdin and/or stdout file streams, this will only effect the child not the parent. You can use open to create file descriptors for inputfile and/or outputfile and then use dup or dup2 to replace either the stdin descriptor ( STDIN_FILENO from unistd.h ) or the stdout descriptor STDOUT_FILENO from unistd.h freopen . This function is one of the three functions you can However, the easiest way to do this is to use use to open a standard IVO streamStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started