home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 200-299 / ff218.lzh / EdLib / getcwd.c < prev    next >
C/C++ Source or Header  |  1989-06-04  |  2KB  |  103 lines

  1. /*
  2.  * edlib v1.1 Copyright 1989 Edwin Hoogerbeets
  3.  * This code is freely redistributable as long as no charge other than
  4.  * reasonable copying fees are levied for it.
  5.  */
  6. #include "edlib.h"
  7. #include <libraries/dos.h>
  8. #include <exec/memory.h>
  9. #include <errno.h>
  10.  
  11. #define NULL                0L
  12. #define FIBSIZE             sizeof(struct FileInfoBlock)
  13. extern char                 *malloc();
  14. extern struct FileInfoBlock *AllocMem();
  15. extern struct FileLock      *ParentDir();
  16. extern struct FileLock      *Lock();
  17.  
  18. char *getcwd(buf,size)
  19. char *buf;
  20. int size;
  21. {
  22.   register struct FileLock *lock;
  23.   register int malloced = 0;
  24.  
  25.   if ( !buf ) {
  26.     if ( !(buf = malloc(size)) ) {
  27.       errno = ENOMEM;
  28.       return(NULL);
  29.     }
  30.     malloced = 1;
  31.   }
  32.  
  33.   /* get a lock on the current working directory */
  34.   lock = Lock("",ACCESS_READ);
  35.  
  36.   buf[0] = '\0';
  37.  
  38.   if ( !follow(lock,buf,size) ) {
  39.  
  40.     if ( malloced ) {
  41.       free(buf);
  42.     }
  43.  
  44.     UnLock(lock);
  45.  
  46.     return(NULL);
  47.   }
  48.  
  49.   UnLock(lock);
  50.  
  51.   return(buf);
  52. }
  53.  
  54. static int follow(lock,buf,size)
  55. struct FileLock *lock;
  56. char *buf;
  57. int size;
  58. {
  59.   register struct FileInfoBlock *fib;
  60.   register struct FileLock *newlock;
  61.  
  62.   /* at end of road */
  63.   if ( !lock ) {
  64.     return(1);
  65.   }
  66.  
  67.   fib = AllocMem(FIBSIZE,MEMF_CLEAR);
  68.  
  69.   if ( !fib )
  70.     return(0);
  71.  
  72.   newlock = ParentDir(lock);
  73.  
  74.   if ( !follow(newlock,buf,size) ) {
  75.     FreeMem(fib,FIBSIZE);
  76.     return(0);
  77.   }
  78.  
  79.   if ( Examine(lock,fib) ) {
  80.     register char c;
  81.  
  82.     if ( strlen(buf) + strlen(fib->fib_FileName) + 2 > size ) {
  83.       FreeMem(fib,FIBSIZE);
  84.       return(0);
  85.     }
  86.  
  87.     if ( newlock && (c = buf[strlen(buf)-1]) != ':' && c != '/' ) {
  88.       strcat(buf,"/");
  89.     }
  90.  
  91.     strcat(buf,&fib->fib_FileName[0]);
  92.  
  93.     if ( !newlock) {
  94.       strcat(buf,":");
  95.     }
  96.   }
  97.  
  98.   FreeMem(fib,FIBSIZE);
  99.  
  100.   return(1);
  101. }
  102.  
  103.