From d13cc398c84b45511c83d01c7dfb667690abc164 Mon Sep 17 00:00:00 2001 From: CPunch Date: Sat, 20 Mar 2021 01:02:13 -0500 Subject: [PATCH] Added os.system() to the os.* library --- src/cbaselib.c | 23 +++++++++++++++++++++-- src/cbaselib.h | 1 + 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/cbaselib.c b/src/cbaselib.c index bcca5e4..fe2da59 100644 --- a/src/cbaselib.c +++ b/src/cbaselib.c @@ -315,15 +315,34 @@ int cosmoB_osTime(CState *state, int nargs, CValue *args) { return 1; } +// os.system() +int cosmoB_osSystem(CState *state, int nargs, CValue *args) { + if (nargs != 1) { + cosmoV_error(state, "os.system() expects 1 argument, got %d!", nargs); + return 0; + } + + if (!IS_STRING(args[0])) { + cosmoV_typeError(state, "os.system()", "", "%s", cosmoV_typeStr(args[0])); + return 0; + } + + // run the command and return the exit code + cosmoV_pushNumber(state, system(cosmoV_readCString(args[0]))); + return 1; +} + COSMO_API void cosmoB_loadOSLib(CState *state) { const char *identifiers[] = { "read", - "time" + "time", + "system" }; CosmoCFunction osLib[] = { cosmoB_osRead, - cosmoB_osTime + cosmoB_osTime, + cosmoB_osSystem }; cosmoV_pushString(state, "os"); diff --git a/src/cbaselib.h b/src/cbaselib.h index b269b4f..7fc03a7 100644 --- a/src/cbaselib.h +++ b/src/cbaselib.h @@ -19,6 +19,7 @@ COSMO_API void cosmoB_loadObjLib(CState *state); /* loads the os library, including: - os.read() + - os.system() - os.time() */ COSMO_API void cosmoB_loadOSLib(CState *state);