From 74ea4e1fe4e8a07e11a7464265ac293e64a37ff9 Mon Sep 17 00:00:00 2001 From: nakst <> Date: Tue, 1 Feb 2022 14:01:10 +0000 Subject: [PATCH] remove usage of which, uname and whoami --- ports/port.script | 33 ++++++++++++++++----------------- util/script.c | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 17 deletions(-) diff --git a/ports/port.script b/ports/port.script index e78be48..5744ca3 100644 --- a/ports/port.script +++ b/ports/port.script @@ -110,7 +110,7 @@ void PortBusybox() { str version = "1.33.1"; if processorCount == 0 processorCount = SystemGetProcessorCount(); - if StringTrim(SystemShellEvaluate("uname")) == "Darwin" { + if SystemGetHostName() == "Darwin" { SystemSetEnvironmentVariable("PATH", "/usr/local/opt/gnu-sed/libexec/gnubin:" + SystemGetEnvironmentVariable("PATH")); } @@ -174,7 +174,7 @@ void PortGCC() { } // Make sure we're not running as root. - assert StringTrim(SystemShellEvaluate("whoami")) != "root"; + assert !SystemRunningAsAdministrator(); // Version strings: str gccVersion = "11.1.0"; @@ -203,24 +203,23 @@ void PortGCC() { assert SystemGetEnvironmentVariable("PATH") == path; // Get the brew library path if we're running on Darwin. - str hostPlatform = StringTrim(SystemShellEvaluate("uname")); str libraryPath = ""; - if hostPlatform == "Darwin" libraryPath = "-L" + StringTrim(SystemShellEvaluate("brew --prefix")) + "/lib"; + if SystemGetHostName() == "Darwin" libraryPath = "-L" + StringTrim(SystemShellEvaluate("brew --prefix")) + "/lib"; // Check all the needed tools are available. - assert SystemShellExecute("which g++"); - assert SystemShellExecute("which make"); - assert SystemShellExecute("which bison"); - assert SystemShellExecute("which flex"); - assert SystemShellExecute("which curl"); - assert SystemShellExecute("which nasm"); - assert SystemShellExecute("which ctags"); - assert SystemShellExecute("which xz"); - assert SystemShellExecute("which gzip"); - assert SystemShellExecute("which tar"); - assert SystemShellExecute("which grep"); - assert SystemShellExecute("which sed"); - assert SystemShellExecute("which awk"); + assert SystemShellExecute("awk -V"); + assert SystemShellExecute("bison -V"); + assert SystemShellExecute("ctags --version"); + assert SystemShellExecute("curl -V"); + assert SystemShellExecute("flex -V"); + assert SystemShellExecute("g++ --version"); + assert SystemShellExecute("grep -V"); + assert SystemShellExecute("gzip -V"); + assert SystemShellExecute("make -v"); + assert SystemShellExecute("nasm -v"); + assert SystemShellExecute("sed --version"); + assert SystemShellExecute("tar --version"); + assert SystemShellExecute("xz -V"); // Check all the needed libraries are available. assert FileWriteAll("bin/test.c", "void main() {}"); diff --git a/util/script.c b/util/script.c index f3b9222..4b409c7 100644 --- a/util/script.c +++ b/util/script.c @@ -395,6 +395,8 @@ char baseModuleSource[] = { // Miscellaneous: "int SystemGetProcessorCount() #extcall;" + "bool SystemRunningAsAdministrator() #extcall;" + "str SystemGetHostName() #extcall;" // File system access: @@ -440,6 +442,8 @@ int ExternalSystemShellEvaluate(ExecutionContext *context, Value *returnValue); int ExternalSystemGetProcessorCount(ExecutionContext *context, Value *returnValue); int ExternalSystemGetEnvironmentVariable(ExecutionContext *context, Value *returnValue); int ExternalSystemSetEnvironmentVariable(ExecutionContext *context, Value *returnValue); +int ExternalSystemRunningAsAdministrator(ExecutionContext *context, Value *returnValue); +int ExternalSystemGetHostName(ExecutionContext *context, Value *returnValue); int ExternalPathCreateDirectory(ExecutionContext *context, Value *returnValue); int ExternalPathCreateLeadingDirectories(ExecutionContext *context, Value *returnValue); int ExternalPathDelete(ExecutionContext *context, Value *returnValue); @@ -467,6 +471,8 @@ ExternalFunction externalFunctions[] = { { .cName = "SystemGetProcessorCount", .callback = ExternalSystemGetProcessorCount }, { .cName = "SystemGetEnvironmentVariable", .callback = ExternalSystemGetEnvironmentVariable }, { .cName = "SystemSetEnvironmentVariable", .callback = ExternalSystemSetEnvironmentVariable }, + { .cName = "SystemRunningAsAdministrator", .callback = ExternalSystemRunningAsAdministrator }, + { .cName = "SystemGetHostName", .callback = ExternalSystemGetHostName }, { .cName = "PathExists", .callback = ExternalPathExists }, { .cName = "PathCreateDirectory", .callback = ExternalPathCreateDirectory }, { .cName = "PathCreateLeadingDirectories", .callback = ExternalPathCreateLeadingDirectories }, @@ -4294,6 +4300,7 @@ void ScriptFree(ExecutionContext *context) { #else #include #include +#include #endif #include #include @@ -5065,6 +5072,35 @@ int ExternalSystemGetProcessorCount(ExecutionContext *context, Value *returnValu return 2; } +int ExternalSystemRunningAsAdministrator(ExecutionContext *context, Value *returnValue) { + (void) context; +#ifdef _WIN32 +#pragma message ("ExternalSystemRunningAsAdministrator unimplemented") + returnValue->i = 0; +#else + returnValue->i = geteuid() == 0; +#endif + return 2; +} + +int ExternalSystemGetHostName(ExecutionContext *context, Value *returnValue) { + (void) context; + const char *name; +#ifdef _WIN32 + name = "Windows"; +#else + struct utsname buffer; + uname(&buffer); + name = buffer.sysname; +#endif + returnValue->i = HeapAllocate(context); + context->heap[returnValue->i].type = T_STR; + context->heap[returnValue->i].bytes = strlen(name); + context->heap[returnValue->i].text = AllocateResize(NULL, context->heap[returnValue->i].bytes); + memcpy(context->heap[returnValue->i].text, name, context->heap[returnValue->i].bytes); + return 3; +} + void *AllocateFixed(size_t bytes) { if (!bytes) { return NULL;