Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/wapython
Path: blob/main/core/kernel/src/wasm/posix/stdio.ts
1070 views
1
export default function stdio(context) {
2
const { fs, send } = context;
3
return {
4
/* char *tmpnam(char *s);
5
6
Linux manpage is funny and clear:
7
8
The tmpnam() function returns a pointer to a string that is a valid filename,
9
and such that a file with this name did not exist at some point in time, so that
10
naive programmers may think it a suitable name for a temporary file. If the
11
argument s is NULL, this name is generated in an internal static buffer and may
12
be overwritten by the next call to tmpnam(). If s is not NULL, the name is copied to
13
the character array (of length at least L_tmpnam) pointed to by s and the value s
14
is returned in case of success.
15
*/
16
17
tmpnam(sPtr: number): number {
18
let s = "/tmp/tmpnam_";
19
for (let i = 0; i < 1000; i++) {
20
let name = s;
21
// very naive, but WASM is a single user VM so a lot of security issues disappear
22
for (let j = 0; j < 6; j++) {
23
name += String.fromCharCode(65 + Math.floor(26 * Math.random()));
24
}
25
if (!fs.existsSync(name)) {
26
if (sPtr) {
27
send.string(name, { ptr: sPtr, len: 20 });
28
return sPtr;
29
} else {
30
if (!context.state.tmpnam_buf) {
31
context.state.tmpnam_buf = send.malloc(20);
32
}
33
send.string(name, { ptr: context.state.tmpnam_buf, len: 20 });
34
return context.state.tmpnam_buf;
35
}
36
}
37
}
38
return 0; // error
39
},
40
41
/*
42
Stubs for popen and pclose that throw an error. I think these would be kind of impossible
43
to do in WASM (without multiple threads... hence sync'd filesystem) because they are
44
nonblocking...?
45
46
FILE* popen(const char* command, const char* type);
47
int pclose(FILE* stream);
48
*/
49
popen(_commandPtr: number, _typePtr: number): number {
50
// returning 0 means it couldn't do it.
51
return 0;
52
},
53
54
pclose(_streamPtr: number): number {
55
return -1;
56
},
57
};
58
}
59
60