Before I answer your question, some concerns.
And MD5 is long, long, long since broken. It's fairly trivial to create a file with a given MD5 sum. SHA1 is on its way out. Use SHA-256 or better. Doesn't matter if your application isn't about security, you and I aren't qualified to make decisions about attack surfaces, don't take the risk.
Have you considered doing the checksum in C? It will be faster, more portable, and more reliable. There's any number of checksum libraries. Gnome Lib provides checksums, and so much more.
#include <stdio.h>
#include <glib.h>
int main() {
char string[] = "Hello!";
printf("checksum for %s is %s\n",
string,
g_compute_checksum_for_string(G_CHECKSUM_SHA256, string, -1)
);
}
Ok, on to the question.
First problem is md5sum
doesn't take a string, it takes a file. This has nothing to do with execlp
, it's how the md5sum program works. You can make md5sum read from stdin, but that involves pipes and is more and I want to take on. Did I mention to use a library?
This leads to your second problem: error checking. I don't see any. Any error checking for an exec
goes immediately after the exec; if it succeeded then the calling program will immediately exit.
Then problem is execlp
is probably overkill unless you're changing the name of the program being run. Use execvp
. I prefer it because it keeps all the program's arguments together in a nice list that can be used for error checking later.
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
int main() {
char *args[] = { "md5um", "Hello!", NULL };
int exit_status = execvp(args[0], args);
fprintf(stderr, "executing %s ", args[0]);
for( int i = 1; args[i] != NULL; i++ ) {
fprintf(stderr, "%s ", args[i]);
}
fprintf(stderr, "exited with %d: %s\n", exit_status, strerror(errno));
}