#include "lib.h"

#ifndef IFDEBUG
#ifdef DEBUG
#define IFDEBUG(foo) foo
#else
#define IFDEBUG(foo)
#endif
#endif /* !IFDEBUG */


#if 0  /* I got this put into libamiga.a */
/* NewList initializes an empty list 
** New, empty lists, have the List point
*/
#include <exec/lists.h>
void NewList(struct List * list)
{
    list->lh_Head = (NODE *)&list->lh_Tail;    /* First node in the list */
    list->lh_Tail = NULL;			/* Always null? */
    list->lh_TailPred = (NODE *)&list->lh_Head; /* Last node in the list */
    list->lh_Type = NT_UNKNOWN;	/* some bogus type for the list */
    list->l_pad  = 0;		/* not actually used except for alignment */
    /* odd that the headers define it as l_pad instead of lh_Pad */
}
#endif

#include <signal.h>

/* DICE signal handling compatibility? */
onbreak( void * proc() ){
    signal(SIGTERM, proc);
}

/* 
** move bytes of memory from area pointed to by foo to area pointed to by bar 
** should probably be done via a macro call instead...
*/
void * BMov( void * src, void * dst, int bytes ){
    /* memmove( dst, src, bytes); -- supports overlapping memory  */
    CopyMem(src, dst, bytes);   /* Faster, doesn't support overlapping */
}

/* return pointer to 'head' Node of list? 
** NULL if it's empty 
**
*/
struct Node * GetHead( struct List *ListPtr ){
    struct Node *node_pointer = NULL;

    IFDEBUG(struct Node * node_pointer2   = NULL;)

    IFDEBUG(printf("GetHead() called ");)

    if(IsListEmpty(ListPtr)) {
    IFDEBUG(printf("- empty list\n");)
	return(NULL);
    }

#ifdef DEBUG
    /*
    ** Here implemented in a rather amusing but crude manner:
    ** remove it from the list with RemHead() and then put it back!
    */
    node_pointer2 = RemHead( ListPtr);
    if(node_pointer2 != NULL)
    	AddHead( ListPtr, node_pointer2);
    /* return(node_pointer2); */
#endif /* DEBUG */

    /* more efficient method: */
    if( ListPtr->lh_Head == ( NODE *) &ListPtr->lh_Tail){ 
	/* check for empty list */
	node_pointer = NULL;
    }else{
	node_pointer =  ListPtr->lh_Head;  /* head pointer */
    }

#ifdef DEBUG
    if(node_pointer2 != node_pointer){
	printf(" generated odd result...  %x %x\n", 
		node_pointer, node_pointer2);
    }
    IFDEBUG(else printf("ok result\n");)
#endif /* DEBUG */

    return node_pointer;
}

#ifndef GetSucc    
/* should be defined as a macro in "dnet/dnet.h" */

/* return next node in the list */
struct Node * GetSucc( struct Node * testnode){
    if(testnode == NULL) return NULL;
    return testnode->ln-Succ;
}
#endif 
