> Erlang中文手册 > host_file/0 读取 .hosts.erlang 文件

net_adm:host_file/0

读取 .hosts.erlang 文件

用法:

host_file() -> Hosts | {error, Reason}

内部实现:

-spec host_file() -> Hosts | {error, Reason} when
      Hosts :: [Host :: atom()],
      %% Copied from file:path_consult/2:
      Reason :: file:posix() | badarg | terminated | system_limit
              | {Line :: integer(), Mod :: module(), Term :: term()}.

host_file() ->
    Home = case init:get_argument(home) of
	       {ok, [[H]]} -> [H];
	       _ -> []
	   end,
    case file:path_consult(["."] ++ Home ++ [code:root_dir()], ".hosts.erlang") of
	{ok, Hosts, _} -> Hosts;
	Error -> Error
    end.

读取文件 .hosts.erlang,文件里的 hosts 数据 以一个列表返回,如果文件不能被堵,或者文件里的 Erlang 项(terms)不能被解析,则返回 {error, Reason}。

net_adm:host_file().