+_relpath() {
+ local base="$(readlink -f "$1")"
+ local dest="$(readlink -f "$2")"
+ local up
+
+ [ -d "$base" ] || base="${base%/*}"
+ [ -d "$dest" ] || dest="${dest%/*}"
+
+ while true; do
+ case "$base"
+ in "$dest"/*)
+ echo "$up/${base#$dest/}"
+ break
+ ;;
+ *)
+ dest="${dest%/*}"
+ up="${up:+$up/}.."
+ ;;
+ esac
+ done
+}
+
+_wrapper() {
+ cat <<-EOT | ${CC:-gcc} -x c -o "$1" -
+ #include <unistd.h>
+ #include <stdio.h>
+
+ int main(int argc, char **argv) {
+ const char *self = argv[0];
+ const char *target = argv[1];
+
+ if (argc < 3) {
+ fprintf(stderr, "Usage: %s executable arg0 [args...]\n", self);
+ return 1;
+ }
+
+ return execv(target, argv + 2);
+ }
+ EOT
+
+ [ -x "$1" ] || {
+ echo "compiling wrapper failed" >&2
+ exit 5
+ }
+}
+