add estrndup

This commit is contained in:
Jakob Kramer 2015-02-11 01:59:04 +01:00 committed by sin
parent 08e93dd4f5
commit c0a3c66a84
2 changed files with 19 additions and 0 deletions

View File

@ -28,6 +28,12 @@ estrdup(const char *s)
return enstrdup(1, s); return enstrdup(1, s);
} }
char *
estrndup(const char *s, size_t n)
{
return enstrndup(1, s, n);
}
void * void *
encalloc(int status, size_t nmemb, size_t size) encalloc(int status, size_t nmemb, size_t size)
{ {
@ -69,3 +75,14 @@ enstrdup(int status, const char *s)
enprintf(status, "strdup: out of memory\n"); enprintf(status, "strdup: out of memory\n");
return p; return p;
} }
char *
enstrndup(int status, const char *s, size_t n)
{
char *p;
p = strndup(s, n);
if (!p)
enprintf(status, "strndup: out of memory\n");
return p;
}

2
util.h
View File

@ -25,10 +25,12 @@ void *ecalloc(size_t, size_t);
void *emalloc(size_t); void *emalloc(size_t);
void *erealloc(void *, size_t); void *erealloc(void *, size_t);
char *estrdup(const char *); char *estrdup(const char *);
char *estrndup(const char *, size_t);
void *encalloc(int, size_t, size_t); void *encalloc(int, size_t, size_t);
void *enmalloc(int, size_t); void *enmalloc(int, size_t);
void *enrealloc(int, void *, size_t); void *enrealloc(int, void *, size_t);
char *enstrdup(int, const char *); char *enstrdup(int, const char *);
char *enstrndup(int, const char *, size_t);
void enprintf(int, const char *, ...); void enprintf(int, const char *, ...);
void eprintf(const char *, ...); void eprintf(const char *, ...);