|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| #include <corecrt_internal.h>
|
| #include <corecrt_internal_traits.h>
|
| #include <errno.h>
|
| #include <stdlib.h>
|
| #include <string.h>
|
| #include <malloc.h>
|
| #include <process.h>
|
| #include <mbstring.h>
|
|
|
|
|
|
|
| template <typename Character>
|
| static intptr_t __cdecl common_spawnvp(
|
| int const mode,
|
| Character const* const file_name,
|
| Character const* const* const arguments,
|
| Character const* const* const environment
|
| ) throw()
|
| {
|
| typedef __crt_char_traits<Character> traits;
|
|
|
| _VALIDATE_RETURN(file_name != nullptr, EINVAL, -1);
|
| _VALIDATE_RETURN(file_name[0] != '\0', EINVAL, -1);
|
| _VALIDATE_RETURN(arguments != nullptr, EINVAL, -1);
|
| _VALIDATE_RETURN(arguments[0] != nullptr, EINVAL, -1);
|
| _VALIDATE_RETURN(arguments[0][0] != '\0', EINVAL, -1);
|
|
|
| __crt_errno_guard const guard_errno;
|
|
|
|
|
|
|
|
|
|
|
| intptr_t const initial_result = traits::tspawnve(mode, file_name, arguments, environment);
|
| if (initial_result != -1)
|
| return initial_result;
|
|
|
|
|
|
|
| if (errno != ENOENT)
|
| return -1;
|
|
|
|
|
|
|
| if (traits::tcschr(file_name, '\\') != nullptr)
|
| return -1;
|
|
|
| if (traits::tcschr(file_name, '/') != nullptr)
|
| return -1;
|
|
|
| if (file_name[1] == ':')
|
| return -1;
|
|
|
|
|
|
|
|
|
| Character const path_name[] = { 'P', 'A', 'T', 'H', '\0' };
|
| __crt_unique_heap_ptr<Character> path_value;
|
| if (_ERRCHECK_EINVAL(traits::tdupenv_s_crt(path_value.get_address_of(), nullptr, path_name)) != 0)
|
| return -1;
|
|
|
| if (!path_value)
|
| return -1;
|
|
|
|
|
| __crt_unique_heap_ptr<Character> const owned_file_buffer(_calloc_crt_t(Character, _MAX_PATH));
|
| if (!owned_file_buffer)
|
| return -1;
|
|
|
| Character* file_buffer = owned_file_buffer.get();
|
| Character* path_state = path_value.get();
|
| while ((path_state = traits::tgetpath(path_state, file_buffer, _MAX_PATH - 1)) != 0)
|
| {
|
| if (!*file_buffer)
|
| break;
|
|
|
|
|
| Character* const last_character_it = file_buffer + traits::tcslen(file_buffer) - 1;
|
| if (last_character_it != traits::tcsrchr(file_buffer, '\\') &&
|
| last_character_it != traits::tcsrchr(file_buffer, '/'))
|
| {
|
| Character const backslash_string[] = { '\\', '\0' };
|
| _ERRCHECK(traits::tcscat_s(file_buffer, _MAX_PATH, backslash_string));
|
| }
|
|
|
|
|
|
|
|
|
|
|
| if (traits::tcslen(file_buffer) + traits::tcslen(file_name) >= _MAX_PATH)
|
| break;
|
|
|
| _ERRCHECK(traits::tcscat_s(file_buffer, _MAX_PATH, file_name));
|
|
|
|
|
|
|
|
|
|
|
| errno = 0;
|
| intptr_t const result = traits::tspawnve(mode, file_buffer, arguments, environment);
|
| if (result != -1)
|
| return result;
|
|
|
|
|
|
|
|
|
| if (errno == ENOENT || _doserrno == ERROR_NOT_READY)
|
| continue;
|
|
|
|
|
| bool const is_unc_path_with_slashes =
|
| traits::tcschr(file_buffer, '/') == file_buffer &&
|
| traits::tcschr(file_buffer + 1, '/') == file_buffer + 1;
|
|
|
| bool const is_unc_path_with_backslashes =
|
| traits::tcschr(file_buffer, '\\') == file_buffer &&
|
| traits::tcschr(file_buffer + 1, '\\') == file_buffer + 1;
|
|
|
| if (is_unc_path_with_slashes || is_unc_path_with_backslashes)
|
| continue;
|
|
|
|
|
| break;
|
| }
|
|
|
| return -1;
|
| }
|
|
|
|
|
|
|
| extern "C" intptr_t __cdecl _execvp(
|
| char const* const file_name,
|
| char const* const* const arguments
|
| )
|
| {
|
| return common_spawnvp(_P_OVERLAY, file_name, arguments, static_cast<char const* const* const>(nullptr));
|
| }
|
|
|
| extern "C" intptr_t __cdecl _execvpe(
|
| char const* const file_name,
|
| char const* const* const arguments,
|
| char const* const* const environment
|
| )
|
| {
|
| return common_spawnvp(_P_OVERLAY, file_name, arguments, environment);
|
| }
|
|
|
| extern "C" intptr_t __cdecl _spawnvp(
|
| int const mode,
|
| char const* const file_name,
|
| char const* const* const arguments
|
| )
|
| {
|
| return common_spawnvp(mode, file_name, arguments, static_cast<char const* const* const>(nullptr));
|
| }
|
|
|
| extern "C" intptr_t __cdecl _spawnvpe(
|
| int const mode,
|
| char const* const file_name,
|
| char const* const* const arguments,
|
| char const* const* const environment
|
| )
|
| {
|
| return common_spawnvp(mode, file_name, arguments, environment);
|
| }
|
|
|
|
|
|
|
| extern "C" intptr_t __cdecl _wexecvp(
|
| wchar_t const* const file_name,
|
| wchar_t const* const* const arguments
|
| )
|
| {
|
| return common_spawnvp(_P_OVERLAY, file_name, arguments, static_cast<wchar_t const* const* const>(nullptr));
|
| }
|
|
|
| extern "C" intptr_t __cdecl _wexecvpe(
|
| wchar_t const* const file_name,
|
| wchar_t const* const* const arguments,
|
| wchar_t const* const* const environment
|
| )
|
| {
|
| return common_spawnvp(_P_OVERLAY, file_name, arguments, environment);
|
| }
|
|
|
| extern "C" intptr_t __cdecl _wspawnvp(
|
| int const mode,
|
| wchar_t const* const file_name,
|
| wchar_t const* const* const arguments
|
| )
|
| {
|
| return common_spawnvp(mode, file_name, arguments, static_cast<wchar_t const* const* const>(nullptr));
|
| }
|
|
|
| extern "C" intptr_t __cdecl _wspawnvpe(
|
| int const mode,
|
| wchar_t const* const file_name,
|
| wchar_t const* const* const arguments,
|
| wchar_t const* const* const environment
|
| )
|
| {
|
| return common_spawnvp(mode, file_name, arguments, environment);
|
| }
|
|
|