mirror of
https://git.notmuchmail.org/git/notmuch
synced 2024-11-25 20:38:08 +01:00
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.
This commit is contained in:
parent
e5316b320a
commit
8b445212e4
2 changed files with 6 additions and 4 deletions
2
TODO
2
TODO
|
@ -41,8 +41,6 @@ Portability
|
||||||
-----------
|
-----------
|
||||||
Fix configure script to test each compiler warning we want to use.
|
Fix configure script to test each compiler warning we want to use.
|
||||||
|
|
||||||
Implement strndup locally (or call talloc_strndup instead).
|
|
||||||
|
|
||||||
Implement getline locally, (look at gnulib).
|
Implement getline locally, (look at gnulib).
|
||||||
|
|
||||||
Completion
|
Completion
|
||||||
|
|
|
@ -18,7 +18,6 @@
|
||||||
* Author: Carl Worth <cworth@cworth.org>
|
* Author: Carl Worth <cworth@cworth.org>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#define _GNU_SOURCE /* For strndup */
|
|
||||||
#include "notmuch-private.h"
|
#include "notmuch-private.h"
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
@ -84,11 +83,16 @@ xstrndup (const char *s, size_t n)
|
||||||
{
|
{
|
||||||
char *ret;
|
char *ret;
|
||||||
|
|
||||||
ret = strndup (s, n);
|
if (strlen (s) <= n)
|
||||||
|
n = strlen (s);
|
||||||
|
|
||||||
|
ret = malloc (n + 1);
|
||||||
if (ret == NULL) {
|
if (ret == NULL) {
|
||||||
fprintf (stderr, "Out of memory.\n");
|
fprintf (stderr, "Out of memory.\n");
|
||||||
exit (1);
|
exit (1);
|
||||||
}
|
}
|
||||||
|
memcpy (ret, s, n);
|
||||||
|
ret[n] = '\0';
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue