Skip to content
Maxime Chevalier-Boisvert edited this page Feb 17, 2014 · 2 revisions

The stdlib library provides wrappers for common functions in the C stdlib.

Importing:

// First import the stdlib module:
var stdlib = require('lib/stdlib');

The following functions are provided:

// Allocate memory with malloc
var mem = stdlib.malloc(32);

// Resize/reallocate memory
mem = stdlib.realloc(64);

// Free memory
stdlib.free(mem);

// Get the value of an environmental variable
var name = stdlib.getenv("LOGNAME");

// Execute a command
stdlib.system("ls -a");

// There is also popen, which will return a file-like object
var output = stdlib.popen("ls -a", "r");
print(output.read());

// Exit with return code
stdlib.exit(0);

// Fork the current process
pid = fork();

if (pid == -1)
    print('issue with forking');
else if (pid == 0)
    print('in child process');
else
    print('in parent process');

// Wait for a child process to terminate
waitpid(pid, $nullptr, 0);