Updated ls(1) to use the DIR API.

This commit is contained in:
Jonas 'Sortie' Termansen 2012-01-15 00:47:38 +01:00
parent d2c4b1d6ac
commit ff9221de1a
1 changed files with 15 additions and 23 deletions

View File

@ -4,38 +4,30 @@
#include <unistd.h>
#include <errno.h>
#include <error.h>
#include <sys/readdirents.h>
#include <libmaxsi/platform.h>
#include <libmaxsi/process.h>
using namespace Maxsi;
#include <dirent.h>
int ls(const char* path)
{
int fd = open(path, O_SEARCH | O_DIRECTORY);
if ( fd < 0 ) { error(2, errno, "%s", path); return 2; }
const size_t BUFFER_SIZE = 512;
char buffer[BUFFER_SIZE];
sortix_dirent* dirent = (sortix_dirent*) buffer;
DIR* dir = opendir(path);
if ( !dir ) { error(2, errno, "%s", path); return 2; }
// TODO: Hack until mountpoints work correctly.
if ( strcmp(path, "/") == 0 ) { printf("bin\ndev\n"); }
while ( true )
struct dirent* entry;
while ( (entry = readdir(dir)) )
{
if ( readdirents(fd, dirent, BUFFER_SIZE) )
{
error(2, errno, "readdirents: %s", path);
return 1;
}
for ( sortix_dirent* iter = dirent; iter; iter = iter->d_next )
{
if ( iter->d_namelen == 0 ) { return 0; }
printf("%s\n", iter->d_name);
}
printf("%s\n", entry->d_name);
}
if ( derror(dir) )
{
error(2, errno, "readdir: %s", path);
}
closedir(dir);
return 0;
}
int main(int argc, char* argv[])