Skip to content

Instantly share code, notes, and snippets.

@flashingpumpkin
Created August 28, 2011 23:16
Show Gist options
  • Save flashingpumpkin/1177376 to your computer and use it in GitHub Desktop.
Save flashingpumpkin/1177376 to your computer and use it in GitHub Desktop.
abspath(Path)->
Bits = filename:split(Path),
Folder = fun(Elem, Acc)->
case Elem of
".." ->
[ lists:nth(Seq, Acc) || Seq <- lists:seq(1, length(Acc)), Seq < length(Acc) ];
_ ->
Acc ++ [Elem]
end
end,
filename:join(lists:foldl(Folder, [], Bits)).
-ifdef(TEST).
-include_lib("eunit/include/eunit.hrl").
abspath_test()->
?assertEqual("/home/foo", abspath("/home/bar/../foo")),
?assertEqual("/etc/foo", abspath("/home/bar/../../etc/foo")).
-endif.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment