home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / c / hce.lha / HCE / LibSource / clib / Misc / src / getcwd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-02  |  1.4 KB  |  77 lines

  1. #include <libraries/dosextens.h>   /* Was include <libraries/dos.h> . J.P. */
  2. #include <exec/memory.h>
  3. #include <limits.h>
  4.  
  5. #ifndef NULL
  6. #define NULL 0L
  7. #endif
  8.  
  9. typedef struct FileInfoBlock    FIB;
  10. typedef struct FileLock        LOCK;   /* Was struct Lock LOCK. J.P. */
  11.  
  12. extern    char *malloc();
  13. extern    void *AllocMem();
  14. LOCK    *ParentDir(), *Lock();
  15.  
  16. /*
  17.  *    GetCurrentPath: get full path of the current directory
  18.  */
  19.  
  20. static void GetCurrentPath( path )
  21. register char *path;
  22. {
  23.  
  24.     char s1[ PATHSIZE ];
  25.     char *name;
  26.     register LOCK    *locka, *lockb;
  27.         register FIB    *fib;
  28.  
  29.     fib = (FIB *)AllocMem((long)sizeof(FIB), MEMF_CHIP | MEMF_CLEAR);
  30.     if ( fib == NULL ) {
  31.         *path = '\0';
  32.         return;
  33.     }
  34.     
  35.     locka = Lock("", ACCESS_READ );
  36.     *path = s1[0] = '\0';
  37.  
  38.     while ( locka != NULL ) {
  39.         Examine( locka, fib );
  40.         name = fib->fib_FileName;
  41.         if ( *name == '\0' )
  42.             strcpy( path, "RAM" ); /* Patch for Ram disk bug */
  43.         else
  44.             strcpy( path, name );
  45.         lockb = ParentDir( locka );
  46.         UnLock( locka );
  47.         
  48.         if ( lockb == NULL )
  49.             strcat( path, ":");
  50.         else if ( s1[0] != '\0' )
  51.             strcat( path, "/");
  52.         strcat( path, s1 );
  53.         strcpy( s1, path );
  54.         locka = lockb;
  55.     }
  56.  
  57.     FreeMem( fib, (long)sizeof(FIB) );
  58. }
  59.  
  60. /*
  61.  *    getcwd: return the path name of the current directory
  62.  */
  63.  
  64. char *getcwd( path, size )
  65. char *path;
  66. int size;
  67. {
  68.     if ( path == (char *)NULL ) {
  69.         if ( (path = malloc( PATHSIZE ) ) == NULL)
  70.             return NULL;
  71.     }
  72.  
  73.     GetCurrentPath( path );
  74.     return path;
  75. }
  76.  
  77.