diff --git a/HISTORY b/HISTORY index e36c11f..9c2489e 100644 --- a/HISTORY +++ b/HISTORY @@ -1,10 +1,11 @@ -Version 1.40 2018-08-24 +Version 1.40 2018-08-27 * add function conn_pool_parse_server_info and conn_pool_load_server_info * support directive: #@add_annotation, for example: #@add_annotation CONFIG_GET /usr/lib/libshmcache.so /etc/libshmcache.conf * add function fc_split_string and fc_match_delim * add json_parser.[hc] for parse json array and map + * add function fc_strdup Version 1.39 2018-07-31 * add #@function REPLACE_VARS diff --git a/src/shared_func.c b/src/shared_func.c index 4aa3ae1..763415e 100644 --- a/src/shared_func.c +++ b/src/shared_func.c @@ -2716,3 +2716,22 @@ bool ends_with(const char *str, const char *needle) return memcmp(str + start_offset, needle, needle_len) == 0; } + +char *fc_strdup(const char *str, const int len) +{ + char *output; + + output = (char *)malloc(len + 1); + if (output == NULL) { + logError("file: "__FILE__", line: %d, " + "malloc %d bytes fail", + __LINE__, len + 1); + return NULL; + } + + if (len > 0) { + memcpy(output, str, len); + } + *(output + len) = '\0'; + return output; +} diff --git a/src/shared_func.h b/src/shared_func.h index 81423d2..220c8d1 100644 --- a/src/shared_func.h +++ b/src/shared_func.h @@ -791,6 +791,14 @@ bool starts_with(const char *str, const char *needle); */ bool ends_with(const char *str, const char *needle); +/** strdup + * parameters: + * str: the string to duplicate + * len: the length of string + * return: the duplicated string, NULL for fail +*/ +char *fc_strdup(const char *str, const int len); + #ifdef __cplusplus } #endif