// Raw.write: whole 32-bit cells between a file and a Bend array (see raw.bend). Runs on the event loop. static uint32_t* raw_write_cells(Env e, Term a, uint64_t need) { if (term_tag(a) != TAG_BUF) { fprintf(stderr, "Raw.write: not a 32-bit array\n"); exit(1); } if ((1ull << blk_cls(a)) < need) { fprintf(stderr, "Raw.write: array holds %llu cells, needs %llu\n", (unsigned long long)(1ull << blk_cls(a)), (unsigned long long)need); exit(1); } return (uint32_t*)(e.mem + blk_loc(e.mem, a)); } Term raw_write_run(Env e, Term* f, IoWork* w) { uint64_t len; char* path = io_cstr(e, f[0], &len); uint64_t cells = (uint32_t)f[2]; uint32_t* p = raw_write_cells(e, f[1], cells); FILE* fp = fopen(path, "wb"); if (fp == NULL) { fprintf(stderr, "Raw.write: cannot open %s\n", path); exit(1); } if (fwrite(p, 4, cells, fp) != cells) { fprintf(stderr, "Raw.write: short write of %s\n", path); exit(1); } fclose(fp); free(path); return f[1]; } static void __attribute__((constructor)) raw_write_use(void) { #ifdef CID(Raw.write) io_eff(CID(Raw.write), raw_write_run); #endif }