ls.c (671B)
1 #include <libc.h> 2 3 int main(int argc, char *argv[]) { 4 const char *path = (argc > 1) ? argv[1] : "."; 5 6 int fd = open(path, o_rdonly | o_directory, 0); 7 if (fd < 0) { 8 write(stderr, "ls: cannot open directory\n", 26); 9 return 1; 10 } 11 12 char buf[1024]; 13 int nread; 14 15 while ((nread = getdents64(fd, (struct linux_dirent64 *)buf, sizeof(buf))) > 0) { 16 int bpos = 0; 17 while (bpos < nread) { 18 struct linux_dirent64 *d = (struct linux_dirent64 *)(buf + bpos); 19 20 if (d->d_name[0] != '.') { 21 write(stdout, d->d_name, strlen(d->d_name)); 22 write(stdout, "\n", 1); 23 } 24 25 bpos += d->d_reclen; 26 } 27 } 28 29 close(fd); 30 return 0; 31 }