Skip to content

Commit

Permalink
update doc
Browse files Browse the repository at this point in the history
  • Loading branch information
U-MTC_Office_1\mmullis authored and U-MTC_Office_1\mmullis committed Sep 22, 2008
1 parent 8ebdad5 commit 3fc940f
Showing 1 changed file with 16 additions and 59 deletions.
75 changes: 16 additions & 59 deletions src/macaddr.erl
@@ -1,58 +1,33 @@
%%%-------------------------------------------------------------------
%%% File : macaddr.erl
%%% Author : Michael Mullis <michael@mullistechnologies.com>
%%% Description : Cross platform MAC address determination.
%%% Works for:
%%% * /sbin/ifconfig
%%% * /bin/ifconfig
%%% * ifconfig
%%% * ipconfig /all
%%%
%%% @doc Cross platform MAC address determination.
%%% Works for: <br/>
%%% * /sbin/ifconfig <br/>
%%% * /bin/ifconfig <br/>
%%% * ifconfig <br/>
%%% * ipconfig /all <br/>
%%% <br/> <pre>
%%% To return the first MAC address on the system:
%%%
%%% macaddr:address()
%%%
%%% <br/>
%%% To return an array of all MAC addresses:
%%%
%%% macaddr:address_list
%%%
%%% </pre>
%%% @end
%%% Created : 22 Sep 2008 by Michael Mullis <michael@mullistechnologies.com>
%%% @copyright 2008 Michael Mullis
%%%-------------------------------------------------------------------
-module(macaddr).
-author("michael@mullistechnologies.com").
-export([]).
-export([address/0, address_list/0]).
-compile([export_all]).

-vsn("0.1.0").
%% ##
%% # Accessor for the system's first MAC address, requires a call to #address
%% # first

%% ##
%% # Discovers and returns the system's MAC addresses. Returns the first
%% # MAC address, and includes an accessor #list for the remaining addresses:
%% #
%% # Mac.addr # => first address
%% # Mac.addr.list # => all addresses



%% Why do I have to find this http://www.trapexit.org/String_join_with
%% It should be in the standard library - Seriously frustrating

%% my_string_join(Items, Sep) ->
%% lists:flatten(lists:reverse(my_string_join1(Items, Sep, []))).

%% my_string_join1([Head | []], _Sep, Acc) ->
%% [Head | Acc];
%% my_string_join1([Head | Tail], Sep, Acc) ->
%% my_string_join1(Tail, Sep, [Sep, Head | Acc]).

%%% @doc Join elements of a list using separator.
join([H|T], Sep) ->
lists:flatten([H | [[Sep, X] || X <- T]]).
%% OR
%% ???
%% lists:merge(lists:flatten(lists:flatmap(fun(X)->[X," "] end, [a,b,c]))).

file_exists(FileName) ->
filelib:is_regular(FileName).
Expand All @@ -65,26 +40,21 @@ identify_null_file() ->

%% Warning: do not export this because it allows arbitrary os command execution
get_interface_info(Cmd) ->
%% TODO: os:type() may be more useful here than the original approach
%% @TODO os:type() may be more useful here than the original approach
CmdString = Cmd ++ " 2>" ++ identify_null_file(),
%% io:format("CmdString: ~p~n", [CmdString]),
case os:cmd(CmdString) of
[] -> "";
{ok, Data} -> Data;
Data -> Data
end.

%%% @spec address_list() -> List
address_list() ->
LinesLists = lists:map(fun get_interface_info/1, ["/sbin/ifconfig", "/bin/ifconfig", "ifconfig", "ipconfig /all"]),
%% io:format("LinesLists: ~p~n",[LinesLists]),
{ok, ALines} = regexp:split(join(LinesLists," "), "[\r\n]"),
%%raise "all of #{ cmds.join ' ' } failed" unless lines
Lines = lists:filter(fun(Elem) -> Elem /= [] end, ALines),

%% io:format("Lines: ~p~n",[Lines]),

MacRegex = "[: ]((?:[0-9A-F][0-9A-F][:\-]){5}[0-9A-F][0-9A-F])",

Candidates0 = lists:foldl(fun(Line, Acc) ->
case re:run(join(Line,""), MacRegex, [{capture,[1]}]) of
{match, [{Start, Length}]} ->
Expand All @@ -96,26 +66,13 @@ address_list() ->
end
end, [], Lines),

%% case regexp:first_match(Elem, Regexp) of
%% {match,Start,Length}
%% [Elem|Acc];
%% nomatch
%% {error,errordesc()}

%% {match, Matches} ->

%% {error, ErrorDesc} ->
%% Acc
%% end

Candidates = lists:reverse(lists:filter(fun(Elem) -> Elem /= "00-00-00-00-00-00" end, Candidates0)),
%% io:format("Candidates: ~p~n", [Candidates]),
case length(Candidates) of
0 -> throw({error, {no_mac_address_candidate, "No Mac Address"}});
_ -> ok
end,
Candidates.

%%% @spec address() -> string()
address() ->
hd(address_list()).

0 comments on commit 3fc940f

Please sign in to comment.