remove usage of which, uname and whoami

This commit is contained in:
nakst 2022-02-01 14:01:10 +00:00
parent 8db3ef4e71
commit 74ea4e1fe4
2 changed files with 52 additions and 17 deletions

View File

@ -110,7 +110,7 @@ void PortBusybox() {
str version = "1.33.1"; str version = "1.33.1";
if processorCount == 0 processorCount = SystemGetProcessorCount(); if processorCount == 0 processorCount = SystemGetProcessorCount();
if StringTrim(SystemShellEvaluate("uname")) == "Darwin" { if SystemGetHostName() == "Darwin" {
SystemSetEnvironmentVariable("PATH", "/usr/local/opt/gnu-sed/libexec/gnubin:" + SystemGetEnvironmentVariable("PATH")); 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. // Make sure we're not running as root.
assert StringTrim(SystemShellEvaluate("whoami")) != "root"; assert !SystemRunningAsAdministrator();
// Version strings: // Version strings:
str gccVersion = "11.1.0"; str gccVersion = "11.1.0";
@ -203,24 +203,23 @@ void PortGCC() {
assert SystemGetEnvironmentVariable("PATH") == path; assert SystemGetEnvironmentVariable("PATH") == path;
// Get the brew library path if we're running on Darwin. // Get the brew library path if we're running on Darwin.
str hostPlatform = StringTrim(SystemShellEvaluate("uname"));
str libraryPath = ""; 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. // Check all the needed tools are available.
assert SystemShellExecute("which g++"); assert SystemShellExecute("awk -V");
assert SystemShellExecute("which make"); assert SystemShellExecute("bison -V");
assert SystemShellExecute("which bison"); assert SystemShellExecute("ctags --version");
assert SystemShellExecute("which flex"); assert SystemShellExecute("curl -V");
assert SystemShellExecute("which curl"); assert SystemShellExecute("flex -V");
assert SystemShellExecute("which nasm"); assert SystemShellExecute("g++ --version");
assert SystemShellExecute("which ctags"); assert SystemShellExecute("grep -V");
assert SystemShellExecute("which xz"); assert SystemShellExecute("gzip -V");
assert SystemShellExecute("which gzip"); assert SystemShellExecute("make -v");
assert SystemShellExecute("which tar"); assert SystemShellExecute("nasm -v");
assert SystemShellExecute("which grep"); assert SystemShellExecute("sed --version");
assert SystemShellExecute("which sed"); assert SystemShellExecute("tar --version");
assert SystemShellExecute("which awk"); assert SystemShellExecute("xz -V");
// Check all the needed libraries are available. // Check all the needed libraries are available.
assert FileWriteAll("bin/test.c", "void main() {}"); assert FileWriteAll("bin/test.c", "void main() {}");

View File

@ -395,6 +395,8 @@ char baseModuleSource[] = {
// Miscellaneous: // Miscellaneous:
"int SystemGetProcessorCount() #extcall;" "int SystemGetProcessorCount() #extcall;"
"bool SystemRunningAsAdministrator() #extcall;"
"str SystemGetHostName() #extcall;"
// File system access: // File system access:
@ -440,6 +442,8 @@ int ExternalSystemShellEvaluate(ExecutionContext *context, Value *returnValue);
int ExternalSystemGetProcessorCount(ExecutionContext *context, Value *returnValue); int ExternalSystemGetProcessorCount(ExecutionContext *context, Value *returnValue);
int ExternalSystemGetEnvironmentVariable(ExecutionContext *context, Value *returnValue); int ExternalSystemGetEnvironmentVariable(ExecutionContext *context, Value *returnValue);
int ExternalSystemSetEnvironmentVariable(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 ExternalPathCreateDirectory(ExecutionContext *context, Value *returnValue);
int ExternalPathCreateLeadingDirectories(ExecutionContext *context, Value *returnValue); int ExternalPathCreateLeadingDirectories(ExecutionContext *context, Value *returnValue);
int ExternalPathDelete(ExecutionContext *context, Value *returnValue); int ExternalPathDelete(ExecutionContext *context, Value *returnValue);
@ -467,6 +471,8 @@ ExternalFunction externalFunctions[] = {
{ .cName = "SystemGetProcessorCount", .callback = ExternalSystemGetProcessorCount }, { .cName = "SystemGetProcessorCount", .callback = ExternalSystemGetProcessorCount },
{ .cName = "SystemGetEnvironmentVariable", .callback = ExternalSystemGetEnvironmentVariable }, { .cName = "SystemGetEnvironmentVariable", .callback = ExternalSystemGetEnvironmentVariable },
{ .cName = "SystemSetEnvironmentVariable", .callback = ExternalSystemSetEnvironmentVariable }, { .cName = "SystemSetEnvironmentVariable", .callback = ExternalSystemSetEnvironmentVariable },
{ .cName = "SystemRunningAsAdministrator", .callback = ExternalSystemRunningAsAdministrator },
{ .cName = "SystemGetHostName", .callback = ExternalSystemGetHostName },
{ .cName = "PathExists", .callback = ExternalPathExists }, { .cName = "PathExists", .callback = ExternalPathExists },
{ .cName = "PathCreateDirectory", .callback = ExternalPathCreateDirectory }, { .cName = "PathCreateDirectory", .callback = ExternalPathCreateDirectory },
{ .cName = "PathCreateLeadingDirectories", .callback = ExternalPathCreateLeadingDirectories }, { .cName = "PathCreateLeadingDirectories", .callback = ExternalPathCreateLeadingDirectories },
@ -4294,6 +4300,7 @@ void ScriptFree(ExecutionContext *context) {
#else #else
#include <dirent.h> #include <dirent.h>
#include <unistd.h> #include <unistd.h>
#include <sys/utsname.h>
#endif #endif
#include <errno.h> #include <errno.h>
#include <stdarg.h> #include <stdarg.h>
@ -5065,6 +5072,35 @@ int ExternalSystemGetProcessorCount(ExecutionContext *context, Value *returnValu
return 2; 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) { void *AllocateFixed(size_t bytes) {
if (!bytes) { if (!bytes) {
return NULL; return NULL;