#include <stdlib.h>
#include <stdio.h>
#include <dos/dos.h>

static BPTR oldlock;
static int havelock;

static void __exitchdir(void)
{
  if (havelock) UnLock(CurrentDir(oldlock));
}


int chdir(const char *path)
{
  BPTR fl;
  int ret;

  if(!havelock){
      if (atexit(__exitchdir)) exit(EXIT_FAILURE);
      if (!(fl=Lock("",SHARED_LOCK))) exit(EXIT_FAILURE);
      oldlock=CurrentDir(fl);
      havelock=1;
  }

  if ((fl=Lock((STRPTR)path,SHARED_LOCK)))
  {
    UnLock(CurrentDir(fl));
    return(0);
  }
  return(EOF);
}

