write a C program, that finds all files in a directory, builds a hash of the files and…
- User
write a C program, that finds all files in a directory, builds a hash of the files and puts hashes and filenames into a key,value store, the keys being the hashes of the file
Assistant
Here is a C program that will find all files in a directory, generate MD5 hashes of the files, and store the hash and filename in a hash table:
#include <stdio.h> #include <dirent.h> #include <string.h> #include <stdlib.h> #include <sys/stat.h> #include <sys/types.h> #include <openssl/md5.h> #define BUFSIZE 1024 typedef struct { char* key; char* value; } pair; pair* hashtable[100]; int hash(char* str) { int hash = 0; int len = strlen(str); for (int i = 0; i < len; i++) { hash += str[i]; hash = (hash * str[i]) % 100; } return hash; } void insert(char* key, char* value) { int index = hash(key); pair* p = malloc(sizeof(pair)); p->key = key; p->value = value; hashtable[index] = p; } void* readFile(char* filename) { FILE* f = fopen(filename, "rb"); if (!f) return NULL; unsigned char* buffer = malloc(BUFSIZE); size_t len; fread(buffer, 1, BUFSIZE, f); while (!feof(f)) { len = fread(buffer, 1, BUFSIZE, f); fread(buffer, 1, len, f); } fclose(f); return buffer; } char* generateHash(void* data, size_t len) { unsigned char* md5 = MD5(data, len, NULL); char* hash = malloc(33); for (int i = 0; i < 16; i++) sprintf(hash + (i * 2), "%02x", (unsigned int)md5[i]); return hash; } void scanDirectory(char* dir) { DIR* d = opendir(dir); struct dirent* de; while ((de = readdir(d)) != NULL
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.