summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMitja Felicijan <m@mitjafelicijan.com>2023-05-20 18:55:29 +0000
committerMitja Felicijan <m@mitjafelicijan.com>2023-05-20 18:55:29 +0000
commitd839586aacdfb88e3e95e1564a535aba9973463a (patch)
treeb45399dbb03a3085592c6af5ec9584ebc2c30f81
parentfe7d054430f42a25b69252b780673fcf5e6046e8 (diff)
downloadp9tree-d839586aacdfb88e3e95e1564a535aba9973463a.tar.gz
Added tree command source
-rw-r--r--tree.c73
1 files changed, 73 insertions, 0 deletions
diff --git a/tree.c b/tree.c
new file mode 100644
index 0000000..ab87cac
--- /dev/null
+++ b/tree.c
@@ -0,0 +1,73 @@
+#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);
+}