summaryrefslogtreecommitdiff
path: root/src/vind.c
blob: f2e7e524f14b9e597a75ea83c4f4455f8558d288 (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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <assert.h>
#include <ftw.h>
#include <getopt.h>
#include <limits.h>
#include <pwd.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>

#include "util/versie.h"

int ftmap = 0;
int max_depth = -1;

ssize_t min_size = 0;
ssize_t max_size = SSIZE_MAX;

static void usage(FILE *f) {
  fprintf(f,
      "Gebruik: vind [-thV] [STARTPUNTEN]...\n"
      "\n"
      "TODO\n"
      "\n"
      "  -t          Bestandtypes om weer te geven, kan zijn b(estand), m(apje) of een combinatie gescheiden door komma's\n"
      "  -d          Maximale diepte\n"
      "  -o          Omvang van n beten (+, - voor meer of minder)\n"
      "  -h          Toon deze hulptekst\n"
      "  -V          Toon versienummer\n");
}

enum filetype_flags {
  FT_FILE = 1 << 0,
  FT_DIR = 1 << 1,
};

static int parse_filetypes(char *s) {
  int res = 0;

  char *pat = NULL;
  while ((pat = strtok(s, "\n")) != NULL) {
    s = NULL;

    if (!strcmp(pat, "b")) {
      res |= FT_FILE;
    } else if (!strcmp(pat, "m")) {
      res |= FT_DIR;
    }
  }

  return res;
}

// Returns pointer to argument array containing the starting points
static char** parse_options(int argc, char **argv) {
  int opt;
  while ((opt = getopt(argc, argv, "t:d:o:hV")) != -1) {
    switch (opt) {
      case 't':
        ftmap = parse_filetypes(optarg);
        break;

      case 'd':
        max_depth = atoi(optarg);
        break;

      case 'o':
        if (optarg[0] == '+') {
          min_size = atoi(optarg + 1);
        } else if (optarg[0] == '-') {
          max_size = atoi(optarg + 1);
        } else {
          min_size = max_size = atoi(optarg);
        }
        break;

      case 'h':
        usage(stdout);
        exit(0);

      case 'V':
        drukkedoos_print_versie(stdout);
        exit(0);

      case '?':
        usage(stderr);
        exit(1);
    }
  }

  return argv + optind;
}

static int f(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf) {
  // check filetype
  bool ftskip = false;
  switch (typeflag) {
  case FTW_F:
    ftskip = !(ftmap == 0 || (ftmap & FT_FILE));
    break;
  case FTW_D:
  case FTW_DNR:
  case FTW_DP:
    ftskip = !(ftmap == 0 || (ftmap & FT_DIR));
    break;
  }
  if (ftskip) return 0;

  // check max depth
  if (max_depth >= 0 && ftwbuf->level >= max_depth) return 0;

  // check file size
  if (sb->st_size < min_size) return 0;
  if (sb->st_size > max_size) return 0;

  puts(fpath);
  return 0;
}

int entry_vind(int argc, char **argv) {
  char **args = parse_options(argc, argv);

  int flags = 0;
  flags |= FTW_PHYS;

  if (*args == NULL) {
    nftw(".", f, 4096, flags);
    return 0;
  }

  for (int i = 0; args[i]; i++) {
    nftw(args[i], f, 4096, flags);
  }

  return 0;
}