Support "ls <path>" rather than just "ls" :^)

This commit is contained in:
Andreas Kling 2018-11-17 01:04:00 +01:00
commit e440c3fa87
Notes: sideshowbarker 2024-07-19 16:09:55 +09:00

View file

@ -4,20 +4,27 @@
#include <LibC/errno.h> #include <LibC/errno.h>
#include <LibC/string.h> #include <LibC/string.h>
static int do_dir(const char* path);
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
(void) argc; if (argc == 2) {
(void) argv; return do_dir(argv[1]);
bool colorize = true; }
return do_dir(".");
}
DIR* dirp = opendir("."); int do_dir(const char* path)
{
DIR* dirp = opendir(path);
if (!dirp) { if (!dirp) {
perror("opendir failed"); perror("opendir");
return 1; return 1;
} }
bool colorize = true;
char pathbuf[256]; char pathbuf[256];
while (auto* de = readdir(dirp)) { while (auto* de = readdir(dirp)) {
sprintf(pathbuf, "%s", de->d_name); sprintf(pathbuf, "%s/%s", path, de->d_name);
struct stat st; struct stat st;
int rc = lstat(pathbuf, &st); int rc = lstat(pathbuf, &st);