Any problems with this D wrapper around libcomcom_run_command() C function from C library libcomcom?
Does it work as expected? Is it idiomatic? Is it the right way to do this?
module libcomcom_wrapper;
import std.string;
import std.algorithm.iteration : map;
import std.array : array;
import std.exception : ErrnoException;
import libcomcom;
string runCommand(string file, string[] argv, string[] envp, string input, int timeout = -1) {
const(char*) output;
size_t output_len;
const char*[] childArgv = map!(s => s.toStringz)(argv).array;
const char*[] childEnvp = map!(s => s.toStringz)(envp).array;
immutable int res = libcomcom_run_command(input.ptr, input.length,
&output, &output_len,
file.toStringz, childArgv.ptr,
childEnvp.ptr,
timeout);
if (res != 0) {
throw new ErrnoException("Run command"); // TODO: Localization
}
import core.stdc.stdlib : free;
scope(exit) free(cast(void*) output);
return output[0..output_len].idup;
}
The code was taken here.
toStringzinstead ofs => s.toStringz. \$\endgroup\$~ nullafter every.array(as otherwise there is no array terminator and it is undefined behavior probably) \$\endgroup\$