/* File to implement the "chdir" command of Unix under AmigaDOS: */

#include <libraries/dos.h>
#include <libraries/dosextens.h>

extern struct FileLock *Lock();
extern void UnLock();
extern struct FileLock *CurrentDir();

int chdir(newpath)
  char *newpath;
{
   register struct FileLock *newdir;
   register struct FileLock *olddir;

   static struct FileInfoBlock fib;


   newdir=Lock(newpath);
   if (newdir==0) {
      puts("Chdir error: Couldn't get the lock!");
      return -1;  /* error: we didn't get a lock on the newpath. */
   }

   Examine(newdir,&fib);        /* see if it's a directory we can cd to... */
   if (fib.fib_DirEntryType<0) {
       UnLock(newdir);            /* it's a file, not a directory. */
       puts("Chdir error: It's a file, dummy!");
       return -1;
    }

    /* eventually we'll have to check protect bits, but not now: */
    olddir=CurrentDir(newdir);
    if (olddir==0)
       return -1;
    UnLock(olddir);  /* no longer needed? */

    return 0;
}

