ReactOS 0.4.16-dev-927-g467dec4
system.cpp File Reference
#include <corecrt_internal.h>
#include <corecrt_internal_traits.h>
#include <process.h>
#include <io.h>
#include <stdlib.h>
#include <errno.h>
Include dependency graph for system.cpp:

Go to the source code of this file.

Functions

template<typename Character >
static int __cdecl common_system (Character const *const command) throw ()
 
int __cdecl system (char const *const command)
 
int __cdecl _wsystem (wchar_t const *const command)
 

Function Documentation

◆ _wsystem()

int __cdecl _wsystem ( wchar_t const *const  command)

Definition at line 83 of file system.cpp.

84{
85 return common_system(command);
86}
static int __cdecl common_system(Character const *const command)
Definition: system.cpp:22

◆ common_system()

template<typename Character >
static int __cdecl common_system ( Character const *const  command)
throw (
)
static

Definition at line 22 of file system.cpp.

23{
24 typedef __crt_char_traits<Character> traits;
25
26 static Character const comspec_name[] = { 'C', 'O', 'M', 'S', 'P', 'E', 'C', '\0' };
27 static Character const cmd_exe[] = { 'c', 'm', 'd', '.', 'e', 'x', 'e', '\0' };
28 static Character const slash_c[] = { '/', 'c', '\0' };
29
30 __crt_unique_heap_ptr<Character> comspec_value;
31 _ERRCHECK_EINVAL(traits::tdupenv_s_crt(comspec_value.get_address_of(), nullptr, comspec_name));
32
33 // If the command is null, return TRUE only if %COMSPEC% is set and the file
34 // to which it points exists.
35 if (!command)
36 {
37 if (!comspec_value)
38 return 0;
39
40 return traits::taccess_s(comspec_value.get(), 0) == 0;
41 }
42
43 _ASSERTE(command[0] != '\0');
44
45 Character const* arguments[4] =
46 {
47 comspec_value.get(),
48 slash_c,
49 command,
50 nullptr
51 };
52
53 if (comspec_value)
54 {
55 errno_t const saved_errno = errno;
56 errno = 0;
57
58 int const result = static_cast<int>(traits::tspawnve(_P_WAIT, arguments[0], arguments, nullptr));
59 if (result != -1)
60 {
61 errno = saved_errno;
62 return result;
63 }
64
65 if (errno != ENOENT && errno != EACCES)
66 {
67 return result;
68 }
69
70 // If the error wasn't one of those two errors, try again with cmd.exe...
71 errno = saved_errno;
72 }
73
74 arguments[0] = cmd_exe;
75 return static_cast<int>(traits::tspawnvpe(_P_WAIT, arguments[0], arguments, nullptr));
76}
#define ENOENT
Definition: acclib.h:79
#define EACCES
Definition: acclib.h:85
#define _ERRCHECK_EINVAL(e)
#define _ASSERTE(expr)
Definition: crtdbg.h:114
GLuint64EXT * result
Definition: glext.h:11304
#define _P_WAIT
Definition: port.h:370
#define errno
Definition: errno.h:18
int errno_t
Definition: corecrt.h:615

Referenced by _wsystem(), and system().

◆ system()

int __cdecl system ( char const *const  command)

Definition at line 78 of file system.cpp.

79{
80 return common_system(command);
81}