diff options
author | Carl Worth <cworth@cworth.org> | 2009-12-01 12:51:39 -0800 |
---|---|---|
committer | Carl Worth <cworth@cworth.org> | 2009-12-01 12:51:39 -0800 |
commit | 8b445212e46a194b59edbd6857449430fe460165 (patch) | |
tree | 7133fd094201bb6ef7702034a65b59858bba66e2 /lib/xutil.c | |
parent | e5316b320a51915fc1dbdd8724643931cd03327f (diff) |
xutil: Implement xstrndup without relying on strndup.
Since we need to do this for portability, (some systems don't have a
strndup function), we might as well do it unconditionally. There's
almost no disadvantage to doing so, and this has the advantages of not
requiring a configure-time check nor having two different
implementations, one of which would often be less tested.
Diffstat (limited to 'lib/xutil.c')
-rw-r--r-- | lib/xutil.c | 8 |
1 files changed, 6 insertions, 2 deletions
diff --git a/lib/xutil.c b/lib/xutil.c index 6fa5eb0..268225b 100644 --- a/lib/xutil.c +++ b/lib/xutil.c @@ -18,7 +18,6 @@ * Author: Carl Worth <cworth@cworth.org> */ -#define _GNU_SOURCE /* For strndup */ #include "notmuch-private.h" #include <stdio.h> @@ -84,11 +83,16 @@ xstrndup (const char *s, size_t n) { char *ret; - ret = strndup (s, n); + if (strlen (s) <= n) + n = strlen (s); + + ret = malloc (n + 1); if (ret == NULL) { fprintf (stderr, "Out of memory.\n"); exit (1); } + memcpy (ret, s, n); + ret[n] = '\0'; return ret; } |