I'm trying to implement a wrapper for md5sum (just to see if I can). Basically the idea is that the user types a string into my program, which will then run md5sum in a child process, send it the string, and then read the output and print it on the screen.
With a normal pipe, you would just do something like
but I need both the parent and child to be able to read and write to the pipe -- the parent needs to write the string and read the hash and the child needs to read the string and write the hash. Since I'm using md5sum in the child process, I also need to redirect stdin and stdout to the pipe.
How can I go about this, or is there an easier way (short of sticking an md5 implementation in my program - I'd like to do this via IPC for future reference)?
You should already have access to C functions that implement md5sum. IIRC, the header
is <md5.h>, which would alleviate the need to redirect stdin/stdout.
You are not using the return value of fork() correctly.
fork() returns zero in the child (not the ASCII character '0'), -1 in the parent on error, and
> 0 (pid of child) in the parent on success. Your default case does not distinguish between
success and failure in the parent.
You should already have access to C functions that implement md5sum. IIRC, the header
is <md5.h>, which would alleviate the need to redirect stdin/stdout.
Really? That's excellent. I was going to just download its source but it's good if I pretty much have it already.
jsmith wrote:
You are not using the return value of fork() correctly.
fork() returns zero in the child (not the ASCII character '0'), -1 in the parent on error, and
> 0 (pid of child) in the parent on success. Your default case does not distinguish between
success and failure in the parent.
That was just a simple example (also it was written at 1:10 am very quickly). Also, as I wrote, I ignored the possibility of error for the sake of brevity.