Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/wapython
Path: blob/main/core/kernel/src/wasm/posix/other.zig
1068 views
1
pub fn keepalive() void {}
2
const stdio = @cImport(@cInclude("stdio.h"));
3
const std = @import("std");
4
const expect = std.testing.expect;
5
6
// "The interface __stack_chk_fail() shall abort the function that called it
7
// with a message that a stack overflow has been detected. The program that
8
// called the function shall then exit. The interface
9
// __stack_chk_fail() does not check for a stack overflow itself. It merely
10
// reports one when invoked."
11
export fn __stack_chk_fail() void {
12
const stderr = std.io.getStdErr().writer();
13
stderr.print("A stack overflow has been detected.\n", .{}) catch |e| {
14
std.debug.print("A stack overflow has been detected. - {}\n", .{e});
15
};
16
std.process.exit(1);
17
}
18
19
// "The strunvis() function decodes the characters pointed to by src into the
20
// buffer pointed to by dst. The strunvis() function simply copies src to
21
// dst, decoding any escape sequences along the way, and returns the number
22
// of characters placed into dst, or -1 if an invalid escape sequence was
23
// detected. The size of dst should be equal to the size of src (that is,
24
// no expansion takes place during decoding).""
25
export fn strunvis(dst: [*:0]u8, src: [*:0]const u8) c_int {
26
var i: usize = 0;
27
while (src[i] != 0) : (i += 1) {
28
dst[i] = src[i];
29
}
30
dst[i] = 0; // null terminate string.
31
// std.debug.print("strunvis src={s}, dst={s}\n", .{ src, dst });
32
return @intCast(c_int, i);
33
}
34
35
export fn strvis(dst: [*:0]u8, src: [*:0]const u8, flag: c_int) c_int {
36
// we ignore the flag which "alters visual representation".
37
_ = flag;
38
var i: usize = 0;
39
while (src[i] != 0) : (i += 1) {
40
dst[i] = src[i];
41
}
42
dst[i] = 0; // null terminate string.
43
// std.debug.print("strvis src={s}, dst={s}\n", .{ src, dst });
44
return @intCast(c_int, i);
45
}
46
47
// int strnvis(char* dst, const char* src, size_t size, int flag);
48
// "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."
49
export fn strnvis(dst: [*:0]u8, src: [*:0]const u8, size: usize, flag: c_int) c_int {
50
_ = flag;
51
var i: usize = 0;
52
while (src[i] != 0 and i < size - 1) : (i += 1) {
53
dst[i] = src[i];
54
}
55
if (i < size) {
56
dst[i] = 0;
57
}
58
while (src[i] != 0) : (i += 1) {}
59
return @intCast(c_int, i);
60
}
61
62
//char * textdomain (const char * domainname);
63
//char * gettext (const char * msgid);
64
//char * dgettext (const char * domainname, const char * msgid);
65
//char * dcgettext (const char * domainname, const char * msgid, int category);
66
// "In the "C" locale, or if none of the used catalogs contain a translation for msgid, the gettext, dgettext and dcgettext functions return msgid."
67
export fn textdomain(domainname: [*:0]const u8) [*:0]const u8 {
68
return domainname;
69
}
70
71
export fn gettext(msgid: [*:0]const u8) [*:0]const u8 {
72
return msgid;
73
}
74
75
export fn dgettext(domainname: [*:0]const u8, msgid: [*:0]const u8) [*:0]const u8 {
76
_ = domainname;
77
return msgid;
78
}
79
80
export fn dcgettext(domainname: [*:0]const u8, msgid: [*:0]const u8, category: c_int) [*:0]const u8 {
81
_ = domainname;
82
_ = category;
83
return msgid;
84
}
85
86
// char * bindtextdomain (const char * domainname, const char * dirname);
87
export fn bindtextdomain(domainname: [*:0]const u8, dirname: [*:0]const u8) [*:0]const u8 {
88
_ = domainname;
89
// this is probably enough of a "stub" for now:
90
return dirname;
91
}
92
93
// Weirdly on wasm @cimport from sys/statvfs.h yields an opaque type for statvfs, so I copy pasted.
94
const statvfs = @cImport(@cInclude("sys/statvfs.h"));
95
const fsblkcnt_t = statvfs.fsblkcnt_t;
96
const fsfilcnt_t = statvfs.fsfilcnt_t;
97
98
const struct_statvfs = struct {
99
f_bsize: c_ulong,
100
f_frsize: c_ulong,
101
f_blocks: fsblkcnt_t,
102
f_bfree: fsblkcnt_t,
103
f_bavail: fsblkcnt_t,
104
f_files: fsfilcnt_t,
105
f_ffree: fsfilcnt_t,
106
f_favail: fsfilcnt_t,
107
f_fsid: c_ulong,
108
padding: u32, // 32 = 8*(2*sizeof(int)-sizeof(long)) in WASM; this is in the header file
109
f_flag: c_ulong,
110
f_namemax: c_ulong,
111
__reserved: [6]c_int,
112
};
113
114
export 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 {
115
buf.f_bsize = f_bsize;
116
buf.f_frsize = f_frsize;
117
buf.f_blocks = f_blocks;
118
buf.f_bfree = f_bfree;
119
buf.f_bavail = f_bavail;
120
buf.f_files = f_files;
121
buf.f_ffree = f_ffree;
122
buf.f_favail = f_favail;
123
buf.f_fsid = f_fsid;
124
buf.f_flag = f_flag;
125
buf.f_namemax = f_namemax;
126
}
127
128
// This is easy, but missing from zig impl, but
129
// needed by some things, e.g., coreutils:tail:
130
// int getpagesize(void);
131
const limits = @cImport(@cInclude("limits.h"));
132
export fn getpagesize() c_int {
133
return limits.PAGE_SIZE;
134
}
135
136
//
137
// Commented out since I realized that there's some emulation of this in wasi, which we will use for now.
138
//
139
// const errno = @import("errno.zig");
140
// // #include <sys/mman.h>
141
// const mman = @cImport(@cInclude("sys/mman.h"));
142
// These mmap functions are not implemented at all yet. However, at least we can make them clearly fail.
143
// void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset);
144
// "On success, mmap() returns a pointer to the mapped area. On error, the value MAP_FAILED
145
// (that is, (void *) -1) is returned, and errno is set to indicate the cause of the error."
146
// export fn mmap(addr: *anyopaque, length: c_long, prot: c_int, flags: c_int, fd: c_int, offset: mman.off_t) ?*anyopaque {
147
// _ = addr;
148
// _ = length;
149
// _ = prot;
150
// _ = flags;
151
// _ = fd;
152
// _ = offset;
153
// errno.setErrno(errno.errno.ENODEV);
154
// return mman.MAP_FAILED;
155
// }
156
157
// // int munmap(void *addr, size_t length);
158
// // "On success, munmap() returns 0. On failure, it returns -1, and errno is set to indicate
159
// // the cause of the error (probably to EINVAL)."
160
// export fn munmap(addr: *anyopaque, length: c_long) c_int {
161
// _ = addr;
162
// _ = length;
163
// errno.setErrno(errno.errno.EINVAL);
164
// return -1;
165
// }
166
167