/* Small pieces of the MoonBit host that need C. */ #include #include #include #include #include #include "moonbit.h" /* A line on stderr (the compositor's log). */ void wh_eprint(moonbit_bytes_t s) { fwrite(s, 1, (size_t)Moonbit_array_length(s), stderr); fputc('\n', stderr); fflush(stderr); } /* No zombies from launched apps. */ void wh_ignore_sigchld(void) { signal(SIGCHLD, SIG_IGN); } void wh_exit(int32_t code) { fflush(stdout); fflush(stderr); exit(code); } /* Append `data` to the file at `path` (created if missing): 0, or -1 on error. */ int32_t wh_append_file(moonbit_bytes_t path, moonbit_bytes_t data) { char p[4096]; size_t n = (size_t)Moonbit_array_length(path); if (n >= sizeof p) return -1; memcpy(p, path, n); p[n] = 0; FILE *f = fopen(p, "ab"); if (!f) return -1; size_t len = (size_t)Moonbit_array_length(data); int ok = fwrite(data, 1, len, f) == len; return fclose(f) == 0 && ok ? 0 : -1; }