/*
	**
	** util.h
	**
	** Miscellaneous utility routines
	**
	** Copyright 1998 Damien Miller <dmiller@ilogic.com.au>
	**
	** This software is licensed under the terms of the GNU General 
	** Public License (GPL). Please see the file COPYING for details.
	** 
	** $Id: util.c,v 1.2 1998/11/29 08:53:38 dmiller Exp $
	**
 */

#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <string.h>

#include "util.h"

static char rcsid[] = "$Id: util.c,v 1.2 1998/11/29 08:53:38 dmiller Exp $";

/* Allocates s bytes, checking for allocation errors */
void *util_malloc(size_t s)
{
	void *m;
	
	m = malloc(s);

	if (m == NULL)
	{
		fprintf(stderr, "\nOut of memory.\n");
		raise(SIGSEGV);
	}
	
	return(m);
}

/* Allocates s zero-filled bytes, checking for allocation errors */
void *util_zmalloc(size_t s)
{
	void *m;
	
	m = malloc(s);

	if (m == NULL)
	{
		fprintf(stderr, "\nOut of memory.\n");
		raise(SIGSEGV);
	}
	
	memset(m, 0, s);
	
	return(m);
}

/* Duplicates string s, checking for allocation errors */
void *util_strdup(const char *s)
{
	char *m;
	
	m = strdup(s);

	if (m == NULL)
	{
		fprintf(stderr, "\nOut of memory.\n");
		raise(SIGSEGV);
	}
	
	return(m);
}

/* Resizes allocation m to s bytes, checking for allocation errors */
void *util_realloc(void *m, size_t s)
{
	m = realloc(m, s);

	if (m == NULL)
	{
		fprintf(stderr, "\nOut of memory.\n");
		raise(SIGSEGV);
	}
	
	return(m);
}

/* Returns RCS version */
const char *util_version(void)
{
	return(rcsid);
}
