/* nearest -- the walk-up dispatcher behind every name in the home scope's bin folder: the home folder is the root scope and the closest scope wins, node's own resolution rule applied to commands. Static, built with the anvil; the binary `nearest` beside this source is pinned like `launcher`. Invoked as ~/bin/ (a symlink to this binary), it resolves the way node resolves a bare import: from the working directory upward, the first /bin/ that is not this dispatcher wins and is exec'd with argv[0] set to its resolved path. When no scope above the working directory provides the name, $HOME/bin/.owners (lines "\t") names the root-scope target. Nothing else: no PATH edits, no env, no config. A shell that resolved a name elsewhere before a provisioning needs `hash -r` once. build: x86_64-linux-musl-gcc -static -Os -o nearest nearest.c */ #define _GNU_SOURCE #include #include #include #include #include #include #include #include #ifndef PATH_MAX #define PATH_MAX 4096 #endif #define EXIT_NOT_FOUND 127 /* the shell's own "command not found" status */ #define OWNERS_FILE_MAX (PATH_MAX * 16) /* .owners is a short registry; 64 KiB is generous */ static void fail(const char *message, const char *path) { if (path) fprintf(stderr, "nearest: %s: %s: %s\n", message, path, strerror(errno)); else fprintf(stderr, "nearest: %s\n", message); exit(126); } static char *join3(const char *left, const char *middle, const char *right) { size_t length = strlen(left) + 1 + strlen(middle) + 1 + strlen(right) + 1; char *result = malloc(length); if (!result) fail("out of memory", NULL); snprintf(result, length, "%s/%s/%s", left, middle, right); return result; } static char *directory_name(const char *path) { char *result = strdup(path); char *slash; if (!result) fail("out of memory", NULL); slash = strrchr(result, '/'); if (!slash) strcpy(result, "."); else if (slash == result) result[1] = '\0'; else *slash = '\0'; return result; } static const char *base_name(const char *path) { const char *slash = strrchr(path, '/'); return slash ? slash + 1 : path; } static int executable_file(const char *path) { struct stat value; return stat(path, &value) == 0 && S_ISREG(value.st_mode) && access(path, X_OK) == 0; } /* exec the candidate if it is an executable that is not this dispatcher; returns only on "no". */ static void try_exec(const char *candidate, const char *self, char **argv) { char real[PATH_MAX + 1]; if (!executable_file(candidate)) return; if (!realpath(candidate, real)) return; if (!strcmp(real, self)) return; argv[0] = real; execv(real, argv); fail("could not execute", real); } static char *read_owners(const char *path) { int descriptor = open(path, O_RDONLY); char *result; ssize_t count; if (descriptor < 0) return NULL; result = malloc(OWNERS_FILE_MAX); if (!result) fail("out of memory", NULL); count = read(descriptor, result, OWNERS_FILE_MAX - 1); close(descriptor); if (count < 0) { free(result); return NULL; } result[count] = '\0'; return result; } /* the registry line "" -> malloc'd target, or NULL */ static char *owners_target(const char *owners, const char *name) { size_t name_length = strlen(name); const char *cursor = owners; while (*cursor) { const char *end = strchr(cursor, '\n'); size_t length = end ? (size_t)(end - cursor) : strlen(cursor); if (length > name_length && !memcmp(cursor, name, name_length) && (cursor[name_length] == '\t' || cursor[name_length] == ' ')) { const char *value = cursor + name_length; size_t value_length; while (*value == '\t' || *value == ' ') value++; value_length = length - (size_t)(value - cursor); while (value_length && (value[value_length - 1] == '\r' || value[value_length - 1] == ' ')) value_length--; if (value_length) return strndup(value, value_length); } if (!end) break; cursor = end + 1; } return NULL; } int main(int argc, char **argv) { char self[PATH_MAX + 1]; char cwd[PATH_MAX + 1]; ssize_t self_length = readlink("/proc/self/exe", self, PATH_MAX); const char *name = argc ? base_name(argv[0]) : ""; const char *home = getenv("HOME"); char *dir; if (self_length < 0 || self_length > PATH_MAX) fail("cannot read /proc/self/exe", NULL); self[self_length] = '\0'; if (!*name || !strcmp(name, base_name(self))) fail("invoke through a ~/bin/ symlink, not directly", NULL); if (!getcwd(cwd, sizeof cwd)) fail("cannot read the working directory", NULL); /* closest wins: /bin/, /bin/, ... up to /bin/ */ dir = strdup(cwd); if (!dir) fail("out of memory", NULL); for (;;) { char *candidate = join3(dir, "bin", name); try_exec(candidate, self, argv); free(candidate); if (!strcmp(dir, "/")) break; char *parent = directory_name(dir); free(dir); dir = parent; } free(dir); /* root scope: $HOME/bin/.owners names the target relative to $HOME/bin */ if (home && *home) { char *owners_path = join3(home, "bin", ".owners"); char *owners = read_owners(owners_path); char *target = owners ? owners_target(owners, name) : NULL; if (target) { char *candidate = target[0] == '/' ? target : join3(home, "bin", target); try_exec(candidate, self, argv); fprintf(stderr, "nearest: %s: registered in %s but %s is not executable\n", name, owners_path, candidate); exit(EXIT_NOT_FOUND); } } fprintf(stderr, "nearest: %s: not provisioned in any scope above %s (looked for /bin/%s, then %s/bin/.owners)\n", name, cwd, name, home && *home ? home : "$HOME"); return EXIT_NOT_FOUND; }