about summary refs log tree commit diff
path: root/pkgs/development/skaware-packages/execline/execlineb-wrapper.c
diff options
context:
space:
mode:
Diffstat (limited to 'pkgs/development/skaware-packages/execline/execlineb-wrapper.c')
-rw-r--r--pkgs/development/skaware-packages/execline/execlineb-wrapper.c51
1 files changed, 51 insertions, 0 deletions
diff --git a/pkgs/development/skaware-packages/execline/execlineb-wrapper.c b/pkgs/development/skaware-packages/execline/execlineb-wrapper.c
new file mode 100644
index 0000000000000..c8e91813b7744
--- /dev/null
+++ b/pkgs/development/skaware-packages/execline/execlineb-wrapper.c
@@ -0,0 +1,51 @@
+/*
+ * A wrapper around execlineb, which provides all execline
+ * tools on execlineb’s PATH.
+ * It is implemented as a C program, because on non-Linux,
+ * nested shebang lines are not supported.
+ */
+
+#include <stdlib.h>
+#include <string.h>
+
+#include <skalibs/stralloc.h>
+#include <skalibs/djbunix.h>
+#include <skalibs/strerr2.h>
+#include <skalibs/env.h>
+#include <skalibs/exec.h>
+
+#define dienomem() strerr_diefu1sys(111, "stralloc_catb")
+
+// macros from outside
+/* const char* EXECLINEB_PATH; */
+/* const char* EXECLINE_BIN_PATH; */
+
+int main(int argc, char const* argv[], char const *const *envp)
+{
+  PROG = "execlineb-wrapper";
+
+  char const* path = getenv("PATH");
+  stralloc path_modif = STRALLOC_ZERO;
+
+  // modify PATH if unset or EXECLINEB_BIN_PATH is not yet there
+  if ( !path || ! strstr(path, EXECLINE_BIN_PATH())) {
+    // prepend our execline path
+    if ( ! stralloc_cats(&path_modif, "PATH=")
+         || ! stralloc_cats(&path_modif, EXECLINE_BIN_PATH()) ) dienomem();
+    // old path was not empty
+    if ( path && path[0] ) {
+      if ( ! stralloc_catb(&path_modif, ":", 1)
+           || ! stralloc_cats(&path_modif, path) ) dienomem();
+    }
+    // append final \0
+    if ( ! stralloc_0(&path_modif) ) dienomem();
+  }
+
+  // exec into execlineb and append path_modif to the environment
+  xmexec_aem(
+    EXECLINEB_PATH(),
+    argv,
+    envp,
+    path_modif.s, path_modif.len
+  );
+}