-module(bug).

-export([pid2name/1]).

-define(FILE_SERVER, file_server_2).   % Registered name
-define(FILE_IO_SERVER_TABLE, file_io_servers).
-type filename_all() :: string() | binary().

-spec pid2name(Pid) -> {ok, Filename} | undefined when
          Filename :: filename_all(),
          Pid :: pid().

pid2name(Pid) when is_pid(Pid) ->
    case whereis(?FILE_SERVER) of
	undefined ->
	    undefined;
	_ ->
	    case ets:lookup(?FILE_IO_SERVER_TABLE, Pid) of
		[{_, Name} | _] ->
		    {ok, Name};
		_ ->
		    undefined
	    end
    end.
