ReactOS 0.4.16-dev-2104-gb84fa49
system.cpp
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS msvcrt
3 * LICENSE: MIT (https://spdx.org/licenses/MIT)
4 * PURPOSE: Implementation of system / _wsystem
5 * COPYRIGHT: Copyright (c) Microsoft Corporation. All rights reserved.
6 * COPYRIGHT: Copyright 2025 Timo Kreuzer <timo.kreuzer@reactos.org>
7 */
8
9#include <process.h>
10#include <io.h>
11#include <stdlib.h>
12#include <errno.h>
13
14extern "C" int _cdecl _access_s(const char* filename, int mode);
15extern "C" int _cdecl _waccess_s(const wchar_t* filename, int mode);
16
17int _cdecl _taccess_s(const char* filename, int mode)
18{
19 return _access_s(filename, mode);
20}
21
22int _cdecl _taccess_s(const wchar_t* filename, int mode)
23{
24 return _waccess_s(filename, mode);
25}
26
27char* __cdecl _tgetenv(_In_z_ char const* _VarName)
28{
29 return getenv(_VarName);
30}
31
32wchar_t* __cdecl _tgetenv(_In_z_ wchar_t const* _VarName)
33{
34 return _wgetenv(_VarName);
35}
36
37intptr_t __cdecl _tspawnve(int flags, const char* name, const char* const* argv,
38 const char* const* envv)
39{
40 return _spawnve(flags, name, argv, envv);
41}
42
43intptr_t __cdecl _tspawnve(int flags, const wchar_t* name, const wchar_t* const* argv,
44 const wchar_t* const* envv)
45{
46 return _wspawnve(flags, name, argv, envv);
47}
48
49intptr_t __cdecl _tspawnvpe(int flags, const char* name, const char* const* argv,
50 const char* const* envv)
51{
52 return _spawnvpe(flags, name, argv, envv);
53}
54
55intptr_t __cdecl _tspawnvpe(int flags, const wchar_t* name, const wchar_t* const* argv,
56 const wchar_t* const* envv)
57{
58 return _wspawnvpe(flags, name, argv, envv);
59}
60
61template <typename Character>
62static int __cdecl common_system(Character const* const command) throw()
63{
64 static Character const comspec_name[] = { 'C', 'O', 'M', 'S', 'P', 'E', 'C', '\0' }; // "COMSPEC"
65 static Character const cmd_exe[] = { 'c', 'm', 'd', '.', 'e', 'x', 'e', '\0' }; // "cmd.exe"
66 static Character const slash_c[] = { '/', 'c', '\0' }; // "/c"
67
68 Character const * comspec_value = _tgetenv(comspec_name);
69
70 // If the command is null, return TRUE only if %COMSPEC% is set and the file
71 // to which it points exists.
72 if (!command)
73 {
74 if (!comspec_value)
75 return 0;
76
77 return _taccess_s(comspec_value, 0) == 0;
78 }
79
80 Character const* arguments[4] =
81 {
82 comspec_value,
83 slash_c,
84 command,
85 nullptr
86 };
87
88 if (comspec_value)
89 {
90 errno_t const saved_errno = errno;
91 errno = 0;
92
93 int const result = static_cast<int>(_tspawnve(_P_WAIT, arguments[0], arguments, nullptr));
94 if (result != -1)
95 {
96 errno = saved_errno;
97 return result;
98 }
99
100 if (errno != ENOENT && errno != EACCES)
101 {
102 return result;
103 }
104
105 // If the error wasn't one of those two errors, try again with cmd.exe...
106 errno = saved_errno;
107 }
108
109 arguments[0] = cmd_exe;
110 return static_cast<int>(_tspawnvpe(_P_WAIT, arguments[0], arguments, nullptr));
111
112 return 0;
113}
114
115extern "C" int __cdecl system(char const* const command)
116{
117 return common_system(command);
118}
119
120extern "C" int __cdecl _wsystem(wchar_t const* const command)
121{
122 return common_system(command);
123}
wchar_t *CDECL _wgetenv(const wchar_t *name)
Definition: environ.c:254
char *CDECL getenv(const char *name)
Definition: environ.c:227
int errno_t
Definition: corecrt.h:249
int intptr_t
Definition: corecrt.h:176
#define __cdecl
Definition: corecrt.h:121
_ACRTIMP intptr_t __cdecl _wspawnvpe(int, const wchar_t *, const wchar_t *const *, const wchar_t *const *)
Definition: process.c:991
_ACRTIMP intptr_t __cdecl _wspawnve(int, const wchar_t *, const wchar_t *const *, const wchar_t *const *)
Definition: process.c:924
#define ENOENT
Definition: errno.h:25
#define errno
Definition: errno.h:120
#define EACCES
Definition: errno.h:36
_ACRTIMP intptr_t __cdecl _spawnvpe(int, const char *, const char *const *, const char *const *)
Definition: process.c:967
#define _P_WAIT
Definition: process.h:15
_ACRTIMP intptr_t __cdecl _spawnve(int, const char *, const char *const *, const char *const *)
Definition: process.c:900
int _cdecl _waccess_s(const wchar_t *filename, int mode)
Definition: file.c:1039
static int __cdecl common_system(Character const *const command)
Definition: system.cpp:62
int _cdecl _access_s(const char *filename, int mode)
Definition: file.c:1004
int __cdecl _wsystem(wchar_t const *const command)
Definition: system.cpp:120
int __cdecl system(char const *const command)
Definition: system.cpp:115
GLenum mode
Definition: glext.h:6217
GLbitfield flags
Definition: glext.h:7161
GLuint64EXT * result
Definition: glext.h:11304
#define _tspawnvpe
Definition: tchar.h:649
#define _tgetenv
Definition: tchar.h:680
#define _tspawnve
Definition: tchar.h:647
#define _taccess_s
Definition: tchar.h:1185
const char * filename
Definition: ioapi.h:137
#define argv
Definition: mplay32.c:18
#define _In_z_
Definition: no_sal2.h:164
Definition: name.c:39