home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD1.bin / useful / comm / tcp / ftpdaemon / source / pathname.c < prev    next >
C/C++ Source or Header  |  1993-12-26  |  3KB  |  127 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <dos.h>
  5. #include <proto/dos.h>
  6. #include <ctype.h>
  7. #include <libraries/dos.h>
  8.  
  9. #include "ftp.h"
  10.  
  11. /* Given a working directory and an arbitrary pathname, resolve them into
  12.  * an absolute pathname. Memory is allocated for the result, which
  13.  * the caller must free
  14.  */
  15. char *pathname(char *cd,char *path)
  16.     /* Current working directory */
  17.     /* Pathname argument */
  18. {
  19.     BPTR    cdlock, pathlock, oldcdlock;
  20.     static char filenamebuf[FMSIZE];
  21.     register char *cp;
  22.  
  23.     if(cd==NULL || path==NULL) return NULL;
  24.  
  25.     /* Strip any leading white space on args */
  26.     while(isspace(*cd)) cd++;
  27.     while(isspace(*path)) path++;
  28.  
  29.     /*
  30.      * Here's the plan:  change the actual working directory to the 
  31.      * directory specified by `cd'.  Then try to lock `path' relative
  32.      * to that directory.  Finally, compute the absolute path based on
  33.      * that lock.
  34.      */
  35.  
  36.     if(!(cdlock = Lock(cd, SHARED_LOCK))) return NULL;
  37.  
  38.     if (!(oldcdlock = CurrentDir(cdlock))) {
  39.         /* this might fail:  `cd' might not have been a directory */
  40.         UnLock(cdlock);
  41.         return NULL;
  42.     }
  43.  
  44.     if (!(pathlock = Lock(path,SHARED_LOCK))) {
  45.         /*
  46.          *  Special kludge:
  47.          *
  48.          *  If we're trying to figure the name of a file which doesn't
  49.          *  exist, then we have to fake it.  Take the easy way out: 
  50.          *  don't allow `path' to have any path seperator characters in
  51.          *  it and just concatenate it to the real name of `cd'.  This
  52.          *  way we don't have to worry about someone trying to change
  53.          *  directories up using /// sequences.
  54.          */
  55.         if (strpbrk(path, "/:")) {
  56.             /* Sorry, too clever, you lose */
  57.             UnLock(CurrentDir(oldcdlock));
  58.             return NULL;
  59.         }
  60.  
  61.         /* Get the real name of `cd' and concatenate `path' to it */
  62.         if (getpath(cdlock, filenamebuf)) {
  63.             UnLock(CurrentDir(oldcdlock));
  64.             return NULL;
  65.         }
  66.         
  67.         /* done with lock */
  68.         UnLock(CurrentDir(oldcdlock));
  69.         /*
  70.          *  Horrible kludge.  getpath() screws up and doesn't put a colon
  71.          *  on the volume name if it is the only component on the path.
  72.          */
  73.         for (cp = filenamebuf; *cp && *cp != '/' && *cp != ':'; cp++)
  74.             /* void */ ;
  75.  
  76.         if (*cp) {    /* did we find a pathname separator? */
  77.             /* yes */
  78.             cp = malloc(strlen(filenamebuf)+strlen(path) + 2);
  79.             strcpy(cp, filenamebuf);
  80.             strcat(cp, "/");
  81.             strcat(cp, path);
  82.             return cp;
  83.         } else {
  84.             cp = malloc(strlen(filenamebuf)+strlen(path) + 2);
  85.             strcpy(cp, filenamebuf);
  86.             strcat(cp, ":");
  87.             strcat(cp, path);
  88.             return cp;
  89.         }
  90.         /*NOTREACHED*/
  91.     }
  92.  
  93.     /*
  94.      * we're done with the the cdlock Lock;  restore the current 
  95.      * working directory to what it was when we started.
  96.      */
  97.       
  98.     UnLock(CurrentDir(oldcdlock));
  99.     /*
  100.      *  Ok, now we've got a lock on the file specified relative to the
  101.      *  directory specified.  Get its absolute pathname.
  102.      */
  103.  
  104.     if (getpath(pathlock, filenamebuf)) {
  105.         UnLock(pathlock);
  106.         printf("pathname(%s,%s): getpath() fails\n", cd, path);
  107.         return NULL;
  108.     }
  109.  
  110.     UnLock(pathlock);
  111.  
  112.     /*
  113.      *  Horrible kludge.  getpath() screws up and doesn't put a colon
  114.      *  on the volume name if it is the only component on the path.
  115.      */
  116.     for (cp = filenamebuf; *cp && *cp != '/' && *cp != ':'; cp++)
  117.         /* void */ ;
  118.  
  119.     if (*cp)    /* did we find a pathname separator? */
  120.         return strdup(filenamebuf);
  121.  
  122.     cp = malloc(strlen(filenamebuf)+2);
  123.     strcpy(cp, filenamebuf);
  124.     strcat(cp, ":");
  125.     return cp;
  126. }
  127.