Embed cocmd.com interpreter for system() / open()

This change lets you use system() in an easier and portable way. The
problem with the call in the past has always been that bourne and
cmd.com on Windows have less than nothing in common, so pretty much the
only command system() could be used for across platforms was maybe echo.
cmd.exe is also a security liability due to its escaping rules.

Since cocmd.com implements 85% of what we need from bourne, in a really
tiny way, it makes perfect sense to be embedded in these functionss. We
get a huge performance boost too.

Fixes #644
This commit is contained in:
Justine Tunney 2022-10-02 15:29:57 -07:00
parent daca5499b9
commit 950a1b310b
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
9 changed files with 313 additions and 320 deletions

View file

@ -23,6 +23,7 @@
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h"
#include "libc/sysv/consts/o.h"
#include "libc/testlib/ezbench.h"
#include "libc/testlib/testlib.h"
#include "libc/x/x.h"
@ -31,11 +32,8 @@ char testlib_enable_tmp_setup_teardown;
TEST(system, testStdoutRedirect) {
int ws;
testlib_extract("/zip/echo.com", "echo.com", 0755);
testlib_extract("/zip/cocmd.com", "cocmd.com", 0755);
setenv("PATH", ".", true); // avoid / vs. \ until cocmd.com is ready
_PATH_BSHELL = "cocmd.com"; // cmd.exe shall still be used on windows
ASSERT_TRUE(system(0));
ws = system("echo.com hello >hello.txt");
ws = system("./echo.com hello >hello.txt");
ASSERT_TRUE(WIFEXITED(ws));
ASSERT_EQ(0, WEXITSTATUS(ws));
EXPECT_STREQ("hello\n", _gc(xslurp("hello.txt", 0)));
@ -44,12 +42,14 @@ TEST(system, testStdoutRedirect) {
TEST(system, testStdoutRedirect_withSpacesInFilename) {
int ws;
testlib_extract("/zip/echo.com", "echo.com", 0755);
testlib_extract("/zip/cocmd.com", "cocmd.com", 0755);
setenv("PATH", ".", true); // avoid / vs. \ until cocmd.com is ready
_PATH_BSHELL = "cocmd.com"; // cmd.exe shall still be used on windows
ASSERT_TRUE(system(0));
ws = system("echo.com hello >\"hello there.txt\"");
ws = system("./echo.com hello >\"hello there.txt\"");
ASSERT_TRUE(WIFEXITED(ws));
ASSERT_EQ(0, WEXITSTATUS(ws));
EXPECT_STREQ("hello\n", _gc(xslurp("hello there.txt", 0)));
}
BENCH(system, bench) {
testlib_extract("/zip/echo.com", "echo.com", 0755);
EZBENCH2("system", donothing, system("./echo.com hi >/dev/null"));
}