#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#if defined (WIN32) || defined (WIN64)
#include <direct.h>
#include <windows.h>
#endif
#include <string.h>
#include <sys/stat.h>
char *
search_env (char *pszenv,
char *pszfilename, char *pszbuff, size_t cbbuff)
{
char *psz = 0;
char *tok = 0;
char *pszpath = 0;
if (!pszenv || !pszbuff)
return (char *) 0;
pszbuff[0] = 0;
pszpath = getenv (pszenv);
if (pszpath)
{
psz = strdup (pszpath);
if (!psz)
return (char *) 0;
tok = strtok (psz, ":");
while (tok)
{
struct stat stf;
pszbuff[0] = 0;
strcat (pszbuff, tok);
strcat (pszbuff, "/");
strcat (pszbuff, pszfilename);
if (stat (pszbuff, &stf) == 0)
{
free (psz);
return pszbuff;
}
tok = strtok (psz, ":");
}
free (psz);
}
return (char *) 0;
}
char *
search_path (char *pszfilename,
char *pszbuff, size_t cbbuff)
{
char *pszfilepart = 0;
char *pszpath = 0;
struct stat stf;
if (!pszfilename || !pszbuff)
return (char *) 0;
pszbuff[0] = 0;
#if defined (WIN32) || defined (WIN64)
if (SearchPathA (0, pszfilename,
0, cbbuff, pszbuff, pszfilepart) != 0)
return pszbuff;
#else
strcat (pszbuff, "./");
strcat (pszbuff, pszfilename);
if (stat (pszfilename, &stf) == 0)
return pszbuff;
if (search_env ("PATH", pszfilename, pszbuff, cbbuff))
return (pszbuff);
if (search_env
("LD_LIBRARY_PATH", pszfilename, pszbuff, cbbuff))
return (pszbuff);
#endif
return (char *) 0;
}
void
syntax (char *pszprog)
{
fprintf (stderr, "syntax error:\n");
fprintf (stderr,
"%s [-d launch_directory] -e application [-arg arg0 -arg arg1 arg2 ... argN]\n",
pszprog);
exit (1);
}
#if defined (WIN32) || defined (WIN64)
void
spawn_proc (char *pszcommand_line)
{
STARTUPINFOA startInfo;
PROCESS_INFORMATION pInfo;
memset (&startInfo, 0, sizeof (STARTUPINFO));
memset (&pInfo, 0, sizeof (PROCESS_INFORMATION));
startInfo.cb = sizeof (STARTUPINFO);
startInfo.lpReserved = 0;
startInfo.lpDesktop = 0;
startInfo.lpTitle = 0;
startInfo.dwFlags = 0;
startInfo.cbReserved2 = 0;
startInfo.lpReserved2 = 0;
startInfo.wShowWindow = SW_SHOW;
CreateProcessA (0, pszcommand_line, 0, 0, FALSE,
CREATE_NO_WINDOW | NORMAL_PRIORITY_CLASS,
0, 0, &startInfo, &pInfo);
}
#else
void
spawn_proc (char *pszcommand_line)
{
putenv ("PATH=./:/bin:/usr/sbin:/usr/local/bin:/etc");
system (pszcommand_line);
}
#endif
int
main (int argc, char *argv[])
{
char szcommand[2048];
char szargs[1024];
int i = 0;
char *pszexe = 0;
szcommand[0] = 0;
szargs[0] = 0;
for (i = 1; i < argc; i++)
{
if ((argv[i][0] == '-' ||
argv[i][0] == '/') && argv[i][2] == 0)
{
switch (argv[i][1])
{
case 'd': /* change directory */
_chdir (argv[i + 1]);
i++;
break;
case 'e': /* executable */
pszexe = argv[i + 1];
i++;
break;
default:
strcat (szargs, " ");
strcat (szargs, argv[i]);
strcat (szargs, " ");
strcat (szargs, argv[i + 1]);
i++;
break;
}
}
else
{
strcat (szargs, " ");
strcat (szargs, argv[i]);
}
}
if (!pszexe)
syntax (argv[0]);
if (sizeof (szcommand) <
strlen (pszexe) + strlen (szargs) + 1)
{
fprintf (stderr,
"error: internal command buffer too small!\n");
exit (1);
}
strcat (szcommand, pszexe);
strcat (szcommand, " ");
strcat (szcommand, szargs);
spawn_proc (szcommand);
exit (0); /* success */
}