summaryrefslogtreecommitdiff
path: root/src/boom.c
blob: 96176f9d4877afad2193a68d3225b4c7d4baeda7 (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
#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"

static int max_depth = -1;

static bool show_hidden = false;
static bool dirs_only = false;

static void usage(FILE *f) {
  fprintf(f,
      "Gebruik: boom [-thV] [STARTPUNTEN]...\n"
      "\n"
      "Geef inhoud van mapjes weer in een boom-achtig formaat.\n"
      "\n"
      "  -a          Alle bestanden worden weergegeven, ookal beginnen ze met een puntje\n"
      "  -m          Geef alleen mapjes weer\n"
      "  -d          Maximale diepte\n"
      "  -o          Omvang van n beten (+, - voor meer of minder)\n"
      "  -h          Toon deze hulptekst\n"
      "  -V          Toon versienummer\n");
}

// Returns pointer to argument array containing the starting points
static char** parse_options(int argc, char **argv) {
  int opt;
  while ((opt = getopt(argc, argv, "amd:hV")) != -1) {
    switch (opt) {
      case 'a':
        show_hidden = true;
        break;

      case 'm':
        dirs_only = true;
        break;

      case 'd':
        max_depth = atoi(optarg);
        if (max_depth <= 0) {
          fprintf(stderr, "boom: maximale diepte moet groter zijn dan 0\n");
          exit(1);
        }
        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 prev_level = -1;
static int skip_level = -1;

static int f(const char *fpath, const struct stat *, int typeflag, struct FTW *ftwbuf) {
  int level = ftwbuf->level;
  bool is_dir
     = typeflag == FTW_D
    || typeflag == FTW_DNR
    || typeflag == FTW_DP;

  if (level < skip_level) {
    skip_level = -1;
  } else if (skip_level != -1 && level >= skip_level) {
    return 0;
  }

  // check max depth
  if (max_depth >= 0 && level >= (max_depth + 1)) return 0;

  const char *fname = basename(fpath);

  // don't show hidden files
  if (!show_hidden && fname[0] == '.' && strlen(fpath) > 2 && level != 0) {
    skip_level = level + 1;
    goto done;
  }
  // don't show files in dirs only mode
  if (dirs_only && !is_dir) return 0;

  for (int i = 1; i < level; i++) {
    printf("|   ");
  }

  if (level != 0) {
    printf("|-- ");
  }
  printf("%s", fname);
  if (typeflag == FTW_SL) {
    char real_name[PATH_MAX] = {0};
    readlink(fpath, real_name, PATH_MAX);
    printf(" -> %s", real_name);
  }
  printf("\n");

done:
  prev_level = level;
  return 0;
}

int entry_boom(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;
}