From: Adam Dickmeiss Date: Thu, 30 Apr 2009 07:04:34 +0000 (+0200) Subject: Added xstrndup (similar to strndup) X-Git-Tag: v3.0.45.rhel53~7 X-Git-Url: http://lists.indexdata.dk/?a=commitdiff_plain;h=3594baeb76423dcee4a138f54bc1d5b5b4477fe3;p=yaz-moved-to-github.git Added xstrndup (similar to strndup) --- 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 + */ +