apps: Add ls
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
apio 2023-03-29 01:07:58 +02:00
parent 3e30f0a88c
commit 7b0b3dabc4
Signed by: apio
GPG Key ID: B8A7D06E42258954
2 changed files with 33 additions and 0 deletions

View File

@ -11,3 +11,4 @@ luna_app(cat.c cat)
luna_app(edit.c edit)
luna_app(sh.c sh)
luna_app(date.c date)
luna_app(ls.c ls)

32
apps/ls.c Normal file
View File

@ -0,0 +1,32 @@
#include <dirent.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
int main(int argc, char** argv)
{
const char* pathname;
if (argc == 1) pathname = "/";
else
pathname = argv[1];
DIR* dp = opendir(pathname);
if (!dp)
{
perror("opendir");
return 1;
}
int first_ent = 1;
do {
struct dirent* ent = readdir(dp);
if (!ent) break;
printf(first_ent ? "%s" : " %s", ent->d_name);
first_ent = 0;
} while (1);
putchar('\n');
closedir(dp);
return 0;
}