Computing an MD5Sum in C

Is it possible to call the unix command md5sum from within a C program. I am trying to write a C program that scans a directory and computes the MD5Sum of all the files in the directory. Whenever I use md5sum 'filename' I get the error 'md5sum undeclared'. Is there a header file or some library that I can include? Thanks for your help.

you can use it like

system("md5sum filename");

Thanks itkamaraj. The problem I'm having now though is that I am scanning the directory so the file that I'm using is stored in entry->d_name and when I call system("md5sum entry->d_name") it is actually trying to use entry->d_name instead of the actual filename. Do you know how I can get around this? Here's some of my code

while( ( entry = readdir( dp ) ) != NULL )
    {
        lstat( entry->d_name, &statbuf );
        
        if( S_ISDIR( statbuf.st_mode ) )
        {
            if( strcmp( ".", entry->d_name ) == 0 || strcmp( "..", entry->d_name ) == 0 )
            {
                continue;
            }
            
            printf( "%*s%s/\n", depth, "", entry->d_name );
            printdir( entry->d_name, depth+4 );
        }
        else
        {
            unsigned int md5;
            md5 = system("md5sum entry->d_name");
            printf( "%*s%s%d\n", depth, "", entry->d_name, md5 );
        }
        
    }

Your example doesn't sound like C code at all, can you show your program?

There is indeed a library you can include, see 'man md5'. You use it by feeding MD5_Update arrays of data, which you can read from any source.

#include <openssl/md5.h>
#include <unistd.h>
int main()
{
        int n;
        MD5_CTX c;
        char buf[512];
        ssize_t bytes;

        MD5_Init(&c);
        bytes=read(STDIN_FILENO, buf, 512);
        while(bytes > 0)
        {
                MD5_Update(&c, buf, bytes);
                bytes=read(STDIN_FILENO, buf, 512);
        }

        MD5_Final(out, &c);

        for(n=0; n<MD5_DIGEST_LENGTH; n++)
                printf("%02x", out[n]);

        return(0);        
}

It reads only from stdin in this example. It outputs the same hash as the commandline md5sum command for the same data. You must link the program with -lssl.

Note that MD5 has been cracked; people can generate disparate strings with the same MD5 hash pretty much on command. For simple checksums this may not be important, for things where security is important MD5 is no good, SHA1 is better.