6 #include <internal/debug.h>
9 #define find_execT find_execW
10 #define argvtosT argvtosW
11 #define do_spawnT do_spawnW
12 #define valisttosT valisttosW
15 #define find_execT find_execA
16 #define argvtosT argvtosA
17 #define do_spawnT do_spawnA
18 #define valisttosT valisttosA
23 _TCHAR
const* extT
[] =
32 const _TCHAR
* find_execT(const _TCHAR
* path
, _TCHAR
* rpath
)
36 unsigned int i
, found
= 0;
38 DPRINT(MK_STR(find_execT
)"('%"sT
"', %x)\n", path
, rpath
);
44 if (_tcslen(path
) > FILENAME_MAX
- 1)
48 /* copy path in rpath */
49 for (rd
= path
, rp
= rpath
; *rd
; *rp
++ = *rd
++)
52 /* try first with the name as is */
53 for (i
= 0; i
< sizeof(extT
) / sizeof(*extT
); i
++)
57 DPRINT("trying '%"sT
"'\n", rpath
);
59 if (_taccess(rpath
, F_OK
) == 0 && access_dirT(rpath
) != 0)
67 _TCHAR
* env
= _tgetenv(_T("PATH"));
76 for (; *ep
&& (*ep
!= ';'); *rp
++ = *ep
++)
81 if (*rp
!= '/' && *rp
!= '\\')
87 for (rd
=path
; *rd
; *rp
++ = *rd
++)
89 for (i
= 0; i
< sizeof(extT
) / sizeof(*extT
); i
++)
93 DPRINT("trying '%"sT
"'\n", rpath
);
95 if (_taccess(rpath
, F_OK
) == 0 && access_dirT(rpath
) != 0)
105 return found
? rpath
: path
;
109 argvtosT(const _TCHAR
* const* argv
, _TCHAR delim
)
117 for (i
= 0, len
= 0; argv
[i
]; i
++)
119 len
+= _tcslen(argv
[i
]) + 1;
122 str
= ptr
= (_TCHAR
*) malloc(len
+ 1);
126 for(i
= 0; argv
[i
]; i
++)
128 len
= _tcslen(argv
[i
]);
129 memcpy(ptr
, argv
[i
], len
);
139 valisttosT(const _TCHAR
* arg0
, va_list alist
, _TCHAR delim
)
141 va_list alist2
= alist
;
152 len
+= _tcslen(ptr
) + 1;
153 ptr
= va_arg(alist
, _TCHAR
*);
157 str
= (_TCHAR
*) malloc(len
+ 1);
165 memcpy(ptr
, arg0
, len
);
168 arg0
= va_arg(alist2
, _TCHAR
*);
177 do_spawnT(int mode
, const _TCHAR
* cmdname
, const _TCHAR
* args
, const _TCHAR
* envp
)
179 STARTUPINFO StartupInfo
= {0};
180 PROCESS_INFORMATION ProcessInformation
;
188 TRACE(MK_STR(do_spawnT
)"(%i,'%"sT
"','%"sT
"','%"sT
"')",mode
,cmdname
,args
,envp
);
191 if (mode
!= _P_NOWAIT
&& mode
!= _P_NOWAITO
&& mode
!= _P_WAIT
&& mode
!= _P_DETACH
&& mode
!= _P_OVERLAY
)
193 __set_errno ( EINVAL
);
197 if (0 != _taccess(cmdname
, F_OK
))
199 __set_errno ( ENOENT
);
202 if (0 == access_dirT(cmdname
))
204 __set_errno ( EISDIR
);
208 //memset (&StartupInfo, 0, sizeof(StartupInfo));
209 StartupInfo
.cb
= sizeof(StartupInfo
);
213 for (last
= i
= 0; i
< FDINFO_FD_MAX
; i
++)
215 if ((void*)-1 != _get_osfhandle(i
))
223 StartupInfo
.cbReserved2
= sizeof(ULONG
) + last
* (sizeof(char) + sizeof(HANDLE
));
224 StartupInfo
.lpReserved2
= malloc(StartupInfo
.cbReserved2
);
225 if (StartupInfo
.lpReserved2
== NULL
)
227 __set_errno ( ENOMEM
);
231 *(DWORD
*)StartupInfo
.lpReserved2
= last
;
232 fmode
= (char*)(StartupInfo
.lpReserved2
+ sizeof(ULONG
));
233 hFile
= (HANDLE
*)(StartupInfo
.lpReserved2
+ sizeof(ULONG
) + last
* sizeof(char));
234 for (i
= 0; i
< last
; i
++)
236 int _mode
= __fileno_getmode(i
);
237 HANDLE h
= _get_osfhandle(i
);
238 /* FIXME: The test of console handles (((ULONG)Handle) & 0x10000003) == 0x3)
241 if ((((ULONG
)h
) & 0x10000003) == 0x3 || _mode
& _O_NOINHERIT
|| (i
< 3 && mode
== _P_DETACH
))
243 *hFile
= INVALID_HANDLE_VALUE
;
250 bFlag
= GetHandleInformation(h
, &dwFlags
);
251 if (bFlag
&& (dwFlags
& HANDLE_FLAG_INHERIT
))
254 *fmode
= (_O_ACCMODE
& _mode
) | (((_O_TEXT
| _O_BINARY
) & _mode
) >> 8);
258 *hFile
= INVALID_HANDLE_VALUE
;
268 create_io_inherit_block((STARTUPINFOA
*) &StartupInfo
);
270 bResult
= CreateProcess((_TCHAR
*)cmdname
,
275 mode
== _P_DETACH
? DETACHED_PROCESS
: 0,
279 &ProcessInformation
);
281 if (StartupInfo
.lpReserved2
)
283 free(StartupInfo
.lpReserved2
);
288 dwError
= GetLastError();
289 DPRINT("%x\n", dwError
);
290 __set_errno(dwError
);
293 CloseHandle(ProcessInformation
.hThread
);
298 return((int)ProcessInformation
.hProcess
);
300 CloseHandle(ProcessInformation
.hProcess
);
303 WaitForSingleObject(ProcessInformation
.hProcess
, INFINITE
);
304 GetExitCodeProcess(ProcessInformation
.hProcess
, &dwExitCode
);
305 CloseHandle(ProcessInformation
.hProcess
);
306 return( (int)dwExitCode
); //CORRECT?
308 CloseHandle(ProcessInformation
.hProcess
);
311 return( (int)ProcessInformation
.hProcess
);
317 int _tspawnl(int mode
, const _TCHAR
*cmdname
, const _TCHAR
* arg0
, ...)
323 DPRINT(MK_STR(_tspawnl
)"('%"sT
"')\n", cmdname
);
325 va_start(argp
, arg0
);
326 args
= valisttosT(arg0
, argp
, ' ');
330 ret
= do_spawnT(mode
, cmdname
, args
, NULL
);
339 int _tspawnv(int mode
, const _TCHAR
*cmdname
, const _TCHAR
* const* argv
)
344 DPRINT(MK_STR(_tspawnv
)"('%"sT
"')\n", cmdname
);
346 args
= argvtosT(argv
, ' ');
350 ret
= do_spawnT(mode
, cmdname
, args
, NULL
);
359 int _tspawnle(int mode
, const _TCHAR
*cmdname
, const _TCHAR
* arg0
, ... /*, NULL, const char* const* envp*/)
364 _TCHAR
const * const* ptr
;
367 DPRINT(MK_STR(_tspawnle
)"('%"sT
"')\n", cmdname
);
369 va_start(argp
, arg0
);
370 args
= valisttosT(arg0
, argp
, ' ');
373 ptr
= (_TCHAR
const* const*)va_arg(argp
, _TCHAR
*);
376 ptr
= (_TCHAR
const* const*)va_arg(argp
, _TCHAR
*);
377 envs
= argvtosT(ptr
, 0);
380 ret
= do_spawnT(mode
, cmdname
, args
, envs
);
394 int _tspawnve(int mode
, const _TCHAR
*cmdname
, const _TCHAR
* const* argv
, const _TCHAR
* const* envp
)
400 DPRINT(MK_STR(_tspawnve
)"('%"sT
"')\n", cmdname
);
402 args
= argvtosT(argv
, ' ');
403 envs
= argvtosT(envp
, 0);
407 ret
= do_spawnT(mode
, cmdname
, args
, envs
);
420 int _tspawnvp(int mode
, const _TCHAR
* cmdname
, const _TCHAR
* const* argv
)
422 _TCHAR pathname
[FILENAME_MAX
];
424 DPRINT(MK_STR(_tspawnvp
)"('%"sT
"')\n", cmdname
);
426 return _tspawnv(mode
, find_execT(cmdname
, pathname
), argv
);
432 int _tspawnlp(int mode
, const _TCHAR
* cmdname
, const _TCHAR
* arg0
, .../*, NULL*/)
437 _TCHAR pathname
[FILENAME_MAX
];
439 DPRINT(MK_STR(_tspawnlp
)"('%"sT
"')\n", cmdname
);
441 va_start(argp
, arg0
);
442 args
= valisttosT(arg0
, argp
, ' ');
445 ret
= do_spawnT(mode
, find_execT(cmdname
, pathname
), args
, NULL
);
455 int _tspawnlpe(int mode
, const _TCHAR
* cmdname
, const _TCHAR
* arg0
, .../*, NULL, const char* const* envp*/)
460 _TCHAR
const* const * ptr
;
462 _TCHAR pathname
[FILENAME_MAX
];
464 DPRINT(MK_STR(_tspawnlpe
)"('%"sT
"')\n", cmdname
);
466 va_start(argp
, arg0
);
467 args
= valisttosT(arg0
, argp
, ' ');
470 ptr
= (_TCHAR
const* const*)va_arg(argp
, _TCHAR
*);
473 ptr
= (_TCHAR
const* const*)va_arg(argp
, _TCHAR
*);
474 envs
= argvtosT(ptr
, 0);
477 ret
= do_spawnT(mode
, find_execT(cmdname
, pathname
), args
, envs
);
490 int _tspawnvpe(int mode
, const _TCHAR
* cmdname
, const _TCHAR
* const* argv
, const _TCHAR
* const* envp
)
492 _TCHAR pathname
[FILENAME_MAX
];
494 DPRINT(MK_STR(_tspawnvpe
)"('%"sT
"')\n", cmdname
);
496 return _tspawnve(mode
, find_execT(cmdname
, pathname
), argv
, envp
);
502 int _texecl(const _TCHAR
* cmdname
, const _TCHAR
* arg0
, ...)
508 DPRINT(MK_STR(_texecl
)"('%"sT
"')\n", cmdname
);
510 va_start(argp
, arg0
);
511 args
= valisttosT(arg0
, argp
, ' ');
515 ret
= do_spawnT(P_OVERLAY
, cmdname
, args
, NULL
);
524 int _texecv(const _TCHAR
* cmdname
, const _TCHAR
* const* argv
)
526 DPRINT(MK_STR(_texecv
)"('%"sT
"')\n", cmdname
);
527 return _tspawnv(P_OVERLAY
, cmdname
, argv
);
533 int _texecle(const _TCHAR
* cmdname
, const _TCHAR
* arg0
, ... /*, NULL, char* const* envp */)
538 _TCHAR
const* const* ptr
;
541 DPRINT(MK_STR(_texecle
)"('%"sT
"')\n", cmdname
);
543 va_start(argp
, arg0
);
544 args
= valisttosT(arg0
, argp
, ' ');
547 ptr
= (_TCHAR
const* const*)va_arg(argp
, _TCHAR
*);
550 ptr
= (_TCHAR
const* const*)va_arg(argp
, _TCHAR
*);
551 envs
= argvtosT(ptr
, 0);
554 ret
= do_spawnT(P_OVERLAY
, cmdname
, args
, envs
);
567 int _texecve(const _TCHAR
* cmdname
, const _TCHAR
* const* argv
, const _TCHAR
* const* envp
)
569 DPRINT(MK_STR(_texecve
)"('%"sT
"')\n", cmdname
);
570 return _tspawnve(P_OVERLAY
, cmdname
, argv
, envp
);
576 int _texeclp(const _TCHAR
* cmdname
, const _TCHAR
* arg0
, ...)
581 _TCHAR pathname
[FILENAME_MAX
];
583 DPRINT(MK_STR(_texeclp
)"('%"sT
"')\n", cmdname
);
585 va_start(argp
, arg0
);
586 args
= valisttosT(arg0
, argp
, ' ');
590 ret
= do_spawnT(P_OVERLAY
, find_execT(cmdname
, pathname
), args
, NULL
);
599 int _texecvp(const _TCHAR
* cmdname
, const _TCHAR
* const* argv
)
601 DPRINT(MK_STR(_texecvp
)"('%"sT
"')\n", cmdname
);
602 return _tspawnvp(P_OVERLAY
, cmdname
, argv
);
608 int _texeclpe(const _TCHAR
* cmdname
, const _TCHAR
* arg0
, ... /*, NULL, char* const* envp */)
613 _TCHAR
const* const* ptr
;
615 _TCHAR pathname
[FILENAME_MAX
];
617 DPRINT(MK_STR(_texeclpe
)"('%"sT
"')\n", cmdname
);
619 va_start(argp
, arg0
);
620 args
= valisttosT(arg0
, argp
, ' ');
623 ptr
= (_TCHAR
const* const*)va_arg(argp
, _TCHAR
*);
626 ptr
= (_TCHAR
const* const*)va_arg(argp
, _TCHAR
*);
627 envs
= argvtosT(ptr
, 0);
630 ret
= do_spawnT(P_OVERLAY
, find_execT(cmdname
, pathname
), args
, envs
);
643 int _texecvpe(const _TCHAR
* cmdname
, const _TCHAR
* const* argv
, const _TCHAR
* const* envp
)
645 DPRINT(MK_STR(_texecvpe
)"('%"sT
"')\n", cmdname
);
646 return _tspawnvpe(P_OVERLAY
, cmdname
, argv
, envp
);