summaryrefslogtreecommitdiff
path: root/src/util/loop_files.c
blob: cdab18c6693dd82bbb80658401cbaed1d50c2b3f (plain)
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
#include "global.h"
#include "loop_args.h"
#include "loop_files.h"

static int (*callback)(struct filebuf*, char *, bool);

static int cb(char *arg, bool isstdin) {
  struct filebuf *fb = NULL;

  if (isstdin) {
    fb = stream_to_filebuf(stdin, 0);
    if (fb == NULL) goto err_stdin;
  } else {
    bool isdir = false;
    fb = file_to_filebuf(arg, 0, &isdir);
    if (isdir) goto err_isdir;
    else if (fb == NULL) goto err_file;
  }

  return callback(fb, arg, isstdin);

err_stdin:
  fprintf(stderr, "%s: fout bij lezen van standaard invoer\n", progname);
  return 1;

err_file:
  fprintf(stderr, "%s: %s: fout bij lezen van bestand\n", progname, arg);
  return 1;

err_isdir:
  fprintf(stderr, "%s: %s: is een mapje\n", progname, arg);
  return -1;
}

int loop_files(char **args, int (*_callback)(struct filebuf*, char *fname, bool isstdin)) {
  // Translate this comment to dutch:
  // // I would like to use nested functions here but it is GCC only, so let's do
  // // this C-style MT unsafe shit.
  //
  // Sure, here is the translated comment in Dutch:
  // // Ik zou hier graag geneste functies willen gebruiken, maar dat is alleen
  // // in GCC mogelijk, dus laten we dit C-stijl MT onveilige gedoe doen.
  callback = _callback;

  return loop_args(args, cb);
}