/*
   <p>Switch point of reference to the nominated directory.</p>

   <kbd>
   <b>environ - C programming support functions</b><br>
   Copyright (C) 2005  Martin C Gregorie
   
   This program is free software; you can redistribute it and/or
   modify it under the terms of the Lesser GNU General Public License.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
  
   You should have received a copy of the Lesser GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
   </kbd>
  
   <p>Contact Martin Gregorie at <i>martin@gregorie.org</i></p>
*/

#include <stdio.h>
#include "environ.h"

#ifdef MSDOS
#include <dir.h>
#endif

#ifdef LINUX
#include <unistd.h>
#endif

/*
   Switch the program's current directory to <u>path</u>.
   Return zero on success and an error indication if the operation failed.

   The OS9/68K and Linux versions of this function are
   identical to <i>chdir()</i>. The MS-DOS version has some
   extra work to do to switch drives as well and to clean
   up in a tidy fashion if the combination of switching
   drive and directory does not work.
*/

#ifdef OS9
int swapdir(path)
char *path;
{
    int r;

    r = chdir(path);
    return r;
}
#endif

#ifdef MSDOS
int swapdir(const char *path)
{
    int     flags,
            olddrive,
            newdrive,
            r;
    char    drive[MAXDRIVE],
            dir[MAXDIR],
            file[MAXFILE],
            ext[MAXEXT];
            
    int fnsplit(const char *path, char *drive, char *dir, 
                char *name, char *ext);

    olddrive = getdisk();                        /* current drive */
    flags = fnsplit(path,drive,dir,file,ext);    /* parse pathname */
    if (flags & DRIVE)
        newdrive = toupper(drive[0]) - (int)'A'; /* drive no */
    else
        newdrive = olddrive;                     /* default = current */
    r = chdir(path);                             /* swap directories */
    if (r == 0)
        setdisk(newdrive);                       /* if OK, swap drives */
    return r;
}
#endif

#ifdef LINUX
int swapdir(char *path)
{
    int r;

    r = chdir(path);
    return r;
}
#endif


