Path: blob/main/core/kernel/src/wasm/posix/other.zig
1068 views
pub fn keepalive() void {}1const stdio = @cImport(@cInclude("stdio.h"));2const std = @import("std");3const expect = std.testing.expect;45// "The interface __stack_chk_fail() shall abort the function that called it6// with a message that a stack overflow has been detected. The program that7// called the function shall then exit. The interface8// __stack_chk_fail() does not check for a stack overflow itself. It merely9// reports one when invoked."10export fn __stack_chk_fail() void {11const stderr = std.io.getStdErr().writer();12stderr.print("A stack overflow has been detected.\n", .{}) catch |e| {13std.debug.print("A stack overflow has been detected. - {}\n", .{e});14};15std.process.exit(1);16}1718// "The strunvis() function decodes the characters pointed to by src into the19// buffer pointed to by dst. The strunvis() function simply copies src to20// dst, decoding any escape sequences along the way, and returns the number21// of characters placed into dst, or -1 if an invalid escape sequence was22// detected. The size of dst should be equal to the size of src (that is,23// no expansion takes place during decoding).""24export fn strunvis(dst: [*:0]u8, src: [*:0]const u8) c_int {25var i: usize = 0;26while (src[i] != 0) : (i += 1) {27dst[i] = src[i];28}29dst[i] = 0; // null terminate string.30// std.debug.print("strunvis src={s}, dst={s}\n", .{ src, dst });31return @intCast(c_int, i);32}3334export fn strvis(dst: [*:0]u8, src: [*:0]const u8, flag: c_int) c_int {35// we ignore the flag which "alters visual representation".36_ = flag;37var i: usize = 0;38while (src[i] != 0) : (i += 1) {39dst[i] = src[i];40}41dst[i] = 0; // null terminate string.42// std.debug.print("strvis src={s}, dst={s}\n", .{ src, dst });43return @intCast(c_int, i);44}4546// int strnvis(char* dst, const char* src, size_t size, int flag);47// "The strnvis() function encodes characters from src up to the first NUL or the end of dst, as indicated by size. The strvisx() function encodes exactly len characters from src (this is useful for encoding a block of data that may contain NULs). All three forms NUL terminate dst, except for strnvis() when size is zero, in which case dst is not touched. strnvis() returns the length that dst would become if it were of unlimited size (similar to snprintf(3) or strlcpy(3)). This can be used to detect truncation but it also means that the return value of strnvis() must not be used without checking it against size."48export fn strnvis(dst: [*:0]u8, src: [*:0]const u8, size: usize, flag: c_int) c_int {49_ = flag;50var i: usize = 0;51while (src[i] != 0 and i < size - 1) : (i += 1) {52dst[i] = src[i];53}54if (i < size) {55dst[i] = 0;56}57while (src[i] != 0) : (i += 1) {}58return @intCast(c_int, i);59}6061//char * textdomain (const char * domainname);62//char * gettext (const char * msgid);63//char * dgettext (const char * domainname, const char * msgid);64//char * dcgettext (const char * domainname, const char * msgid, int category);65// "In the "C" locale, or if none of the used catalogs contain a translation for msgid, the gettext, dgettext and dcgettext functions return msgid."66export fn textdomain(domainname: [*:0]const u8) [*:0]const u8 {67return domainname;68}6970export fn gettext(msgid: [*:0]const u8) [*:0]const u8 {71return msgid;72}7374export fn dgettext(domainname: [*:0]const u8, msgid: [*:0]const u8) [*:0]const u8 {75_ = domainname;76return msgid;77}7879export fn dcgettext(domainname: [*:0]const u8, msgid: [*:0]const u8, category: c_int) [*:0]const u8 {80_ = domainname;81_ = category;82return msgid;83}8485// char * bindtextdomain (const char * domainname, const char * dirname);86export fn bindtextdomain(domainname: [*:0]const u8, dirname: [*:0]const u8) [*:0]const u8 {87_ = domainname;88// this is probably enough of a "stub" for now:89return dirname;90}9192// Weirdly on wasm @cimport from sys/statvfs.h yields an opaque type for statvfs, so I copy pasted.93const statvfs = @cImport(@cInclude("sys/statvfs.h"));94const fsblkcnt_t = statvfs.fsblkcnt_t;95const fsfilcnt_t = statvfs.fsfilcnt_t;9697const struct_statvfs = struct {98f_bsize: c_ulong,99f_frsize: c_ulong,100f_blocks: fsblkcnt_t,101f_bfree: fsblkcnt_t,102f_bavail: fsblkcnt_t,103f_files: fsfilcnt_t,104f_ffree: fsfilcnt_t,105f_favail: fsfilcnt_t,106f_fsid: c_ulong,107padding: u32, // 32 = 8*(2*sizeof(int)-sizeof(long)) in WASM; this is in the header file108f_flag: c_ulong,109f_namemax: c_ulong,110__reserved: [6]c_int,111};112113export fn set_statvfs(buf: *struct_statvfs, f_bsize: c_ulong, f_frsize: c_ulong, f_blocks: fsblkcnt_t, f_bfree: fsblkcnt_t, f_bavail: fsblkcnt_t, f_files: fsfilcnt_t, f_ffree: fsfilcnt_t, f_favail: fsfilcnt_t, f_fsid: c_ulong, f_flag: c_ulong, f_namemax: c_ulong) void {114buf.f_bsize = f_bsize;115buf.f_frsize = f_frsize;116buf.f_blocks = f_blocks;117buf.f_bfree = f_bfree;118buf.f_bavail = f_bavail;119buf.f_files = f_files;120buf.f_ffree = f_ffree;121buf.f_favail = f_favail;122buf.f_fsid = f_fsid;123buf.f_flag = f_flag;124buf.f_namemax = f_namemax;125}126127// This is easy, but missing from zig impl, but128// needed by some things, e.g., coreutils:tail:129// int getpagesize(void);130const limits = @cImport(@cInclude("limits.h"));131export fn getpagesize() c_int {132return limits.PAGE_SIZE;133}134135//136// Commented out since I realized that there's some emulation of this in wasi, which we will use for now.137//138// const errno = @import("errno.zig");139// // #include <sys/mman.h>140// const mman = @cImport(@cInclude("sys/mman.h"));141// These mmap functions are not implemented at all yet. However, at least we can make them clearly fail.142// void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset);143// "On success, mmap() returns a pointer to the mapped area. On error, the value MAP_FAILED144// (that is, (void *) -1) is returned, and errno is set to indicate the cause of the error."145// export fn mmap(addr: *anyopaque, length: c_long, prot: c_int, flags: c_int, fd: c_int, offset: mman.off_t) ?*anyopaque {146// _ = addr;147// _ = length;148// _ = prot;149// _ = flags;150// _ = fd;151// _ = offset;152// errno.setErrno(errno.errno.ENODEV);153// return mman.MAP_FAILED;154// }155156// // int munmap(void *addr, size_t length);157// // "On success, munmap() returns 0. On failure, it returns -1, and errno is set to indicate158// // the cause of the error (probably to EINVAL)."159// export fn munmap(addr: *anyopaque, length: c_long) c_int {160// _ = addr;161// _ = length;162// errno.setErrno(errno.errno.EINVAL);163// return -1;164// }165166167