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
|
#include <u.h>
#include <libc.h>
#include <fcall.h>
#include <thread.h>
#include <9p.h>
void
print_tree(char *basepath, int depth, char *prefix, int max_depth)
{
Dir *dirs;
int i, n, fd;
char *newprefix;
if (max_depth >= 0 && depth > max_depth)
return;
if ((fd = open(basepath, OREAD)) < 0)
return;
while ((n = dirread(fd, &dirs)) > 0)
{
for (i = 0; i < n; i++)
{
Dir *d = &dirs[i];
if (strcmp(d->name, ".") == 0 || strcmp(d->name, "..") == 0)
continue;
if (i == n - 1)
{
print("%s└── %s\n", prefix, d->name);
newprefix = smprint("%s ", prefix);
}
else
{
print("%s├── %s\n", prefix, d->name);
newprefix = smprint("%s│ ", prefix);
}
if (d->mode & DMDIR)
{
char *newpath = smprint("%s/%s", basepath, d->name);
print_tree(newpath, depth + 1, newprefix, max_depth);
free(newpath);
}
free(newprefix);
}
free(dirs);
}
close(fd);
}
void
main(int argc, char *argv[])
{
int max_depth = -1;
char *start_dir = ".";
ARGBEGIN
{
case 'L':
max_depth = atoi(ARGF());
break;
default:
fprint(2, "usage: %s [-L depth] [dir]\n", argv0);
exits("usage");
}
ARGEND
if (argc > 0)
start_dir = argv[0];
print_tree(start_dir, 0, "", max_depth);
exits(0);
}
|