diff options
author | Dacav <dacav@tilde.institute> | 2020-08-28 11:47:33 +0200 |
---|---|---|
committer | Dacav <dacav@tilde.institute> | 2020-08-28 11:47:33 +0200 |
commit | b71bf6f6a95f49b1d2b53a2f673d5d93836b4fc1 (patch) | |
tree | 678cdb8ffcc855322744f10dc276b87378c09696 /filehash.c | |
download | forg-b71bf6f6a95f49b1d2b53a2f673d5d93836b4fc1.tar.gz |
init
Diffstat (limited to 'filehash.c')
-rw-r--r-- | filehash.c | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/filehash.c b/filehash.c new file mode 100644 index 0000000..5616226 --- /dev/null +++ b/filehash.c @@ -0,0 +1,53 @@ +#include "filehash.h" + +#include <string.h> +#include <stdlib.h> + +struct filehash_t +{ + const char *hash_program; + const char **argv; + unsigned max_per_invocation; +}; + +filehash_t *filehash_new(const char *hash_program, unsigned max_per_invocation) +{ + filehash_t *fh; + + fh = malloc(sizeof(filehash_t)); + if (!fh) + return NULL; + + *fh = (filehash_t){ + .hash_program = strdup(hash_program), + .max_per_invocation = max_per_invocation, + .argv = reallocarray( + NULL, + 1 + max_per_invocation, + sizeof(const char *) + ), + }; + + if (!fh->hash_program) + goto fail; + if (!fh->argv) + goto fail; + + fh->argv[max_per_invocation] = NULL; + + return fh; + +fail: + filehash_free(fh); + return NULL; +} + +void filehash_free(filehash_t *fh) +{ + if (!fh) + return; + + free(fh->argv); + free((void *)fh->hash_program); + free(fh); +} |