xref: /reactos/sdk/lib/ucrt/exec/spawnl.cpp (revision 04e0dc4a)
1 //
2 // spawnl.cpp
3 //
4 //      Copyright (c) Microsoft Corporation.  All rights reserved.
5 //
6 // Defines the -l and -le flavors of the _exec() and _spawn() functions.  See
7 // the comments in spawnv.cpp for details of the various flavors of these
8 // functions.
9 //
10 #include <corecrt_internal.h>
11 #include <corecrt_internal_traits.h>
12 #include <stddef.h>
13 #include <process.h>
14 #include <stdarg.h>
15 
16 
17 
18 template <typename Character>
common_spawnl(bool const pass_environment,int const mode,Character const * const file_name,Character const * const arguments,va_list varargs)19 static intptr_t __cdecl common_spawnl(
20     bool             const pass_environment,
21     int              const mode,
22     Character const* const file_name,
23     Character const* const arguments,
24     va_list                varargs
25     ) throw()
26 {
27     typedef __crt_char_traits<Character> traits;
28 
29     _VALIDATE_RETURN(file_name    != nullptr, EINVAL, -1);
30     _VALIDATE_RETURN(file_name[0] != '\0',    EINVAL, -1);
31     _VALIDATE_RETURN(arguments    != nullptr, EINVAL, -1);
32     _VALIDATE_RETURN(arguments[0] != '\0',    EINVAL, -1);
33 
34     Character* arguments_buffer[64];
35     Character** const captured_arguments = traits::capture_argv(
36         &varargs,
37         arguments,
38         &arguments_buffer[0],
39         64);
40 
41     _VALIDATE_RETURN_NOEXC(captured_arguments != nullptr, ENOMEM, -1);
42 
43     __crt_unique_heap_ptr<Character*> const captured_arguments_cleanup(
44         captured_arguments == arguments_buffer ? nullptr : captured_arguments);
45 
46     Character const* const* const environment = pass_environment
47         ? va_arg(varargs, Character const* const*)
48         : nullptr;
49 
50     return traits::tspawnve(mode, file_name, captured_arguments, environment);
51 }
52 
53 
54 
_execl(char const * const file_name,char const * const arguments,...)55 extern "C" intptr_t __cdecl _execl(
56     char const* const file_name,
57     char const* const arguments,
58     ...
59     )
60 {
61     va_list varargs;
62     va_start(varargs, arguments);
63     return common_spawnl(false, _P_OVERLAY, file_name, arguments, varargs);
64 }
65 
_execle(char const * const file_name,char const * const arguments,...)66 extern "C" intptr_t __cdecl _execle(
67     char const* const file_name,
68     char const* const arguments,
69     ...)
70 {
71     va_list varargs;
72     va_start(varargs, arguments);
73     return common_spawnl(true, _P_OVERLAY, file_name, arguments, varargs);
74 }
75 
_spawnl(int const mode,char const * const file_name,char const * const arguments,...)76 extern "C" intptr_t __cdecl _spawnl(
77     int         const mode,
78     char const* const file_name,
79     char const* const arguments,
80     ...
81     )
82 {
83     va_list varargs;
84     va_start(varargs, arguments);
85     return common_spawnl(false, mode, file_name, arguments, varargs);
86 }
87 
_spawnle(int const mode,char const * const file_name,char const * const arguments,...)88 extern "C" intptr_t __cdecl _spawnle(
89     int         const mode,
90     char const* const file_name,
91     char const* const arguments,
92     ...)
93 {
94     va_list varargs;
95     va_start(varargs, arguments);
96     return common_spawnl(true, mode, file_name, arguments, varargs);
97 }
98 
99 
100 
_wexecl(wchar_t const * const file_name,wchar_t const * const arguments,...)101 extern "C" intptr_t __cdecl _wexecl(
102     wchar_t const* const file_name,
103     wchar_t const* const arguments,
104     ...
105     )
106 {
107     va_list varargs;
108     va_start(varargs, arguments);
109     return common_spawnl(false, _P_OVERLAY, file_name, arguments, varargs);
110 }
111 
_wexecle(wchar_t const * const file_name,wchar_t const * const arguments,...)112 extern "C" intptr_t __cdecl _wexecle(
113     wchar_t const* const file_name,
114     wchar_t const* const arguments,
115     ...)
116 {
117     va_list varargs;
118     va_start(varargs, arguments);
119     return common_spawnl(true, _P_OVERLAY, file_name, arguments, varargs);
120 }
121 
_wspawnl(int const mode,wchar_t const * const file_name,wchar_t const * const arguments,...)122 extern "C" intptr_t __cdecl _wspawnl(
123     int            const mode,
124     wchar_t const* const file_name,
125     wchar_t const* const arguments,
126     ...
127     )
128 {
129     va_list varargs;
130     va_start(varargs, arguments);
131     return common_spawnl(false, mode, file_name, arguments, varargs);
132 }
133 
_wspawnle(int const mode,wchar_t const * const file_name,wchar_t const * const arguments,...)134 extern "C" intptr_t __cdecl _wspawnle(
135     int            const mode,
136     wchar_t const* const file_name,
137     wchar_t const* const arguments,
138     ...)
139 {
140     va_list varargs;
141     va_start(varargs, arguments);
142     return common_spawnl(true, mode, file_name, arguments, varargs);
143 }
144