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 provides the -ALL switch to WHICH.
%	This function is provided 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

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

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

  pathlist = which(fun, '-all');

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

  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
  % Otherwise,

end

if strcmp(functionname,'where')
	dispmess='Sorry, I can''t look for occurrences of ''where''';
	if nargout==0
		disp(dispmess)
	else
		%messages=str2mat(messages,dispmess);
		%if size(messages,1)>1, messages(1,:)=''; end
		messages=0;
	end
	return;
end
exfn = exist(functionname);

if exfn == 0,
	dispmess=[functionname ' not found.'];
        if nargout==0
                disp(dispmess)
        else
                %messages=str2mat(messages,dispmess);
                %if size(messages,1)>1, messages(1,:)=''; end
		messages=exfn;
        end
	return
elseif exfn == 1,
	dispmess=[functionname ' is a variable.'];
        if nargout==0
                disp(dispmess)
        else
                %messages=str2mat(messages,dispmess);
                %if size(messages,1)>1, messages(1,:)=''; end
		messages=exfn;
        end
	return
elseif exfn == 5,
	dispmess=[functionname ' is a built-in function.'];
        if nargout==0
                disp(dispmess)
        else
                %messages=str2mat(messages,dispmess);
                %if size(messages,1)>1, messages(1,:)=''; end
                messages=exfn;
        end
	return
end

while flag,

    first = which(functionname);

    if ~isstr(first) %not found as a file
	if ~first %not found
		if ~found %not found at all
		  if any(functionname=='.')
		    dispmess=[functionname ' not found by ''which'', but',...
		    	' exist(''',functionname,''') returns ',...
			'code ',int2str(exfn)];
		  else
		    dispmess=[functionname '.m not found by ''which'', but',...
		    	' exist(''',functionname,''') returns ',...
			'code ',int2str(exfn)];
		  end
        	  if nargout==0
			disp(dispmess)
		  else
                  	messages=str2mat(messages,dispmess);
        	  end
		else
			matlabpath(oldpath);	
		end
		cd(oldpwd);
        	if nargout>0
                      if size(messages,1)>1, messages(1,:)=''; end
        	end
		return
	elseif first == 5,
		dispmess=[functionname ' is a built-in function.'];
                if nargout==0
			disp(dispmess)
		else
                    messages=str2mat(messages,dispmess);
                end
		flag = 0;
	end
    else %found as a file
	found = found + 1;
	if nargout==0
		disp(first)
	else
            	messages=str2mat(messages,first);
        end

        % Is a MEX file?, look for a .m

	if any(findstr(first,'.mex'))|any(findstr(first,'.dll'))
		exfn = [first(1:max(find(first == '.'))),'m'];
		if exist(exfn) == 2,
			if nargout==0
			  disp(exfn)
			else
             		  messages=str2mat(messages,exfn);
        		end
		end
	end

        fp = first(1:max(find(first == dirsep))-1);

        % Is this the current dir?

	if strcmp(fp,oldpwd),
		cd(m); %switch to another directory to hide the current one
		if findstr([oldpath,pathsep],[fp,pathsep]),
			rmpath(fp);
		end
	else
		rmpath(fp);
	end
    end

end %while
%
if nargout>0
	if size(messages,1)>1
		if all(messages(1,:)==' '), messages(1,:)=''; end
	end
end
%
%Restore path and change back to working directory
matlabpath(oldpath);
cd(oldpwd);
%
