/*
 * tron: check cronlog package
 */

#include	"tron.h"

ckcron()
{
	long so;
	short fd;
	register FILE *fp;
	static char rbuf[256], tbuf[32];
	register short i;
	struct stat statb;

	if((fd = open(".cskpt", 2)) == -1){
		fd = creat(".cskpt", 0600);
		so = 0;
	} else	if(read(fd, &so, 4) != 4) so = 0;

	if(stat(CRONLOG, &statb) == -1){
		close(fd);
		fprintf(stdout, "*TRON: No cronlog %s\n", CRONLOG);
		return(1);
	}

	lseek(fd, 0L, 0);
	write(fd, &statb.st_size, 4);
	close(fd);
	if((fp = fopen(CRONLOG, "r")) == NULL){
		fprintf(stdout, "**TRON: Can't open %s after stat\n", CRONLOG);
		return(1);
	}
	if(statb.st_size < so)
		so = 0;
	fseek(fp, so, 0);
	while(fgets(rbuf, 255, fp)){
		rbuf[strlen(rbuf)-1] = 0;
		if(rbuf[0] == 0)continue;
		if(rbuf[13] == ':' && rbuf[16] == ':'){
			strncpy(tbuf, rbuf, 24);
			tbuf[24] = 0;
			continue;
		}
		for(i = 0; mess[i]; i++)
			if(gmatch(rbuf, mess[i]) == 1)
				break;

		if(mess[i])
			continue;
		fprintf(stdout, "**TRON: Cronlog: %s: %s\n", tbuf, rbuf);
	}
	fclose(fp);
	return(0);
}

