libevent基础文件strlcpy.c

strlcpy.c只是实现了字符串拷贝函数_event_strlcpy(char * dst,const char *src,size_t siz);
在头文件中定义了个宏:#define strlcpy _event_strlcpy
对应的头文件是:strlcpy-internal.h,内容如下:
 
 
#ifndef _STRLCPY_INTERNAL_H_
#define _STRLCPY_INTERNAL_H_


#ifdef __cplusplus
extern "C" {
#endif


#include "event2/event-config.h"


#ifndef _EVENT_HAVE_STRLCPY
#include <string.h>
size_t _event_strlcpy(char *dst, const char *src, size_t siz);
#define strlcpy _event_strlcpy
#endif


#ifdef __cplusplus
}
#endif


#endif
strlcpy.c文件内容如下:(注意该函数并不检查dst,src参数的合法性。)
#if defined(LIBC_SCCS) && !defined(lint)
static char *rcsid = "$OpenBSD: strlcpy.c,v 1.5 2001/05/13 15:40:16 deraadt Exp $";
#endif /* LIBC_SCCS and not lint */

#include <sys/types.h>

#include "event2/event-config.h"

#ifndef _EVENT_HAVE_STRLCPY
#include "strlcpy-internal.h"

/*
 * Copy src to string dst of size siz.  At most siz-1 characters
 * will be copied.  Always NUL terminates (unless siz == 0).
 * Returns strlen(src); if retval >= siz, truncation occurred.
 */
size_t
_event_strlcpy(dst, src, siz)
	char *dst;
	const char *src;
	size_t siz;
{
	register char *d = dst;
	register const char *s = src;
	register size_t n = siz;

	/* Copy as many bytes as will fit */
	if (n != 0 && --n != 0) {
		do {
			if ((*d++ = *s++) == 0)
				break;
		} while (--n != 0);
	}

	/* Not enough room in dst, add NUL and traverse rest of src */
	if (n == 0) {
		if (siz != 0)
			*d = '\0';		/* NUL-terminate dst */
		while (*s++)
			;
	}

	return (s - src - 1);	/* count does not include NUL */
}
#endif

猜你喜欢

转载自blog.csdn.net/handsomehong/article/details/79252646