1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 23:17:45 +00:00

More work towards getting bash to build.

Implemented some syscalls: dup(), dup2(), getdtablesize().
FileHandle is now a retainable, since that's needed for dup()'ed fd's.
I didn't really test any of this beyond a basic smoke check.
This commit is contained in:
Andreas Kling 2018-11-05 19:01:22 +01:00
parent 82f84bab11
commit 9f2b9c82bf
17 changed files with 114 additions and 23 deletions

View file

@ -3,6 +3,7 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <Kernel/Syscall.h>
extern "C" {
@ -20,6 +21,16 @@ DIR* opendir(const char* name)
return dirp;
}
int closedir(DIR* dirp)
{
if (!dirp || dirp->fd == -1)
return -EBADF;
int rc = close(dirp->fd);
if (rc == 0)
dirp->fd = -1;
return rc;
}
struct sys_dirent {
ino_t ino;
byte file_type;

View file

@ -23,7 +23,8 @@ struct __DIR {
typedef struct __DIR DIR;
DIR* opendir(const char* name);
struct dirent* readdir(DIR* dirp);
int closedir(DIR*);
struct dirent* readdir(DIR*);
__END_DECLS

View file

@ -193,6 +193,17 @@ FILE* fopen(const char* pathname, const char* mode)
return fp;
}
FILE* fdopen(int fd, const char* mode)
{
assert(!strcmp(mode, "r") || !strcmp(mode, "rb"));
if (fd < 0)
return nullptr;
auto* fp = (FILE*)malloc(sizeof(FILE));
fp->fd = fd;
fp->eof = false;
return fp;
}
int fclose(FILE* stream)
{
int rc = close(stream->fd);

View file

@ -29,6 +29,7 @@ int fileno(FILE*);
int fgetc(FILE*);
int getc(FILE*);
int getchar();
FILE* fdopen(int fd, const char* mode);
FILE* fopen(const char* pathname, const char* mode);
int fclose(FILE*);
void rewind(FILE*);

0
LibC/sys/resource.h Normal file
View file

View file

@ -190,5 +190,22 @@ int isatty(int fd)
__RETURN_WITH_ERRNO(rc, 1, 0);
}
int getdtablesize()
{
int rc = Syscall::invoke(Syscall::Getdtablesize);
__RETURN_WITH_ERRNO(rc, 1, 0);
}
int dup(int old_fd)
{
int rc = Syscall::invoke(Syscall::Dup, (dword)old_fd);
__RETURN_WITH_ERRNO(rc, rc, -1);
}
int dup2(int old_fd, int new_fd)
{
int rc = Syscall::invoke(Syscall::Dup2, (dword)old_fd, (dword)new_fd);
__RETURN_WITH_ERRNO(rc, rc, -1);
}
}

View file

@ -39,6 +39,9 @@ int ttyname_r(int fd, char* buffer, size_t);
off_t lseek(int fd, off_t, int whence);
int link(const char* oldpath, const char* newpath);
int unlink(const char* pathname);
int getdtablesize();
int dup(int old_fd);
int dup2(int old_fd, int new_fd);
#define WEXITSTATUS(status) (((status) & 0xff00) >> 8)
#define WTERMSIG(status) ((status) & 0x7f)