Compare commits

...

4 Commits

Author SHA1 Message Date
7b0b3dabc4
apps: Add ls
All checks were successful
continuous-integration/drone/push Build is passing
2023-03-29 01:07:58 +02:00
3e30f0a88c
libc: Add opendir, readdir and closedir 2023-03-29 01:06:57 +02:00
5623f3c699
tmpfs: Set the mode of the root directory on creation 2023-03-29 01:06:26 +02:00
0320ffb485
Vector: Fix operator[]
Istg Vector has a curse or something, the annoying and inexplicable bugs always end up in Vector.
2023-03-29 01:05:30 +02:00
7 changed files with 124 additions and 2 deletions

View File

@ -11,3 +11,4 @@ luna_app(cat.c cat)
luna_app(edit.c edit) luna_app(edit.c edit)
luna_app(sh.c sh) luna_app(sh.c sh)
luna_app(date.c date) 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;
}

View File

@ -10,6 +10,7 @@ namespace TmpFS
{ {
SharedPtr<FileSystem> fs = TRY(adopt_shared_if_nonnull(new (std::nothrow) FileSystem())); SharedPtr<FileSystem> fs = TRY(adopt_shared_if_nonnull(new (std::nothrow) FileSystem()));
SharedPtr<VFS::Inode> root = TRY(fs->create_dir_inode({})); SharedPtr<VFS::Inode> root = TRY(fs->create_dir_inode({}));
root->chmod(0755);
fs->set_root(root); fs->set_root(root);
return (SharedPtr<VFS::FileSystem>)fs; return (SharedPtr<VFS::FileSystem>)fs;
} }

View File

@ -13,6 +13,7 @@ set(SOURCES
src/ctype.cpp src/ctype.cpp
src/time.cpp src/time.cpp
src/init.cpp src/init.cpp
src/dirent.cpp
src/setjmp.cpp src/setjmp.cpp
src/sys/stat.cpp src/sys/stat.cpp
src/sys/mman.cpp src/sys/mman.cpp

32
libc/include/dirent.h Normal file
View 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
View 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;
}
}

View File

@ -106,13 +106,13 @@ template <typename T> class Vector
const T& operator[](usize index) const const T& operator[](usize index) const
{ {
check(index < m_size); check(index < m_size);
return m_data[m_size]; return m_data[index];
} }
T& operator[](usize index) T& operator[](usize index)
{ {
check(index < m_size); check(index < m_size);
return m_data[m_size]; return m_data[index];
} }
Iterator begin() Iterator begin()