Commit 5f4b4e80

5f4b4e80e6fbMerged

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

Description

Update $? from the backtick command's exit status

sp_backtick discarded pclose's return, so Ruby's $? stayed stale after a backtick (e.g. false; $? == 0 read true). Store the pclose result in sp_last_status, matching sp_system_args' layout (POSIX wait-status as-is; MSVCRT's plain exit code shifted <<8).

Refs #1388.

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

Details

Commit
5f4b4e80e6fb
Parent
36fbf7742bc6

lib/sp_runtime.hmodifiedView file

@@ -4862,7 +4862,22 @@
48624862 #endif
48634863 #endif
48644864 }
4865-static const char *sp_backtick(const char *cmd) { FILE *p = popen(cmd, "r"); if (!p) return sp_str_empty; char *buf = sp_str_alloc_raw(4096); size_t n = fread(buf, 1, 4095, p); buf[n] = 0; pclose(p); return buf; }
4865+static const char *sp_backtick(const char *cmd) {
4866+ FILE *p = popen(cmd, "r");
4867+ if (!p) { sp_last_status = -1; return sp_str_empty; }
4868+ char *buf = sp_str_alloc_raw(4096);
4869+ size_t n = fread(buf, 1, 4095, p);
4870+ buf[n] = 0;
4871+ int st = pclose(p);
4872+ /* Mirror sp_system_args' $? layout: POSIX pclose returns a wait-status,
4873+ MSVCRT _pclose returns the plain exit code (shift to match). */
4874+#ifdef _WIN32
4875+ sp_last_status = st << 8;
4876+#else
4877+ sp_last_status = st;
4878+#endif
4879+ return buf;
4880+}
48664881 static const char *sp_file_basename(const char *path) {
48674882 const char *s = strrchr(path, '/');
48684883 const char *base = s ? s + 1 : path;