wm: teppich

Download patch

ref: 213184b2eda8809ecfca949cedb604896530344f
parent: 8b8814e4df3e370608da903e6cdb96ba33aacdad
author: mkf <mkf@cloud9p.org>
date: Tue Nov 28 08:27:00 EST 2023

cmd/ls.c: import

there are few things that i'd like to change, but it's okay so far.

--- /dev/null
+++ b/cmd/ls.c
@@ -1,0 +1,82 @@
+#include <u.h>
+#include <libc.h>
+
+#include <err.h>
+
+#include <mem.h>
+#include <vfs.h>
+
+char*
+permtoletter(uint16 perms)
+{
+	char *s;
+	int i, j;
+	s = malloc(16);
+
+	i = 0;
+	j = 0;
+	while(i < 9)
+	{
+		s[j++] = (perms & (4 << i)) ? 'r' : '-';
+		s[j++] = (perms & (2 << i)) ? 'w' : '-';
+		s[j++] = (perms & (1 << i)) ? 'x' : '-';
+		
+		/* we iterate 3 bits each time */
+		i += 3;
+	}
+		
+	s[j++] = '\0';
+	return s;
+}
+
+int
+ls_main(int argc, char **argv, char *cwd)
+{
+	ll_t *t, *head;
+	fs_t *fs;
+	file_t *f, *dir;
+	char *perms;
+
+	if(argc == 1)
+	{
+		argv[1] = "/";
+		argc = 2;
+	}
+	
+	fs = getfs(argv[1]);
+	if(fs == nil)
+	{
+		printf("No such fs\n");
+		return NO_SUCH_FS;
+	}
+	
+	dir = pathtofile(fs, argv[1], nil);
+	if(dir == nil)
+	{
+		printf("No such file\n");
+		return NO_SUCH_FILE;
+	}
+
+	head = readdir(fs, dir); 
+	t = (ll_t*)head->next; /* first entry is always trash */
+	
+	while(t != nil)
+	{
+		f = ((file_t*)t->val);
+	
+		printf("%d\t", f->fid);
+		printf("%d\t", f->size);
+
+		printf("%d\t", f->owner);
+		printf("%s\t", permtoletter(f->perms));
+
+		printf("%s", f->path);
+		printf("%s\n", f->name);
+			
+		t = (ll_t*)t->next;
+	}
+	
+	llfree(head);
+	free(perms);
+	return OK;
+}