1%% 2%% %CopyrightBegin% 3%% 4%% Copyright Ericsson AB 2004-2020. All Rights Reserved. 5%% 6%% Licensed under the Apache License, Version 2.0 (the "License"); 7%% you may not use this file except in compliance with the License. 8%% You may obtain a copy of the License at 9%% 10%% http://www.apache.org/licenses/LICENSE-2.0 11%% 12%% Unless required by applicable law or agreed to in writing, software 13%% distributed under the License is distributed on an "AS IS" BASIS, 14%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15%% See the License for the specific language governing permissions and 16%% limitations under the License. 17%% 18%% %CopyrightEnd% 19%% 20 21%% 22%% Common library routines in the SSH application 23%% 24 25-module(ssh_lib). 26 27-export([ 28 format_address_port/2, format_address_port/1, 29 format_address/1, 30 format_time_ms/1, 31 comp/2 32 ]). 33 34-include("ssh.hrl"). 35 36%%%---------------------------------------------------------------- 37format_address_port({IP,Port}) when is_integer(Port) -> 38 format_address_port(IP, Port); 39format_address_port(X) -> 40 io_lib:format("~p", [X]). 41 42%%%---------------------------------------------------------------- 43format_address_port(Address, Port) -> 44 try lists:concat([format_address(Address), ":", Port]) 45 catch 46 _:_ -> io_lib:format("~p:~p",[Address,Port]) 47 end. 48 49%%%---------------------------------------------------------------- 50format_address(#address{address=A, port=P}) -> 51 format_address_port(A,P); 52format_address(A) -> 53 try inet:ntoa(A) 54 catch 55 _:_ when is_list(A) -> A; 56 _:_ -> io_lib:format('~p',[A]) 57 end. 58 59%%%---------------------------------------------------------------- 60format_time_ms(T) when is_integer(T) -> 61 if 62 T < 60000 -> io_lib:format("~.3f sec", [T/1000]); 63 true -> io_lib:format("~p min ~s", [T div 60000, format_time_ms(T rem 60000)]) 64 end. 65 66 67%%%---------------------------------------------------------------- 68 69comp(X1, X2) -> 70 comp(X1, X2, true). 71 72%%% yes - very far from best implementation 73comp(<<B1,R1/binary>>, <<B2,R2/binary>>, Truth) -> 74 comp(R1, R2, Truth and (B1 == B2)); 75comp(<<_,R1/binary>>, <<>>, Truth) -> 76 comp(R1, <<>>, Truth and false); 77comp(<<>>, <<>>, Truth) -> 78 Truth; 79 80comp([H1|T1], [H2|T2], Truth) -> 81 comp(T1, T2, Truth and (H1 == H2)); 82comp([_|T1], [], Truth) -> 83 comp(T1, [], Truth and false); 84comp([], [], Truth) -> 85 Truth; 86 87comp(_, _, _) -> 88 false. 89