diff options
author | Paul Smith <psmith@gnu.org> | 2009-05-24 18:31:18 +0000 |
---|---|---|
committer | Paul Smith <psmith@gnu.org> | 2009-05-24 18:31:18 +0000 |
commit | 14f3f501bc1abc821d859e964f71e69b8ed7eaa2 (patch) | |
tree | dff17f1aa8f78ee5a59b32b0beedf6c37a7adf82 /misc.c | |
parent | 3fd62c76c261e10658a1ba365710ee26b6fb34fe (diff) | |
download | gunmake-14f3f501bc1abc821d859e964f71e69b8ed7eaa2.tar.gz |
Found this change in an old CVS workspace: rewrite savestring() to the
more standard xstrndup().
Diffstat (limited to 'misc.c')
-rw-r--r-- | misc.c | 20 |
1 files changed, 14 insertions, 6 deletions
@@ -388,13 +388,21 @@ xstrdup (const char *ptr) #endif /* HAVE_DMALLOC_H */ char * -savestring (const char *str, unsigned int length) +xstrndup (const char *str, unsigned int length) { - char *out = xmalloc (length + 1); - if (length > 0) - memcpy (out, str, length); - out[length] = '\0'; - return out; + char *result; + +#ifdef HAVE_STRNDUP + result = strndup (str, length); + if (result == 0) + fatal (NILF, _("virtual memory exhausted")); +#else + result = xmalloc (length + 1); + strncpy (result, str, length); + result[length] = '\0'; +#endif + + return result; } |