function [paths] = where(fun)
%WHERE	List all occurences of a function on your path
%	
%	USAGE: WHERE(functionname)
%	messages = WHERE(functioname) also returns a string array
%
%	Matlab 5 now provides the -ALL switch to WHICH;
%	this function is provided just for backward compatibility.

% Original script by Drea Thomas, reimplemented for Matlab 5 =)
% Notable differences from original implementation:
%    * "where foo.ext" should work regardless of what "ext" is
%    * "where where" works
%    * variables are properly dealt with

if ~isstr(fun)
  error('Argument is not a string.');
end

pathlist = evalin('caller', [ 'which(''' fun ''', ''-all'')' ]);

if nargout == 0
  % With no output arguments, works almost like which('fun','all').
  % Except it only displays one line for built-in functions.

  if isempty(pathlist)
    disp([fun ' not found.']);

  elseif strcmp(pathlist(1), 'built-in')
    disp([fun ' is a built-in function.']);

  elseif strcmp(pathlist(1), 'variable')
    disp([fun ' is a variable.']);

  else
    % If we use str2mat, and *one* line is too long, *each* gets a line
    % break.  So we use disp() in a loop instead.
    for path = pathlist,
      if strcmp(path, 'built-in')
	disp([fun ' is a built-in function.']);
      else
	disp(path{:});
      end
    end

  end

else
  % Return either a path or the value from exists().

  if isempty(pathlist)
    paths = 0;

  elseif strcmp(pathlist(1), 'built-in') | strcmp(pathlist(1), 'variable')
    paths = evalin('caller', [ 'exist(''' fun ''')' ]);

  else
    paths = pathlist(1);

  end
end
