Skip to content

Instantly share code, notes, and snippets.

@rnewson
Created July 15, 2014 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rnewson/e3d6425244528881c2b7 to your computer and use it in GitHub Desktop.
Save rnewson/e3d6425244528881c2b7 to your computer and use it in GitHub Desktop.
#!/usr/bin/env escript
main(Path) ->
{ok, Fd} = file:open(Path, [binary, read, raw]),
Count = count_headers(Fd),
file:close(Fd),
io:format("~s contains ~B headers.~n", [Path, Count]).
count_headers(Fd) ->
count_headers(Fd, 0, 0).
count_headers(Fd, Offset, Acc) ->
case file:pread(Fd, Offset, 1) of
{ok, <<1>>} ->
count_headers(Fd, Offset + 4096, Acc + 1);
{ok, <<0>>} ->
count_headers(Fd, Offset + 4096, Acc);
eof ->
Acc
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment