about summary refs log tree commit diff stats
path: root/filehash.c
blob: 5616226b912f718888eb364e510ae35c2e33cbb9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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);
}