ls.c (742B)
1 #include "linux.h" 2 3 #define assert(x) if (!(x)) __builtin_trap() 4 5 size_t strlen(char const* p) 6 { 7 char const* const begin = p; 8 while (*p) 9 ++p; 10 11 return p - begin; 12 } 13 14 int main() 15 { 16 int fd = openat(AT_FDCWD, ".", O_RDONLY, 0); 17 assert(fd >= 0); 18 19 struct linux_dirent64 buf[4]; 20 struct iovecc ios[4]; 21 for (;;) 22 { 23 unsigned n_io = 0; 24 ssize_t n = getdents64(fd, buf, sizeof(buf)); 25 assert (n >= 0); 26 27 if (!n) 28 break; 29 30 char* const begin = (void*)buf; 31 32 for (ssize_t i = 0; i + sizeof(struct linux_dirent64) < n;) 33 { 34 struct linux_dirent64* e = (void*)(begin + i); 35 size_t len = strlen(e->d_name); 36 e->d_name[len++] = '\n'; 37 ios[n_io++] = (struct iovecc){e->d_name, len}; 38 i += e->d_reclen; 39 } 40 writev(1, ios, n_io); 41 } 42 }