shithub: tree

Download patch

ref: 55e6ac75ac8bb1e1994073bcb3417edc34d4c940
author: Adam Pasztor <shepard47@protonmail.com>
date: Mon Nov 30 01:24:50 EST 2020

init

--- /dev/null
+++ b/mkfile
@@ -1,0 +1,5 @@
+</$objtype/mkfile
+TARG=tree
+OFILES=tree.$O
+BIN=/$objtype/bin
+</sys/src/cmd/mkone
\ No newline at end of file
--- /dev/null
+++ b/tree.c
@@ -1,0 +1,48 @@
+#include <u.h> 
+#include <libc.h> 
+
+int level; 
+
+void 
+indent(void) 
+{ 
+	int i; 
+	for(i=0; i<level; ++i){ 
+		print("  "); 
+	} 
+} 
+
+void 
+tree(char *path) 
+{ 
+	Dir *dir = dirstat(path); 
+	indent(); 
+	print("%s\n", dir->name); 
+	level++; 
+	free(dir); 
+	int fd = open(path, OREAD); 
+	long ndir = dirreadall(fd, &dir); 
+	int i; 
+	for(i=0; i<ndir; ++i){ 
+		if((dir+i)->qid.type){ 
+			int len = strlen(path) + strlen((dir+i)->name); 
+			char *name = malloc(sizeof(char) * len); 
+			sprint(name, "%s/%s", path, (dir+i)->name); 
+			tree(name); 
+			level--; 
+			/*free(name);*/ 
+		}else{ 
+			indent(); 
+			print("%s\n", (dir+i)->name); 
+		} 
+	}
+	free(dir);
+}
+
+void
+main(int argc, char **argv)
+{
+	if(argc == 1) argv[1] = ".";
+	level = 0;
+	tree(argv[1]);
+}
\ No newline at end of file