More LibC portability work while trying to get figlet building.

This commit is contained in:
Andreas Kling 2018-10-31 10:14:56 +01:00
parent bb90c8ecab
commit 9160fd0d47
12 changed files with 128 additions and 2 deletions

View file

@ -63,6 +63,8 @@ DWORD handle(DWORD function, DWORD arg1, DWORD arg2, DWORD arg3)
return current->sys$get_dir_entries((int)arg1, (void*)arg2, (size_t)arg3);
case Syscall::PosixLstat:
return current->sys$lstat((const char*)arg1, (Unix::stat*)arg2);
case Syscall::PosixStat:
return current->sys$stat((const char*)arg1, (Unix::stat*)arg2);
case Syscall::PosixGetcwd:
return current->sys$getcwd((char*)arg1, (size_t)arg2);
case Syscall::PosixOpen:

View file

@ -38,6 +38,7 @@ enum Function {
PosixReadlink = 0x2006,
PosixWrite = 0x2007,
PosixTtynameR = 0x2008,
PosixStat = 0x2009,
};
void initialize();

View file

@ -851,6 +851,17 @@ int Task::sys$lstat(const char* path, Unix::stat* statbuf)
return 0;
}
int Task::sys$stat(const char* path, Unix::stat* statbuf)
{
VALIDATE_USER_BUFFER(statbuf, sizeof(Unix::stat));
int error;
auto handle = VirtualFileSystem::the().open(move(path), error, 0, cwdInode());
if (!handle)
return error;
handle->stat(statbuf);
return 0;
}
int Task::sys$readlink(const char* path, char* buffer, size_t size)
{
VALIDATE_USER_BUFFER(path, strlen(path));

View file

@ -93,6 +93,7 @@ public:
ssize_t sys$read(int fd, void* outbuf, size_t nread);
ssize_t sys$write(int fd, const void*, size_t);
int sys$lstat(const char*, Unix::stat*);
int sys$stat(const char*, Unix::stat*);
int sys$seek(int fd, int offset);
int sys$kill(pid_t pid, int sig);
int sys$geterror() { return m_error; }

4
LibC/alloca.h Normal file
View file

@ -0,0 +1,4 @@
#pragma once
#define alloca __builtin_alloca

View file

@ -1,4 +1,35 @@
#pragma once
#define isascii(c) (((c) & ~0x7f) == 0)
inline int isascii(int ch)
{
return (ch & ~0x7f) == 0;
}
inline int isspace(int ch)
{
return ch == ' ' || ch == '\f' || ch == '\n' || ch == '\r' || ch == '\t' == '\v';
}
inline int islower(int c)
{
return c >= 'a' && c <= 'z';
}
inline int isupper(int c)
{
return c >= 'A' && c <= 'Z';
}
inline int tolower(int c)
{
if (isupper(c))
return c | 0x20;
return c;
}
inline int toupper(int c)
{
if (islower(c))
return c & ~0x20;
return c;
}

View file

@ -20,6 +20,22 @@ static void sys_putch(char*&, char ch)
putchar(ch);
}
static FILE* __current_stream = nullptr;
static void stream_putch(char*&, char ch)
{
write(__current_stream->fd, &ch, 1);
}
int fprintf(FILE* fp, const char* fmt, ...)
{
__current_stream = fp;
va_list ap;
va_start(ap, fmt);
int ret = printfInternal(stream_putch, nullptr, fmt, ap);
va_end(ap);
return ret;
}
int printf(const char* fmt, ...)
{
va_list ap;

View file

@ -10,6 +10,7 @@ void free(void*);
void* calloc(size_t nmemb, size_t);
void* realloc(void *ptr, size_t);
void exit(int status);
void abort();

View file

@ -40,6 +40,53 @@ void memcpy(void* dest, const void* src, size_t n)
*(bdest++) = *(bsrc++);
}
char* strcpy(char* dest, const char *src)
{
char* originalDest = dest;
while ((*dest++ = *src++) != '\0');
return originalDest;
}
char* strncpy(char* dest, const char* src, size_t n)
{
size_t i;
for (i = 0; i < n && src[i] != '\0'; ++i)
dest[i] = src[i];
for ( ; i < n; ++i)
dest[i] = '\0';
return dest;
}
char* strchr(const char* str, int c)
{
if (!str)
return nullptr;
char* ptr = (char*)str;
while (*ptr != c)
++ptr;
return ptr;
}
char* strcat(char *dest, const char *src)
{
size_t destLength = strlen(dest);
size_t i;
for (i = 0 ; src[i] != '\0' ; i++)
dest[destLength + i] = src[i];
dest[destLength + i] = '\0';
return dest;
}
char* strncat(char *dest, const char *src, size_t n)
{
size_t destLength = strlen(dest);
size_t i;
for (i = 0 ; i < n && src[i] != '\0' ; i++)
dest[destLength + i] = src[i];
dest[destLength + i] = '\0';
return dest;
}
const char* strerror(int errnum)
{
switch (errnum) {

View file

@ -9,6 +9,11 @@ size_t strlen(const char*);
int strcmp(const char*, const char*);
int memcmp(const void*, const void*, size_t);
void memcpy(void*, const void*, size_t);
char* strcpy(char* dest, const char* src);
char* strncpy(char* dest, const char* src, size_t);
char* strchr(const char*, int c);
char* strcat(char *dest, const char *src);
char* strncat(char *dest, const char *src, size_t);
const char* strerror(int errnum);
__END_DECLS

View file

@ -64,12 +64,18 @@ pid_t waitpid(pid_t waitee, int* wstatus, int options)
__RETURN_WITH_ERRNO(rc, rc, -1);
}
int lstat(const char* path, stat* statbuf)
int lstat(const char* path, struct stat* statbuf)
{
int rc = Syscall::invoke(Syscall::PosixLstat, (dword)path, (dword)statbuf);
__RETURN_WITH_ERRNO(rc, rc, -1);
}
int stat(const char* path, struct stat* statbuf)
{
int rc = Syscall::invoke(Syscall::PosixStat, (dword)path, (dword)statbuf);
__RETURN_WITH_ERRNO(rc, rc, -1);
}
int chdir(const char* path)
{
int rc = Syscall::invoke(Syscall::PosixChdir, (dword)path);

View file

@ -16,6 +16,7 @@ pid_t waitpid(pid_t, int* wstatus, int options);
int chdir(const char* path);
char* getcwd(char* buffer, size_t size);
int lstat(const char* path, struct stat* statbuf);
int stat(const char* path, struct stat* statbuf);
int sleep(unsigned seconds);
int gethostname(char*, size_t);
ssize_t readlink(const char* path, char* buffer, size_t);