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 >
Wrap
C/C++ Source or Header
|
1994-08-28
|
3KB
|
196 lines
/*
* Routine to convert a UNIX-style path to an AmigaDOS path.
*
* Based on routine by Martin W. Scott (from UnixDirs II).
* Major cleanup and bugfixing for use in wu-ftpd by Blaz Zupan, 07/94
*/
#include <string.h>
#include <stdlib.h>
#include <exec/memory.h>
#include <proto/exec.h>
extern char AmigaRootDir[];
extern int anonymous, guest;
void *pool = NULL; /* my private memory pool */
/*
* "path" is a pointer to the Unix path to be
* converted. The result is put back into "path"
* because it is never longer than the original path.
*/
void
UnixToAmiga (char *path)
{
char *s, *t, *start;
s = path;
start = path;
if (t = strchr (path, ':')) /* check for ':' in path */
{
t++; /* copy device component */
while (path < t)
*s++ = *path++;
}
while (path[0])
{
if ((path == start || path[-1] == '/') && path[0] == '.')
{
if (path[1] == '.')
{
if (path[2] == '/')
{
path += 2;
*s++ = *path++;
}
else if (!path[2])
{
path += 2;
*s++ = '/';
}
else
*s++ = *path++;
}
else
{
if (path[1] == '/')
path += 2;
else if (!path[1])
path += 1;
else
*s++ = *path++;
}
}
else
*s++ = *path++;
}
*s = '\0';
}
/*
* Allocates memory using V39 pool routines
* (uses amiga.lib routines which also work under 2.0)
*/
void *
MyAlloc (int size)
{
void *mem = NULL;
if (!pool)
pool = LibCreatePool (MEMF_CLEAR, 256, 256);
if (pool)
mem = LibAllocPooled (pool, size);
return mem;
}
/*
* Frees all memory that was allocated by
* GetAmigaPath(). Should be called after
* every FTP command that is executed.
*/
void
FreeAmigaPath (void)
{
if (pool)
LibDeletePool (pool);
pool = NULL;
}
/*
* Converts a Unix absolute path (like /pub/incoming)
* to an Amiga absolute path (like Work:Archive/pub/incoming)
* and also does Unix special directory conversion with
* UnixToAmiga().
*
* Result *MUST* be freed with free()!
*/
char *
GetAmigaPath (char *path)
{
char *newpath;
static BOOL done_atexit = FALSE;
if (!done_atexit)
{
atexit (FreeAmigaPath);
done_atexit = TRUE;
}
if ((anonymous || guest) && *path == '/')
{
newpath = MyAlloc (strlen (AmigaRootDir) + strlen (path) + 1);
if (newpath)
{
strcpy (newpath, AmigaRootDir);
strcat (newpath, path);
}
}
else
{
newpath = MyAlloc (strlen (path) + 1);
if (newpath)
strcpy (newpath, path);
}
if (newpath)
UnixToAmiga (newpath);
return newpath;
}
/*
* Returns current working directory in Unix notation.
*/
char *
GetUnixPath (char *path)
{
char *newpath;
if (newpath = MyAlloc (strlen (path + 1)))
{
if ((anonymous || guest) && !strnicmp (path, AmigaRootDir, strlen (AmigaRootDir)))
{
if (*(path + strlen (AmigaRootDir)) == '/')
strcpy (newpath, path + strlen (AmigaRootDir));
else if (!(*(path + strlen (AmigaRootDir))))
{
*newpath = '/';
*(newpath + 1) = 0;
}
}
else
strcpy (newpath, path);
}
return newpath;
}
#ifdef TEST
#include <proto/exec.h>
#include <stdio.h>
int
main (int argc, char **argv)
{
int i;
for (i = 1; i < argc; i++)
{
char *buf;
if (buf = AllocMem (512, 0))
{
CopyMem (argv[i], buf, 512);
UnixToAmiga (buf);
printf ("%s --> %s\n", argv[i], buf);
FreeMem (buf, 512);
}
}
return 0;
}
#endif