home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 4 / CDPD_IV.bin / networking / tcpip / amitcp-support / wustl-ftpdaemon / support / unixdirs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-29  |  2.2 KB  |  116 lines

  1. /*
  2.  * Routine to convert a UNIX-style path to an AmigaDOS path.
  3.  * A UNIX-style path is understood here as follows:
  4.  * 
  5.  *     leading . means current dir; like "", but filled in explicitly
  6.  *     ../ means /
  7.  *     ..  means /
  8.  * 
  9.  * Written as part of UnixDirs2, a (to be written) system patch which allows
  10.  * use of UNIX-style paths everywhere.
  11.  * 
  12.  * Martin W. Scott,  8 January 1993
  13.  */
  14. #include <exec/types.h>
  15. #include <exec/execbase.h>
  16. #include <dos/dos.h>
  17. #include <dos/dosextens.h>
  18. #include <string.h>
  19. #include <proto/dos.h>
  20. #include <proto/exec.h>
  21.  
  22. extern struct ExecBase *SysBase;
  23.  
  24. /* insert $cwd into s, return pointer to next free char */
  25. char *
  26. insertcwd(char *s, LONG len)
  27. {
  28.     if (NameFromLock(((struct Process *)(SysBase->ThisTask))->pr_CurrentDir, s, len))
  29.     {
  30.         while (*s)
  31.             s++;
  32.     }
  33.     return s;
  34. }
  35.  
  36. /* adjust path from UNIX-style to Amiga-style */
  37. /* TO DO: length checking when building new path */
  38. BOOL
  39. adjustpath(char *path, char *newpath, LONG len)
  40. {
  41.     char *s, *t;
  42. #ifdef DEBUG
  43.     char *origpath = path;
  44. #endif
  45.     s = newpath;
  46.  
  47.     if (path == NULL)    /* bypass */
  48.         return FALSE;
  49.  
  50.     if (t = strchr(path, ':'))        /* check for ':' in path */
  51.     {
  52.         t++;                /* copy device component */
  53.         while (path < t)
  54.             *s++ = *path++;
  55.     }
  56.     else if (path[0] == '.')        /*** translate '.' to $cwd ***/
  57.     {
  58.         if (!path[1])            /* only "." */
  59.         {
  60.             s = insertcwd(s, len);
  61.             path++;            /* path[0] == '\0' - STOP */
  62.         }
  63.         else if (path[1] == '/')    /* initial component is $cwd */
  64.         {
  65.             s = insertcwd(s, len);
  66.             if (*(s-1) != ':')
  67.                 *s++ = '/';    /* copy '/', increment pointers */
  68.             path += 2;
  69.         }
  70.     }
  71.  
  72.     while (path[0])            /*** copy remainder of path ***/
  73.     {
  74.         if (path[0] == '.' && path[1] == '.')
  75.         {
  76.             if (path[2] == '/')    /* just skip "..", copying '/' */
  77.             {
  78.                 path += 2;
  79.                 *s++ = *path++;
  80.             }
  81.             else if (!path[2])    /* append '/' and stop */
  82.             {
  83.                 path += 2;
  84.                 *s++ = '/';
  85.             }
  86.             else *s++ = *path++;
  87.         }
  88.         else *s++ = *path++;
  89.     }
  90.     *s = '\0';
  91.  
  92.     return TRUE;
  93. }
  94.  
  95. #ifdef TEST
  96.  
  97. void
  98. main(int argc, char **argv)
  99. {
  100.     UWORD i;
  101.  
  102.     for (i = 1; i < argc; i++)
  103.     {
  104.         char *buf;
  105.  
  106.         if (buf = AllocMem(512, 0L))
  107.         {
  108.             adjustpath(argv[i], buf, 512);
  109.             printf("%s --> %s\n", argv[i], buf);
  110.             FreeMem(buf, 512);
  111.         }
  112.     }
  113.  
  114. } /* main */
  115.  
  116. #endif