1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-24 22:27:42 +00:00

LibC: A whole bunch of compat work towards porting Lynx.

This commit is contained in:
Andreas Kling 2019-03-14 15:18:15 +01:00
parent 48590a0e51
commit 2c3cf22bc9
10 changed files with 125 additions and 5 deletions

View file

@ -351,8 +351,20 @@ void perror(const char* s)
FILE* fopen(const char* pathname, const char* mode)
{
assert(!strcmp(mode, "r") || !strcmp(mode, "rb"));
int fd = open(pathname, O_RDONLY);
int flags = 0;
if (!strcmp(mode, "r") || !strcmp(mode, "rb"))
flags = O_RDONLY;
else if (!strcmp(mode, "r+") || !strcmp(mode, "rb+"))
flags = O_RDWR;
else if (!strcmp(mode, "w") || !strcmp(mode, "wb"))
flags = O_WRONLY | O_CREAT | O_TRUNC;
else if (!strcmp(mode, "w+") || !strcmp(mode, "wb+"))
flags = O_RDWR | O_CREAT | O_TRUNC;
else {
fprintf(stderr, "FIXME(LibC): fopen('%s', '%s')\n", pathname, mode);
ASSERT_NOT_REACHED();
}
int fd = open(pathname, flags, 0666);
if (fd < 0)
return nullptr;
return make_FILE(fd);
@ -368,7 +380,7 @@ FILE* freopen(const char* pathname, const char* mode, FILE* stream)
FILE* fdopen(int fd, const char* mode)
{
assert(!strcmp(mode, "r") || !strcmp(mode, "rb"));
// FIXME: Verify that the mode matches how fd is already open.
if (fd < 0)
return nullptr;
return make_FILE(fd);
@ -404,5 +416,13 @@ int pclose(FILE*)
assert(false);
}
int remove(const char* pathname)
{
int rc = unlink(pathname);
if (rc < 0 && errno != EISDIR)
return -1;
return rmdir(pathname);
}
}