Path: blob/main/core/kernel/src/wasm/posix/stdio.ts
1070 views
export default function stdio(context) {1const { fs, send } = context;2return {3/* char *tmpnam(char *s);45Linux manpage is funny and clear:67The tmpnam() function returns a pointer to a string that is a valid filename,8and such that a file with this name did not exist at some point in time, so that9naive programmers may think it a suitable name for a temporary file. If the10argument s is NULL, this name is generated in an internal static buffer and may11be overwritten by the next call to tmpnam(). If s is not NULL, the name is copied to12the character array (of length at least L_tmpnam) pointed to by s and the value s13is returned in case of success.14*/1516tmpnam(sPtr: number): number {17let s = "/tmp/tmpnam_";18for (let i = 0; i < 1000; i++) {19let name = s;20// very naive, but WASM is a single user VM so a lot of security issues disappear21for (let j = 0; j < 6; j++) {22name += String.fromCharCode(65 + Math.floor(26 * Math.random()));23}24if (!fs.existsSync(name)) {25if (sPtr) {26send.string(name, { ptr: sPtr, len: 20 });27return sPtr;28} else {29if (!context.state.tmpnam_buf) {30context.state.tmpnam_buf = send.malloc(20);31}32send.string(name, { ptr: context.state.tmpnam_buf, len: 20 });33return context.state.tmpnam_buf;34}35}36}37return 0; // error38},3940/*41Stubs for popen and pclose that throw an error. I think these would be kind of impossible42to do in WASM (without multiple threads... hence sync'd filesystem) because they are43nonblocking...?4445FILE* popen(const char* command, const char* type);46int pclose(FILE* stream);47*/48popen(_commandPtr: number, _typePtr: number): number {49// returning 0 means it couldn't do it.50return 0;51},5253pclose(_streamPtr: number): number {54return -1;55},56};57}585960