home *** CD-ROM | disk | FTP | other *** search
/ Meeting Pearls 3 / Meeting_Pearls_III.iso / Pearls / tcp / Networking / TCP / Server / wu-ftpd / src / unixdirs.c < prev    next >
C/C++ Source or Header  |  1994-08-28  |  3KB  |  196 lines

  1. /*
  2.  * Routine to convert a UNIX-style path to an AmigaDOS path.
  3.  * 
  4.  * Based on routine by Martin W. Scott (from UnixDirs II).
  5.  * Major cleanup and bugfixing for use in wu-ftpd by Blaz Zupan, 07/94
  6.  */
  7.  
  8. #include <string.h>
  9. #include <stdlib.h>
  10. #include <exec/memory.h>
  11. #include <proto/exec.h>
  12.  
  13. extern char AmigaRootDir[];
  14. extern int anonymous, guest;
  15.  
  16. void *pool = NULL;        /* my private memory pool */
  17.  
  18. /*
  19.  * "path" is a pointer to the Unix path to be
  20.  * converted. The result is put back into "path"
  21.  * because it is never longer than the original path.
  22.  */
  23. void
  24. UnixToAmiga (char *path)
  25. {
  26.   char *s, *t, *start;
  27.  
  28.   s = path;
  29.   start = path;
  30.  
  31.   if (t = strchr (path, ':'))    /* check for ':' in path */
  32.   {
  33.     t++;            /* copy device component */
  34.     while (path < t)
  35.       *s++ = *path++;
  36.   }
  37.  
  38.   while (path[0])
  39.   {
  40.     if ((path == start || path[-1] == '/') && path[0] == '.')
  41.     {
  42.       if (path[1] == '.')
  43.       {
  44.     if (path[2] == '/')
  45.     {
  46.       path += 2;
  47.       *s++ = *path++;
  48.     }
  49.     else if (!path[2])
  50.     {
  51.       path += 2;
  52.       *s++ = '/';
  53.     }
  54.     else
  55.       *s++ = *path++;
  56.       }
  57.       else
  58.       {
  59.     if (path[1] == '/')
  60.       path += 2;
  61.     else if (!path[1])
  62.       path += 1;
  63.     else
  64.       *s++ = *path++;
  65.       }
  66.     }
  67.     else
  68.       *s++ = *path++;
  69.   }
  70.   *s = '\0';
  71. }
  72.  
  73. /*
  74.  * Allocates memory using V39 pool routines
  75.  * (uses amiga.lib routines which also work under 2.0)
  76.  */
  77. void *
  78. MyAlloc (int size)
  79. {
  80.   void *mem = NULL;
  81.  
  82.   if (!pool)
  83.     pool = LibCreatePool (MEMF_CLEAR, 256, 256);
  84.  
  85.   if (pool)
  86.     mem = LibAllocPooled (pool, size);
  87.  
  88.   return mem;
  89. }
  90.  
  91. /*
  92.  * Frees all memory that was allocated by
  93.  * GetAmigaPath(). Should be called after
  94.  * every FTP command that is executed.
  95.  */
  96. void
  97. FreeAmigaPath (void)
  98. {
  99.   if (pool)
  100.     LibDeletePool (pool);
  101.   pool = NULL;
  102. }
  103.  
  104. /*
  105.  * Converts a Unix absolute path (like /pub/incoming)
  106.  * to an Amiga absolute path (like Work:Archive/pub/incoming)
  107.  * and also does Unix special directory conversion with
  108.  * UnixToAmiga().
  109.  *
  110.  * Result *MUST* be freed with free()!
  111.  */
  112. char *
  113. GetAmigaPath (char *path)
  114. {
  115.   char *newpath;
  116.   static BOOL done_atexit = FALSE;
  117.  
  118.   if (!done_atexit)
  119.   {
  120.     atexit (FreeAmigaPath);
  121.     done_atexit = TRUE;
  122.   }
  123.  
  124.   if ((anonymous || guest) && *path == '/')
  125.   {
  126.     newpath = MyAlloc (strlen (AmigaRootDir) + strlen (path) + 1);
  127.     if (newpath)
  128.     {
  129.       strcpy (newpath, AmigaRootDir);
  130.       strcat (newpath, path);
  131.     }
  132.   }
  133.   else
  134.   {
  135.     newpath = MyAlloc (strlen (path) + 1);
  136.     if (newpath)
  137.       strcpy (newpath, path);
  138.   }
  139.   if (newpath)
  140.     UnixToAmiga (newpath);
  141.   return newpath;
  142. }
  143.  
  144. /*
  145.  * Returns current working directory in Unix notation.
  146.  */
  147. char *
  148. GetUnixPath (char *path)
  149. {
  150.   char *newpath;
  151.  
  152.   if (newpath = MyAlloc (strlen (path + 1)))
  153.   {
  154.     if ((anonymous || guest) && !strnicmp (path, AmigaRootDir, strlen (AmigaRootDir)))
  155.     {
  156.       if (*(path + strlen (AmigaRootDir)) == '/')
  157.     strcpy (newpath, path + strlen (AmigaRootDir));
  158.       else if (!(*(path + strlen (AmigaRootDir))))
  159.       {
  160.     *newpath = '/';
  161.     *(newpath + 1) = 0;
  162.       }
  163.     }
  164.     else
  165.       strcpy (newpath, path);
  166.   }
  167.   return newpath;
  168. }
  169.  
  170. #ifdef TEST
  171.  
  172. #include <proto/exec.h>
  173. #include <stdio.h>
  174.  
  175. int
  176. main (int argc, char **argv)
  177. {
  178.   int i;
  179.  
  180.   for (i = 1; i < argc; i++)
  181.   {
  182.     char *buf;
  183.  
  184.     if (buf = AllocMem (512, 0))
  185.     {
  186.       CopyMem (argv[i], buf, 512);
  187.       UnixToAmiga (buf);
  188.       printf ("%s --> %s\n", argv[i], buf);
  189.       FreeMem (buf, 512);
  190.     }
  191.   }
  192.   return 0;
  193. }
  194.  
  195. #endif
  196.