home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d1xx / d109 / uupc.lha / UUpc / Source / LOCAL / getcwd.c < prev    next >
C/C++ Source or Header  |  1987-10-28  |  2KB  |  98 lines

  1.  
  2. #include <libraries/dos.h>
  3. #include <exec/memory.h>
  4. #include <functions.h>
  5.  
  6. #ifdef TEST
  7. #include <stdio.h>
  8. #endif
  9.  
  10. #ifndef NULL
  11. #define NULL 0L
  12. #endif
  13.  
  14. char * malloc();
  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[ 108 ];
  25.     char *name;
  26.     register struct Lock *locka, *lockb;
  27.         register struct FileInfoBlock *fib;
  28.  
  29.     fib = (struct FileInfoBlock *)AllocMem(
  30.           (long)sizeof(struct FileInfoBlock), MEMF_CHIP | MEMF_CLEAR);
  31.     if ( fib == 0L )
  32.        {
  33. #ifdef TEST
  34.          fprintf( stderr, "GetCurrentPath: Oops - Memory allocation failed\n");
  35. #endif
  36.          *path = '\0';
  37.          return;
  38.        }
  39.     
  40.     locka = Lock("", ACCESS_READ );
  41.     *path = s1[0] = '\0';
  42.  
  43.     while ( locka != NULL )
  44.       {
  45.         Examine( locka, fib );
  46.         name = fib->fib_FileName;
  47.         if ( *name == '\0' )
  48.         strcpy( path, "RAM" ); /* Patch for Ram disk bug */
  49.         else
  50.         strcpy( path, name );
  51.         lockb = ParentDir( locka );
  52.         UnLock( locka );
  53.         
  54.         if ( lockb == NULL )
  55.             strcat( path, ":");
  56.         else if ( s1[0] != '\0' )
  57.         strcat( path, "/");
  58.         strcat( path, s1 );
  59.         strcpy( s1, path );
  60.         locka = lockb;
  61.       }
  62.  
  63.     FreeMem( fib, (long)sizeof(struct FileInfoBlock) );
  64. }
  65.  
  66. /*--------------------------------------------------------------*/
  67. /*    getcwd: return the path name of the current directory    */
  68. /*--------------------------------------------------------------*/
  69.  
  70. char *getcwd( path, size )
  71. char *path;
  72. int size;
  73. {
  74.     if ( path == (char *)NULL )
  75.        {
  76.          if ( (path = malloc(108)) == NULL)
  77.         {
  78. #ifdef TEST
  79.            fprintf( stderr, "getcwd: malloc failed to get 108 bytes\n");
  80. #endif
  81.            return NULL;
  82.         }
  83.        }
  84.     GetCurrentPath( path );
  85.     return path;
  86. }
  87.  
  88. #ifdef TEST
  89. main()
  90. {
  91.  
  92.     fprintf( stderr, "%s\n", getcwd( NULL, 0 ));
  93. }
  94.  
  95. #endif
  96.  
  97.  
  98.