2001-01-11 00:59:25 +01:00
|
|
|
/*
|
|
|
|
* msvcrt.dll spawn/exec functions
|
|
|
|
*
|
|
|
|
* Copyright 1996,1998 Marcus Meissner
|
|
|
|
* Copyright 1996 Jukka Iivonen
|
|
|
|
* Copyright 1997,2000 Uwe Bonnes
|
|
|
|
* Copyright 2000 Jon Griffiths
|
|
|
|
*
|
2002-03-10 00:29:33 +01:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2002-03-10 00:29:33 +01:00
|
|
|
*
|
2001-01-11 00:59:25 +01:00
|
|
|
* FIXME:
|
|
|
|
* -File handles need some special handling. Sometimes children get
|
|
|
|
* open file handles, sometimes not. The docs are confusing
|
|
|
|
* -No check for maximum path/argument/environment size is done
|
|
|
|
*/
|
2002-08-09 21:49:31 +02:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
#include "msvcrt.h"
|
2002-01-22 01:57:16 +01:00
|
|
|
#include "wine/debug.h"
|
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
|
2001-01-11 00:59:25 +01:00
|
|
|
|
|
|
|
/* INTERNAL: Spawn a child process */
|
2005-11-03 14:17:51 +01:00
|
|
|
static MSVCRT_intptr_t msvcrt_spawn(int flags, const char* exe, char* cmdline, char* env)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
STARTUPINFOA si;
|
|
|
|
PROCESS_INFORMATION pi;
|
|
|
|
|
2004-06-25 03:19:15 +02:00
|
|
|
if ((unsigned)flags > MSVCRT__P_DETACH)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2002-07-19 05:24:50 +02:00
|
|
|
*MSVCRT__errno() = MSVCRT_EINVAL;
|
2001-01-11 00:59:25 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&si, 0, sizeof(si));
|
|
|
|
si.cb = sizeof(si);
|
2004-09-03 03:05:30 +02:00
|
|
|
msvcrt_create_io_inherit_block(&si);
|
2001-09-11 01:07:07 +02:00
|
|
|
if (!CreateProcessA(exe, cmdline, NULL, NULL, TRUE,
|
2004-06-25 03:19:15 +02:00
|
|
|
flags == MSVCRT__P_DETACH ? DETACHED_PROCESS : 0,
|
2001-01-11 00:59:25 +01:00
|
|
|
env, NULL, &si, &pi))
|
|
|
|
{
|
2004-06-25 03:19:15 +02:00
|
|
|
msvcrt_set_errno(GetLastError());
|
2004-12-06 17:08:36 +01:00
|
|
|
MSVCRT_free(si.lpReserved2);
|
2001-01-11 00:59:25 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2004-12-06 17:08:36 +01:00
|
|
|
MSVCRT_free(si.lpReserved2);
|
2001-01-11 00:59:25 +01:00
|
|
|
switch(flags)
|
|
|
|
{
|
2004-06-25 03:19:15 +02:00
|
|
|
case MSVCRT__P_WAIT:
|
2004-04-09 21:03:13 +02:00
|
|
|
WaitForSingleObject(pi.hProcess, INFINITE);
|
2001-01-11 00:59:25 +01:00
|
|
|
GetExitCodeProcess(pi.hProcess,&pi.dwProcessId);
|
|
|
|
CloseHandle(pi.hProcess);
|
|
|
|
CloseHandle(pi.hThread);
|
2005-11-03 14:17:51 +01:00
|
|
|
return pi.dwProcessId;
|
2004-06-25 03:19:15 +02:00
|
|
|
case MSVCRT__P_DETACH:
|
2001-01-11 00:59:25 +01:00
|
|
|
CloseHandle(pi.hProcess);
|
|
|
|
pi.hProcess = 0;
|
|
|
|
/* fall through */
|
2004-06-25 03:19:15 +02:00
|
|
|
case MSVCRT__P_NOWAIT:
|
|
|
|
case MSVCRT__P_NOWAITO:
|
2001-01-11 00:59:25 +01:00
|
|
|
CloseHandle(pi.hThread);
|
2005-11-03 14:17:51 +01:00
|
|
|
return (MSVCRT_intptr_t)pi.hProcess;
|
2004-06-25 03:19:15 +02:00
|
|
|
case MSVCRT__P_OVERLAY:
|
2001-01-11 00:59:25 +01:00
|
|
|
MSVCRT__exit(0);
|
|
|
|
}
|
|
|
|
return -1; /* can't reach here */
|
|
|
|
}
|
|
|
|
|
2001-09-11 01:07:07 +02:00
|
|
|
/* INTERNAL: Convert argv list to a single 'delim'-separated string, with an
|
|
|
|
* extra '\0' to terminate it
|
|
|
|
*/
|
2001-04-11 01:25:25 +02:00
|
|
|
static char* msvcrt_argvtos(const char* const* arg, char delim)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2001-09-11 01:07:07 +02:00
|
|
|
const char* const* a;
|
|
|
|
long size;
|
|
|
|
char* p;
|
|
|
|
char* ret;
|
2001-01-11 00:59:25 +01:00
|
|
|
|
|
|
|
if (!arg && !delim)
|
2001-09-11 01:07:07 +02:00
|
|
|
{
|
|
|
|
/* Return NULL for an empty environment list */
|
|
|
|
return NULL;
|
|
|
|
}
|
2001-01-11 00:59:25 +01:00
|
|
|
|
|
|
|
/* get length */
|
2001-09-11 01:07:07 +02:00
|
|
|
a = arg;
|
|
|
|
size = 0;
|
|
|
|
while (*a)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2001-09-11 01:07:07 +02:00
|
|
|
size += strlen(*a) + 1;
|
|
|
|
a++;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
2001-09-11 01:07:07 +02:00
|
|
|
ret = (char*)MSVCRT_malloc(size + 1);
|
|
|
|
if (!ret)
|
2001-01-11 00:59:25 +01:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* fill string */
|
2001-09-11 01:07:07 +02:00
|
|
|
a = arg;
|
|
|
|
p = ret;
|
|
|
|
while (*a)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2001-09-11 01:07:07 +02:00
|
|
|
int len = strlen(*a);
|
2002-11-13 05:20:54 +01:00
|
|
|
memcpy(p,*a,len);
|
2001-09-11 01:07:07 +02:00
|
|
|
p += len;
|
|
|
|
*p++ = delim;
|
|
|
|
a++;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
2002-11-13 05:20:54 +01:00
|
|
|
if (delim && p > ret) p[-1] = 0;
|
|
|
|
else *p = 0;
|
2001-01-11 00:59:25 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2001-09-11 01:07:07 +02:00
|
|
|
/* INTERNAL: Convert va_list to a single 'delim'-separated string, with an
|
|
|
|
* extra '\0' to terminate it
|
|
|
|
*/
|
|
|
|
static char* msvcrt_valisttos(const char* arg0, va_list alist, char delim)
|
2001-05-18 22:58:08 +02:00
|
|
|
{
|
2002-08-09 21:49:31 +02:00
|
|
|
va_list alist2;
|
2001-05-18 22:58:08 +02:00
|
|
|
long size;
|
2001-09-11 01:07:07 +02:00
|
|
|
const char *arg;
|
|
|
|
char* p;
|
2001-05-18 22:58:08 +02:00
|
|
|
char *ret;
|
|
|
|
|
2003-09-30 02:33:47 +02:00
|
|
|
#ifdef HAVE_VA_COPY
|
2002-08-09 21:49:31 +02:00
|
|
|
va_copy(alist2,alist);
|
|
|
|
#else
|
2003-09-30 02:33:47 +02:00
|
|
|
# ifdef HAVE___VA_COPY
|
2002-08-09 21:49:31 +02:00
|
|
|
__va_copy(alist2,alist);
|
|
|
|
# else
|
|
|
|
alist2 = alist;
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2001-05-18 22:58:08 +02:00
|
|
|
if (!arg0 && !delim)
|
|
|
|
{
|
2001-09-11 01:07:07 +02:00
|
|
|
/* Return NULL for an empty environment list */
|
|
|
|
return NULL;
|
2001-05-18 22:58:08 +02:00
|
|
|
}
|
|
|
|
|
2001-09-11 01:07:07 +02:00
|
|
|
/* get length */
|
|
|
|
arg = arg0;
|
|
|
|
size = 0;
|
|
|
|
do {
|
|
|
|
size += strlen(arg) + 1;
|
|
|
|
arg = va_arg(alist, char*);
|
|
|
|
} while (arg != NULL);
|
|
|
|
|
|
|
|
ret = (char*)MSVCRT_malloc(size + 1);
|
|
|
|
if (!ret)
|
2001-05-18 22:58:08 +02:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* fill string */
|
2001-09-11 01:07:07 +02:00
|
|
|
arg = arg0;
|
|
|
|
p = ret;
|
|
|
|
do {
|
|
|
|
int len = strlen(arg);
|
|
|
|
memcpy(p,arg,len);
|
|
|
|
p += len;
|
|
|
|
*p++ = delim;
|
|
|
|
arg = va_arg(alist2, char*);
|
|
|
|
} while (arg != NULL);
|
2002-11-13 05:20:54 +01:00
|
|
|
if (delim && p > ret) p[-1] = 0;
|
|
|
|
else *p = 0;
|
2001-05-18 22:58:08 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _cwait (MSVCRT.@)
|
|
|
|
*/
|
2006-06-13 11:21:19 +02:00
|
|
|
MSVCRT_intptr_t CDECL _cwait(int *status, MSVCRT_intptr_t pid, int action)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
HANDLE hPid = (HANDLE)pid;
|
|
|
|
int doserrno;
|
|
|
|
|
|
|
|
action = action; /* Remove warning */
|
|
|
|
|
2004-04-09 21:03:13 +02:00
|
|
|
if (!WaitForSingleObject(hPid, INFINITE))
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
if (status)
|
|
|
|
{
|
|
|
|
DWORD stat;
|
|
|
|
GetExitCodeProcess(hPid, &stat);
|
|
|
|
*status = (int)stat;
|
|
|
|
}
|
2005-11-03 14:17:51 +01:00
|
|
|
return pid;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
doserrno = GetLastError();
|
|
|
|
|
|
|
|
if (doserrno == ERROR_INVALID_HANDLE)
|
|
|
|
{
|
2002-07-19 05:24:50 +02:00
|
|
|
*MSVCRT__errno() = MSVCRT_ECHILD;
|
2004-03-16 20:17:11 +01:00
|
|
|
*MSVCRT___doserrno() = doserrno;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
else
|
2004-06-25 03:19:15 +02:00
|
|
|
msvcrt_set_errno(doserrno);
|
2001-01-11 00:59:25 +01:00
|
|
|
|
|
|
|
return status ? *status = -1 : -1;
|
|
|
|
}
|
|
|
|
|
2001-05-22 21:18:51 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* _execl (MSVCRT.@)
|
2001-09-11 01:07:07 +02:00
|
|
|
*
|
2002-06-01 01:06:46 +02:00
|
|
|
* Like on Windows, this function does not handle arguments with spaces
|
2001-09-11 01:07:07 +02:00
|
|
|
* or double-quotes.
|
2001-05-22 21:18:51 +02:00
|
|
|
*/
|
2006-06-13 11:21:19 +02:00
|
|
|
MSVCRT_intptr_t CDECL _execl(const char* name, const char* arg0, ...)
|
2001-05-22 21:18:51 +02:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
char * args;
|
2005-11-03 14:17:51 +01:00
|
|
|
MSVCRT_intptr_t ret;
|
2001-05-22 21:18:51 +02:00
|
|
|
|
|
|
|
va_start(ap, arg0);
|
|
|
|
args = msvcrt_valisttos(arg0, ap, ' ');
|
|
|
|
va_end(ap);
|
|
|
|
|
2004-06-25 03:19:15 +02:00
|
|
|
ret = msvcrt_spawn(MSVCRT__P_OVERLAY, name, args, NULL);
|
2001-05-22 21:18:51 +02:00
|
|
|
MSVCRT_free(args);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2003-11-14 04:29:51 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _execle (MSVCRT.@)
|
|
|
|
*/
|
2006-06-13 11:21:19 +02:00
|
|
|
MSVCRT_intptr_t CDECL _execle(const char* name, const char* arg0, ...)
|
2003-11-14 04:29:51 +01:00
|
|
|
{
|
|
|
|
FIXME("stub\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2001-05-22 21:18:51 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* _execlp (MSVCRT.@)
|
2001-09-11 01:07:07 +02:00
|
|
|
*
|
2002-06-01 01:06:46 +02:00
|
|
|
* Like on Windows, this function does not handle arguments with spaces
|
2001-09-11 01:07:07 +02:00
|
|
|
* or double-quotes.
|
2001-05-22 21:18:51 +02:00
|
|
|
*/
|
2006-06-13 11:21:19 +02:00
|
|
|
MSVCRT_intptr_t CDECL _execlp(const char* name, const char* arg0, ...)
|
2001-05-22 21:18:51 +02:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
char * args;
|
2005-11-03 14:17:51 +01:00
|
|
|
MSVCRT_intptr_t ret;
|
2001-05-22 21:18:51 +02:00
|
|
|
char fullname[MAX_PATH];
|
|
|
|
|
|
|
|
_searchenv(name, "PATH", fullname);
|
|
|
|
|
|
|
|
va_start(ap, arg0);
|
|
|
|
args = msvcrt_valisttos(arg0, ap, ' ');
|
|
|
|
va_end(ap);
|
|
|
|
|
2004-06-25 03:19:15 +02:00
|
|
|
ret = msvcrt_spawn(MSVCRT__P_OVERLAY, fullname[0] ? fullname : name, args, NULL);
|
2001-05-22 21:18:51 +02:00
|
|
|
MSVCRT_free(args);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2003-11-14 04:29:51 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _execlpe (MSVCRT.@)
|
|
|
|
*/
|
2006-06-13 11:21:19 +02:00
|
|
|
MSVCRT_intptr_t CDECL _execlpe(const char* name, const char* arg0, ...)
|
2003-11-14 04:29:51 +01:00
|
|
|
{
|
|
|
|
FIXME("stub\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2001-05-22 21:18:51 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* _execv (MSVCRT.@)
|
2001-09-11 01:07:07 +02:00
|
|
|
*
|
2002-06-01 01:06:46 +02:00
|
|
|
* Like on Windows, this function does not handle arguments with spaces
|
2001-09-11 01:07:07 +02:00
|
|
|
* or double-quotes.
|
2001-05-22 21:18:51 +02:00
|
|
|
*/
|
2006-06-13 11:21:19 +02:00
|
|
|
MSVCRT_intptr_t CDECL _execv(const char* name, char* const* argv)
|
2001-05-22 21:18:51 +02:00
|
|
|
{
|
2004-06-25 03:19:15 +02:00
|
|
|
return _spawnve(MSVCRT__P_OVERLAY, name, (const char* const*) argv, NULL);
|
2001-05-22 21:18:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _execve (MSVCRT.@)
|
2001-09-11 01:07:07 +02:00
|
|
|
*
|
2002-06-01 01:06:46 +02:00
|
|
|
* Like on Windows, this function does not handle arguments with spaces
|
2001-09-11 01:07:07 +02:00
|
|
|
* or double-quotes.
|
2001-05-22 21:18:51 +02:00
|
|
|
*/
|
2006-08-09 21:28:52 +02:00
|
|
|
MSVCRT_intptr_t CDECL MSVCRT__execve(const char* name, char* const* argv, const char* const* envv)
|
2001-05-22 21:18:51 +02:00
|
|
|
{
|
2004-06-25 03:19:15 +02:00
|
|
|
return _spawnve(MSVCRT__P_OVERLAY, name, (const char* const*) argv, envv);
|
2001-05-22 21:18:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _execvpe (MSVCRT.@)
|
2001-09-11 01:07:07 +02:00
|
|
|
*
|
2002-06-01 01:06:46 +02:00
|
|
|
* Like on Windows, this function does not handle arguments with spaces
|
2001-09-11 01:07:07 +02:00
|
|
|
* or double-quotes.
|
2001-05-22 21:18:51 +02:00
|
|
|
*/
|
2006-06-13 11:21:19 +02:00
|
|
|
MSVCRT_intptr_t CDECL _execvpe(const char* name, char* const* argv, const char* const* envv)
|
2001-05-22 21:18:51 +02:00
|
|
|
{
|
|
|
|
char fullname[MAX_PATH];
|
|
|
|
|
|
|
|
_searchenv(name, "PATH", fullname);
|
2004-06-25 03:19:15 +02:00
|
|
|
return _spawnve(MSVCRT__P_OVERLAY, fullname[0] ? fullname : name,
|
2001-05-22 21:18:51 +02:00
|
|
|
(const char* const*) argv, envv);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _execvp (MSVCRT.@)
|
2001-09-11 01:07:07 +02:00
|
|
|
*
|
2002-06-01 01:06:46 +02:00
|
|
|
* Like on Windows, this function does not handle arguments with spaces
|
2001-09-11 01:07:07 +02:00
|
|
|
* or double-quotes.
|
2001-05-22 21:18:51 +02:00
|
|
|
*/
|
2006-06-13 11:21:19 +02:00
|
|
|
MSVCRT_intptr_t CDECL _execvp(const char* name, char* const* argv)
|
2001-05-22 21:18:51 +02:00
|
|
|
{
|
|
|
|
return _execvpe(name, argv, NULL);
|
|
|
|
}
|
|
|
|
|
2001-05-18 22:58:08 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* _spawnl (MSVCRT.@)
|
2001-09-11 01:07:07 +02:00
|
|
|
*
|
2002-06-01 01:06:46 +02:00
|
|
|
* Like on Windows, this function does not handle arguments with spaces
|
2001-09-11 01:07:07 +02:00
|
|
|
* or double-quotes.
|
2001-05-18 22:58:08 +02:00
|
|
|
*/
|
2006-06-13 11:21:19 +02:00
|
|
|
MSVCRT_intptr_t CDECL _spawnl(int flags, const char* name, const char* arg0, ...)
|
2001-05-18 22:58:08 +02:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
char * args;
|
2005-11-03 14:17:51 +01:00
|
|
|
MSVCRT_intptr_t ret;
|
2001-05-18 22:58:08 +02:00
|
|
|
|
|
|
|
va_start(ap, arg0);
|
|
|
|
args = msvcrt_valisttos(arg0, ap, ' ');
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
ret = msvcrt_spawn(flags, name, args, NULL);
|
|
|
|
MSVCRT_free(args);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2003-11-14 04:29:51 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _spawnle (MSVCRT.@)
|
|
|
|
*/
|
2006-06-13 11:21:19 +02:00
|
|
|
MSVCRT_intptr_t CDECL _spawnle(int flags, const char* name, const char* arg0, ...)
|
2003-11-14 04:29:51 +01:00
|
|
|
{
|
2004-07-16 05:08:19 +02:00
|
|
|
va_list ap;
|
|
|
|
char *args, *envs = NULL;
|
|
|
|
const char * const *envp;
|
2005-11-03 14:17:51 +01:00
|
|
|
MSVCRT_intptr_t ret;
|
2004-07-16 05:08:19 +02:00
|
|
|
|
|
|
|
va_start(ap, arg0);
|
|
|
|
args = msvcrt_valisttos(arg0, ap, ' ');
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
va_start(ap, arg0);
|
|
|
|
while (va_arg( ap, char * ) != NULL) /*nothing*/;
|
|
|
|
envp = va_arg( ap, const char * const * );
|
|
|
|
if (envp) envs = msvcrt_argvtos(envp, 0);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
ret = msvcrt_spawn(flags, name, args, envs);
|
|
|
|
|
|
|
|
MSVCRT_free(args);
|
2006-08-09 01:17:41 +02:00
|
|
|
MSVCRT_free(envs);
|
2004-07-16 05:08:19 +02:00
|
|
|
return ret;
|
2003-11-14 04:29:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-05-18 22:58:08 +02:00
|
|
|
/*********************************************************************
|
2001-05-22 21:18:51 +02:00
|
|
|
* _spawnlp (MSVCRT.@)
|
2001-09-11 01:07:07 +02:00
|
|
|
*
|
2002-06-01 01:06:46 +02:00
|
|
|
* Like on Windows, this function does not handle arguments with spaces
|
2001-09-11 01:07:07 +02:00
|
|
|
* or double-quotes.
|
2001-05-18 22:58:08 +02:00
|
|
|
*/
|
2006-06-13 11:21:19 +02:00
|
|
|
MSVCRT_intptr_t CDECL _spawnlp(int flags, const char* name, const char* arg0, ...)
|
2001-05-18 22:58:08 +02:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
char * args;
|
2005-11-03 14:17:51 +01:00
|
|
|
MSVCRT_intptr_t ret;
|
2001-05-18 22:58:08 +02:00
|
|
|
char fullname[MAX_PATH];
|
|
|
|
|
|
|
|
_searchenv(name, "PATH", fullname);
|
|
|
|
|
|
|
|
va_start(ap, arg0);
|
|
|
|
args = msvcrt_valisttos(arg0, ap, ' ');
|
|
|
|
va_end(ap);
|
|
|
|
|
2001-05-22 21:18:51 +02:00
|
|
|
ret = msvcrt_spawn(flags, fullname[0] ? fullname : name, args, NULL);
|
2001-05-18 22:58:08 +02:00
|
|
|
MSVCRT_free(args);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2003-11-14 04:29:51 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _spawnlpe (MSVCRT.@)
|
|
|
|
*/
|
2006-06-13 11:21:19 +02:00
|
|
|
MSVCRT_intptr_t CDECL _spawnlpe(int flags, const char* name, const char* arg0, ...)
|
2003-11-14 04:29:51 +01:00
|
|
|
{
|
2004-07-16 05:08:19 +02:00
|
|
|
va_list ap;
|
|
|
|
char *args, *envs = NULL;
|
|
|
|
const char * const *envp;
|
2005-11-03 14:17:51 +01:00
|
|
|
MSVCRT_intptr_t ret;
|
2004-07-16 05:08:19 +02:00
|
|
|
char fullname[MAX_PATH];
|
|
|
|
|
|
|
|
_searchenv(name, "PATH", fullname);
|
|
|
|
|
|
|
|
va_start(ap, arg0);
|
|
|
|
args = msvcrt_valisttos(arg0, ap, ' ');
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
va_start(ap, arg0);
|
|
|
|
while (va_arg( ap, char * ) != NULL) /*nothing*/;
|
|
|
|
envp = va_arg( ap, const char * const * );
|
|
|
|
if (envp) envs = msvcrt_argvtos(envp, 0);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
ret = msvcrt_spawn(flags, fullname[0] ? fullname : name, args, envs);
|
|
|
|
|
|
|
|
MSVCRT_free(args);
|
2006-08-09 01:17:41 +02:00
|
|
|
MSVCRT_free(envs);
|
2004-07-16 05:08:19 +02:00
|
|
|
return ret;
|
2003-11-14 04:29:51 +01:00
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _spawnve (MSVCRT.@)
|
2001-09-11 01:07:07 +02:00
|
|
|
*
|
2002-06-01 01:06:46 +02:00
|
|
|
* Like on Windows, this function does not handle arguments with spaces
|
2001-09-11 01:07:07 +02:00
|
|
|
* or double-quotes.
|
2001-01-11 00:59:25 +01:00
|
|
|
*/
|
2006-06-13 11:21:19 +02:00
|
|
|
MSVCRT_intptr_t CDECL _spawnve(int flags, const char* name, const char* const* argv,
|
|
|
|
const char* const* envv)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2001-04-10 23:16:07 +02:00
|
|
|
char * args = msvcrt_argvtos(argv,' ');
|
|
|
|
char * envs = msvcrt_argvtos(envv,0);
|
2006-04-05 20:37:01 +02:00
|
|
|
char fullname[MAX_PATH];
|
|
|
|
const char *p;
|
|
|
|
int len;
|
2005-11-03 14:17:51 +01:00
|
|
|
MSVCRT_intptr_t ret = -1;
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2004-12-14 16:13:54 +01:00
|
|
|
TRACE(":call (%s), params (%s), env (%s)\n",debugstr_a(name),debugstr_a(args),
|
|
|
|
envs?"Custom":"Null");
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2006-04-05 20:37:01 +02:00
|
|
|
/* no check for NULL name.
|
|
|
|
native doesn't do it */
|
|
|
|
|
|
|
|
p = memchr(name, '\0', MAX_PATH);
|
|
|
|
if( !p )
|
|
|
|
p = name + MAX_PATH - 1;
|
|
|
|
len = p - name;
|
|
|
|
|
|
|
|
/* extra-long names are silently truncated. */
|
|
|
|
memcpy(fullname, name, len);
|
|
|
|
|
|
|
|
for( p--; p >= name; p-- )
|
|
|
|
{
|
|
|
|
if( *p == '\\' || *p == '/' || *p == ':' || *p == '.' )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if no extension is given, assume .exe */
|
|
|
|
if( (p < name || *p != '.') && len <= MAX_PATH - 5 )
|
|
|
|
{
|
|
|
|
FIXME("only trying .exe when no extension given\n");
|
|
|
|
memcpy(fullname+len, ".exe", 4);
|
|
|
|
len += 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
fullname[len] = '\0';
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
if (args)
|
|
|
|
{
|
2001-04-10 23:16:07 +02:00
|
|
|
ret = msvcrt_spawn(flags, fullname, args, envs);
|
2001-01-11 00:59:25 +01:00
|
|
|
MSVCRT_free(args);
|
|
|
|
}
|
2006-08-09 01:17:41 +02:00
|
|
|
MSVCRT_free(envs);
|
2001-01-11 00:59:25 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _spawnv (MSVCRT.@)
|
2001-09-11 01:07:07 +02:00
|
|
|
*
|
2002-06-01 01:06:46 +02:00
|
|
|
* Like on Windows, this function does not handle arguments with spaces
|
2001-09-11 01:07:07 +02:00
|
|
|
* or double-quotes.
|
2001-01-11 00:59:25 +01:00
|
|
|
*/
|
2006-06-13 11:21:19 +02:00
|
|
|
MSVCRT_intptr_t CDECL _spawnv(int flags, const char* name, const char* const* argv)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2001-04-10 23:16:07 +02:00
|
|
|
return _spawnve(flags, name, argv, NULL);
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _spawnvpe (MSVCRT.@)
|
2001-09-11 01:07:07 +02:00
|
|
|
*
|
2002-06-01 01:06:46 +02:00
|
|
|
* Like on Windows, this function does not handle arguments with spaces
|
2001-09-11 01:07:07 +02:00
|
|
|
* or double-quotes.
|
2001-01-11 00:59:25 +01:00
|
|
|
*/
|
2006-06-13 11:21:19 +02:00
|
|
|
MSVCRT_intptr_t CDECL _spawnvpe(int flags, const char* name, const char* const* argv,
|
|
|
|
const char* const* envv)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
char fullname[MAX_PATH];
|
2001-04-10 23:16:07 +02:00
|
|
|
_searchenv(name, "PATH", fullname);
|
|
|
|
return _spawnve(flags, fullname[0] ? fullname : name, argv, envv);
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _spawnvp (MSVCRT.@)
|
2001-09-11 01:07:07 +02:00
|
|
|
*
|
2002-06-01 01:06:46 +02:00
|
|
|
* Like on Windows, this function does not handle arguments with spaces
|
2001-09-11 01:07:07 +02:00
|
|
|
* or double-quotes.
|
2001-01-11 00:59:25 +01:00
|
|
|
*/
|
2006-06-13 11:21:19 +02:00
|
|
|
MSVCRT_intptr_t CDECL _spawnvp(int flags, const char* name, const char* const* argv)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2001-04-10 23:16:07 +02:00
|
|
|
return _spawnvpe(flags, name, argv, NULL);
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
2003-08-20 05:34:20 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* _popen (MSVCRT.@)
|
2004-12-14 16:13:54 +01:00
|
|
|
* FIXME: convert to _wpopen and call that from here instead? But it
|
|
|
|
* would have to convert the command back to ANSI to call msvcrt_spawn,
|
|
|
|
* less than ideal.
|
2003-08-20 05:34:20 +02:00
|
|
|
*/
|
2006-06-13 11:21:19 +02:00
|
|
|
MSVCRT_FILE* CDECL MSVCRT__popen(const char* command, const char* mode)
|
2003-08-20 05:34:20 +02:00
|
|
|
{
|
2006-09-04 06:13:08 +02:00
|
|
|
static const char wcmd[] = "cmd", cmdFlag[] = " /C ", comSpec[] = "COMSPEC";
|
2004-12-14 16:13:54 +01:00
|
|
|
MSVCRT_FILE *ret;
|
|
|
|
BOOL readPipe = TRUE;
|
|
|
|
int textmode, fds[2], fdToDup, fdToOpen, fdStdHandle = -1, fdStdErr = -1;
|
|
|
|
const char *p;
|
|
|
|
char *cmdcopy;
|
|
|
|
DWORD comSpecLen;
|
|
|
|
|
|
|
|
TRACE("(command=%s, mode=%s)\n", debugstr_a(command), debugstr_a(mode));
|
|
|
|
|
|
|
|
if (!command || !mode)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
textmode = *__p__fmode() & (MSVCRT__O_BINARY | MSVCRT__O_TEXT);
|
|
|
|
for (p = mode; *p; p++)
|
|
|
|
{
|
|
|
|
switch (*p)
|
|
|
|
{
|
|
|
|
case 'W':
|
|
|
|
case 'w':
|
|
|
|
readPipe = FALSE;
|
|
|
|
break;
|
|
|
|
case 'B':
|
|
|
|
case 'b':
|
|
|
|
textmode |= MSVCRT__O_BINARY;
|
|
|
|
textmode &= ~MSVCRT__O_TEXT;
|
|
|
|
break;
|
|
|
|
case 'T':
|
|
|
|
case 't':
|
|
|
|
textmode |= MSVCRT__O_TEXT;
|
|
|
|
textmode &= ~MSVCRT__O_BINARY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (_pipe(fds, 0, textmode) == -1)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
fdToDup = readPipe ? 1 : 0;
|
|
|
|
fdToOpen = readPipe ? 0 : 1;
|
|
|
|
|
|
|
|
if ((fdStdHandle = _dup(fdToDup)) == -1)
|
|
|
|
goto error;
|
|
|
|
if (_dup2(fds[fdToDup], fdToDup) != 0)
|
|
|
|
goto error;
|
|
|
|
if (readPipe)
|
|
|
|
{
|
|
|
|
if ((fdStdErr = _dup(MSVCRT_STDERR_FILENO)) == -1)
|
|
|
|
goto error;
|
|
|
|
if (_dup2(fds[fdToDup], MSVCRT_STDERR_FILENO) != 0)
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
_close(fds[fdToDup]);
|
|
|
|
|
|
|
|
comSpecLen = GetEnvironmentVariableA(comSpec, NULL, 0);
|
|
|
|
if (!comSpecLen)
|
|
|
|
comSpecLen = strlen(wcmd) + 1;
|
2005-03-24 22:01:35 +01:00
|
|
|
cmdcopy = HeapAlloc(GetProcessHeap(), 0, comSpecLen + strlen(cmdFlag)
|
2004-12-14 16:13:54 +01:00
|
|
|
+ strlen(command));
|
|
|
|
if (!GetEnvironmentVariableA(comSpec, cmdcopy, comSpecLen))
|
|
|
|
strcpy(cmdcopy, wcmd);
|
|
|
|
strcat(cmdcopy, cmdFlag);
|
|
|
|
strcat(cmdcopy, command);
|
|
|
|
if (msvcrt_spawn(MSVCRT__P_NOWAIT, NULL, cmdcopy, NULL) == -1)
|
|
|
|
{
|
|
|
|
_close(fds[fdToOpen]);
|
|
|
|
ret = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = MSVCRT__fdopen(fds[fdToOpen], mode);
|
|
|
|
if (!ret)
|
|
|
|
_close(fds[fdToOpen]);
|
|
|
|
}
|
|
|
|
HeapFree(GetProcessHeap(), 0, cmdcopy);
|
|
|
|
_dup2(fdStdHandle, fdToDup);
|
|
|
|
_close(fdStdHandle);
|
|
|
|
if (readPipe)
|
|
|
|
{
|
|
|
|
_dup2(fdStdErr, MSVCRT_STDERR_FILENO);
|
|
|
|
_close(fdStdErr);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
error:
|
|
|
|
if (fdStdHandle != -1) _close(fdStdHandle);
|
|
|
|
if (fdStdErr != -1) _close(fdStdErr);
|
|
|
|
_close(fds[0]);
|
|
|
|
_close(fds[1]);
|
2003-08-20 05:34:20 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _wpopen (MSVCRT.@)
|
|
|
|
*/
|
2006-06-13 11:21:19 +02:00
|
|
|
MSVCRT_FILE* CDECL MSVCRT__wpopen(const MSVCRT_wchar_t* command, const MSVCRT_wchar_t* mode)
|
2003-08-20 05:34:20 +02:00
|
|
|
{
|
|
|
|
FIXME("(command=%s, mode=%s): stub\n", debugstr_w(command), debugstr_w(mode));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _pclose (MSVCRT.@)
|
|
|
|
*/
|
2006-06-13 11:21:19 +02:00
|
|
|
int CDECL MSVCRT__pclose(MSVCRT_FILE* file)
|
2003-08-20 05:34:20 +02:00
|
|
|
{
|
2004-12-14 16:13:54 +01:00
|
|
|
return MSVCRT_fclose(file);
|
2003-08-20 05:34:20 +02:00
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* system (MSVCRT.@)
|
|
|
|
*/
|
2006-06-13 11:21:19 +02:00
|
|
|
int CDECL MSVCRT_system(const char* cmd)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2001-09-11 01:07:07 +02:00
|
|
|
char* cmdcopy;
|
|
|
|
int res;
|
|
|
|
|
|
|
|
/* Make a writable copy for CreateProcess */
|
|
|
|
cmdcopy=_strdup(cmd);
|
|
|
|
/* FIXME: should probably launch cmd interpreter in COMSPEC */
|
2004-06-25 03:19:15 +02:00
|
|
|
res=msvcrt_spawn(MSVCRT__P_WAIT, NULL, cmdcopy, NULL);
|
2001-09-11 01:07:07 +02:00
|
|
|
MSVCRT_free(cmdcopy);
|
|
|
|
return res;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _loaddll (MSVCRT.@)
|
|
|
|
*/
|
2006-06-13 11:21:19 +02:00
|
|
|
MSVCRT_intptr_t CDECL _loaddll(const char* dllname)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2005-11-03 14:17:51 +01:00
|
|
|
return (MSVCRT_intptr_t)LoadLibraryA(dllname);
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _unloaddll (MSVCRT.@)
|
|
|
|
*/
|
2006-06-13 11:21:19 +02:00
|
|
|
int CDECL _unloaddll(MSVCRT_intptr_t dll)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2002-10-04 02:27:10 +02:00
|
|
|
if (FreeLibrary((HMODULE)dll))
|
2001-01-11 00:59:25 +01:00
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int err = GetLastError();
|
2004-06-25 03:19:15 +02:00
|
|
|
msvcrt_set_errno(err);
|
2001-01-11 00:59:25 +01:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
2003-03-17 01:05:44 +01:00
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _getdllprocaddr (MSVCRT.@)
|
|
|
|
*/
|
2006-06-13 11:21:19 +02:00
|
|
|
void * CDECL _getdllprocaddr(MSVCRT_intptr_t dll, const char *name, int ordinal)
|
2003-03-17 01:05:44 +01:00
|
|
|
{
|
|
|
|
if (name)
|
|
|
|
{
|
|
|
|
if (ordinal != -1) return NULL;
|
|
|
|
return GetProcAddress( (HMODULE)dll, name );
|
|
|
|
}
|
|
|
|
if (HIWORD(ordinal)) return NULL;
|
|
|
|
return GetProcAddress( (HMODULE)dll, (LPCSTR)(ULONG_PTR)ordinal );
|
|
|
|
}
|