The system function passes command to the command interpreter, which executes the string as an operating-system command. system uses the COMSPEC and PATH environment variables to locate the command-interpreter file CMD.exe. If command is NULL, the function just checks whether the command interpreter exists.
You must explicitly flush—by using fflush or _flushall—or close any stream before you call system.
https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/system-wsystem
In case, there are any doubts here's the actual snippet from the MS' implementation (very simple and straightforward):
// omitted for brevity
argv[1] = _T("/c");
argv[2] = (_TSCHAR *) command;
argv[3] = NULL;
/* If there is a COMSPEC defined, try spawning the shell */
/* Do not try to spawn the null string */
if (argv[0])
{
// calls spawnve on value of COMSPEC vairable, if present
// omitted for brevity
}
/* No COMSPEC so set argv[0] to what COMSPEC should be. */
argv[0] = _T("cmd.exe");
/* Let the _spawnvpe routine do the path search and spawn. */
retval = (int)_tspawnvpe(_P_WAIT,argv[0],argv,NULL);
// clean-up part omitted
As to concerns of what _tspawnvpe
may actually be doing, the answer is: nothing magical. The exact invocation sequence for spawnvpe
and friends goes as following (as anybody with licensed version of MSVC can easily learn by inspecting the spanwnvpe.c
source file):
- Do some sanity checks on parameters
- Try to invoke
_tspawnve
on the passed file name. spawnve
will succeed if the file name represents an absolute path to an executable or a valid path relative to the current working directory. No further checks are done - so yes, if a file named cmd.exe
exists in current directory it will be invoked first in the context of system()
call discussed.
- In a loop: obtain the next path element using `_getpath()
- Append the file name to the path element
- Pass the resulted path to
spwanvpe
, check if it was successful
That's it. No special tricks/checks involved.