/*
**  zsh-wrapper.c -- ZSH Run-Time Executable Wrapper
**  This adjusts the "LD_LIBRARY_PATH" environment variable to let "zsh"
**  find "libzsh.so" and is a C program (instead of the expected wrapper
**  shell script) to still allow ZSH to be used via Unix "she-bang".
*/

#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int main(int argn, char **argv)
{
    char *ldlp_old;
    char *ldlp_new;

    /* adjust LD_LIBRARY_PATH environment variable */
    if ((ldlp_old = getenv("LD_LIBRARY_PATH")) == NULL)
        ldlp_old = "/lib:/usr/lib";
    if ((ldlp_new = (char *)malloc(strlen(PREFIX "/lib/zsh:") + strlen(ldlp_old) + 1)) == NULL)
        abort();
    strcpy(ldlp_new, PREFIX "/lib/zsh:");
    strcat(ldlp_new, ldlp_old);
    if (setenv("LD_LIBRARY_PATH", ldlp_new, 1) != 0)
        abort();

    /* pass-through control to real ZSH executable */
    argv[0] = PREFIX "/libexec/zsh/zsh";
    if (execv(argv[0], argv) == -1)
        abort();

    /* not reached */
    return 0;
}

