/* memcopy - copy bytes from one memory location to another
 *
 * Copyright (C) 1994 by Ingo Wilken (Ingo.Wilken@informatik.uni-oldenburg.de)
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose and without fee is hereby granted, provided
 * that the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation.  This software is provided "as is" without express or
 * implied warranty.
 *
 * V1.0: 09/Dec/94 first version
 */
#define THIS_PROGRAM    "memcopy"
#define THIS_VERSION    "1.0"

#include <exec/types.h>
#include <exec/libraries.h>
#include <dos/dos.h>
#include <dos/rdargs.h>
#include <stdlib.h>
#include <string.h>

#define SysBase_DECLARED
#include <proto/exec.h>
#include <proto/dos.h>
#include <proto/utility.h>

extern struct Library *SysBase;

static const char amiga_version[] = "\0$VER: " THIS_PROGRAM " " THIS_VERSION " (" __COMMODORE_DATE__ ")";

const char Template[] = "FROM/A,TO/A,NUM/N,STRING/S";
__aligned struct {
    STRPTR  from;
    STRPTR  to;
    LONG *  num;
    LONG    isstring;
} Args;

void
_main()
{
    struct RDArgs *rdargs;
    APTR addr1, addr2;
    LONG rc = RETURN_OK;

    if( SysBase->lib_Version < 37 ) {
        #define MESSAGE "requires AmigaOS 2.04 or higher\n"
        Write(Output(), MESSAGE, sizeof(MESSAGE)-1);
        _exit(RETURN_FAIL);
        #undef MESSAGE
    }

    if( rdargs = ReadArgs(Template, (LONG *)&Args, NULL) ) {
        char *endp1, *endp2;
        addr1 = (APTR)strtoul(Args.from, &endp1, 0);
        addr2 = (APTR)strtoul(Args.to  , &endp2, 0);
        if( *endp2 ) {
            PutStr("invalid address\n");
            rc = RETURN_ERROR;
        }
        else {
            if( *endp1 || Args.isstring ) {  /* FROM arg is a string */
                if( Args.num )
                    strncpy((char *)addr2, Args.from, *(Args.num));
                else
                    strcpy((char *)addr2, Args.from);
            }
            else {
                if( Args.num )
                    CopyMem(addr1, addr2, *(Args.num));
                else
                    strcpy((char *)addr2, (char *)addr1);
            }
        }
        FreeArgs(rdargs);
    }
    else {
        PutStr("required argument missing\n");
        rc = RETURN_ERROR;
    }
    _exit((int)rc);
}

