#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/dir.h>
#include <dos.h>
#include <proto/dos.h>

FILE *msgf;

#define LOCK "T:HISTORY.LCK"

struct _history {
	long stamp;
	char msgid[80];
} *history;
long histcnt;
long histsiz;
int loaded;

int length = 0;

int SetLock(const char *name)
{
	FILE *lck;

	if (lck = fopen(name,"r"))
	{
		fclose(lck);
		return 1;
	}
	lck = fopen(name,"w");
	fputs("LOCK",lck);
	fclose(lck);
	return 0;
}

void FreeLock(const char *name)
{
	remove(name);
}

void main(argc,argv)
	int argc;
	char *argv[];
{
	long i;
	int len;
	FILE *output;
	
	if (SetLock(LOCK))
		exit(0);

	if (argc == 2)
		msgf = fopen(argv[1],"r");
	else
		msgf = fopen("UULIB:HISTORY.FIL","r");
	fread(&histcnt,sizeof(long),1,msgf);
	if (!feof(msgf))
		histsiz = ((histcnt/1024)+1)*1024;
	else
	{
		histsiz = 1024;
		histcnt = 0;
	}
	history = calloc(histsiz,sizeof(struct _history));
	if (histcnt)
		fread(history,sizeof(struct _history),histcnt,msgf);
	loaded = 1;
	fclose(msgf);
	
	FreeLock(LOCK);
	
	output = fopen("ram:hist","w");

	fprintf(output,"%d\n",histcnt);	
	for (i = 0; i < histcnt; i++)
	{
		len = strlen(history[i].msgid);
		fprintf(output,"%d\t%s\n",history[i].stamp,history[i].msgid);
	}
	fclose(output);
	
	exit(0);
}
