Null termination of strndup() implementation on systems missing it.

Also, implementation of strnlen() on systems missing it.
This commit is contained in:
Marcelo Roberto Jimenez
2011-01-14 22:05:22 -02:00
parent 4815e52586
commit 189ce59dbe
2 changed files with 25 additions and 28 deletions

View File

@@ -2,6 +2,11 @@
Version 1.6.11 Version 1.6.11
******************************************************************************* *******************************************************************************
2011-01-14 Chandra Penke <chandrapenke(at)mcntech.com>
- Null termination of strndup() implementation on systems missing it.
- Implementation of strnlen() on systems missing it.
2011-01-14 Chandra Penke <chandrapenke(at)mcntech.com> 2011-01-14 Chandra Penke <chandrapenke(at)mcntech.com>
Minor change in membuffer.c to include "membuffer.h" without looking Minor change in membuffer.c to include "membuffer.h" without looking

View File

@@ -1,5 +1,3 @@
/*! /*!
* \addtogroup UpnpString * \addtogroup UpnpString
* *
@@ -17,37 +15,44 @@
* \brief UpnpString object implementation. * \brief UpnpString object implementation.
*/ */
#include "config.h" #include "config.h"
#include "UpnpString.h" #include "UpnpString.h"
#include <stdlib.h> /* for calloc(), free() */ #include <stdlib.h> /* for calloc(), free() */
#include <string.h> /* for strlen(), strdup() */ #include <string.h> /* for strlen(), strdup() */
#ifdef WIN32 #ifdef WIN32
#define strcasecmp stricmp #define strcasecmp stricmp
#else #else
/* Other systems have strncasecmp */ /* Other systems have strncasecmp */
#endif #endif
/* strndup() is a GNU extension. Other systems must fix it with elif's. */ /* 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); 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) static char *strndup(const char *__string, size_t __n)
{ {
size_t strsize = strnlen(__string,__n); size_t strsize = strnlen(__string, __n);
char *newstr = (char *) malloc(strsize + 1); char *newstr = (char *)malloc(strsize + 1);
strncpy(newstr,__string,__n);
return(newstr);
}
#endif
strncpy(newstr, __string, newstr);
newstr[strsize] = 0;
return newstr;
}
#endif /* (defined(__GNUC__) && defined(__APPLE__)) || defined(WIN32) */
/*! /*!
* \brief Internal implementation of the class UpnpString. * \brief Internal implementation of the class UpnpString.
@@ -63,7 +68,6 @@ struct SUpnpString
char *m_string; char *m_string;
}; };
UpnpString *UpnpString_new() UpnpString *UpnpString_new()
{ {
/* All bytes are zero, and so is the length of the string. */ /* All bytes are zero, and so is the length of the string. */
@@ -90,7 +94,6 @@ error_handler1:
return NULL; return NULL;
} }
void UpnpString_delete(UpnpString *p) void UpnpString_delete(UpnpString *p)
{ {
struct SUpnpString *q = (struct SUpnpString *)p; struct SUpnpString *q = (struct SUpnpString *)p;
@@ -105,7 +108,6 @@ void UpnpString_delete(UpnpString *p)
free(p); free(p);
} }
UpnpString *UpnpString_dup(const UpnpString *p) UpnpString *UpnpString_dup(const UpnpString *p)
{ {
struct SUpnpString *q = calloc(1, sizeof (struct SUpnpString)); struct SUpnpString *q = calloc(1, sizeof (struct SUpnpString));
@@ -127,7 +129,6 @@ error_handler1:
return NULL; return NULL;
} }
void UpnpString_assign(UpnpString *p, const UpnpString *q) void UpnpString_assign(UpnpString *p, const UpnpString *q)
{ {
if (p != q) { if (p != q) {
@@ -135,13 +136,11 @@ void UpnpString_assign(UpnpString *p, const UpnpString *q)
} }
} }
size_t UpnpString_get_Length(const UpnpString *p) size_t UpnpString_get_Length(const UpnpString *p)
{ {
return ((struct SUpnpString *)p)->m_length; return ((struct SUpnpString *)p)->m_length;
} }
void UpnpString_set_Length(UpnpString *p, size_t n) void UpnpString_set_Length(UpnpString *p, size_t n)
{ {
if (((struct SUpnpString *)p)->m_length > 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) const char *UpnpString_get_String(const UpnpString *p)
{ {
return ((struct SUpnpString *)p)->m_string; return ((struct SUpnpString *)p)->m_string;
} }
int UpnpString_set_String(UpnpString *p, const char *s) int UpnpString_set_String(UpnpString *p, const char *s)
{ {
char *q = strdup(s); char *q = strdup(s);
@@ -170,7 +167,6 @@ error_handler1:
return q != NULL; return q != NULL;
} }
int UpnpString_set_StringN(UpnpString *p, const char *s, size_t n) int UpnpString_set_StringN(UpnpString *p, const char *s, size_t n)
{ {
char *q = strndup(s, n); char *q = strndup(s, n);
@@ -183,7 +179,6 @@ error_handler1:
return q != NULL; return q != NULL;
} }
void UpnpString_clear(UpnpString *p) void UpnpString_clear(UpnpString *p)
{ {
((struct SUpnpString *)p)->m_length = 0; ((struct SUpnpString *)p)->m_length = 0;
@@ -191,7 +186,6 @@ void UpnpString_clear(UpnpString *p)
((struct SUpnpString *)p)->m_string[0] = 0; ((struct SUpnpString *)p)->m_string[0] = 0;
} }
int UpnpString_cmp(UpnpString *p, UpnpString *q) int UpnpString_cmp(UpnpString *p, UpnpString *q)
{ {
const char *cp = UpnpString_get_String(p); const char *cp = UpnpString_get_String(p);
@@ -200,7 +194,6 @@ int UpnpString_cmp(UpnpString *p, UpnpString *q)
return strcmp(cp, cq); return strcmp(cp, cq);
} }
int UpnpString_casecmp(UpnpString *p, UpnpString *q) int UpnpString_casecmp(UpnpString *p, UpnpString *q)
{ {
const char *cp = UpnpString_get_String(p); const char *cp = UpnpString_get_String(p);
@@ -210,4 +203,3 @@ int UpnpString_casecmp(UpnpString *p, UpnpString *q)
} }
/* @} UpnpString */ /* @} UpnpString */