Files
fx/fx.diagnostics/test/redirect.c
T

66 lines
1.4 KiB
C

#include <fx/diagnostics/process.h>
int main(int argc, const char **argv)
{
if (argc < 3) {
return -1;
}
const char *exec = argv[1];
const char *msg = argv[2];
const fx_value arg_values[] = {
FX_CSTR("hello"),
FX_CSTR("world"),
};
fx_array *args = fx_array_create_with_values(
arg_values,
sizeof arg_values / sizeof arg_values[0]);
fx_process *self = fx_process_get_self();
const fx_hashtable *env = fx_process_get_environment(self);
fx_process_start_info info = {
.proc_args = args,
.proc_redirect_stdout = true,
.proc_redirect_stdin = true,
.proc_file_name = exec,
.proc_environment = env,
};
fx_process *process = fx_process_create(&info);
if (!process) {
fprintf(stderr, "Process creation failed\n");
return -1;
}
fx_status status = fx_process_start(process);
if (!FX_OK(status)) {
fprintf(stderr,
"Process creation failed: %s\n",
fx_status_description(status));
return -1;
}
printf("process stdin: %s\n", msg);
fx_stream *in = fx_process_get_stdin(process);
fx_stream_write_cstr(in, msg, NULL);
fx_stream_write_char(in, '\n');
fx_process_close_stdin(process);
fx_stream *out = fx_process_get_stdout(process);
printf("process stdout:\n");
printf("---------------------\n");
while (FX_OK(fx_stream_read_line_s(out, fx_stdout))) { }
printf("---------------------\n");
int result;
fx_process_wait(process, &result);
fx_process_unref(process);
return 0;
}