aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/libc/CMakeLists.txt32
-rw-r--r--src/libc/include/errno.h43
-rw-r--r--src/libc/include/stddef.h8
-rw-r--r--src/libc/include/stdio.h35
-rw-r--r--src/libc/include/stdlib.h24
-rw-r--r--src/libc/include/string.h1
-rw-r--r--src/libc/private_include/libc/file.h15
-rw-r--r--src/libc/src/errno.c3
-rw-r--r--src/libc/src/free.c23
-rw-r--r--src/libc/src/malloc.c25
-rw-r--r--src/libc/src/memset.c7
-rw-r--r--src/libc/src/streams.c6
-rw-r--r--src/libc/src/strerror.c46
13 files changed, 254 insertions, 14 deletions
diff --git a/src/libc/CMakeLists.txt b/src/libc/CMakeLists.txt
index b9bb597..952df35 100644
--- a/src/libc/CMakeLists.txt
+++ b/src/libc/CMakeLists.txt
@@ -1,8 +1,30 @@
-set(inc "include")
+# wanix, a Unix-like operating system for WebAssembly
+# Copyright (C) 2025 Xavier Del Campo Romero
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see <https://www.gnu.org/licenses/>.
+
set(src
-"src/memcpy.c"
-"src/memset.c"
-"src/strlen.c"
+ "src/errno.c"
+ "src/free.c"
+ "src/malloc.c"
+ "src/memcmp.c"
+ "src/memcpy.c"
+ "src/memset.c"
+ "src/strchr.c"
+ "src/streams.c"
+ "src/strerror.c"
+ "src/strlen.c"
)
add_library(c ${src})
-target_include_directories(c PUBLIC ${inc})
+target_include_directories(c PUBLIC "include" PRIVATE "private_include")
diff --git a/src/libc/include/errno.h b/src/libc/include/errno.h
new file mode 100644
index 0000000..0456645
--- /dev/null
+++ b/src/libc/include/errno.h
@@ -0,0 +1,43 @@
+#ifndef _ERRNO_H
+#define _ERRNO_H
+
+extern int errno;
+
+enum
+{
+ OK,
+ EPERM,
+ ENOENT,
+ ESRCH,
+ EINTR,
+ EIO,
+ ENXIO,
+ E2BIG,
+ ENOEXEC,
+ EBADF,
+ ECHILD,
+ EAGAIN,
+ ENOMEM,
+ EACCES,
+ EFAULT,
+ ENOTBLK,
+ EBUSY,
+ EEXIST,
+ EXDEV,
+ ENODEV,
+ ENOTDIR,
+ EISDIR,
+ EINVAL,
+ ENFILE,
+ EMFILE,
+ ENOTTY,
+ ETXTBSY,
+ EFBIG,
+ ENOSPC,
+ ESPIPE,
+ EROFS,
+ EMLINK,
+ EPIPE,
+};
+
+#endif
diff --git a/src/libc/include/stddef.h b/src/libc/include/stddef.h
deleted file mode 100644
index 123e46f..0000000
--- a/src/libc/include/stddef.h
+++ /dev/null
@@ -1,8 +0,0 @@
-#ifndef _STDDEF_H
-#define _STDDEF_H
-
-#define NULL ((void *)0)
-
-typedef unsigned long size_t;
-
-#endif
diff --git a/src/libc/include/stdio.h b/src/libc/include/stdio.h
new file mode 100644
index 0000000..1d0e27c
--- /dev/null
+++ b/src/libc/include/stdio.h
@@ -0,0 +1,35 @@
+/*
+ * wanix, a Unix-like operating system for WebAssembly
+ * Copyright (C) 2025 Xavier Del Campo Romero
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#ifndef _STDIO_H
+#define _STDIO_H
+
+enum
+{
+ EOF = -1
+};
+
+typedef struct __file FILE;
+
+FILE *fopen(const char *__path, const char *__mode);
+int printf(const char *__fmt, ...);
+int fprintf(FILE *__stream, const char *__fmt, ...);
+
+extern FILE *stdin, *stdout, *stderr;
+
+#endif
diff --git a/src/libc/include/stdlib.h b/src/libc/include/stdlib.h
index 79d3122..4065f1c 100644
--- a/src/libc/include/stdlib.h
+++ b/src/libc/include/stdlib.h
@@ -1,7 +1,31 @@
+/*
+ * wanix, a Unix-like operating system for WebAssembly
+ * Copyright (C) 2025 Xavier Del Campo Romero
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
#ifndef _STDLIB_H
#define _STDLIB_H
+#include <stddef.h>
+
#define EXIT_SUCCESS 0
#define EXIT_FAILURE 1
+void *malloc(size_t __n);
+void *calloc(size_t __nemb, size_t __size);
+void free(void *__p);
+
#endif
diff --git a/src/libc/include/string.h b/src/libc/include/string.h
index 7efda4e..20c9cf3 100644
--- a/src/libc/include/string.h
+++ b/src/libc/include/string.h
@@ -7,6 +7,7 @@ int memcmp(const void *__s1, const void *__s2, size_t __n);
void *memcpy(void *__dst, const void *__src, size_t __n);
void *memset(void *__dst, int __c, size_t __n);
char *strchr(const char *__s, int __n);
+char *strerror(int errnum);
size_t strlen(const char *__s);
#endif
diff --git a/src/libc/private_include/libc/file.h b/src/libc/private_include/libc/file.h
new file mode 100644
index 0000000..3ba554c
--- /dev/null
+++ b/src/libc/private_include/libc/file.h
@@ -0,0 +1,15 @@
+#ifndef LIBC_FILE_H
+#define LIBC_FILE_H
+
+enum
+{
+ STDIN_FILENO,
+ STDOUT_FILENO,
+ STDERR_FILENO
+};
+
+struct __file
+{
+};
+
+#endif
diff --git a/src/libc/src/errno.c b/src/libc/src/errno.c
new file mode 100644
index 0000000..d013499
--- /dev/null
+++ b/src/libc/src/errno.c
@@ -0,0 +1,3 @@
+#include <errno.h>
+
+int errno;
diff --git a/src/libc/src/free.c b/src/libc/src/free.c
new file mode 100644
index 0000000..d89a80a
--- /dev/null
+++ b/src/libc/src/free.c
@@ -0,0 +1,23 @@
+/*
+ * wanix, a Unix-like operating system for WebAssembly
+ * Copyright (C) 2025 Xavier Del Campo Romero
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include <stdlib.h>
+
+void free(void *const ptr)
+{
+}
diff --git a/src/libc/src/malloc.c b/src/libc/src/malloc.c
new file mode 100644
index 0000000..aa5ff31
--- /dev/null
+++ b/src/libc/src/malloc.c
@@ -0,0 +1,25 @@
+/*
+ * wanix, a Unix-like operating system for WebAssembly
+ * Copyright (C) 2025 Xavier Del Campo Romero
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include <stdlib.h>
+#include <stddef.h>
+
+void *malloc(const size_t __n)
+{
+ return NULL;
+}
diff --git a/src/libc/src/memset.c b/src/libc/src/memset.c
index d3f00c1..90eb611 100644
--- a/src/libc/src/memset.c
+++ b/src/libc/src/memset.c
@@ -3,5 +3,10 @@
void *memset(void *const dst, const int c, const size_t n)
{
- return NULL;
+ unsigned char *d = dst;
+
+ for (size_t i = 0; i < n; i++)
+ *d++ = c;
+
+ return dst;
}
diff --git a/src/libc/src/streams.c b/src/libc/src/streams.c
new file mode 100644
index 0000000..95289e3
--- /dev/null
+++ b/src/libc/src/streams.c
@@ -0,0 +1,6 @@
+#include <libc/file.h>
+#include <stdio.h>
+
+static FILE f[3];
+FILE *stdin = &f[STDIN_FILENO], *stdout = &f[STDOUT_FILENO],
+ *stderr = &f[STDERR_FILENO];
diff --git a/src/libc/src/strerror.c b/src/libc/src/strerror.c
new file mode 100644
index 0000000..6444e98
--- /dev/null
+++ b/src/libc/src/strerror.c
@@ -0,0 +1,46 @@
+#include <string.h>
+#include <errno.h>
+
+char *strerror(const int errnum)
+{
+ static const char *const s[] =
+ {
+ [EPERM] = "Operation not permitted",
+ [ENOENT] = "No such file or directory",
+ [ESRCH] = "No such process",
+ [EINTR] = "Interrupted system call",
+ [EIO] = "Input/output error",
+ [ENXIO] = "No such device or address",
+ [E2BIG] = "Argument list too long",
+ [ENOEXEC] = "Exec format error",
+ [EBADF] = "Bad file descriptor",
+ [ECHILD] = "No child processes",
+ [EAGAIN] = "Resource temporarily unavailable",
+ [ENOMEM] = "Cannot allocate memory",
+ [EACCES] = "Permission denied",
+ [EFAULT] = "Bad address",
+ [ENOTBLK] = "Block device required",
+ [EBUSY] = "Device or resource busy",
+ [EEXIST] = "File exists",
+ [EXDEV] = "Invalid cross-device link",
+ [ENODEV] = "No such device",
+ [ENOTDIR] = "Not a directory",
+ [EISDIR] = "Is a directory",
+ [EINVAL] = "Invalid argument",
+ [ENFILE] = "Too many open files in system",
+ [EMFILE] = "Too many open files",
+ [ENOTTY] = "Inappropriate ioctl for device",
+ [ETXTBSY] = "Text file busy",
+ [EFBIG] = "File too large",
+ [ENOSPC] = "No space left on device",
+ [ESPIPE] = "Illegal seek",
+ [EROFS] = "Read-only file system",
+ [EMLINK] = "Too many links",
+ [EPIPE] = "Broken pipe",
+ };
+
+ if (errnum < 0 || errnum >= sizeof s / sizeof *s)
+ return "Unknown errno";
+
+ return (char *)s[errnum];
+}