/* $Id$ */
-#include <windows.h>
#include <msvcrt/process.h>
#include <msvcrt/stdlib.h>
-#include <msvcrt/fcntl.h>
+#include <msvcrt/string.h>
#include <msvcrt/errno.h>
+#include <msvcrt/internal/file.h>
#define NDEBUG
#include <msvcrt/msvcrtdbg.h>
extern int maxfno;
+char const* ext[] =
+{
+ "",
+ ".bat",
+ ".cmd",
+ ".com",
+ ".exe"
+};
+
+const char* find_exec(const char* path, char* rpath)
+{
+ char *rp;
+ const char *rd;
+ int i, found = 0;
+
+ DPRINT("find_exec('%s', %x)\n", path, rpath);
+
+ if (path == NULL)
+ {
+ return NULL;
+ }
+ if (strlen(path) > FILENAME_MAX - 1)
+ {
+ return path;
+ }
+ /* copy path in rpath */
+ for (rd = path, rp = rpath; *rd; *rp++ = *rd++);
+ *rp = 0;
+ /* try first with the name as is */
+ for (i = 0; i < sizeof(ext) / sizeof(*ext); i++)
+ {
+ strcpy(rp, ext[i]);
+
+ DPRINT("trying '%s'\n", rpath);
+
+ if (_access(rpath, F_OK) == 0 && _access(rpath, D_OK) != 0)
+ {
+ found = 1;
+ break;
+ }
+ }
+ if (!found)
+ {
+ char* env = getenv("PATH");
+ if (env)
+ {
+ char* ep = env;
+ while (*ep && !found)
+ {
+ if (*ep == ';') ep++;
+ rp=rpath;
+ for (; *ep && (*ep != ';'); *rp++ = *ep++);
+ if (rp > rpath)
+ {
+ rp--;
+ if (*rp != '/' && *rp != '\\')
+ {
+ *++rp = '\\';
+ }
+ rp++;
+ }
+ for (rd=path; *rd; *rp++ = *rd++);
+ for (i = 0; i < sizeof(ext) / sizeof(*ext); i++)
+ {
+ strcpy(rp, ext[i]);
+
+ DPRINT("trying '%s'\n", rpath);
+
+ if (_access(rpath, F_OK) == 0 && _access(rpath, D_OK) != 0)
+ {
+ found = 1;
+ break;
+ }
+ }
+ }
+ free(env);
+ }
+ }
+
+ return found ? rpath : path;
+}
+
static char*
argvtos(char* const* argv, char delim)
{
DPRINT("_spawnvp('%s')\n", cmdname);
- _searchenv(cmdname, "PATH", pathname);
-
- return _spawnv(mode, pathname[0] ? pathname : cmdname, argv);
+ return _spawnv(mode, find_exec(cmdname, pathname), argv);
}
int _spawnlpe(int mode, const char* cmdname, const char* arg0, .../*, NULL, const char* const* envp*/)
DPRINT("_spawnlpe('%s')\n", cmdname);
- _searchenv(cmdname, "PATH", pathname);
-
va_start(argp, arg0);
args = valisttos(arg0, argp, ' ');
do
envs = argvtos(ptr, 0);
if (args)
{
- ret = do_spawn(mode, pathname[0] ? pathname : cmdname, args, envs);
+ ret = do_spawn(mode, find_exec(cmdname, pathname), args, envs);
free(args);
}
if (envs)
DPRINT("_spawnvpe('%s')\n", cmdname);
- _searchenv(cmdname, "PATH", pathname);
-
- return _spawnve(mode, pathname[0] ? pathname : cmdname, argv, envp);
+ return _spawnve(mode, find_exec(cmdname, pathname), argv, envp);
}
int _execl(const char* cmdname, const char* arg0, ...)
DPRINT("_execlp('%s')\n", cmdname);
- _searchenv(cmdname, "PATH", pathname);
-
va_start(argp, arg0);
args = valisttos(arg0, argp, ' ');
if (args)
{
- ret = do_spawn(P_OVERLAY, pathname[0] ? pathname : cmdname, args, NULL);
+ ret = do_spawn(P_OVERLAY, find_exec(cmdname, pathname), args, NULL);
free(args);
}
return ret;
DPRINT("_execlpe('%s')\n", cmdname);
- _searchenv(cmdname, "PATH", pathname);
-
va_start(argp, arg0);
args = valisttos(arg0, argp, ' ');
do
envs = argvtos(ptr, 0);
if (args)
{
- ret = do_spawn(P_OVERLAY, pathname[0] ? pathname : cmdname, args, envs);
+ ret = do_spawn(P_OVERLAY, find_exec(cmdname, pathname), args, envs);
free(args);
}
if (envs)