home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <dos.h>
- #include <proto/dos.h>
- #include <ctype.h>
- #include <libraries/dos.h>
-
- #include "ftp.h"
-
- /* Given a working directory and an arbitrary pathname, resolve them into
- * an absolute pathname. Memory is allocated for the result, which
- * the caller must free
- */
- char *pathname(char *cd,char *path)
- /* Current working directory */
- /* Pathname argument */
- {
- BPTR cdlock, pathlock, oldcdlock;
- static char filenamebuf[FMSIZE];
- register char *cp;
-
- if(cd==NULL || path==NULL) return NULL;
-
- /* Strip any leading white space on args */
- while(isspace(*cd)) cd++;
- while(isspace(*path)) path++;
-
- /*
- * Here's the plan: change the actual working directory to the
- * directory specified by `cd'. Then try to lock `path' relative
- * to that directory. Finally, compute the absolute path based on
- * that lock.
- */
-
- if(!(cdlock = Lock(cd, SHARED_LOCK))) return NULL;
-
- if (!(oldcdlock = CurrentDir(cdlock))) {
- /* this might fail: `cd' might not have been a directory */
- UnLock(cdlock);
- return NULL;
- }
-
- if (!(pathlock = Lock(path,SHARED_LOCK))) {
- /*
- * Special kludge:
- *
- * If we're trying to figure the name of a file which doesn't
- * exist, then we have to fake it. Take the easy way out:
- * don't allow `path' to have any path seperator characters in
- * it and just concatenate it to the real name of `cd'. This
- * way we don't have to worry about someone trying to change
- * directories up using /// sequences.
- */
- if (strpbrk(path, "/:")) {
- /* Sorry, too clever, you lose */
- UnLock(CurrentDir(oldcdlock));
- return NULL;
- }
-
- /* Get the real name of `cd' and concatenate `path' to it */
- if (getpath(cdlock, filenamebuf)) {
- UnLock(CurrentDir(oldcdlock));
- return NULL;
- }
-
- /* done with lock */
- UnLock(CurrentDir(oldcdlock));
- /*
- * Horrible kludge. getpath() screws up and doesn't put a colon
- * on the volume name if it is the only component on the path.
- */
- for (cp = filenamebuf; *cp && *cp != '/' && *cp != ':'; cp++)
- /* void */ ;
-
- if (*cp) { /* did we find a pathname separator? */
- /* yes */
- cp = malloc(strlen(filenamebuf)+strlen(path) + 2);
- strcpy(cp, filenamebuf);
- strcat(cp, "/");
- strcat(cp, path);
- return cp;
- } else {
- cp = malloc(strlen(filenamebuf)+strlen(path) + 2);
- strcpy(cp, filenamebuf);
- strcat(cp, ":");
- strcat(cp, path);
- return cp;
- }
- /*NOTREACHED*/
- }
-
- /*
- * we're done with the the cdlock Lock; restore the current
- * working directory to what it was when we started.
- */
-
- UnLock(CurrentDir(oldcdlock));
- /*
- * Ok, now we've got a lock on the file specified relative to the
- * directory specified. Get its absolute pathname.
- */
-
- if (getpath(pathlock, filenamebuf)) {
- UnLock(pathlock);
- printf("pathname(%s,%s): getpath() fails\n", cd, path);
- return NULL;
- }
-
- UnLock(pathlock);
-
- /*
- * Horrible kludge. getpath() screws up and doesn't put a colon
- * on the volume name if it is the only component on the path.
- */
- for (cp = filenamebuf; *cp && *cp != '/' && *cp != ':'; cp++)
- /* void */ ;
-
- if (*cp) /* did we find a pathname separator? */
- return strdup(filenamebuf);
-
- cp = malloc(strlen(filenamebuf)+2);
- strcpy(cp, filenamebuf);
- strcat(cp, ":");
- return cp;
- }
-