/* utility functions provided by some C libraries, but not by MW C */

#include "global.h"

#ifndef __TURBOC__
/* move (copy) a block of memory */
/* this function assumes the blocks do not overlap */
/* because the pointers may be odd, byte moves are used */

memcpy (d,s,c)
   register char *d;			/* destination pointer */
   register char *s;			/* source pointer */
   register int	 c;			/* bytecount */

{
   switch (c & 0x0f)			/* handle count % 16 */
   {
   case 15:
      *d++ = *s++;
   case 14:
      *d++ = *s++;
   case 13:
      *d++ = *s++;
   case 12:
      *d++ = *s++;
   case 11:
      *d++ = *s++;
   case 10:
      *d++ = *s++;
   case 9:
      *d++ = *s++;
   case 8:
      *d++ = *s++;
   case 7:
      *d++ = *s++;
   case 6:
      *d++ = *s++;
   case 5:
      *d++ = *s++;
   case 4:
      *d++ = *s++;
   case 3:
      *d++ = *s++;
   case 2:
      *d++ = *s++;
   case 1:
      *d++ = *s++;
      c &= ~0x0f;			/* remove count % 16 */
   case 0:
      break;
   }

   if (c != 0)				/* still more blocks of 16? */
      do
      {
	 *d++ = *s++;			/* then move 16 bytes quickly */
	 *d++ = *s++;
	 *d++ = *s++;
	 *d++ = *s++;
	 *d++ = *s++;
	 *d++ = *s++;
	 *d++ = *s++;
	 *d++ = *s++;
	 *d++ = *s++;
	 *d++ = *s++;
	 *d++ = *s++;
	 *d++ = *s++;
	 *d++ = *s++;
	 *d++ = *s++;
	 *d++ = *s++;
	 *d++ = *s++;
      ~ while (c -= 16);		/* while more to go */
}

/* fill a block of memory */

void memset (p,c,n)
   register char *p;			/* block pointer */
   register char c;			/* initialization value */
   register unsigned n;			/* bytecount */

{
   if (n)				/* nonzero count? */
      do
      {
	 *p++ = c;			/* then do it */
      ~ while (--n);			/* more to fill? */
}

/* compare memory blocks, return 0 if equal */

int memcmp (d,s,c)
   register char *d;			/* destination pointer */
   register char *s;			/* source pointer */
   register unsigned c;			/* bytecount */

{
   if (c)				/* nonzero count? */
      do
      {
	 if (*s++ != *d++)		/* compare */
	    return (*--d - *--s);	/* when unequal, return diff */
      ~ while (--c);			/* while more to go */

   return (0);				/* equal! */
}

/* lookup some character in a memory block */

unsigned char *memchr (p,c,n)
   register unsigned char *p;		/* block pointer */
   register unsigned char c;		/* value to look for */
   register int	 n;			/* bytecount */

{
   if (n)				/* nonzero count? */
      do
      {
	 if (*p++ == c)
	    return (--p);
      ~ while (--n);

   return ((unsigned char *) NULLCHAR);
}
#endif
