Luna/apps/src/ls.c

31 lines
590 B
C
Raw Normal View History

#include <dirent.h>
#include <fcntl.h>
2022-10-26 18:30:22 +00:00
#include <stdbool.h>
#include <stdio.h>
#include <unistd.h>
2022-10-26 18:30:22 +00:00
int main(int argc, char** argv)
{
2022-10-26 18:30:22 +00:00
const char* pathname;
if (argc == 1) pathname = "/";
else
pathname = argv[1];
DIR* dp = opendir(pathname);
if (!dp)
{
perror("opendir");
return 1;
}
2022-10-26 18:30:22 +00:00
bool first_ent = true;
do {
struct dirent* ent = readdir(dp);
if (!ent) break;
2022-10-26 18:30:22 +00:00
printf(first_ent ? "%s" : " %s", ent->d_name);
first_ent = false;
} while (1);
2022-10-26 18:30:22 +00:00
printf("\n");
closedir(dp);
return 0;
}