2 * msvcrt.dll spawn/exec functions
4 * Copyright 1996,1998 Marcus Meissner
5 * Copyright 1996 Jukka Iivonen
6 * Copyright 1997,2000 Uwe Bonnes
7 * Copyright 2000 Jon Griffiths
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * -File handles need some special handling. Sometimes children get
25 * open file handles, sometimes not. The docs are confusing
26 * -No check for maximum path/argument/environment size is done
33 #include "msvcrt/errno.h"
35 #include "msvcrt/stdio.h"
36 #include "msvcrt/process.h"
37 #include "msvcrt/stdlib.h"
38 #include "msvcrt/string.h"
40 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
44 /* INTERNAL: Spawn a child process */
45 static int msvcrt_spawn(int flags, const char* exe, char* cmdline, char* env)
48 PROCESS_INFORMATION pi;
50 if (sizeof(HANDLE) != sizeof(int))
51 WARN("This call is unsuitable for your architecture\n");
53 if ((unsigned)flags > _P_DETACH)
55 *MSVCRT__errno() = MSVCRT_EINVAL;
59 FIXME(":must dup/kill streams for child process\n");
61 memset(&si, 0, sizeof(si));
64 if (!CreateProcessA(exe, cmdline, NULL, NULL, TRUE,
65 flags == _P_DETACH ? DETACHED_PROCESS : 0,
68 MSVCRT__set_errno(GetLastError());
75 WaitForSingleObject(pi.hProcess,-1); /* wait forvever */
76 GetExitCodeProcess(pi.hProcess,&pi.dwProcessId);
77 CloseHandle(pi.hProcess);
78 CloseHandle(pi.hThread);
79 return (int)pi.dwProcessId;
81 CloseHandle(pi.hProcess);
86 CloseHandle(pi.hThread);
87 return (int)pi.hProcess;
91 return -1; /* can't reach here */
94 /* INTERNAL: Convert argv list to a single 'delim'-separated string, with an
95 * extra '\0' to terminate it
97 static char* msvcrt_argvtos(const char* const* arg, char delim)
106 /* Return NULL for an empty environment list */
115 size += strlen(*a) + 1;
119 ret = (char*)MSVCRT_malloc(size + 1);
128 int len = strlen(*a);
134 if (delim && p > ret) p[-1] = 0;
139 /* INTERNAL: Convert va_list to a single 'delim'-separated string, with an
140 * extra '\0' to terminate it
142 static char* msvcrt_valisttos(const char* arg0, va_list alist, char delim)
151 va_copy(alist2,alist);
153 # ifdef HAVE___VA_COPY
154 __va_copy(alist2,alist);
162 /* Return NULL for an empty environment list */
170 size += strlen(arg) + 1;
171 arg = va_arg(alist, char*);
172 } while (arg != NULL);
174 ret = (char*)MSVCRT_malloc(size + 1);
182 int len = strlen(arg);
186 arg = va_arg(alist2, char*);
187 } while (arg != NULL);
188 if (delim && p > ret) p[-1] = 0;
193 /*********************************************************************
196 int _cwait(int *status, int pid, int action)
198 HANDLE hPid = (HANDLE)pid;
201 action = action; /* Remove warning */
203 if (!WaitForSingleObject(hPid, -1)) /* wait forever */
208 GetExitCodeProcess(hPid, &stat);
213 doserrno = GetLastError();
215 if (doserrno == ERROR_INVALID_HANDLE)
217 *MSVCRT__errno() = MSVCRT_ECHILD;
218 *__doserrno() = doserrno;
221 MSVCRT__set_errno(doserrno);
223 return status ? *status = -1 : -1;
226 /*********************************************************************
229 * Like on Windows, this function does not handle arguments with spaces
232 int _execl(const char* name, const char* arg0, ...)
239 args = msvcrt_valisttos(arg0, ap, ' ');
242 ret = msvcrt_spawn(_P_OVERLAY, name, args, NULL);
248 /*********************************************************************
251 * Like on Windows, this function does not handle arguments with spaces
254 int _execlp(const char* name, const char* arg0, ...)
259 char fullname[MAX_PATH];
261 _searchenv(name, "PATH", fullname);
264 args = msvcrt_valisttos(arg0, ap, ' ');
267 ret = msvcrt_spawn(_P_OVERLAY, fullname[0] ? fullname : name, args, NULL);
273 /*********************************************************************
276 * Like on Windows, this function does not handle arguments with spaces
279 int _execv(const char* name, char* const* argv)
281 return _spawnve(_P_OVERLAY, name, (const char* const*) argv, NULL);
284 /*********************************************************************
287 * Like on Windows, this function does not handle arguments with spaces
290 int _execve(const char* name, char* const* argv, const char* const* envv)
292 return _spawnve(_P_OVERLAY, name, (const char* const*) argv, envv);
295 /*********************************************************************
296 * _execvpe (MSVCRT.@)
298 * Like on Windows, this function does not handle arguments with spaces
301 int _execvpe(const char* name, char* const* argv, const char* const* envv)
303 char fullname[MAX_PATH];
305 _searchenv(name, "PATH", fullname);
306 return _spawnve(_P_OVERLAY, fullname[0] ? fullname : name,
307 (const char* const*) argv, envv);
310 /*********************************************************************
313 * Like on Windows, this function does not handle arguments with spaces
316 int _execvp(const char* name, char* const* argv)
318 return _execvpe(name, argv, NULL);
321 /*********************************************************************
324 * Like on Windows, this function does not handle arguments with spaces
327 int _spawnl(int flags, const char* name, const char* arg0, ...)
334 args = msvcrt_valisttos(arg0, ap, ' ');
337 ret = msvcrt_spawn(flags, name, args, NULL);
343 /*********************************************************************
344 * _spawnlp (MSVCRT.@)
346 * Like on Windows, this function does not handle arguments with spaces
349 int _spawnlp(int flags, const char* name, const char* arg0, ...)
354 char fullname[MAX_PATH];
356 _searchenv(name, "PATH", fullname);
359 args = msvcrt_valisttos(arg0, ap, ' ');
362 ret = msvcrt_spawn(flags, fullname[0] ? fullname : name, args, NULL);
368 /*********************************************************************
369 * _spawnve (MSVCRT.@)
371 * Like on Windows, this function does not handle arguments with spaces
374 int _spawnve(int flags, const char* name, const char* const* argv,
375 const char* const* envv)
377 char * args = msvcrt_argvtos(argv,' ');
378 char * envs = msvcrt_argvtos(envv,0);
379 const char *fullname = name;
382 FIXME(":not translating name %s to locate program\n",fullname);
383 TRACE(":call (%s), params (%s), env (%s)\n",name,args,envs?"Custom":"Null");
387 ret = msvcrt_spawn(flags, fullname, args, envs);
396 /*********************************************************************
399 * Like on Windows, this function does not handle arguments with spaces
402 int _spawnv(int flags, const char* name, const char* const* argv)
404 return _spawnve(flags, name, argv, NULL);
407 /*********************************************************************
408 * _spawnvpe (MSVCRT.@)
410 * Like on Windows, this function does not handle arguments with spaces
413 int _spawnvpe(int flags, const char* name, const char* const* argv,
414 const char* const* envv)
416 char fullname[MAX_PATH];
417 _searchenv(name, "PATH", fullname);
418 return _spawnve(flags, fullname[0] ? fullname : name, argv, envv);
421 /*********************************************************************
422 * _spawnvp (MSVCRT.@)
424 * Like on Windows, this function does not handle arguments with spaces
427 int _spawnvp(int flags, const char* name, const char* const* argv)
429 return _spawnvpe(flags, name, argv, NULL);
432 /*********************************************************************
435 MSVCRT_FILE* _popen(const char* command, const char* mode)
437 FIXME("(command=%s, mode=%s): stub\n", debugstr_a(command), debugstr_a(mode));
441 /*********************************************************************
444 MSVCRT_FILE* _wpopen(const MSVCRT_wchar_t* command, const MSVCRT_wchar_t* mode)
446 FIXME("(command=%s, mode=%s): stub\n", debugstr_w(command), debugstr_w(mode));
450 /*********************************************************************
453 int _pclose(MSVCRT_FILE* file)
455 FIXME("(file=%p): stub\n", file);
459 /*********************************************************************
462 int MSVCRT_system(const char* cmd)
467 /* Make a writable copy for CreateProcess */
468 cmdcopy=_strdup(cmd);
469 /* FIXME: should probably launch cmd interpreter in COMSPEC */
470 res=msvcrt_spawn(_P_WAIT, NULL, cmdcopy, NULL);
471 MSVCRT_free(cmdcopy);
475 /*********************************************************************
476 * _loaddll (MSVCRT.@)
478 int _loaddll(const char* dllname)
480 return (int)LoadLibraryA(dllname);
483 /*********************************************************************
484 * _unloaddll (MSVCRT.@)
486 int _unloaddll(int dll)
488 if (FreeLibrary((HMODULE)dll))
492 int err = GetLastError();
493 MSVCRT__set_errno(err);
498 /*********************************************************************
499 * _getdllprocaddr (MSVCRT.@)
501 void *_getdllprocaddr(int dll, const char *name, int ordinal)
505 if (ordinal != -1) return NULL;
506 return GetProcAddress( (HMODULE)dll, name );
508 if (HIWORD(ordinal)) return NULL;
509 return GetProcAddress( (HMODULE)dll, (LPCSTR)(ULONG_PTR)ordinal );