Added os.system() to the os.* library

This commit is contained in:
CPunch 2021-03-20 01:02:13 -05:00
parent b9e9dedac6
commit 057716e0d4
2 changed files with 22 additions and 2 deletions

View File

@ -315,15 +315,34 @@ int cosmoB_osTime(CState *state, int nargs, CValue *args) {
return 1; 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()", "<string>", "%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) { COSMO_API void cosmoB_loadOSLib(CState *state) {
const char *identifiers[] = { const char *identifiers[] = {
"read", "read",
"time" "time",
"system"
}; };
CosmoCFunction osLib[] = { CosmoCFunction osLib[] = {
cosmoB_osRead, cosmoB_osRead,
cosmoB_osTime cosmoB_osTime,
cosmoB_osSystem
}; };
cosmoV_pushString(state, "os"); cosmoV_pushString(state, "os");

View File

@ -19,6 +19,7 @@ COSMO_API void cosmoB_loadObjLib(CState *state);
/* loads the os library, including: /* loads the os library, including:
- os.read() - os.read()
- os.system()
- os.time() - os.time()
*/ */
COSMO_API void cosmoB_loadOSLib(CState *state); COSMO_API void cosmoB_loadOSLib(CState *state);