xref: /reactos/sdk/lib/ucrt/exec/spawnlp.cpp (revision b09b5584)
1 //
2 // spawnlp.cpp
3 //
4 //      Copyright (c) Microsoft Corporation.  All rights reserved.
5 //
6 // Defines the -lp and -lpe 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>
19 static intptr_t __cdecl common_spawnlp(
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::tspawnvpe(mode, file_name, captured_arguments, environment);
51 }
52 
53 
54 
55 extern "C" intptr_t _execlp(
56     char const* const file_name,
57     char const* const arguments,
58     ...)
59 {
60     va_list varargs;
61     va_start(varargs, arguments);
62     return common_spawnlp(false, _P_OVERLAY, file_name, arguments, varargs);
63 }
64 
65 extern "C" intptr_t _execlpe(
66     char const* const file_name,
67     char const* const arguments,
68     ...)
69 {
70     va_list varargs;
71     va_start(varargs, arguments);
72     return common_spawnlp(true, _P_OVERLAY, file_name, arguments, varargs);
73 }
74 
75 extern "C" intptr_t _spawnlp(
76     int         const mode,
77     char const* const file_name,
78     char const* const arguments,
79     ...)
80 {
81     va_list varargs;
82     va_start(varargs, arguments);
83     return common_spawnlp(false, mode, file_name, arguments, varargs);
84 }
85 
86 extern "C" intptr_t _spawnlpe(
87     int         const mode,
88     char const* const file_name,
89     char const* const arguments,
90     ...)
91 {
92     va_list varargs;
93     va_start(varargs, arguments);
94     return common_spawnlp(true, mode, file_name, arguments, varargs);
95 }
96 
97 
98 
99 extern "C" intptr_t _wexeclp(
100     wchar_t const* const file_name,
101     wchar_t const* const arguments,
102     ...)
103 {
104     va_list varargs;
105     va_start(varargs, arguments);
106     return common_spawnlp(false, _P_OVERLAY, file_name, arguments, varargs);
107 }
108 
109 extern "C" intptr_t _wexeclpe(
110     wchar_t const* const file_name,
111     wchar_t const* const arguments,
112     ...)
113 {
114     va_list varargs;
115     va_start(varargs, arguments);
116     return common_spawnlp(true, _P_OVERLAY, file_name, arguments, varargs);
117 }
118 
119 extern "C" intptr_t _wspawnlp(
120     int            const mode,
121     wchar_t const* const file_name,
122     wchar_t const* const arguments,
123     ...)
124 {
125     va_list varargs;
126     va_start(varargs, arguments);
127     return common_spawnlp(false, mode, file_name, arguments, varargs);
128 }
129 
130 extern "C" intptr_t _wspawnlpe(
131     int            const mode,
132     wchar_t const* const file_name,
133     wchar_t const* const arguments,
134     ...)
135 {
136     va_list varargs;
137     va_start(varargs, arguments);
138     return common_spawnlp(true, mode, file_name, arguments, varargs);
139 }
140