/************************************************
 *  checkmunge - the companion to Bryce			*
 *		Nesbitt's munge and my munge II			*
 *												*
 *  by Joe Pearce, El Cajon, CA (8/20/88)		*
 ************************************************
 *  Checkmunge will give a count of the			*
 *  number of changed memory locations and		*
 *  the addresses of the first twenty since		*
 *  munge (or munge II) was run. If the			*
 *  option 0 if given, it will instead check	*
 *  for changs since munge II was put into		*
 *  zeroing mode.								*
 *												*
 *  USAGE: checkmunge [0]						*
 *	Note: only use after running munge (II)		*
 *												*
 *  Manx compile:								*
 *		cc checkmunge.c							*
 *		ln checkmunge.o -lc (ignore error msg)	*
 ************************************************/

#include "exec/types.h"
#include "exec/memory.h"
#include "exec/execbase.h"

extern struct ExecBase *SysBase;
long Output();

_main(alen,aptr) long alen; char *aptr;
{	long	*addr[20],
			*check;
	long 	i,
			val = (alen && *aptr == '0' ? 0 : 0x01010101);
	WORD 	hits = 0;
	ULONG	total = 0;
	struct MemHeader *mh;
	struct MemChunk *mc;
	char buf[60];

	Write(Output(),"Working...\n",12L);

	Forbid();
	for ( mh = (struct MemHeader *)SysBase->MemList.lh_Head;
		  mh->mh_Node.ln_Succ;
		  mh = (struct MemHeader *)mh->mh_Node.ln_Succ )
	{	for (mc = mh->mh_First; mc; mc = mc->mc_Next)
		{	i = mc->mc_Bytes - 8 >> 2;  /* number of longwords */
			if (i == 0) continue;
			check = (long *)mc + 2;
			while (i--)
			{	if (*check != val)
				{	total++;
					if (hits < 19) addr[hits++] = check;
				}
				check++;
			}
		}
	}
	Permit();

	sprintf(buf,"Number of changed longwords in memory: %lu.\n",total);
	Write(Output(),buf,(long)strlen(buf));

	for (i=0; i<hits; i++)
	{	sprintf(buf,"#%2ld: %08lx\n",i,addr[i]);
		Write(Output(),buf,(long)strlen(buf));
	}
}
