1
0
mirror of https://frontier.innolan.net/rainlance/c-ares.git synced 2025-10-05 22:39:39 +00:00
Files
c-ares/ares_strdup.c
David Drysdale f1bcfa1122 api: Allow injection of user-specified malloc/free functions
Add a new ares_library_init_mem() initialization function for the
library which allows the library user to specify their own malloc,
realloc & free equivalents for use library-wide.

Store these function pointers in library-wide global variables,
defaulting to libc's malloc(), realloc() and free().

Change all calls to malloc, realloc and free to use the function pointer
instead.  Also ensure that ares_strdup() is always available
(even if the local environment includes strdup(3)), and change the
library code to always use it.

Convert calls to calloc() to use ares_malloc() + memset
2016-02-02 10:05:07 +00:00

50 lines
1.3 KiB
C

/* Copyright 1998 by the Massachusetts Institute of Technology.
*
* Permission to use, copy, modify, and distribute this
* software and its documentation for any purpose and without
* fee is hereby granted, provided that the above copyright
* notice appear in all copies and that both that copyright
* notice and this permission notice appear in supporting
* documentation, and that the name of M.I.T. not be used in
* advertising or publicity pertaining to distribution of the
* software without specific, written prior permission.
* M.I.T. makes no representations about the suitability of
* this software for any purpose. It is provided "as is"
* without express or implied warranty.
*/
#include "ares_setup.h"
#include "ares_strdup.h"
#include "ares.h"
#include "ares_private.h"
char *ares_strdup(const char *s1)
{
#ifdef HAVE_STRDUP
if (ares_malloc == malloc)
return strdup(s1);
else
#endif
{
size_t sz;
char * s2;
if(s1) {
sz = strlen(s1);
if(sz < (size_t)-1) {
sz++;
if(sz < ((size_t)-1) / sizeof(char)) {
s2 = ares_malloc(sz * sizeof(char));
if(s2) {
memcpy(s2, s1, sz * sizeof(char));
return s2;
}
}
}
}
return (char *)NULL;
}
}