CLI apps transfer to Userspace

This commit is contained in:
boreddevnl
2026-02-27 22:13:53 +01:00
parent 8c6d751254
commit 304c2e1383
97 changed files with 1669 additions and 2398 deletions

Binary file not shown.

View File

@@ -123,14 +123,142 @@ void *realloc(void *ptr, size_t size) {
}
void *memset(void *s, int c, size_t n) {
unsigned char *p = s;
unsigned char *p = (unsigned char *)s;
while (n--) *p++ = (unsigned char)c;
return s;
}
void *memcpy(void *dest, const void *src, size_t n) {
unsigned char *d = dest;
const unsigned char *s = src;
unsigned char *d = (unsigned char *)dest;
const unsigned char *s = (const unsigned char *)src;
while (n--) *d++ = *s++;
return dest;
}
// String functions
size_t strlen(const char *s) {
size_t len = 0;
while (s[len]) len++;
return len;
}
int strcmp(const char *s1, const char *s2) {
while (*s1 && (*s1 == *s2)) {
s1++;
s2++;
}
return *(const unsigned char*)s1 - *(const unsigned char*)s2;
}
char* strcpy(char *dest, const char *src) {
char *ret = dest;
while (*src) *dest++ = *src++;
*dest = 0;
return ret;
}
char* strcat(char *dest, const char *src) {
char *ret = dest;
while (*dest) dest++;
while (*src) *dest++ = *src++;
*dest = 0;
return ret;
}
int atoi(const char *nptr) {
int res = 0;
int sign = 1;
if (*nptr == '-') {
sign = -1;
nptr++;
}
while (*nptr >= '0' && *nptr <= '9') {
res = res * 10 + (*nptr - '0');
nptr++;
}
return sign * res;
}
void itoa(int n, char *buf) {
if (n == 0) {
buf[0] = '0'; buf[1] = 0; return;
}
int i = 0;
int sign = n < 0;
if (sign) n = -n;
while (n > 0) {
buf[i++] = (n % 10) + '0';
n /= 10;
}
if (sign) buf[i++] = '-';
buf[i] = 0;
// Reverse
for (int j = 0; j < i / 2; j++) {
char t = buf[j];
buf[j] = buf[i - 1 - j];
buf[i - 1 - j] = t;
}
}
// IO functions
void puts(const char *s) {
sys_write(1, s, strlen(s));
sys_write(1, "\n", 1);
}
void printf(const char *fmt, ...) {
// Simple printf implementation
__builtin_va_list args;
__builtin_va_start(args, fmt);
char buf[1024];
int buf_idx = 0;
while (*fmt) {
if (*fmt == '%') {
fmt++;
if (*fmt == 's') {
char *s = __builtin_va_arg(args, char *);
while (*s) buf[buf_idx++] = *s++;
} else if (*fmt == 'd') {
int d = __builtin_va_arg(args, int);
char ibuf[32];
itoa(d, ibuf);
char *s = ibuf;
while (*s) buf[buf_idx++] = *s++;
} else if (*fmt == 'c') {
char c = (char)__builtin_va_arg(args, int);
buf[buf_idx++] = c;
} else if (*fmt == '%') {
buf[buf_idx++] = '%';
}
} else {
buf[buf_idx++] = *fmt;
}
fmt++;
if (buf_idx >= 1022) break; // Simple overflow protection
}
buf[buf_idx] = 0;
sys_write(1, buf, buf_idx);
__builtin_va_end(args);
}
// System/Process functions
int chdir(const char *path) {
return sys_chdir(path);
}
char* getcwd(char *buf, int size) {
if (sys_getcwd(buf, size) == 0) return buf;
return NULL;
}
void sleep(int ms) {
// We don't have a sleep syscall yet, so we'll just busy wait for now or skip
// Actually, BoredOS doesn't seem to have a sleep syscall.
// I'll add one if needed, but for now I'll just skip.
(void)ms;
}
void exit(int status) {
sys_exit(status);
}

View File

@@ -11,4 +11,22 @@ void* realloc(void* ptr, size_t size);
void *memset(void *s, int c, size_t n);
void *memcpy(void *dest, const void *src, size_t n);
// String functions
size_t strlen(const char *s);
int strcmp(const char *s1, const char *s2);
char* strcpy(char *dest, const char *src);
char* strcat(char *dest, const char *src);
int atoi(const char *nptr);
void itoa(int n, char *buf);
// IO functions
void puts(const char *s);
void printf(const char *fmt, ...);
// System/Process functions
int chdir(const char *path);
char* getcwd(char *buf, int size);
void sleep(int ms);
void exit(int status);
#endif

Binary file not shown.

View File

@@ -121,3 +121,15 @@ int sys_exists(const char *path) {
return (int)syscall2(SYS_FS, FS_CMD_EXISTS, (uint64_t)path);
}
int sys_getcwd(char *buf, int size) {
return (int)syscall3(SYS_FS, FS_CMD_GETCWD, (uint64_t)buf, (uint64_t)size);
}
int sys_chdir(const char *path) {
return (int)syscall2(SYS_FS, FS_CMD_CHDIR, (uint64_t)path);
}
void sys_kill(int pid) {
syscall1(SYS_KILL, (uint64_t)pid);
}

View File

@@ -9,6 +9,7 @@
#define SYS_GUI 3
#define SYS_FS 4
#define SYS_SYSTEM 5
#define SYS_KILL 10
#define SYS_SBRK 9
// FS Commands
@@ -23,6 +24,8 @@
#define FS_CMD_SIZE 9
#define FS_CMD_MKDIR 10
#define FS_CMD_EXISTS 11
#define FS_CMD_GETCWD 12
#define FS_CMD_CHDIR 13
// Internal assembly entry into Ring 0
extern uint64_t syscall0(uint64_t sys_num);
@@ -36,6 +39,7 @@ extern uint64_t syscall5(uint64_t sys_num, uint64_t arg1, uint64_t arg2, uint64_
void sys_exit(int status);
int sys_write(int fd, const char *buf, int len);
void *sys_sbrk(int incr);
void sys_kill(int pid);
int sys_system(int cmd, uint64_t arg1, uint64_t arg2, uint64_t arg3, uint64_t arg4);
// FS API
@@ -49,8 +53,18 @@ uint32_t sys_size(int fd);
int sys_delete(const char *path);
int sys_mkdir(const char *path);
int sys_exists(const char *path);
int sys_getcwd(char *buf, int size);
int sys_chdir(const char *path);
struct FAT32_FileInfo;
int sys_list(const char *path, struct FAT32_FileInfo *entries, int max_entries);
typedef struct {
char name[256];
uint32_t size;
uint8_t is_directory;
uint32_t start_cluster;
uint16_t write_date;
uint16_t write_time;
} FAT32_FileInfo;
int sys_list(const char *path, FAT32_FileInfo *entries, int max_entries);
#endif

Binary file not shown.