Usage of BIF port_control/3?

Scott Lystig Fritchie scott@REDACTED
Mon Feb 28 08:06:31 CET 2000


I hope this isn't a "I didn't dig quite deep enough" question...

... I'm experimenting with the BIF port_control/3.  One of the very
few examples I can find is in inet.erl:ctl_getstat/2, which seems
pretty straightforward.  So I'm trying something like this:

sync_cmd2(Port, Cmd, Bytes, Rep) ->
    case catch port_control(Port, Cmd, Bytes) of
	{'EXIT', _Stuff} -> io:format("XXX sync_cmd2 err clause 1\n"),
                            {error, einval};
	[Rep|Data]  -> {ok, Data};
	_Error      -> io:format("XXX sync_cmd2 err clause 3: ~w\n", [_Error]),
                       {error, einval}
    end.

But it doesn't work.  All I get are matches in clause 3, _Error = [],
which causes my experiment to reimplement inet:ll_name/1 to barf.

ll_name(Inet) -> 
    case sync_cmd2(Inet, ?INET_REQ_NAME, [], ?INET_REP_NAME) of  
        {ok, [P1, P0 | Addr]} ->
            {ok, { bytes_to_ip(Addr), ?u16(P1, P0) }};
        Error -> Error
    end.    

If sync_cmd2(Port, Cmd, Bytes, Rep) starts out as ->
    Port ! {self(), {command, [Cmd|Bytes]}},
    receive
	{Port, {data, [Rep|T]}} -> {ok, T};
	%% ... and so on....

... then it works.  Also, if I use sync_cmd2(Port, Cmd, Bytes, Rep) ->
    port_command(Port, [Cmd|Bytes]),
    receive 
        {Port, {data, [Rep | T]}} -> {ok, T};
	%% ... and so on....

... it also works.  Am I overlooking something simple?

-Scott



More information about the erlang-questions mailing list