summaryrefslogtreecommitdiff
path: root/src/boom.c
blob: 266180c202beeb8ef6dea6c069aaa1e50cf25610 (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "util/versie.h"
#include "util/option.h"

static int max_depth = -1;

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

static const char *usage_string =
  "Gebruik: %s [OPTIES]... [STARTPUNTEN]...\n"
  "\n"
  "Geef inhoud van mapjes weer in een boom-achtig formaat.\n"
  "\n"
  "  -a          Alle bestanden worden weergegeven, ook al beginnen ze met een puntje\n"
  "  -m          Geef alleen mapjes weer\n"
  "  -d DIEPTE   Maximale diepte\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) {
  char *max_depth_str;

  const struct option_spec spec[] = {
    {'a', OPTION_SETBOOL(&show_hidden)},
    {'m', OPTION_SETBOOL(&dirs_only)},
    {'d', OPTION_WITHARG(&max_depth_str)},
    {'h', OPTION_HELPUSAGE(usage_string)},
    {'V', OPTION_VERSION()},
    OPTION_SPEC_END
  };

  char **rest = option_parse(argc, argv, spec);

  if (max_depth_str != NULL) {
    errno = 0;
    max_depth = strtol(max_depth_str, NULL, 10);
    if (errno != 0) {
      fprintf(stderr, "boom: maximale diepte moet een getal zijn\n");
      exit(1);
    }
    if (max_depth <= 0) {
      fprintf(stderr, "boom: maximale diepte moet groter zijn dan 0\n");
      exit(1);
    }
  }

  return rest;
}

static int scandir_filterfunc(const struct dirent *ent) {
  if (dirs_only && ent->d_type != DT_DIR) return 0;
  if (!show_hidden && ent->d_name[0] == '.') return 0;
  if (strcmp(ent->d_name, ".") == 0) return 0;
  if (strcmp(ent->d_name, "..") == 0) return 0;
  return 1;
}

static const char *branches[] = {
  "├── ",
  "└── ",
  "│   ",
  "    "
};

// Contains indices into 'branches'
static int8_t prefix[PATH_MAX / 2 + 1];

// Buffer to store the target of a symlink in
static char linknamebuf[PATH_MAX];

static size_t total_dirs = 0;
static size_t total_files = 0;

static bool encountered_error = false;

static void print_prefix(int depth) {
  for (int i = 0; i < depth; i++) {
    printf("%s", branches[prefix[i]]);
  }
}

// Pass -1 as parentdirfd if dirname is to be resolved relative to $PWD or is absolute.
static void boom(int parentdirfd, const char *dirname, unsigned depth) {
  total_dirs++;

  if (depth > (unsigned)max_depth) return;

  struct dirent **list;
  const int nitems =
    parentdirfd == -1
      ? scandir(dirname, &list, scandir_filterfunc, alphasort)
      : scandirat(parentdirfd, dirname, &list, scandir_filterfunc, alphasort);

  if (nitems == -1) {
    printf("%s  [kon mapje niet openen]\n", dirname);
    total_dirs--;  // not a directory after all
    encountered_error = true;
    return;
  }

  const int dirfd = parentdirfd == -1
                      ? open(dirname, O_DIRECTORY)
                      : openat(parentdirfd, dirname, O_DIRECTORY);

  if (dirfd == -1) {
    printf("%s  [kon mapje niet open(2)en]\n", dirname);
    total_dirs--;  // not a directory after all
    encountered_error = true;
    return;
  }

  if (parentdirfd == -1) {
    printf("%s\n", dirname);
  }

  for (int i = 0; i < nitems; i++) {
    if (i < nitems - 1) prefix[depth-1] = 0;
    else prefix[depth-1] = 1;

    print_prefix(depth);
    if (list[i]->d_type == DT_LNK) {
      ssize_t len = readlinkat(dirfd, list[i]->d_name, linknamebuf, PATH_MAX - 1);
      if (len == -1) {
        printf("%s -> [kon niet leeslinken]\n", list[i]->d_name);
      } else {
        linknamebuf[len] = '\0';
        printf("%s -> %s\n", list[i]->d_name, linknamebuf);
      }
    } else printf("%s\n", list[i]->d_name);

    if (list[i]->d_type == DT_DIR) {
      if (i < nitems - 1) prefix[depth-1] = 2;
      else prefix[depth-1] = 3;
      boom(dirfd, list[i]->d_name, depth + 1);
    } else {
      total_files++;
    }

    free(list[i]);
  }

  close(dirfd);

  free(list);
}

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

  if (*args == NULL) {
    boom(-1, ".", 1);
  } else for (int i = 0; args[i]; i++) {
    boom(-1, args[i], 1);
  }

  putchar('\n');
  printf("%zu map%s", total_dirs, total_dirs == 1 ? "" : "jes");
  if (!dirs_only) printf(", %zu bestand%s", total_files, total_files == 1 ? "" : "en");
  putchar('\n');

  return encountered_error;
}