/*
 * util.c - a set of useful subroutines for the bible processing program
 *
 *	$Source:$
 * 	$Header:$
 * 	$Author:$
 */

#include <stdio.h>
#include <strings.h>
#include <ctype.h>
#include "bible.h"

char	*malloc(), *re_comp();
char	*progname = "util";

/*
 * Duplicate a string in malloc'ed memory
 */
char *strdup(s)
	char	*s;
{
	register char	*cp;
	
	if (!(cp = malloc(strlen(s)+1))) {
		printf("Out of memory!!!\n");
		abort();
	}
	return(strcpy(cp,s));
}

/*
 * Duplicate up to the first n characters of a string in malloc'ed memory
 */
char *strndup(s, n)
	char	*s;
	int	n;
{
	register char	*cp;
	register int	len;

	len = strlen(s);
	len = (len > n) ? n : len;
	
	if (!(cp = malloc(len+1))) {
		printf("Out of memory!!!\n");
		abort();
	}
	cp[len] = '\0';
	return(strncpy(cp,s,len));
}

char	*parse_skip(p, c)
	register char	*p, c;
{
	while (*p && *p != c && *p != '\n')
		p++;
	if (*p)
		*p++ = '\0';
	return(p);
}

/*
 * If the filename has an extension, replace it with the passed extension.
 * Otherwise, add said extension to the filename and return it in a
 * static string
 */
char *frob_ext(fn, ext)
	char	*fn;
	char	*ext;
{
	static char	s[512];
	char	*cp;

	strcpy(s, fn);
	cp = index(s, '.');
	if (cp)
		strcpy(cp, ext);
	else
		strcat(s, ext);
	return(s);
}
	
	
		  
