简体   繁体   中英

Execlp with echo and md5sum

I am trying to write a program using execlp() to save the output of an md5sum of a string to the standard output. Basically, to simulate this:

echo "Hello!" | md5sum

Which gets this output:

31ebdfce8b77ac49d7f5506dd1495830  -

Here is what I tried first to figure this out:

char string[] = "Hello!";
execlp("md5sum", "md5sum", string, NULL); 

Although, using execlp() in this way expects the argument to be a file, not a string. So then I tried this:

char string[] = "Hello!";
execlp("echo", "echo", string, "md5sum", NULL);

Although, this generates an output of Hello! md5sum Hello! md5sum . It recognizes the "md5sum" as a string, not the system call.

What can I do to make echo and md5sum cooperate together? Or what can I do to make md5sum work on a string, not a file? Maybe should I be using a different function than execlp() ?

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));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM