summaryrefslogtreecommitdiff
path: root/src/vind.c
blob: 037acd4dfbc22ae6d4aa6bd5817dd8e6112ac95e (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
139
140
141
142
143
144
145
146
147
148
149
#include <assert.h>
#include <ftw.h>
#include <getopt.h>
#include <fnmatch.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"

static int ftmap = 0;
static int max_depth = -1;

static ssize_t min_size = 0;
static ssize_t max_size = SSIZE_MAX;

static char *nameglob = NULL;

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"
      "  -n          Filter bestanden die voldoen aan het gegeven klodder-gebaseerde draadje\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:n: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 'n':
        nameglob = 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;

  // check filename
  if (!(nameglob == NULL || fnmatch(nameglob, basename(fpath), 0) == 0)) 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;
}