Commit 466115d3

466115d356b6Merged

ydah <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified

Description

fix(codegen,runtime): pass system argument lists

Details

Commit
466115d356b6
Parent
25c6cc357a4a

lib/sp_runtime.hmodifiedView file

@@ -24,8 +24,10 @@
2424 #include <stdarg.h>
2525 #include <time.h>
2626 #include <setjmp.h>
27+#include <errno.h>
2728 #ifdef _WIN32
2829 #include <windows.h>
30+#include <process.h>
2931 /* POSIX compat shims for MinGW */
3032 #define mmap(a,l,p,f,fd,off) VirtualAlloc(NULL,(l),MEM_RESERVE|MEM_COMMIT,PAGE_READWRITE)
3133 #define munmap(a,l) (VirtualFree((a),0,MEM_RELEASE)?0:-1)
@@ -39,6 +41,7 @@
3941 #include <ucontext.h>
4042 #include <unistd.h>
4143 #include <sys/mman.h>
44+#include <sys/wait.h>
4245 #endif
4346 #if !defined(__APPLE__) && !defined(_WIN32) && !defined(__FreeBSD__)
4447 #include <malloc.h>
@@ -1748,6 +1751,40 @@
17481751
17491752 /* System/backtick support */
17501753 static int sp_last_status = 0;
1754+static mrb_bool sp_system_args(int argc, const char *const *argv) {
1755+ if (argc <= 0 || argv == NULL || argv[0] == NULL) {
1756+ sp_last_status = -1;
1757+ return FALSE;
1758+ }
1759+ fflush(stdout);
1760+#ifdef _WIN32
1761+ intptr_t rc = _spawnvp(_P_WAIT, argv[0], argv);
1762+ if (rc < 0) {
1763+ sp_last_status = -1;
1764+ return FALSE;
1765+ }
1766+ sp_last_status = (int)rc;
1767+ return rc == 0 ? TRUE : FALSE;
1768+#else
1769+ pid_t pid = fork();
1770+ if (pid < 0) {
1771+ sp_last_status = -1;
1772+ return FALSE;
1773+ }
1774+ if (pid == 0) {
1775+ execvp(argv[0], (char * const *)argv);
1776+ _exit(127);
1777+ }
1778+ int status = 0;
1779+ while (waitpid(pid, &status, 0) < 0) {
1780+ if (errno == EINTR) continue;
1781+ sp_last_status = -1;
1782+ return FALSE;
1783+ }
1784+ sp_last_status = status;
1785+ return (WIFEXITED(status) && WEXITSTATUS(status) == 0) ? TRUE : FALSE;
1786+#endif
1787+}
17511788
17521789 /* Bigint (linked from libspinel_rt.a) */
17531790 typedef struct sp_Bigint sp_Bigint;