Dir.c (745B)
1 #include <u.h> 2 #include <libc.h> 3 #include <bio.h> 4 5 char *rroot = "/mnt/richterm"; 6 7 void 8 main(int argc, char **argv) 9 { 10 int fd; 11 long i, n; 12 Dir *dp, *dbuf; 13 char *path, buf[4096]; 14 15 path = getwd(buf, sizeof(buf)); 16 17 if (argc == 2) path = argv[1]; 18 19 fd = open(path, OREAD); 20 if (fd < 0) sysfatal("%r"); 21 22 dp = dirfstat(fd); 23 if (dp == nil) sysfatal("%r"); 24 25 if (dp->mode & DMDIR) { 26 27 dbuf = mallocz(DIRMAX, 1); 28 n = dirreadall(fd, &dbuf); 29 30 print("f\n" "l..\n" ".../\n" "l\n" "n\n"); 31 32 for (i = 0; i < n; i++) { 33 char *spacer = ""; 34 if (dbuf[i].mode & DMEXEC) spacer = "*"; 35 if (dbuf[i].mode & DMDIR) spacer = "/"; 36 37 print("l%s\n" ".%s%s\n" "l\n" "n\n", dbuf[i].name, dbuf[i].name, spacer); 38 } 39 } else sysfatal("not a directory"); 40 }