diff options
| author | user <user@clank> | 2026-09-22 12:57:26 +0200 |
|---|---|---|
| committer | user <user@clank> | 2026-09-22 12:57:26 +0200 |
| commit | 68ff62e9492accd4c1c9a83edbcbd225afb1a11a (patch) | |
| tree | b744ad33cdbaa25fca333f28f13b5e7df28838dd /src/path.c | |
| parent | themes: a picker that wears them as you browse (C-a t) (diff) | |
| download | slosh-68ff62e9492accd4c1c9a83edbcbd225afb1a11a.tar.gz slosh-68ff62e9492accd4c1c9a83edbcbd225afb1a11a.tar.xz slosh-68ff62e9492accd4c1c9a83edbcbd225afb1a11a.zip | |
themes: ask the binary where it is, not the prefix it was built for
`theme_name "sl0p"` with all seven themes sitting in /usr/share/slosh/themes:
"no theme named sl0p". The lookup knew two places -- your own themes directory
and the PREFIX this build was compiled with -- and on that machine the first
did not exist and the second was /usr/local, because that is the Makefile's
default and the package was built with PREFIX=/usr. Neither was where the
themes actually were, and nothing in the search could ever have found them.
A compiled-in prefix is a guess about where the binary will end up, and the
binary is the one thing that knows where it really is. So the search now asks
it: path_self() (/proc/self/exe, _NSGetExecutablePath, GetModuleFileName --
lifted out of server.c, which was already doing this for SLOSH_BIN) gives
<bin>/../share/slosh/themes for an installed slosh wherever it was installed
to, and <repo>/contrib/themes for one being run out of a build tree, which
makes a checkout able to name a theme without installing anything first. The
compiled-in prefix stays as the last candidate, for a binary sitting somewhere
that says nothing about its data, and duplicates collapse so the usual install
does not report the same directory twice.
Your own directory is still first and still shadows everything, which is the
property that matters; the rest is about the shipped set being findable at all.
The tests that assumed a closed world of two temp themes now narrow to their
own names by typing, since the shipped ones are (correctly) always in the list
-- and the "no themes anywhere" toast lost its test, because with the shipped
set reachable that state takes a binary with nothing beside it. The case that
started this is a test of its own.
Diffstat (limited to 'src/path.c')
| -rw-r--r-- | src/path.c | 40 |
1 files changed, 40 insertions, 0 deletions
@@ -6,6 +6,7 @@ * session happened to be in. Silently \u2014 which is the worst way for a path to * be wrong, because the pane still starts and still works. */ +#define _GNU_SOURCE #include "slosh.h" #include <sys/stat.h> @@ -14,6 +15,14 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <unistd.h> + +#ifdef __APPLE__ +#include <mach-o/dyld.h> /* Darwin has no /proc/self/exe */ +#endif +#ifdef _WIN32 +#include "compat_win.h" +#endif /* Whether a path names a place outright, rather than one to be read against * somewhere else. @@ -149,6 +158,37 @@ const char *path_relative(const char *path, const char *base) { return *rest ? rest : "."; } +/* This binary, as a path: what the program can find *relative to itself*. + * + * Read from the kernel rather than from argv[0], which is whatever the caller + * felt like passing. It is how the shipped themes are found without being told + * where they are: a build compiles in a PREFIX, but the binary that ends up + * running may have been installed under a different one, packaged by somebody + * else, or not installed at all -- and `<the binary>/../share/slosh` is true in + * every one of those cases while a compiled-in guess is true in one. + */ +bool path_self(char *out, size_t cap) { + if (!out || cap < 2) return false; +#ifdef _WIN32 + return sl_self_exe(out, cap); +#elif defined(__APPLE__) + char raw[1024]; + uint32_t sz = sizeof raw; + /* What Darwin hands back can still hold symlinks and `..`; realpath + * finishes the job, and falls back to the raw answer rather than to + * nothing. */ + if (_NSGetExecutablePath(raw, &sz) != 0) return false; + char resolved[1024]; + snprintf(out, cap, "%s", realpath(raw, resolved) ? resolved : raw); + return true; +#else + ssize_t n = readlink("/proc/self/exe", out, cap - 1); + if (n <= 0) return false; + out[n] = 0; + return true; +#endif +} + /* Create a directory and everything above it. `mkdir` of one level is enough * on a machine where ~/.config already exists, which is most of them and not * all of them -- a fresh container has neither, and "could not write your |
