%%% -*- mode: erlang -*-
%%% $Id: reversefile.erlang,v 1.1 2001/05/15 08:05:58 doug Exp $
%%% http://www.bagley.org/~doug/shootout/

-module(reversefile).
-export([main/0, main/1]).
-export([init/0]).

main() -> main(['1']).
main(Args) ->
    init(),
    halt(0).

init() ->
    Port = open_port({fd, 1, 0}, [eof, {line, 128}]),
    printlist(rev(Port, [])).

rev(Port, Lines) ->
    receive
    {Port, {_, {_, Line}}} ->
        rev(Port, [Line|Lines]);
    {Port, eof} ->
        Lines
    end.

printlist([])     -> [];
printlist([H|T])  -> io:format("~s~n", [H]), printlist(T).