/****************************************************/
/*                                                  */
/*  Executable wrapper for MakeTeX... programs.     */
/*  Calls its namesake from TOOLS directory.        */
/*  Provide links with different names to make it   */
/*  multipurpose.                                   */
/*                                                  */
/*  Michal Jaegermann, Feb 11 1995                  */
/*                                                  */
/****************************************************/

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

#define VERSION_S "0.2"
/*
 * If you do not have ANSI compiler you may use
 * an "explicit"single string in TOOLS define; this
 * is just a way to make future modifications easier.
 */
#define TOOLS   "/usr/local/tex/scripts-" VERSION_S "/bin/"
#define ASIZE  120

/*
 * This is a list of names under which we are willing
 * to execute. It has be NULL terminated.
 */
const char *accepted[] = {
    "MakeTeXPK",
    "MakeTeXTFM",
    "MakeTeXMF",
    NULL
};

int
main(int argc, char **argv)
{
    char doer[ASIZE] = TOOLS;
    int idx = 0;
    /*
     * If your compiler is broken and the construction below
     * does not work then "tail = strchr(doer, '\0');", or
     * equivalent, will serve as well.
     */
    char *tail = doer + (sizeof(TOOLS) - 1);
    char *start;

    /* find our base name */
    start = (start = strrchr(argv[0], '/')) ?
                               (start + 1) : argv[0];
    /* check if we are on the list */
    while (1) {
	if (NULL == accepted[idx])
	    exit(1);          /* not on the list - bye-bye */
	if (0 == strcmp(accepted[idx], start))
	    break;            /* this is ours */
	idx += 1;
    }
    /*
     * Set pretty bland, but hopefuly secure environment;
     * we intend to run this program 'suid'.
     */
    setenv("PATH", "/bin:/usr/bin", 1);
    setenv("IFS", " ", 1);
    /*
     * You may want/need some other calls to setenv().
     * For example, if your system has an environment
     * variable pointing to shared libraries it should
     * be set here.
     */

    /*
     * Attach our name at the end of a directory string.
     * This assumes that real scripts in TOOLS directory
     * will be called by their own names (but indirectly)
     */
    strcpy(tail, start);
    setuid(geteuid());		/* we want priviledges of
				   an owner of this program */
    return execv(doer, argv);   /* do it and tell results   */
}

/*
 * Local variables:
 * compile-command: "gcc -s -O6 -fomit-frame-pointer -Wall -pipe -m486 -Xlinker -N -o ../MakeTeXPK maketex.c"
 * End:
 */