-module(opener_ffi). -export([get_os/0, exec/2]). get_os() -> case os:type() of {win32, _} -> nt; {unix, darwin} -> darwin; {unix, linux} -> linux; {_, _} -> unknown end. exec(Command, Args) -> Command_ = binary_to_list(Command), Args_ = lists:map(fun(Arg) -> binary_to_list(Arg) end, Args), Name = {spawn_executable, os:find_executable(Command_)}, Port = open_port( Name, [ exit_status, binary, hide, stream, eof, stderr_to_stdout, {args, Args_}]), do_exec(Port, []). %% Helper function to handle the execution of the command and collect output. %% %% @param Port The port used for communication with the external process. %% @param Acc An accumulator for collecting output data. %% %% @return A tuple indicating success or failure of the command execution. do_exec(Port, Acc) -> receive {Port, {data, Data}} -> do_exec(Port, [Data | Acc]); {Port, {exit_status, 0}} -> port_close(Port), {ok, list_to_binary(lists:reverse(Acc))}; {Port, {exit_status, Code}} -> port_close(Port), {error, {Code, list_to_binary(lists:reverse(Acc))}} end.