// Raw.read_at: whole 32-bit cells from a file offset into a Bend array at a cell offset (see raw.bend). Term raw_read_at_run(Env e, Term* f, IoWork* w) { uint64_t len; char* path = io_cstr(e, f[0], &len); uint64_t off = ((uint64_t)(uint32_t)f[1] << 32) | (uint32_t)f[2]; uint64_t dst = (uint32_t)f[4], cells = (uint32_t)f[5]; Term a = f[3]; if (term_tag(a) != TAG_BUF) { fprintf(stderr, "Raw.read_at: not a 32-bit array\n"); exit(1); } if ((1ull << blk_cls(a)) < dst + cells) { fprintf(stderr, "Raw.read_at: array holds %llu cells, needs %llu\n", (unsigned long long)(1ull << blk_cls(a)), (unsigned long long)(dst + cells)); exit(1); } uint32_t* p = (uint32_t*)(e.mem + blk_loc(e.mem, a)) + dst; FILE* fp = fopen(path, "rb"); if (fp == NULL) { fprintf(stderr, "Raw.read_at: cannot open %s\n", path); exit(1); } if (fseeko(fp, (off_t)off, SEEK_SET) != 0 || fread(p, 4, cells, fp) != cells) { fprintf(stderr, "Raw.read_at: short read of %s at %llu\n", path, (unsigned long long)off); exit(1); } fclose(fp); free(path); return f[3]; } static void __attribute__((constructor)) raw_read_at_use(void) { #ifdef CID(Raw.read_at) io_eff(CID(Raw.read_at), raw_read_at_run); #endif }