From 3594baeb76423dcee4a138f54bc1d5b5b4477fe3 Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Thu, 30 Apr 2009 09:04:34 +0200 Subject: [PATCH] Added xstrndup (similar to strndup) --- include/yaz/xmalloc.h | 3 +++ src/xmalloc.c | 14 ++++++++++++ test/Makefile.am | 4 +++- test/tstxmalloc.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 test/tstxmalloc.c diff --git a/include/yaz/xmalloc.h b/include/yaz/xmalloc.h index 1b813d1..2c3ff9f 100644 --- a/include/yaz/xmalloc.h +++ b/include/yaz/xmalloc.h @@ -44,6 +44,7 @@ YAZ_BEGIN_CDECL #define xcalloc(x,y) xcalloc_f(x,y, __FILE__, __LINE__) #define xfree(x) xfree_f(x, __FILE__, __LINE__) #define xstrdup(s) xstrdup_f(s, __FILE__, __LINE__) +#define xstrndup(s, n) xstrndup_f(s, n, __FILE__, __LINE__) #define xmalloc_trav(s) xmalloc_trav_f(s, __FILE__, __LINE__) YAZ_EXPORT void *xrealloc_f (void *o, size_t size, const char *file, int line); @@ -51,6 +52,8 @@ YAZ_EXPORT void *xmalloc_f (size_t size, const char *file, int line); YAZ_EXPORT void *xcalloc_f (size_t nmemb, size_t size, const char *file, int line); YAZ_EXPORT char *xstrdup_f (const char *p, const char *file, int line); +YAZ_EXPORT char *xstrndup_f(const char *p, size_t n, + const char *file, int line); YAZ_EXPORT void xfree_f (void *p, const char *file, int line); YAZ_EXPORT void xmalloc_trav_f(const char *s, const char *file, int line); diff --git a/src/xmalloc.c b/src/xmalloc.c index 94a6565..4aa922c 100644 --- a/src/xmalloc.c +++ b/src/xmalloc.c @@ -341,6 +341,20 @@ void xfree_f(void *p, const char *file, int line) yaz_log (log_level, "%s:%d: xfree %p", file, line, p); xfree_d(p, file, line); } + +char *xstrndup_f(const char *s, size_t n, const char *file, int line) +{ + size_t l = strlen(s); + if (l < n) + return xstrdup_f(s, file, line); + { + char *a = xmalloc_f(n+1, file, line); + memcpy(a, s, n); + a[n] = '\0'; + return a; + } +} + /* * Local variables: * c-basic-offset: 4 diff --git a/test/Makefile.am b/test/Makefile.am index 735f5b1..da862d4 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -1,7 +1,8 @@ ## This file is part of the YAZ toolkit. ## Copyright (C) 1995-2009 Index Data -check_PROGRAMS = tsticonv tstnmem tstmatchstr tstwrbuf tstodr tstccl tstlog \ +check_PROGRAMS = tstxmalloc tsticonv tstnmem tstmatchstr tstwrbuf tstodr \ + tstccl tstlog \ tstsoap1 tstsoap2 tstodrstack tstlogthread tstxmlquery tstpquery \ tst_comstack tst_filepath tst_record_conv tst_retrieval tst_tpath \ tst_timing tst_query_charset tst_oid tst_icu_I18N tst_match_glob tst_rpn2cql @@ -50,6 +51,7 @@ tst_icu_I18N_LDADD = ../src/libyaz_icu.la $(ICU_LIBS) CONFIG_CLEAN_FILES=*.log +tstxmalloc_SOURCES = tstxmalloc.c tsticonv_SOURCES = tsticonv.c tstnmem_SOURCES = tstnmem.c tstmatchstr_SOURCES = tstmatchstr.c diff --git a/test/tstxmalloc.c b/test/tstxmalloc.c new file mode 100644 index 0000000..4d5f8e5 --- /dev/null +++ b/test/tstxmalloc.c @@ -0,0 +1,61 @@ +/* This file is part of the YAZ toolkit. + * Copyright (C) 1995-2009 Index Data + * See the file LICENSE for details. + */ + +#if HAVE_CONFIG_H +#include +#endif + +#include +#include +#include +#include + +void tst(void) +{ + char *p = 0; + + p = xmalloc(10); + YAZ_CHECK(p); + p = xrealloc(p, 20); + YAZ_CHECK(p); + xfree(p); + + p = xstrdup("hello"); + YAZ_CHECK(p); + if (!p) + return; + YAZ_CHECK(!strcmp(p, "hello")); + xfree(p); + + p = xstrndup("hello", 2); + YAZ_CHECK(p); + if (!p) + return; + YAZ_CHECK(!strcmp(p, "he")); + xfree(p); + + p = xstrndup("hello", 6); + YAZ_CHECK(p); + if (!p) + return; + YAZ_CHECK(!strcmp(p, "hello")); + xfree(p); +} + +int main (int argc, char **argv) +{ + YAZ_CHECK_INIT(argc, argv); + tst(); + YAZ_CHECK_TERM; +} +/* + * Local variables: + * c-basic-offset: 4 + * c-file-style: "Stroustrup" + * indent-tabs-mode: nil + * End: + * vim: shiftwidth=4 tabstop=8 expandtab + */ + -- 1.7.10.4