#include <stdio.h>
#include "boxlite.h"
void output_callback(const char* text, int is_stderr, void* user_data) {
FILE* stream = is_stderr ? stderr : stdout;
fprintf(stream, "%s", text);
}
int main() {
CBoxliteRuntime* runtime = NULL;
CBoxHandle* box = NULL;
CBoxliteError error = {0};
// Create runtime
if (boxlite_runtime_new(NULL, NULL, &runtime, &error) != Ok) {
fprintf(stderr, "Runtime error: %s\n", error.message);
boxlite_error_free(&error);
return 1;
}
printf("BoxLite v%s\n", boxlite_version());
// Create box with JSON configuration
const char* options = "{"
"\"rootfs\":{\"Image\":\"alpine:3.19\"},"
"\"cpus\":2,"
"\"memory_mib\":512,"
"\"env\":[],"
"\"volumes\":[],"
"\"network\":\"Isolated\","
"\"ports\":[]"
"}";
if (boxlite_create_box(runtime, options, &box, &error) != Ok) {
fprintf(stderr, "Box error: %s\n", error.message);
boxlite_error_free(&error);
boxlite_runtime_free(runtime);
return 1;
}
// Execute commands with streaming output
int exit_code = 0;
printf("\n--- Running: ls -la / ---\n");
if (boxlite_execute(box, "/bin/ls", "[\"-la\", \"/\"]", output_callback, NULL, &exit_code, &error) == Ok) {
printf("\nExit code: %d\n", exit_code);
} else {
fprintf(stderr, "Execute error: %s\n", error.message);
boxlite_error_free(&error);
}
// Cleanup (runtime frees all boxes)
boxlite_runtime_free(runtime);
return 0;
}