Skip to content
Snippets Groups Projects
Commit abec217a authored by Kevin P. Fleming's avatar Kevin P. Fleming
Browse files

add experimental ast_copy_string() function to be used in place of strncpy()...

add experimental ast_copy_string() function to be used in place of strncpy() (see discussion on asterisk-dev)


git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@5547 65c4cc65-6c06-0410-ace0-fbb531ad65f3
parent 405000c8
Branches
Tags
No related merge requests found
...@@ -9,18 +9,20 @@ ...@@ -9,18 +9,20 @@
* the GNU General Public License * the GNU General Public License
*/ */
#ifndef _ASTERISK_UTIL_H #ifndef _ASTERISK_UTILS_H
#define _ASTERISK_UTIL_H #define _ASTERISK_UTILS_H
#ifdef SOLARIS #ifdef SOLARIS
#include <solaris-compat/compat.h> #include <solaris-compat/compat.h>
#endif #endif
#include <netinet/in.h> #include <netinet/in.h>
#include <netdb.h> #include <netdb.h>
#include <pthread.h> #include <pthread.h>
#include "asterisk/lock.h"
#include <limits.h> #include <limits.h>
#include "asterisk/lock.h"
/* Note: /* Note:
It is very important to use only unsigned variables to hold It is very important to use only unsigned variables to hold
bit flags, as otherwise you can fall prey to the compiler's bit flags, as otherwise you can fall prey to the compiler's
...@@ -166,10 +168,33 @@ struct ast_realloca { ...@@ -166,10 +168,33 @@ struct ast_realloca {
#define AST_STACKSIZE 256 * 1024 #define AST_STACKSIZE 256 * 1024
#define ast_pthread_create(a,b,c,d) ast_pthread_create_stack(a,b,c,d,0) #define ast_pthread_create(a,b,c,d) ast_pthread_create_stack(a,b,c,d,0)
extern int ast_pthread_create_stack(pthread_t *thread, pthread_attr_t *attr, void *(*start_routine)(void *), void *data, size_t stacksize);
#ifdef __linux__ #ifdef __linux__
#define ast_strcasestr strcasestr #define ast_strcasestr strcasestr
#else #else
extern char *ast_strcasestr(const char *, const char *); extern char *ast_strcasestr(const char *, const char *);
#endif /* __linux__ */ #endif /* __linux__ */
extern int ast_pthread_create_stack(pthread_t *thread, pthread_attr_t *attr, void *(*start_routine)(void *), void *data, size_t stacksize);
#if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 96)
#define __builtin_expect(exp, c) (exp)
#endif #endif
/*!
\brief Size-limited null-terminating string copy.
\param dst The destination buffer.
\param src The source string
\param size The size of the destination buffer
This is similar to \a strncpy, with two important differences:
- the destination buffer will \b always be null-terminated
- the destination buffer is not filled with zeros past the copied string length
These differences make it slightly more efficient, and safer to use since it will
not leave the destination buffer unterminated. There is no need to pass an artificially
reduced buffer size to this function (unlike \a strncpy), and the buffer does not need
to be initialized to zeroes prior to calling this function.
No return value.
*/
void ast_copy_string(char *dst, const char *src, size_t size);
#endif /* _ASTERISK_UTILS_H */
...@@ -418,6 +418,15 @@ int ast_wait_for_input(int fd, int ms) ...@@ -418,6 +418,15 @@ int ast_wait_for_input(int fd, int ms)
return poll(pfd, 1, ms); return poll(pfd, 1, ms);
} }
void ast_copy_string(char *dst, const char *src, size_t size)
{
while (*src && size--)
*dst++ = *src++;
if (__builtin_expect(!size, 0))
dst--;
*dst = '\0';
}
/* Case-insensitive substring matching */ /* Case-insensitive substring matching */
#ifndef LINUX #ifndef LINUX
static char *upper(const char *orig, char *buf, int bufsize) static char *upper(const char *orig, char *buf, int bufsize)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment