function [status, result] = dos( varargin )
%DOS Execute DOS command in save current directory and return result.
%   Uses built-in DOS command after changing the current directory to
%   one that is not a UNC path or nested too deeply to work reliably.
%
%   See also DOS.

%   Copyright (c) 1984-98 by The MathWorks, Inc.
%   $Revision: 1.2 $  $Date: 1998/10/06 21:55:41 $

if ~isa(varargin{1},'printjob')
    error('Requires PrintJob object')
end

if ~strncmp(computer,'PC',2),
    error('Printjob DOS method called on non-DOS based platform.')
end

wasErr = 0;
try
    %Check to see if cwd is going to be a problem.
    %UNC test is new (DOS doesn't work on UNC path), CD test is from Jason:
    %If cwd is bad, move to what is considered the temporary directory
    %or c:\ if tempdir is itself a UNC directory.
    if strncmp(tempdir, '\\', 2)
        tdir = 'c:\';
    else
        tdir = tempdir;
    end
    OldDir = pwd;
    if strncmp( OldDir, '\\', 2 ) %i.e. path starts with \\
        cd(tdir)
    else
        % This is to check and see if the dos command is working.  In Win95
        % if the current directory is a deeply nested directory or sometimes
        % for TAS served file systems, the output pipe does not work.  The 
        % solution is to make the current directory safe, C:\ and put it back
        % when we are done.  The test is the cd command, which should always
        % return something.
        [status, result] = dos('cd');
        if isempty(result)
            cd(tdir)
        else
            OldDir = [];
        end
    end
    
    %Execute the given DOS command.
    [status, result] = dos(varargin{2:end});
    
catch
    wasErr = 1;
end

%Move back if neccesary.
if ~isempty(OldDir)
    cd(OldDir);
end

if wasErr
    error(lasterr)
end

