Compare commits
4 Commits
0847cfcb65
...
7b0b3dabc4
Author | SHA1 | Date | |
---|---|---|---|
7b0b3dabc4 | |||
3e30f0a88c | |||
5623f3c699 | |||
0320ffb485 |
@ -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
32
apps/ls.c
Normal 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;
|
||||
}
|
@ -10,6 +10,7 @@ namespace TmpFS
|
||||
{
|
||||
SharedPtr<FileSystem> fs = TRY(adopt_shared_if_nonnull(new (std::nothrow) FileSystem()));
|
||||
SharedPtr<VFS::Inode> root = TRY(fs->create_dir_inode({}));
|
||||
root->chmod(0755);
|
||||
fs->set_root(root);
|
||||
return (SharedPtr<VFS::FileSystem>)fs;
|
||||
}
|
||||
|
@ -13,6 +13,7 @@ set(SOURCES
|
||||
src/ctype.cpp
|
||||
src/time.cpp
|
||||
src/init.cpp
|
||||
src/dirent.cpp
|
||||
src/setjmp.cpp
|
||||
src/sys/stat.cpp
|
||||
src/sys/mman.cpp
|
||||
|
32
libc/include/dirent.h
Normal file
32
libc/include/dirent.h
Normal file
@ -0,0 +1,32 @@
|
||||
/* dirent.h: Directory streams interface. */
|
||||
|
||||
#ifndef _DIRENT_H
|
||||
#define _DIRENT_H
|
||||
|
||||
#include <sys/types.h>
|
||||
|
||||
typedef struct
|
||||
{
|
||||
int _fd;
|
||||
} DIR;
|
||||
|
||||
struct dirent
|
||||
{
|
||||
ino_t d_ino;
|
||||
char d_name[129];
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
DIR* opendir(const char* path);
|
||||
struct dirent* readdir(DIR* stream);
|
||||
int closedir(DIR* stream);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
55
libc/src/dirent.cpp
Normal file
55
libc/src/dirent.cpp
Normal file
@ -0,0 +1,55 @@
|
||||
#include <bits/getdents.h>
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/syscall.h>
|
||||
#include <unistd.h>
|
||||
|
||||
extern "C"
|
||||
{
|
||||
DIR* opendir(const char* path)
|
||||
{
|
||||
DIR* dp = (DIR*)malloc(sizeof(DIR));
|
||||
if (!dp) { return nullptr; }
|
||||
|
||||
// FIXME: Use O_DIRECTORY (validate that path is actually a directory)
|
||||
int fd = open(path, O_RDONLY);
|
||||
if (fd < 0)
|
||||
{
|
||||
free(dp);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
dp->_fd = fd;
|
||||
|
||||
return dp;
|
||||
}
|
||||
|
||||
struct dirent* readdir(DIR* stream)
|
||||
{
|
||||
// FIXME: Very hackish, right now luna_dirent and dirent have the same layout.
|
||||
static luna_dirent ent;
|
||||
|
||||
long rc = syscall(SYS_getdents, stream->_fd, &ent, 1);
|
||||
if (rc < 0)
|
||||
{
|
||||
errno = -rc;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// End-of-file (no more directory entries)
|
||||
if (rc < 1) return nullptr;
|
||||
|
||||
return (struct dirent*)&ent;
|
||||
}
|
||||
|
||||
int closedir(DIR* stream)
|
||||
{
|
||||
if (close(stream->_fd) < 0) return -1;
|
||||
|
||||
free(stream);
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
@ -106,13 +106,13 @@ template <typename T> class Vector
|
||||
const T& operator[](usize index) const
|
||||
{
|
||||
check(index < m_size);
|
||||
return m_data[m_size];
|
||||
return m_data[index];
|
||||
}
|
||||
|
||||
T& operator[](usize index)
|
||||
{
|
||||
check(index < m_size);
|
||||
return m_data[m_size];
|
||||
return m_data[index];
|
||||
}
|
||||
|
||||
Iterator begin()
|
||||
|
Loading…
Reference in New Issue
Block a user