/** * @file ls.c * @brief List directory contents. * * Lists entries in the current working directory (from the CWD * environment variable). Uses the SYS_READDIR syscall to enumerate * directory entries. */ #include "syscalls.h" int main(void) { /* Check for an explicit path argument first */ char cwd[128]; if (getenv("ARG1", cwd, sizeof(cwd)) < 0 || cwd[0] == '\0') { /* No argument; use the current working directory */ if (getenv("CWD", cwd, sizeof(cwd)) < 0) { cwd[0] = '/'; cwd[1] = '\0'; } } char name[128]; uint32_t idx = 0; int32_t type; while ((type = readdir(cwd, idx, name)) >= 0) { puts(name); if (type == 2) { /* Directory: append / indicator */ putchar('/'); } putchar('\n'); idx++; } return 0; }