sortix-mirror/utils/ls.cpp

44 lines
768 B
C++
Raw Normal View History

2011-08-27 21:26:11 +00:00
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <error.h>
2012-01-14 23:47:38 +00:00
#include <dirent.h>
int ls(const char* path)
2011-08-27 21:26:11 +00:00
{
2012-01-14 23:47:38 +00:00
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"); }
2012-01-14 23:47:38 +00:00
struct dirent* entry;
while ( (entry = readdir(dir)) )
{
2012-01-14 23:47:38 +00:00
printf("%s\n", entry->d_name);
}
2012-01-14 23:47:38 +00:00
if ( derror(dir) )
{
error(2, errno, "readdir: %s", path);
}
closedir(dir);
return 0;
}
int main(int argc, char* argv[])
{
const size_t CWD_SIZE = 512;
char cwd[CWD_SIZE];
const char* path = getcwd(cwd, CWD_SIZE);
if ( !path ) { path = "."; }
if ( 1 < argc ) { path = argv[1]; }
2011-08-27 21:26:11 +00:00
return ls(path);
2011-08-27 21:26:11 +00:00
}