about summary refs log tree commit diff stats
path: root/filehash.c
blob: 03f75a5ac7a325ee561aa1dde24d3be3eaef7fd8 (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include "filehash.h"

#include <err.h>
#include <stdlib.h>
#include <string.h>

struct filehash_t
{
  const char *hash_program;
  const char **argv;
  unsigned max_per_invocation;
  unsigned next;
};

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->argv)
    goto fail;

  for (int i = 0; i <= max_per_invocation; ++i)
    fh->argv[i] = NULL;

  if (!fh->hash_program)
    goto fail;

  return fh;

fail:
  filehash_free(fh);
  return NULL;
}

#include <stdio.h>
static int run_hash(filehash_t *fh)
{
  fputs(fh->hash_program, stderr);
  for (int i = 0; i < fh->next; ++i) {
    fputc(' ', stderr);
    fputs(fh->argv[i], stderr);
  }
  fputc('\n', stderr);
  return 0;
}

filehash_state_t filehash_send(filehash_t *fh, const char *file_name)
{
  if (fh->next >= fh->max_per_invocation)
    return fhs_rejected;

  const char *copy = strdup(file_name);
  if (!copy) {
    warn("strdup");
    return fhs_failure;
  }

  fh->argv[fh->next++] = copy;
  if (fh->next < fh->max_per_invocation)
    return fhs_accepts;

  if (run_hash(fh) == -1)
    return fhs_failure;

  return fhs_full;
}

void
filehash_free(filehash_t *fh)
{
  if (!fh)
    return;

  if (fh->argv) {
    for (int i = 0; i <= fh->max_per_invocation; ++i)
      free((void *)fh->argv[i]);
    free(fh->argv);
  }
  free((void *)fh->hash_program);
  free(fh);
}