shithub: castor9

ref: be3ff72d76d6c529e45d138eca51fb0a3380119e
dir: /util.c/

View raw version
#include <u.h>
#include <libc.h>
#include "castor.h"

void *
emalloc(int n)
{
	void *v;
	if((v = malloc(n)) == nil) {
		fprint(2, "out of memory allocating %d\n", n);
		sysfatal("mem");
	}
	setmalloctag(v, getcallerpc(&n));
	memset(v, 0, n);
	return v;
}

char *
estrdup(char *s)
{
	char *t;
	if((t = strdup(s)) == nil) {
		fprint(2, "out of memory in strdup(%.10s)\n", s);
		sysfatal("mem");
	}
	setmalloctag(t, getcallerpc(&t));
	return t;
}

int
strbeg(char *str, char *prefix)
{
	return strncmp(str, prefix, strlen(prefix)) == 0 ? 0 : -1;
}

char* 
replacechar(char* str, char find, char replace)
{
	char *current_pos = strchr(str,find);
	while(current_pos){
		*current_pos = replace;
		current_pos = strchr(current_pos,find);
	}
	return str;
}