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 (c) 1984-98 by The MathWorks, Inc.
%   $Revision: 5.9 $  $Date: 1997/11/21 23:36:57 $

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
elseif (comp(1:3) == 'MAC')
    tmp_dir = getenv('TMP');
    last_char = tmp_dir(length(tmp_dir));
    if (last_char ~= ':')
        tmp_dir = [tmp_dir ':'];
    end
elseif (comp(5:7) == 'VMS')
    tmp_dir = 'SYS$SCRATCH:';
else
    % Assume we are on a UNIX system
    tmp_dir = '/tmp/';
end

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