function tmp_dir = tempdir
%TEMPDIR Get temporary directory.
%   TEMPDIR returns the name of the temporary directory if one exists
%
%   See also TEMPNAME, FULLFILE.

%   Marc Ullman  2-8-93
%   Copyright 1984-2000 The MathWorks, Inc. 
%   $Revision: 5.12 $  $Date: 2000/06/11 23:22:32 $

comp = [computer '    '];
if (comp(1:2) =='PC')
    tmp_dir = getenv('TEMP');       % Microsoft's recommended name
    if ( isempty(tmp_dir) )
        tmp_dir = getenv('TMP');    % What everybody else uses
    end
    if ( ~isempty(tmp_dir) )
        % Make sure name ends with a valid path separator
        last_char = tmp_dir(length(tmp_dir));
        if ((last_char ~= '\') & (last_char ~= '/'))
            tmp_dir = [tmp_dir '\'];
        end
    end
else
    % Assume we are on a UNIX system
    tmp_dir = getenv('TMP');
    if isempty(tmp_dir)
        tmp_dir = '/tmp/';
    end
    if (tmp_dir(length(tmp_dir)) ~= '/')
        tmp_dir = [tmp_dir '/'];
    end
end

% Protect against an empty tmp_dir
if isempty(tmp_dir),
  tmp_dir = cd; % Use current directory
end
