src/util: provide strndup definitions where needed
Signed-off-by: Dave Reisner <dreisner@archlinux.org> Signed-off-by: Dan McGee <dan@archlinux.org>
This commit is contained in:
parent
07e89c1e5d
commit
83ee9708b1
2 changed files with 44 additions and 0 deletions
|
@ -48,6 +48,28 @@ static struct options_t {
|
||||||
char delim;
|
char delim;
|
||||||
} opts;
|
} opts;
|
||||||
|
|
||||||
|
#ifndef HAVE_STRNDUP
|
||||||
|
/* A quick and dirty implementation derived from glibc */
|
||||||
|
static size_t strnlen(const char *s, size_t max)
|
||||||
|
{
|
||||||
|
register const char *p;
|
||||||
|
for(p = s; *p && max--; ++p);
|
||||||
|
return (p - s);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *strndup(const char *s, size_t n)
|
||||||
|
{
|
||||||
|
size_t len = strnlen(s, n);
|
||||||
|
char *new = (char *) malloc(len + 1);
|
||||||
|
|
||||||
|
if(new == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
new[len] = '\0';
|
||||||
|
return (char *)memcpy(new, s, len);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
static struct buffer_t *buffer_new(size_t initial_size)
|
static struct buffer_t *buffer_new(size_t initial_size)
|
||||||
{
|
{
|
||||||
struct buffer_t *buf;
|
struct buffer_t *buf;
|
||||||
|
|
|
@ -91,6 +91,28 @@ int unique = 0;
|
||||||
int searchsyncs = 0;
|
int searchsyncs = 0;
|
||||||
const char *dbpath = DBPATH;
|
const char *dbpath = DBPATH;
|
||||||
|
|
||||||
|
#ifndef HAVE_STRNDUP
|
||||||
|
/* A quick and dirty implementation derived from glibc */
|
||||||
|
static size_t strnlen(const char *s, size_t max)
|
||||||
|
{
|
||||||
|
register const char *p;
|
||||||
|
for(p = s; *p && max--; ++p);
|
||||||
|
return (p - s);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *strndup(const char *s, size_t n)
|
||||||
|
{
|
||||||
|
size_t len = strnlen(s, n);
|
||||||
|
char *new = (char *) malloc(len + 1);
|
||||||
|
|
||||||
|
if(new == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
new[len] = '\0';
|
||||||
|
return (char *)memcpy(new, s, len);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
static char *strtrim(char *str)
|
static char *strtrim(char *str)
|
||||||
{
|
{
|
||||||
char *pch = str;
|
char *pch = str;
|
||||||
|
|
Loading…
Add table
Reference in a new issue