summaryrefslogtreecommitdiff
path: root/src/util/loop_args.c
blob: 0ab6a7a8c13f965ac0af623048228135a86717e8 (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
#include <string.h>

#include "loop_args.h"

int loop_args(char **args, int (*callback)(char *argv, bool isstdin)) {
  if (*args == NULL) {
    return callback(*args, true);
  }

  int stickyErr = 0;

  while (*args != NULL) {
    int res = 0;
    if (!strcmp(*args, "-")) {
      res = callback(*args, true);
    } else {
      res = callback(*args, false);
    }

    if (res > 0) return res;
    else if (res < 0 && stickyErr == 0) stickyErr = -res;

    args++;
  }

  return stickyErr;
}