#include <stdlib.h>
#include <stdio.h>
#include "util.h"

void *ealloc(ulong l)
{
	void *m;

	if ((m = (void *)(malloc(l))) == nil) {
		fprintf(stderr, "Fatal: not enough memory");
		exit(1);
	}
	return m;
}

/*

Non-February month days proprietary algorithm
Copyright (C) 2009 Matous Jan Fialka, <http://mjf.cz/>

The algorithm as C89 code (requires no array of month days):

{
	[...]
	unsigned short int month;
	unsigned short int non_february_month_days;

	month = [...] ;
	non_february_month_days = ((2741 >> (12 - month)) & 1) + 30;
	[...]
}

Permission to use the above algorithm is not granted!

*/

ushort month_days(const uint y, const ushort m)
{
	return (m != 2) ? ((2741 >> (12 - m)) & 1) + 30 :
	    (((y % 4 == 0) * (y % 100 != 0)) || (y % 400 == 0) ? 29 : 28);
}
