#include <exec/memory.h>
#include <libraries/dos.h>
#include <libraries/dosextens.h>
extern void *AllocMem();
extern struct Task *FindTask();
extern struct FileLock *CurrentDir();
extern struct FileLock *DupLock();
extern struct FileLock *ParentDir();

int getcwd(buf,l)
char *buf;
int l;
{
  struct FileLock *lock, *newlock;
  struct FileInfoBlock *fib;
  struct Process *myproc;
  int i,len;
  char *name;

  if((fib=(struct FileInfoBlock *)AllocMem(sizeof(struct FileInfoBlock),
                                           MEMF_CLEAR|MEMF_PUBLIC))==NULL) {
    return(NULL);
  }
  CurrentDir(DupLock(lock=CurrentDir(NULL)));

  buf[i=l]='\0';
  while (lock) {
    newlock = (struct FileLock *)ParentDir(lock);
    Examine(lock, fib);
    name = fib->fib_FileName;
    if(*name == '\0')            /* HACK TO FIX RAM: DISK BUG */
      name = "ram";

    len = strlen(name);
    if(newlock) {
      if(i == 255) {
        i -= len;
        bcopy(name, buf + i, len);
      }
      else {
        i -= len +1;
        bcopy(name, buf + i, len);
        buf[i+len] = '/';
      }
    }
    else {
      i -= len + 1;
      bcopy(name, buf + i, len);
      buf[i+len] = ':';
    }
    UnLock(lock);
    lock = newlock;
  }
  FreeMem(fib, (long)sizeof(struct FileInfoBlock));
  bcopy(buf+i,buf,(l-i)+1);
  return(1);
}

main()
{
  char buf[255];
  getcwd(buf,255);
  puts(buf);
}

