I have a difficulty with a recent task. I have a very little programming experience and although I got myself familiar with each function and understand what each does, I can't somehow compose them together to a good functioning C program. So I would be very thankful if someone who understands C could take a look at it.
I have to write popen() and pclose() functions following the steps: creating a pipe (with pipe(2)) and a child process (with fork(2)). Then associate child process one end of the pipe ("r" or "w") with stdin or stdout, respectively, (with dup2(2)). Afterwards the child process executes the specified command (with execl(3) or execv(3)). Use the shell (sh(1)) similar to the original popen(3) to execute that command. At last the main function has to construct a FILE * from a filedescriptor of the pipe using the fdopen(3) library function.
On pclose(), the calling process waits (waitpid(2)) for the termination of the child process. To ease the implementation, it is sufficient to support at most one pipe at a time. Guarantee that a program cannot call popen() two (or more) times without appropriate and sufficient pclose() in between.
Export all necessary entities (e.g., extern declarations of the two functions) in a corresponding header file.