refactor(libc): move Lua an DOOM stubs into shared libc modules

This commit is contained in:
boreddevnl
2026-04-19 21:58:25 +02:00
parent f788ba416d
commit 78ae0f154d
36 changed files with 2145 additions and 1263 deletions

View File

@@ -1,219 +0,0 @@
#include "boredos_libc.h"
#include <syscall.h>
int errno = 0;
static FILE _stderr = {2, 0, 0};
static FILE _stdout = {1, 0, 0};
static FILE _stdin = {0, 0, 0};
FILE* stderr = &_stderr;
FILE* stdout = &_stdout;
FILE* stdin = &_stdin;
FILE *fopen(const char *path, const char *mode) {
int fd = sys_open(path, mode);
if (fd < 0) return NULL;
FILE *f = malloc(sizeof(FILE));
f->fd = fd;
f->eof = 0;
f->error = 0;
return f;
}
int fclose(FILE *stream) {
if (!stream) return EOF;
if (stream != stderr && stream != stdout && stream != stdin) {
sys_close(stream->fd);
free(stream);
}
return 0;
}
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream) {
if (!stream) return 0;
int bytes = sys_read(stream->fd, ptr, size * nmemb);
if (bytes < 0) {
stream->error = 1;
return 0;
}
if (bytes == 0) stream->eof = 1;
return bytes / size;
}
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) {
if (!stream) return 0;
if (stream == stdout || stream == stderr) {
sys_write(stream->fd, ptr, size * nmemb);
return nmemb;
}
int bytes = sys_write_fs(stream->fd, ptr, size * nmemb);
if (bytes < 0) {
stream->error = 1;
return 0;
}
return bytes / size;
}
int fseek(FILE *stream, long offset, int whence) {
if (!stream) return -1;
stream->eof = 0;
return sys_seek(stream->fd, offset, whence);
}
long ftell(FILE *stream) {
if (!stream) return -1;
return sys_tell(stream->fd);
}
int remove(const char *pathname) {
return sys_delete(pathname);
}
int rename(const char *oldpath, const char *newpath) {
return -1;
}
int fputc(int c, FILE *stream) {
unsigned char ch = c;
if (fwrite(&ch, 1, 1, stream) != 1) return EOF;
return ch;
}
int fputs(const char *s, FILE *stream) {
size_t len = strlen(s);
if (fwrite(s, 1, len, stream) != len) return EOF;
return 0;
}
long filelength(FILE *f) {
if (!f) return -1;
return sys_size(f->fd);
}
int mkdir(const char *pathname, int mode) {
return sys_mkdir(pathname);
}
int access(const char *pathname, int mode) {
if (sys_exists(pathname)) return 0;
return -1;
}
int stat(const char *pathname, struct stat *statbuf) {
if (sys_exists(pathname)) {
if (statbuf) {
statbuf->st_size = 0;
statbuf->st_mode = 0;
}
return 0;
}
return -1;
}
int strncasecmp(const char *s1, const char *s2, size_t n) {
while (n--) {
char c1 = tolower(*s1++);
char c2 = tolower(*s2++);
if (c1 != c2) return c1 - c2;
if (!c1) break;
}
return 0;
}
int strcasecmp(const char *s1, const char *s2) {
while (1) {
char c1 = tolower(*s1++);
char c2 = tolower(*s2++);
if (c1 != c2) return c1 - c2;
if (!c1) break;
}
return 0;
}
char *strncpy(char *dest, const char *src, size_t n) {
char *ret = dest;
while (n && *src) { *dest++ = *src++; n--; }
while (n) { *dest++ = 0; n--; }
return ret;
}
int strncmp(const char *s1, const char *s2, size_t n) {
while (n--) {
if (*s1 != *s2) return *s1 - *s2;
if (!*s1) break;
s1++; s2++;
}
return 0;
}
char *strrchr(const char *s, int c) {
const char *last = NULL;
while (*s) { if (*s == c) last = s; s++; }
if (c == 0) last = s;
return (char*)last;
}
char *strdup(const char *s) {
size_t len = strlen(s) + 1;
char *dup = malloc(len);
if (dup) memcpy(dup, s, len);
return dup;
}
int toupper(int c) { return (c >= 'a' && c <= 'z') ? c - 32 : c; }
int tolower(int c) { return (c >= 'A' && c <= 'Z') ? c + 32 : c; }
int isspace(int c) { return c == ' ' || c == '\n' || c == '\t' || c == '\r' || c == '\v' || c == '\f'; }
int isdigit(int c) { return c >= '0' && c <= '9'; }
int isprint(int c) { return c >= 32 && c <= 126; }
int isalpha(int c) { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); }
int isalnum(int c) { return isalpha(c) || isdigit(c); }
int isgraph(int c) { return c > 32 && c <= 126; }
int ispunct(int c) { return isprint(c) && !isspace(c) && !isalnum(c); }
int isupper(int c) { return c >= 'A' && c <= 'Z'; }
void _exit(int status) {
exit(status);
}
int fflush(FILE *stream) { return 0; }
int abs(int x) { return x < 0 ? -x : x; }
int putchar(int c) { return fputc(c, stdout); }
int system(const char *command) { return -1; }
#define STB_SPRINTF_IMPLEMENTATION
#define STB_SPRINTF_NOFLOAT
#include "stb_sprintf.h"
int vfprintf(FILE *stream, const char *format, va_list ap) {
char buf[1024];
int len = stbsp_vsnprintf(buf, sizeof(buf), format, ap);
if (len > 0) fwrite(buf, 1, len, stream);
return len;
}
int fprintf(FILE *stream, const char *format, ...) {
va_list ap;
va_start(ap, format);
int len = vfprintf(stream, format, ap);
va_end(ap);
return len;
}
int sprintf(char *str, const char *format, ...) {
va_list ap;
va_start(ap, format);
int len = stbsp_vsprintf(str, format, ap);
va_end(ap);
return len;
}
int snprintf(char *str, size_t size, const char *format, ...) {
va_list ap;
va_start(ap, format);
int len = stbsp_vsnprintf(str, size, format, ap);
va_end(ap);
return len;
}
int vsnprintf(char *str, size_t size, const char *format, va_list ap) {
return stbsp_vsnprintf(str, size, format, ap);
}
int sscanf(const char *str, const char *format, ...) { return 0; }

