diff options
Diffstat (limited to 'src/libc')
34 files changed, 1024 insertions, 27 deletions
diff --git a/src/libc/CMakeLists.txt b/src/libc/CMakeLists.txt index 952df35..e0663cf 100644 --- a/src/libc/CMakeLists.txt +++ b/src/libc/CMakeLists.txt @@ -1,4 +1,4 @@ -# wanix, a Unix-like operating system for WebAssembly +# wanix, a Unix-like operating system for WebAssembly applications. # Copyright (C) 2025 Xavier Del Campo Romero # # This program is free software: you can redistribute it and/or modify @@ -15,16 +15,15 @@ # along with this program. If not, see <https://www.gnu.org/licenses/>. set(src - "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" + printf/printf.c + tinyalloc/tinyalloc.c ) add_library(c ${src}) -target_include_directories(c PUBLIC "include" PRIVATE "private_include") +add_subdirectory(src) +target_include_directories(c PUBLIC include printf + PRIVATE private_include tinyalloc) +target_link_libraries(c PRIVATE aio fs drv) + +if(PS1_BUILD) + add_subdirectory(src/ps1) +endif() diff --git a/src/libc/include/errno.h b/src/libc/include/errno.h index 0456645..60037ef 100644 --- a/src/libc/include/errno.h +++ b/src/libc/include/errno.h @@ -1,3 +1,21 @@ +/* + * wanix, a Unix-like operating system for WebAssembly applications. + * 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 _ERRNO_H #define _ERRNO_H @@ -5,7 +23,7 @@ extern int errno; enum { - OK, + SUCCESS, EPERM, ENOENT, ESRCH, @@ -38,6 +56,105 @@ enum EROFS, EMLINK, EPIPE, + EDOM, + ERANGE, + EDEADLK, + ENAMETOOLONG, + ENOLCK, + ENOSYS, + ENOTEMPTY, + ELOOP, + ENOMSG, + EIDRM, + ECHRNG, + EL2NSYNC, + EL3HLT, + EL3RST, + ELNRNG, + EUNATCH, + ENOCSI, + EL2HLT, + EBADE, + EBADR, + EXFULL, + ENOANO, + EBADRQC, + EBADSLT, + EBFONT, + ENOSTR, + ENODATA, + ETIME, + ENOSR, + ENONET, + ENOPKG, + EREMOTE, + ENOLINK, + EADV, + ESRMNT, + ECOMM, + EPROTO, + EMULTIHOP, + EDOTDOT, + EBADMSG, + EOVERFLOW, + ENOTUNIQ, + EBADFD, + EREMCHG, + ELIBACC, + ELIBBAD, + ELIBSCN, + ELIBMAX, + ELIBEXEC, + EILSEQ, + ERESTART, + ESTRPIPE, + EUSERS, + ENOTSOCK, + EDESTADDRREQ, + EMSGSIZE, + EPROTOTYPE, + ENOPROTOOPT, + EPROTONOSUPPORT, + ESOCKTNOSUPPORT, + ENOTSUP, + EPFNOSUPPORT, + EAFNOSUPPORT, + EADDRINUSE, + EADDRNOTAVAIL, + ENETDOWN, + ENETUNREACH, + ENETRESET, + ECONNABORTED, + ECONNRESET, + ENOBUFS, + EISCONN, + ENOTCONN, + ESHUTDOWN, + ETOOMANYREFS, + ETIMEDOUT, + ECONNREFUSED, + EHOSTDOWN, + EHOSTUNREACH, + EALREADY, + EINPROGRESS, + ESTALE, + EUCLEAN, + ENOTNAM, + ENAVAIL, + EISNAM, + EREMOTEIO, + EDQUOT, + ENOMEDIUM, + EMEDIUMTYPE, + ECANCELED, + ENOKEY, + EKEYEXPIRED, + EKEYREVOKED, + EKEYREJECTED, + EOWNERDEAD, + ENOTRECOVERABLE, + ERFKILL, + EHWPOISON }; #endif diff --git a/src/libc/include/fcntl.h b/src/libc/include/fcntl.h new file mode 100644 index 0000000..9713a9f --- /dev/null +++ b/src/libc/include/fcntl.h @@ -0,0 +1,29 @@ +/* + * wanix, a Unix-like operating system for WebAssembly applications. + * 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 _FCNTL_H +#define _FCNTL_H + +enum +{ + O_RDONLY = 1, + O_WRONLY = 1 << 1, + O_RDWR = O_RDONLY | O_WRONLY +}; + +#endif diff --git a/src/libc/include/inttypes.h b/src/libc/include/inttypes.h new file mode 100644 index 0000000..f07e94b --- /dev/null +++ b/src/libc/include/inttypes.h @@ -0,0 +1,34 @@ +/* + * wanix, a Unix-like operating system for WebAssembly applications. + * 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 _INTTYPES_H +#define _INTTYPES_H + +#define PRIu8 "hhu" +#define PRIu16 "hu" +#define PRIu32 "lu" +#define PRIu64 "llu" +#define PRIi8 "hhd" +#define PRIi16 "hd" +#define PRIi32 "ld" +#define PRIi64 "lld" +#define PRIx8 "hhx" +#define PRIx16 "hx" +#define PRIx32 "lx" +#define PRIx64 "llx" + +#endif diff --git a/src/libc/include/stdint.h b/src/libc/include/stdint.h new file mode 100644 index 0000000..d2701b1 --- /dev/null +++ b/src/libc/include/stdint.h @@ -0,0 +1,33 @@ +/* + * wanix, a Unix-like operating system for WebAssembly applications. + * 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 _STDINT_H +#define _STDINT_H + +typedef unsigned char uint8_t, uint_least8_t; +typedef signed char int8_t, int_least8_t; +typedef unsigned short uint16_t, uint_least16_t; +typedef short int16_t, int_least16_t; +typedef unsigned uintptr_t, uint32_t, uint_least32_t, uint_fast8_t, + uint_fast16_t, uint_fast32_t; +typedef int intptr_t, int32_t, int_least32_t, int_fast8_t, int_fast16_t, + int_fast32_t; +typedef unsigned long long uint64_t, uint_least64_t, uint_fast64_t, uintmax_t; +typedef long long int64_t, int_least64_t, int_fast64_t, intmax_t; + +#endif diff --git a/src/libc/include/stdio.h b/src/libc/include/stdio.h index 1d0e27c..7c6504a 100644 --- a/src/libc/include/stdio.h +++ b/src/libc/include/stdio.h @@ -1,5 +1,5 @@ /* - * wanix, a Unix-like operating system for WebAssembly + * wanix, a Unix-like operating system for WebAssembly applications. * Copyright (C) 2025 Xavier Del Campo Romero * * This program is free software: you can redistribute it and/or modify @@ -19,17 +19,14 @@ #ifndef _STDIO_H #define _STDIO_H -enum -{ - EOF = -1 -}; - +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, ...); +int puts(const char *__s); +int putchar(int __c); extern FILE *stdin, *stdout, *stderr; +#include <printf.h> + #endif diff --git a/src/libc/include/stdlib.h b/src/libc/include/stdlib.h index 4065f1c..0d2fee7 100644 --- a/src/libc/include/stdlib.h +++ b/src/libc/include/stdlib.h @@ -1,5 +1,5 @@ /* - * wanix, a Unix-like operating system for WebAssembly + * wanix, a Unix-like operating system for WebAssembly applications. * Copyright (C) 2025 Xavier Del Campo Romero * * This program is free software: you can redistribute it and/or modify @@ -24,8 +24,12 @@ #define EXIT_SUCCESS 0 #define EXIT_FAILURE 1 +#define abort() __abort(__FILE__, __LINE__) + void *malloc(size_t __n); void *calloc(size_t __nemb, size_t __size); +void *realloc(void *__ptr, size_t __size); void free(void *__p); +void __abort(const char *__file, int __lineno); #endif diff --git a/src/libc/include/string.h b/src/libc/include/string.h index 20c9cf3..0a7b7f2 100644 --- a/src/libc/include/string.h +++ b/src/libc/include/string.h @@ -1,3 +1,21 @@ +/* + * wanix, a Unix-like operating system for WebAssembly applications. + * 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 _STRING_H #define _STRING_H @@ -7,7 +25,12 @@ 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); +int strcmp(const char *__a, const char *__b); +char *strdup(const char *__s); +int strncmp(const char *__a, const char *__b, size_t __n); +char *strndup(const char *__s, size_t __n); char *strerror(int errnum); size_t strlen(const char *__s); +char *strrchr(const char *__s, int __c); #endif diff --git a/src/libc/include/sys/stat.h b/src/libc/include/sys/stat.h new file mode 100644 index 0000000..0a5dd23 --- /dev/null +++ b/src/libc/include/sys/stat.h @@ -0,0 +1,40 @@ +/* + * wanix, a Unix-like operating system for WebAssembly applications. + * 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 _SYS_STAT_H +#define _SYS_STAT_H + +#include <sys/types.h> +#include <time.h> + +struct stat +{ + dev_t st_dev; + ino_t st_ino; + mode_t st_mode; + nlink_t st_nlink; + uid_t st_uid; + gid_t st_gid; + dev_t st_rdev; + off_t st_size; + struct timespec st_atim, st_mtim, st_ctim; + blksize_t st_blksize; + blkcnt_t st_blocks; +}; + +#endif diff --git a/src/libc/include/sys/types.h b/src/libc/include/sys/types.h new file mode 100644 index 0000000..3175740 --- /dev/null +++ b/src/libc/include/sys/types.h @@ -0,0 +1,25 @@ +/* + * wanix, a Unix-like operating system for WebAssembly applications. + * 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 _SYS_TYPES_H +#define _SYS_TYPES_H + +typedef unsigned dev_t, mode_t, nlink_t, uid_t, gid_t, blksize_t, blkcnt_t; +typedef unsigned long ino_t, off_t; + +#endif diff --git a/src/libc/include/time.h b/src/libc/include/time.h new file mode 100644 index 0000000..99a8e6c --- /dev/null +++ b/src/libc/include/time.h @@ -0,0 +1,39 @@ +/* + * wanix, a Unix-like operating system for WebAssembly applications. + * 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 _TIME_H +#define _TIME_H + +typedef enum +{ + CLOCK_REALTIME +} clockid_t; + +typedef long long time_t; + +struct timespec +{ + time_t tv_sec; + long tv_nsec; +}; + +int clock_getres(clockid_t __id, struct timespec *__ts); +int clock_gettime(clockid_t __id, struct timespec *__ts); +int clock_settime(clockid_t __id, const struct timespec *__ts); + +#endif diff --git a/src/libc/private_include/libc/file.h b/src/libc/private_include/libc/file.h index 3ba554c..2a55ced 100644 --- a/src/libc/private_include/libc/file.h +++ b/src/libc/private_include/libc/file.h @@ -1,6 +1,8 @@ #ifndef LIBC_FILE_H #define LIBC_FILE_H +#include <aio.h> + enum { STDIN_FILENO, @@ -10,6 +12,7 @@ enum struct __file { + struct aio *aio; }; #endif diff --git a/src/libc/private_include/libc/malloc.h b/src/libc/private_include/libc/malloc.h new file mode 100644 index 0000000..3662496 --- /dev/null +++ b/src/libc/private_include/libc/malloc.h @@ -0,0 +1,24 @@ +/* + * wanix, a Unix-like operating system for WebAssembly applications. + * 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 LIBC_MALLOC_H +#define LIBC_MALLOC_H + +extern int __malloc_ta_init; + +#endif diff --git a/src/libc/src/CMakeLists.txt b/src/libc/src/CMakeLists.txt new file mode 100644 index 0000000..8fa0428 --- /dev/null +++ b/src/libc/src/CMakeLists.txt @@ -0,0 +1,40 @@ +# wanix, a Unix-like operating system for WebAssembly applications. +# 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/>. + +target_sources(c PRIVATE + abort.c + calloc.c + clock_getres.c + clock_gettime.c + clock_settime.c + errno.c + free.c + malloc.c + memcmp.c + memcpy.c + memset.c + realloc.c + strchr.c + strcmp.c + strdup.c + streams.c + strerror.c + strlen.c + strncmp.c + strndup.c + strrchr.c + ta_init.c +) diff --git a/src/libc/src/abort.c b/src/libc/src/abort.c new file mode 100644 index 0000000..9dc31a8 --- /dev/null +++ b/src/libc/src/abort.c @@ -0,0 +1,28 @@ +/* + * wanix, a Unix-like operating system for WebAssembly applications. + * 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 <stdio.h> + +void __abort(const char *const filename, const int lineno) +{ + printf("%s:%d: aborted\n", filename, lineno); + + for (;;) + ; +} diff --git a/src/libc/src/calloc.c b/src/libc/src/calloc.c new file mode 100644 index 0000000..5d1c24f --- /dev/null +++ b/src/libc/src/calloc.c @@ -0,0 +1,32 @@ +/* + * wanix, a Unix-like operating system for WebAssembly applications. + * 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> +#include <string.h> + +void *calloc(const size_t nemb, const size_t size) +{ + const size_t n = nemb * size; + void *const ret = malloc(n); + + if (ret) + memset(ret, 0, n); + + return ret; +} diff --git a/src/libc/src/clock_getres.c b/src/libc/src/clock_getres.c new file mode 100644 index 0000000..bbd719e --- /dev/null +++ b/src/libc/src/clock_getres.c @@ -0,0 +1,25 @@ +/* + * wanix, a Unix-like operating system for WebAssembly applications. + * 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 <time.h> +#include <drv/time.h> + +int clock_getres(const clockid_t id, struct timespec *const ts) +{ + return drv_time_getres(id, ts); +} diff --git a/src/libc/src/clock_gettime.c b/src/libc/src/clock_gettime.c new file mode 100644 index 0000000..8e38f01 --- /dev/null +++ b/src/libc/src/clock_gettime.c @@ -0,0 +1,25 @@ +/* + * wanix, a Unix-like operating system for WebAssembly applications. + * 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 <time.h> +#include <drv/time.h> + +int clock_gettime(const clockid_t id, struct timespec *const ts) +{ + return drv_time_gettime(id, ts); +} diff --git a/src/libc/src/clock_settime.c b/src/libc/src/clock_settime.c new file mode 100644 index 0000000..17a64bc --- /dev/null +++ b/src/libc/src/clock_settime.c @@ -0,0 +1,25 @@ +/* + * wanix, a Unix-like operating system for WebAssembly applications. + * 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 <time.h> +#include <drv/time.h> + +int clock_settime(const clockid_t id, const struct timespec *const ts) +{ + return drv_time_settime(id, ts); +} diff --git a/src/libc/src/free.c b/src/libc/src/free.c index d89a80a..1aa9f4d 100644 --- a/src/libc/src/free.c +++ b/src/libc/src/free.c @@ -1,5 +1,5 @@ /* - * wanix, a Unix-like operating system for WebAssembly + * wanix, a Unix-like operating system for WebAssembly applications. * Copyright (C) 2025 Xavier Del Campo Romero * * This program is free software: you can redistribute it and/or modify @@ -16,8 +16,14 @@ * along with this program. If not, see <https://www.gnu.org/licenses/>. */ +#include <tinyalloc.h> +#include <libc/malloc.h> #include <stdlib.h> void free(void *const ptr) { + if (!ptr) + return; + else if (!__malloc_ta_init || !ta_free(ptr)) + abort(); } diff --git a/src/libc/src/malloc.c b/src/libc/src/malloc.c index aa5ff31..4955af5 100644 --- a/src/libc/src/malloc.c +++ b/src/libc/src/malloc.c @@ -1,5 +1,5 @@ /* - * wanix, a Unix-like operating system for WebAssembly + * wanix, a Unix-like operating system for WebAssembly applications. * Copyright (C) 2025 Xavier Del Campo Romero * * This program is free software: you can redistribute it and/or modify @@ -17,9 +17,32 @@ */ #include <stdlib.h> +#include <libc/malloc.h> +#include <errno.h> +#include <tinyalloc.h> +#include <stdint.h> #include <stddef.h> -void *malloc(const size_t __n) +void *malloc(const size_t n) { - return NULL; + extern char __bss_end[], __ram_end[]; + + if (!__malloc_ta_init) + { + enum {BLOCKSZ = 256}; + const size_t blocks = (__ram_end - __bss_end + 1) / BLOCKSZ; + + /* README.md states 16 is a "good default value". */ + if (!ta_init(__bss_end, __ram_end, blocks, 16, sizeof (intmax_t))) + abort(); + + __malloc_ta_init = 1; + } + + void *const ret = ta_alloc(n); + + if (!ret) + errno = ENOMEM; + + return ret; } diff --git a/src/libc/src/memset.c b/src/libc/src/memset.c index 90eb611..653eb76 100644 --- a/src/libc/src/memset.c +++ b/src/libc/src/memset.c @@ -1,3 +1,21 @@ +/* + * wanix, a Unix-like operating system for WebAssembly applications. + * 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 <string.h> #include <stddef.h> diff --git a/src/libc/src/mkdir.c b/src/libc/src/mkdir.c new file mode 100644 index 0000000..8e9209a --- /dev/null +++ b/src/libc/src/mkdir.c @@ -0,0 +1,37 @@ +/* + * wanix, a Unix-like operating system for WebAssembly applications. + * 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 <sys/stat.h> +#include <aio.h> + +int mkdir(const char *const path, const mode_t m) +{ + const struct aio_mkdir mk = + { + .path = path, + .mode = m + }; + + struct aio *const aio = aio_mkdir(&mk); + const struct aio_poll p = {.aio = aio}; + + if (!aio || aio_poll(&p, 1, -1) < 0) + return -1; + + return 0; +} diff --git a/src/libc/src/ps1/CMakeLists.txt b/src/libc/src/ps1/CMakeLists.txt new file mode 100644 index 0000000..1cf3d2d --- /dev/null +++ b/src/libc/src/ps1/CMakeLists.txt @@ -0,0 +1,17 @@ +# wanix, a Unix-like operating system for WebAssembly applications. +# 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/>. + +target_sources(c PRIVATE "putchar.c") diff --git a/src/libc/src/ps1/putchar.c b/src/libc/src/ps1/putchar.c new file mode 100644 index 0000000..085a37e --- /dev/null +++ b/src/libc/src/ps1/putchar.c @@ -0,0 +1,29 @@ +/* + * wanix, a Unix-like operating system for WebAssembly applications. + * 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 <stdio.h> + +int putchar(const int c) +{ + return EOF; +} + +void _putchar(const char c) +{ + putchar(c); +} diff --git a/src/libc/src/puts.c b/src/libc/src/puts.c new file mode 100644 index 0000000..0c22ed7 --- /dev/null +++ b/src/libc/src/puts.c @@ -0,0 +1,28 @@ +/* + * wanix, a Unix-like operating system for WebAssembly applications. + * 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 <stdio.h> + +int puts(const char *s) +{ + for (; *s; s++) + if (putchar(*s) < 0) + return -1; + + return putchar('\n'); +} diff --git a/src/libc/src/realloc.c b/src/libc/src/realloc.c new file mode 100644 index 0000000..2d830ab --- /dev/null +++ b/src/libc/src/realloc.c @@ -0,0 +1,25 @@ +/* + * wanix, a Unix-like operating system for WebAssembly applications. + * 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 <tinyalloc.h> +#include <stdlib.h> + +void *realloc(void *const ptr, const size_t size) +{ + return ta_realloc(ptr, size); +} diff --git a/src/libc/src/strcmp.c b/src/libc/src/strcmp.c new file mode 100644 index 0000000..dfd66b2 --- /dev/null +++ b/src/libc/src/strcmp.c @@ -0,0 +1,27 @@ +/* + * wanix, a Unix-like operating system for WebAssembly applications. + * 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 <string.h> + +int strcmp(const char *a, const char *b) +{ + while (*a && *b && *a++ == *b++) + ; + + return !!*a || !!*b; +} diff --git a/src/libc/src/strdup.c b/src/libc/src/strdup.c new file mode 100644 index 0000000..71993de --- /dev/null +++ b/src/libc/src/strdup.c @@ -0,0 +1,32 @@ +/* + * wanix, a Unix-like operating system for WebAssembly applications. + * 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 <string.h> +#include <stdlib.h> + +char *strdup(const char *const s) +{ + const size_t n = strlen(s) + 1; + char *const ret = malloc(n); + + if (!ret) + return NULL; + + memcpy(ret, s, n); + return ret; +} diff --git a/src/libc/src/strerror.c b/src/libc/src/strerror.c index 6444e98..4ccd924 100644 --- a/src/libc/src/strerror.c +++ b/src/libc/src/strerror.c @@ -5,6 +5,7 @@ char *strerror(const int errnum) { static const char *const s[] = { + [0] = "Success", [EPERM] = "Operation not permitted", [ENOENT] = "No such file or directory", [ESRCH] = "No such process", @@ -37,6 +38,105 @@ char *strerror(const int errnum) [EROFS] = "Read-only file system", [EMLINK] = "Too many links", [EPIPE] = "Broken pipe", + [EDOM] = "Numerical argument out of domain", + [ERANGE] = "Numerical result out of range", + [EDEADLK] = "Resource deadlock avoided", + [ENAMETOOLONG] = "File name too long", + [ENOLCK] = "No locks available", + [ENOSYS] = "Function not implemented", + [ENOTEMPTY] = "Directory not empty", + [ELOOP] = "Too many levels of symbolic links", + [ENOMSG] = "No message of desired type", + [EIDRM] = "Identifier removed", + [ECHRNG] = "Channel number out of range", + [EL2NSYNC] = "Level 2 not synchronized", + [EL3HLT] = "Level 3 halted", + [EL3RST] = "Level 3 reset", + [ELNRNG] = "Link number out of range", + [EUNATCH] = "Protocol driver not attached", + [ENOCSI] = "No CSI structure available", + [EL2HLT] = "Level 2 halted", + [EBADE] = "Invalid exchange", + [EBADR] = "Invalid request descriptor", + [EXFULL] = "Exchange full", + [ENOANO] = "No anode", + [EBADRQC] = "Invalid request code", + [EBADSLT] = "Invalid slot", + [EBFONT] = "Bad font file format", + [ENOSTR] = "Device not a stream", + [ENODATA] = "No data available", + [ETIME] = "Timer expired", + [ENOSR] = "Out of streams resources", + [ENONET] = "Machine is not on the network", + [ENOPKG] = "Package not installed", + [EREMOTE] = "Object is remote", + [ENOLINK] = "Link has been severed", + [EADV] = "Advertise error", + [ESRMNT] = "Srmount error", + [ECOMM] = "Communication error on send", + [EPROTO] = "Protocol error", + [EMULTIHOP] = "Multihop attempted", + [EDOTDOT] = "RFS specific error", + [EBADMSG] = "Bad message", + [EOVERFLOW] = "Value too large for defined data type", + [ENOTUNIQ] = "Name not unique on network", + [EBADFD] = "File descriptor in bad state", + [EREMCHG] = "Remote address changed", + [ELIBACC] = "Can not access a needed shared library", + [ELIBBAD] = "Accessing a corrupted shared library", + [ELIBSCN] = ".lib section in a.out corrupted", + [ELIBMAX] = "Attempting to link in too many shared libraries", + [ELIBEXEC] = "Cannot exec a shared library directly", + [EILSEQ] = "Invalid or incomplete multibyte or wide character", + [ERESTART] = "Interrupted system call should be restarted", + [ESTRPIPE] = "Streams pipe error", + [EUSERS] = "Too many users", + [ENOTSOCK] = "Socket operation on non-socket", + [EDESTADDRREQ] = "Destination address required", + [EMSGSIZE] = "Message too long", + [EPROTOTYPE] = "Protocol wrong type for socket", + [ENOPROTOOPT] = "Protocol not available", + [EPROTONOSUPPORT] = "Protocol not supported", + [ESOCKTNOSUPPORT] = "Socket type not supported", + [ENOTSUP] = "Operation not supported", + [EPFNOSUPPORT] = "Protocol family not supported", + [EAFNOSUPPORT] = "Address family not supported by protocol", + [EADDRINUSE] = "Address already in use", + [EADDRNOTAVAIL] = "Cannot assign requested address", + [ENETDOWN] = "Network is down", + [ENETUNREACH] = "Network is unreachable", + [ENETRESET] = "Network dropped connection on reset", + [ECONNABORTED] = "Software caused connection abort", + [ECONNRESET] = "Connection reset by peer", + [ENOBUFS] = "No buffer space available", + [EISCONN] = "Transport endpoint is already connected", + [ENOTCONN] = "Transport endpoint is not connected", + [ESHUTDOWN] = "Cannot send after transport endpoint shutdown", + [ETOOMANYREFS] = "Too many references: cannot splice", + [ETIMEDOUT] = "Connection timed out", + [ECONNREFUSED] = "Connection refused", + [EHOSTDOWN] = "Host is down", + [EHOSTUNREACH] = "No route to host", + [EALREADY] = "Operation already in progress", + [EINPROGRESS] = "Operation now in progress", + [ESTALE] = "Stale file handle", + [EUCLEAN] = "Structure needs cleaning", + [ENOTNAM] = "Not a XENIX named type file", + [ENAVAIL] = "No XENIX semaphores available", + [EISNAM] = "Is a named type file", + [EREMOTEIO] = "Remote I/O error", + [EDQUOT] = "Disk quota exceeded", + [ENOMEDIUM] = "No medium found", + [EMEDIUMTYPE] = "Wrong medium type", + [ECANCELED] = "Operation canceled", + [ENOKEY] = "Required key not available", + [EKEYEXPIRED] = "Key has expired", + [EKEYREVOKED] = "Key has been revoked", + [EKEYREJECTED] = "Key was rejected by service", + [EOWNERDEAD] = "Owner died", + [ENOTRECOVERABLE] = "State not recoverable", + [ERFKILL] = "Operation not possible due to RF-kill", + [EHWPOISON] = "Memory page has hardware error", }; if (errnum < 0 || errnum >= sizeof s / sizeof *s) diff --git a/src/libc/src/strncmp.c b/src/libc/src/strncmp.c new file mode 100644 index 0000000..c7ba771 --- /dev/null +++ b/src/libc/src/strncmp.c @@ -0,0 +1,29 @@ +/* + * wanix, a Unix-like operating system for WebAssembly applications. + * 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 <string.h> +#include <stddef.h> + +int strncmp(const char *a, const char *b, const size_t n) +{ + for (size_t i = 0; i < n; i++) + if (*a && *b && *a++ != *b++) + return -1; + + return 0; +} diff --git a/src/libc/src/strndup.c b/src/libc/src/strndup.c new file mode 100644 index 0000000..d35a42e --- /dev/null +++ b/src/libc/src/strndup.c @@ -0,0 +1,32 @@ +/* + * wanix, a Unix-like operating system for WebAssembly applications. + * 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 <string.h> +#include <stdlib.h> + +char *strndup(const char *const s, const size_t n) +{ + char *const ret = malloc(n + 1); + + if (!ret) + return NULL; + + memcpy(ret, s, n); + ret[n] = '\0'; + return ret; +} diff --git a/src/libc/src/strrchr.c b/src/libc/src/strrchr.c new file mode 100644 index 0000000..e18a4c0 --- /dev/null +++ b/src/libc/src/strrchr.c @@ -0,0 +1,31 @@ +/* + * wanix, a Unix-like operating system for WebAssembly applications. + * 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 <string.h> +#include <stddef.h> + +char *strrchr(const char *s, const int c) +{ + const size_t len = strlen(s); + + for (size_t i = len; i <= len; i--) + if (s[i] == c) + return (char *)&s[i]; + + return NULL; +} diff --git a/src/libc/src/ta_init.c b/src/libc/src/ta_init.c new file mode 100644 index 0000000..b38e45e --- /dev/null +++ b/src/libc/src/ta_init.c @@ -0,0 +1,21 @@ +/* + * wanix, a Unix-like operating system for WebAssembly applications. + * 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 <libc/malloc.h> + +int __malloc_ta_init; |
