Commit 8d644148

8d644148ccbbMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified

Description

Run single-string system() through the shell on Windows

The POSIX branch of sp_system_args already routes a 1-argument system("cmd ...") through /bin/sh -c so shell builtins and word splitting work. The Windows branch lacked the equivalent: it passed the whole command string to _spawnvp as an executable name, so system("echo hello") failed to find an "echo hello" binary -- echo is a cmd.exe builtin -- producing no output, a false return, and $? = -1.

Add an argc==1 fast path that calls C system(), which routes through cmd.exe /c on MSVCRT. MSVCRT system() returns the plain exit code, so store rc<<8 in sp_last_status to match the POSIX $? layout callers test.

Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]

Details

Commit
8d644148ccbb
Parent
7b32d151bdfd

lib/sp_system.cmodifiedView file

@@ -118,6 +118,20 @@
118118 }
119119 fflush(NULL);
120120 #ifdef _WIN32
121+ if (argc == 1) {
122+ /* Single-string form: run through the shell (cmd.exe) so builtins like
123+ `echo` and shell parsing work, mirroring the POSIX `/bin/sh -c` path.
124+ _spawnvp would instead try to locate the whole string as an .exe. */
125+ int rc = system(argv[0]);
126+ if (rc == -1) {
127+ sp_last_status = -1;
128+ return FALSE;
129+ }
130+ /* MSVCRT system() returns the plain exit code; shift to match the POSIX
131+ `$?` layout the callers compare against. */
132+ sp_last_status = rc << 8;
133+ return rc == 0 ? TRUE : FALSE;
134+ }
121135 char **quoted_argv = (char **)malloc(sizeof(char *) * (size_t)(argc + 1));
122136 if (!quoted_argv) {
123137 sp_last_status = -1;