pub const FILE_RUNTIME: &str = "/* ========== Files ========== */\n\n/* The files a program reads, writes and mounts, as the native file system resolves them: a path names a file under\n the working directory, its leading `/`s and then its leading `../`s dropped; a write replaces the file whole,\n through a sibling of the same name with the extension `tmp`, after creating its directories; a read takes the\n whole file as Text. A failure is reported as the interpreter words it, \"Read error: \" or \"Write error: \" and then\n the file system\'s own account of the fault. */\n\n#include <errno.h>\n#include <sys/stat.h>\n\n__attribute__((unused)) static char *logos_file_path(const char *path) {\n while (*path == \'/\') path++;\n while (strncmp(path, \"../\", 3) == 0) path += 3;\n size_t n = strlen(path);\n char *out = (char *)lc_gc_alloc(n + 3);\n memcpy(out, \"./\", 2);\n memcpy(out + 2, path, n + 1);\n return out;\n}\n\n/* The account of an operating-system fault the native file system gives: its kind, then the error\'s own text. */\n__attribute__((unused, noreturn)) static void logos_file_fail(const char *verb, int code) {\n const char *kind = code == ENOENT ? \"Not found\"\n : (code == EACCES || code == EPERM) ? \"Permission denied\"\n : code == EEXIST ? \"Already exists\"\n : \"IO error\";\n logos_fatalf(\"%s error: %s: %s (os error %d)\", verb, kind, strerror(code), code);\n}\n\n__attribute__((unused)) static int logos_utf8_valid(const unsigned char *s, size_t n) {\n size_t i = 0;\n while (i < n) {\n unsigned char c = s[i];\n size_t len;\n uint32_t cp;\n if (c < 0x80) { i++; continue; }\n else if ((c & 0xE0) == 0xC0) { len = 2; cp = c & 0x1F; }\n else if ((c & 0xF0) == 0xE0) { len = 3; cp = c & 0x0F; }\n else if ((c & 0xF8) == 0xF0) { len = 4; cp = c & 0x07; }\n else return 0;\n if (i + len > n) return 0;\n for (size_t k = 1; k < len; k++) {\n if ((s[i + k] & 0xC0) != 0x80) return 0;\n cp = (cp << 6) | (s[i + k] & 0x3F);\n }\n if ((len == 2 && cp < 0x80) || (len == 3 && cp < 0x800) || (len == 4 && (cp < 0x10000 || cp > 0x10FFFF))\n || (cp >= 0xD800 && cp <= 0xDFFF)) return 0;\n i += len;\n }\n return 1;\n}\n\n/* The whole file at `path` as Text, or NULL with `errno` saying why (EILSEQ: not UTF-8). */\n__attribute__((unused)) static char *logos_file_slurp(const char *path) {\n char *full = logos_file_path(path);\n FILE *f = fopen(full, \"rb\");\n lc_gc_free(full);\n if (!f) return NULL;\n size_t cap = 4096, len = 0;\n char *buf = (char *)lc_gc_alloc(cap);\n size_t got;\n while ((got = fread(buf + len, 1, cap - len - 1, f)) > 0) {\n len += got;\n if (cap - len - 1 == 0) { cap *= 2; buf = (char *)lc_gc_realloc(buf, cap); }\n }\n int failed = ferror(f);\n int saved = errno;\n fclose(f);\n if (failed) { lc_gc_free(buf); errno = saved; return NULL; }\n buf[len] = \'\\0\';\n if (!logos_utf8_valid((const unsigned char *)buf, len)) { lc_gc_free(buf); errno = EILSEQ; return NULL; }\n return buf;\n}\n\n__attribute__((unused)) static char *logos_file_read(const char *path) {\n char *text = logos_file_slurp(path);\n if (text) return text;\n if (errno == EILSEQ) logos_fatal(\"Read error: IO error: stream did not contain valid UTF-8\");\n logos_file_fail(\"Read\", errno);\n}\n\n/* `Mount v at \"path\"`: the journal\'s text, or empty Text when there is none yet. */\n__attribute__((unused)) static char *logos_file_mount(const char *path) {\n char *text = logos_file_slurp(path);\n return text ? text : lc_gc_strdup(\"\");\n}\n\n__attribute__((unused)) static void logos_file_write(const char *path, const char *content) {\n char *full = logos_file_path(path);\n for (char *p = full + 2; *p; p++) {\n if (*p != \'/\') continue;\n *p = \'\\0\';\n if (mkdir(full, 0777) != 0 && errno != EEXIST) { int code = errno; *p = \'/\'; logos_file_fail(\"Write\", code); }\n *p = \'/\';\n }\n size_t n = strlen(full);\n const char *name = strrchr(full, \'/\') + 1;\n const char *dot = strrchr(name, \'.\');\n size_t stem = (dot && dot != name) ? (size_t)(dot - full) : n;\n char *temp = (char *)lc_gc_alloc(stem + 5);\n memcpy(temp, full, stem);\n memcpy(temp + stem, \".tmp\", 5);\n FILE *f = fopen(temp, \"wb\");\n if (!f) logos_file_fail(\"Write\", errno);\n size_t len = strlen(content);\n if (fwrite(content, 1, len, f) != len) { int code = errno; fclose(f); logos_file_fail(\"Write\", code); }\n if (fclose(f) != 0) logos_file_fail(\"Write\", errno);\n if (rename(temp, full) != 0) logos_file_fail(\"Write\", errno);\n lc_gc_free(temp);\n lc_gc_free(full);\n}\n";Expand description
The files a program reads, writes and mounts (Read … from file, Write … to file, Mount), resolved under
the working directory as the native file system resolves them. Emitted only for a program that uses one.