rops: init at 0.1.4 (#364869)
[NixPkgs.git] / pkgs / development / skaware-packages / execline / execlineb-wrapper.c
blobc8e91813b77444c7cf7c6bcd03b6c3fc99735193
1 /*
2 * A wrapper around execlineb, which provides all execline
3 * tools on execlineb’s PATH.
4 * It is implemented as a C program, because on non-Linux,
5 * nested shebang lines are not supported.
6 */
8 #include <stdlib.h>
9 #include <string.h>
11 #include <skalibs/stralloc.h>
12 #include <skalibs/djbunix.h>
13 #include <skalibs/strerr2.h>
14 #include <skalibs/env.h>
15 #include <skalibs/exec.h>
17 #define dienomem() strerr_diefu1sys(111, "stralloc_catb")
19 // macros from outside
20 /* const char* EXECLINEB_PATH; */
21 /* const char* EXECLINE_BIN_PATH; */
23 int main(int argc, char const* argv[], char const *const *envp)
25 PROG = "execlineb-wrapper";
27 char const* path = getenv("PATH");
28 stralloc path_modif = STRALLOC_ZERO;
30 // modify PATH if unset or EXECLINEB_BIN_PATH is not yet there
31 if ( !path || ! strstr(path, EXECLINE_BIN_PATH())) {
32 // prepend our execline path
33 if ( ! stralloc_cats(&path_modif, "PATH=")
34 || ! stralloc_cats(&path_modif, EXECLINE_BIN_PATH()) ) dienomem();
35 // old path was not empty
36 if ( path && path[0] ) {
37 if ( ! stralloc_catb(&path_modif, ":", 1)
38 || ! stralloc_cats(&path_modif, path) ) dienomem();
40 // append final \0
41 if ( ! stralloc_0(&path_modif) ) dienomem();
44 // exec into execlineb and append path_modif to the environment
45 xmexec_aem(
46 EXECLINEB_PATH(),
47 argv,
48 envp,
49 path_modif.s, path_modif.len