/*
 * util.c - a set of useful subroutines
 *
 *	$Source:$
 * 	$Header:$
 * 	$Author:$
 */

#include <std.h>
#include <stddef.h>

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

#ifdef notdef
/*
 * 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));
}
#endif

/*
 * Duplicate up to the first n characters of a string in malloc'ed memory
 */
char *strndup(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));
}

int list_compare(char *s, char **list)
{
      char *err;

      while (*list!=NULL) {
              err=re_comp(*list++);
              if (err) {
                      fprintf(stderr,"%s: %s - %s\n",progname,err,*(--list));
                      exit(1);
                      }
              if (re_exec(s))
                      return(1);
      }
      return(0);
}

MakeLowerCase(char *s)
{
      int i;
      for (i=0;s[i];i++)
              s[i]=isupper(s[i]) ? tolower(s[i]) : s[i];
}
