ref: 48cc33b5b8717b83c4dea9130c4396b451480c56
dir: /cmd/ls.c/
#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 = 6; /* most significant bit */ j = 0; while(i >= 0) { 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) { ll_t *t, *head; fs_t *fs; file_t *f, *dir; char *perms; int lflag; lflag = 0; if(argc == 1) { argv[1] = "/"; argc = 2; } if(argc > 1 && !strcmp(argv[1], "-l")) { lflag = 1; argv++; } 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 = dirread(fs, dir); t = (ll_t*)head->next; /* first entry is always trash */ while(t != nil) { f = ((file_t*)t->val); if(lflag) { 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); } else { printf("%s", f->path); printf("%s\n", f->name); } t = (ll_t*)t->next; } llfree(head); free(perms); return OK; }