
#include <libraries/dos.h>
#include <exec/memory.h>
#include <functions.h>

#ifdef TEST
#include <stdio.h>
#endif

#ifndef NULL
#define NULL 0L
#endif

char * malloc();

/*--------------------------------------------------------------*/
/*    GetCurrentPath: get full path of the current directory    */
/*--------------------------------------------------------------*/

static void GetCurrentPath( path )
register char *path;
{

	char s1[ 108 ];
	char *name;
	register struct Lock *locka, *lockb;
        register struct FileInfoBlock *fib;

	fib = (struct FileInfoBlock *)AllocMem(
	      (long)sizeof(struct FileInfoBlock), MEMF_CHIP | MEMF_CLEAR);
	if ( fib == 0L )
	   {
#ifdef TEST
	     fprintf( stderr, "GetCurrentPath: Oops - Memory allocation failed\n");
#endif
	     *path = '\0';
	     return;
	   }
	
	locka = Lock("", ACCESS_READ );
	*path = s1[0] = '\0';

	while ( locka != NULL )
	  {
	    Examine( locka, fib );
	    name = fib->fib_FileName;
	    if ( *name == '\0' )
		strcpy( path, "RAM" ); /* Patch for Ram disk bug */
	    else
		strcpy( path, name );
	    lockb = ParentDir( locka );
	    UnLock( locka );
	    
	    if ( lockb == NULL )
	        strcat( path, ":");
	    else if ( s1[0] != '\0' )
		strcat( path, "/");
	    strcat( path, s1 );
	    strcpy( s1, path );
	    locka = lockb;
	  }

	FreeMem( fib, (long)sizeof(struct FileInfoBlock) );
}

/*--------------------------------------------------------------*/
/*	getcwd: return the path name of the current directory	*/
/*--------------------------------------------------------------*/

char *getcwd( path, size )
char *path;
int size;
{
	if ( path == (char *)NULL )
	   {
	     if ( (path = malloc(108)) == NULL)
		{
#ifdef TEST
		   fprintf( stderr, "getcwd: malloc failed to get 108 bytes\n");
#endif
		   return NULL;
		}
	   }
	GetCurrentPath( path );
	return path;
}

#ifdef TEST
main()
{

	fprintf( stderr, "%s\n", getcwd( NULL, 0 ));
}

#endif


