#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <dirent.h>
#include <string.h>
#include <signal.h>
#define FilePath "./include/"
/*******************************************************************************
*@brief FuncName: get_md5
*
*@brief Desc: 获取指定路径文件md5值
*
*@param[in] file_path: 文件路径名
*
*@param[out] md5:128bit位md5值(16字节)
*
*@return 0 : OK
*@return -1: FAIL
******************************************************************************/
static int get_md5(char *file_path, char *md5)
{
int ret = 0;
FILE *fd = NULL;
char buff[1024] = {0};
char *md5sum = "md5sum ";
signal(SIGCHLD, SIG_IGN);
char *cmd = (char *)malloc(strlen(md5sum) + strlen(file_path));
sprintf(cmd, "%s%s", md5sum, file_path);
fd = popen(cmd, "r");
if(NULL == fgets(buff, sizeof(buff), fd)){
printf("get the commond output failed!\n");
ret = -1;
}
pclose(fd);
free(cmd);
sscanf(buff, "%32s", md5);
return ret;
}
int main(int argc, char **argv)
{
int i = 0;
DIR *dir = NULL;
struct dirent *entry;
char md5[33] = {0};
char filename[1024] = {0};
if((dir = opendir(FilePath)) == NULL)
{
printf("opendir failed!\n");
return -1;
}
else
{
while((entry = readdir(dir)))
{
i++;
if (entry->d_type == 8)
{
sprintf(filename, "%s%s", FilePath, entry->d_name);
get_md5(filename, md5);
printf("%s: %s\n",filename, md5);
memset(md5, 0, 33);
memset(filename, 0, 1024);
}
}
closedir(dir);
}
return 0;
}
获取指定文件md5
最新推荐文章于 2024-04-03 10:21:33 发布