View File

@@ -5,6 +5,9 @@
#include <stddef.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#define SEEK_SET 0
#define SEEK_CUR 1
@@ -12,12 +15,6 @@
#define EOF (-1)
typedef struct {
int fd;
int eof;
int error;
} FILE;
extern FILE* stderr;
extern FILE* stdout;
extern FILE* stdin;
@@ -34,6 +31,14 @@ extern FILE* stdin;
#define W_OK 2
#define X_OK 1
int open(const char *pathname, int flags, ...);
int close(int fd);
ssize_t read(int fd, void *buf, size_t count);
ssize_t write(int fd, const void *buf, size_t count);
off_t lseek(int fd, off_t offset, int whence);
int unlink(const char *pathname);
int isatty(int fd);
FILE *fopen(const char *path, const char *mode);
int fclose(FILE *stream);
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
@@ -73,11 +78,6 @@ int isupper(int c);
int mkdir(const char *pathname, int mode);
int access(const char *pathname, int mode);
struct stat {
int st_size;
int st_mode;
};
int stat(const char *pathname, struct stat *statbuf);
char *strstr(const char *haystack, const char *needle);

View File

@@ -25,6 +25,7 @@
#include "deh_main.h"
#include "i_swap.h"
#include "i_system.h"
#include "i_video.h"
#include "hu_stuff.h"
@@ -288,14 +289,47 @@ void HU_Init(void)
int i;
int j;
int fallback_lump = -1;
char buffer[9];
// load the heads-up font
j = HU_FONTSTART;
for (i=0;i<HU_FONTSIZE;i++)
{
DEH_snprintf(buffer, 9, "STCFN%.3d", j++);
hu_font[i] = (patch_t *) W_CacheLumpName(buffer, PU_STATIC);
int lump;
DEH_snprintf(buffer, 9, "STCFN%.3d", j++);
lump = W_CheckNumForName(buffer);
if (lump < 0 && fallback_lump < 0)
{
int probe;
for (probe = HU_FONTSTART; probe <= HU_FONTEND; ++probe)
{
DEH_snprintf(buffer, 9, "STCFN%.3d", probe);
fallback_lump = W_CheckNumForName(buffer);
if (fallback_lump >= 0)
{
break;
}
}
if (fallback_lump < 0)
{
fallback_lump = W_CheckNumForName("STTNUM0");
}
}
if (lump < 0)
{
lump = fallback_lump;
}
if (lump < 0)
{
I_Error("HU_Init: no usable HUD font lumps (missing STCFNxxx/STTNUM0)");
}
hu_font[i] = (patch_t *) W_CacheLumpNum(lump, PU_STATIC);
}
}

View File

@@ -264,7 +264,7 @@ void I_Quit (void)
#endif
}
#if !defined(_WIN32) && !defined(__MACOSX__) && !defined(__DJGPP__)
#if !defined(_WIN32) && !defined(__MACOSX__) && !defined(__DJGPP__) && !defined(BOREDOS)
#define ZENITY_BINARY "/usr/bin/zenity"
// returns non-zero if zenity is available
@@ -347,7 +347,7 @@ static int ZenityErrorBox(char *message)
return result;
}
#endif /* !defined(_WIN32) && !defined(__MACOSX__) && !defined(__DJGPP__) */
#endif /* !defined(_WIN32) && !defined(__MACOSX__) && !defined(__DJGPP__) && !defined(BOREDOS) */
//
@@ -456,7 +456,9 @@ void I_Error (char *error, ...)
#else
{
#if !defined(BOREDOS)
ZenityErrorBox(msgbuf);
#endif
}
#endif

View File

@@ -1 +1,15 @@
#include "../boredos_libc.h"
#ifndef DOOM_COMPAT_SYS_STAT_H
#define DOOM_COMPAT_SYS_STAT_H
#include "types.h"
struct stat {
int st_size;
int st_mode;
};
int stat(const char *pathname, struct stat *statbuf);
int fstat(int fd, struct stat *statbuf);
int mkdir(const char *pathname, int mode);
#endif

View File

@@ -1 +1,8 @@
#include "../boredos_libc.h"
#ifndef DOOM_COMPAT_SYS_TYPES_H
#define DOOM_COMPAT_SYS_TYPES_H
typedef long ssize_t;
typedef long off_t;
typedef unsigned int mode_t;
#endif