From 189ce59dbe3861a9eaa23671540d61483ae9e26d Mon Sep 17 00:00:00 2001 From: Marcelo Roberto Jimenez Date: Fri, 14 Jan 2011 22:05:22 -0200 Subject: [PATCH] Null termination of strndup() implementation on systems missing it. Also, implementation of strnlen() on systems missing it. --- ChangeLog | 5 ++++ upnp/src/api/UpnpString.c | 48 ++++++++++++++++----------------------- 2 files changed, 25 insertions(+), 28 deletions(-) diff --git a/ChangeLog b/ChangeLog index 8625294..ea72ff3 100644 --- a/ChangeLog +++ b/ChangeLog @@ -2,6 +2,11 @@ Version 1.6.11 ******************************************************************************* +2011-01-14 Chandra Penke + + - Null termination of strndup() implementation on systems missing it. + - Implementation of strnlen() on systems missing it. + 2011-01-14 Chandra Penke Minor change in membuffer.c to include "membuffer.h" without looking diff --git a/upnp/src/api/UpnpString.c b/upnp/src/api/UpnpString.c index 3c9eb6b..e29fbf4 100644 --- a/upnp/src/api/UpnpString.c +++ b/upnp/src/api/UpnpString.c @@ -1,5 +1,3 @@ - - /*! * \addtogroup UpnpString * @@ -17,37 +15,44 @@ * \brief UpnpString object implementation. */ - #include "config.h" - #include "UpnpString.h" - #include /* for calloc(), free() */ #include /* for strlen(), strdup() */ - #ifdef WIN32 #define strcasecmp stricmp #else /* Other systems have strncasecmp */ #endif - /* strndup() is a GNU extension. Other systems must fix it with elif's. */ -#ifdef __GNUC__ +#if defined(__GNUC__) && !defined(__APPLE__) extern char *strndup(__const char *__string, size_t __n); -#elif defined(WIN32) +#endif /* defined(__GNUC__) && !defined(__APPLE__) */ + +#if defined(__GNUC__) && defined(__APPLE__) + static size_t strnlen(const char *s, size_t n) + { + const char *p = (const char *)memchr(s, 0, n); + return p ? p - s : n; + } +#endif /* defined(__GNUC__) && defined(__APPLE__) */ + +#if (defined(__GNUC__) && defined(__APPLE__)) || defined(WIN32) static char *strndup(const char *__string, size_t __n) { - size_t strsize = strnlen(__string,__n); - char *newstr = (char *) malloc(strsize + 1); - strncpy(newstr,__string,__n); - return(newstr); - } -#endif + size_t strsize = strnlen(__string, __n); + char *newstr = (char *)malloc(strsize + 1); + strncpy(newstr, __string, newstr); + newstr[strsize] = 0; + + return newstr; + } +#endif /* (defined(__GNUC__) && defined(__APPLE__)) || defined(WIN32) */ /*! * \brief Internal implementation of the class UpnpString. @@ -63,7 +68,6 @@ struct SUpnpString char *m_string; }; - UpnpString *UpnpString_new() { /* All bytes are zero, and so is the length of the string. */ @@ -90,7 +94,6 @@ error_handler1: return NULL; } - void UpnpString_delete(UpnpString *p) { struct SUpnpString *q = (struct SUpnpString *)p; @@ -105,7 +108,6 @@ void UpnpString_delete(UpnpString *p) free(p); } - UpnpString *UpnpString_dup(const UpnpString *p) { struct SUpnpString *q = calloc(1, sizeof (struct SUpnpString)); @@ -127,7 +129,6 @@ error_handler1: return NULL; } - void UpnpString_assign(UpnpString *p, const UpnpString *q) { if (p != q) { @@ -135,13 +136,11 @@ void UpnpString_assign(UpnpString *p, const UpnpString *q) } } - size_t UpnpString_get_Length(const UpnpString *p) { return ((struct SUpnpString *)p)->m_length; } - void UpnpString_set_Length(UpnpString *p, size_t n) { if (((struct SUpnpString *)p)->m_length > n) { @@ -151,13 +150,11 @@ void UpnpString_set_Length(UpnpString *p, size_t n) } } - const char *UpnpString_get_String(const UpnpString *p) { return ((struct SUpnpString *)p)->m_string; } - int UpnpString_set_String(UpnpString *p, const char *s) { char *q = strdup(s); @@ -170,7 +167,6 @@ error_handler1: return q != NULL; } - int UpnpString_set_StringN(UpnpString *p, const char *s, size_t n) { char *q = strndup(s, n); @@ -183,7 +179,6 @@ error_handler1: return q != NULL; } - void UpnpString_clear(UpnpString *p) { ((struct SUpnpString *)p)->m_length = 0; @@ -191,7 +186,6 @@ void UpnpString_clear(UpnpString *p) ((struct SUpnpString *)p)->m_string[0] = 0; } - int UpnpString_cmp(UpnpString *p, UpnpString *q) { const char *cp = UpnpString_get_String(p); @@ -200,7 +194,6 @@ int UpnpString_cmp(UpnpString *p, UpnpString *q) return strcmp(cp, cq); } - int UpnpString_casecmp(UpnpString *p, UpnpString *q) { const char *cp = UpnpString_get_String(p); @@ -210,4 +203,3 @@ int UpnpString_casecmp(UpnpString *p, UpnpString *q) } /* @} UpnpString */ -