1/* Generic string.h */
2/* $OpenLDAP$ */
3/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 *
5 * Copyright 1998-2011 The OpenLDAP Foundation.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
10 * Public License.
11 *
12 * A copy of this license is available in file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
15 */
16
17#ifndef _AC_STRING_H
18#define _AC_STRING_H
19
20#ifdef STDC_HEADERS
21#	include <string.h>
22
23#else
24#	ifdef HAVE_STRING_H
25#		include <string.h>
26#	endif
27#	if defined(HAVE_STRINGS_H) && (!defined(HAVE_STRING_H) || defined(BOTH_STRINGS_H))
28#		include <strings.h>
29#	endif
30
31#	ifdef HAVE_MEMORY_H
32#		include <memory.h>
33#	endif
34
35#	ifndef HAVE_STRRCHR
36#		undef strchr
37#		define strchr index
38#		undef strrchr
39#		define strrchr rindex
40#	endif
41
42#	ifndef HAVE_MEMCPY
43#		undef memcpy
44#		define memcpy(d, s, n)		((void) bcopy ((s), (d), (n)))
45#		undef memmove
46#		define memmove(d, s, n)		((void) bcopy ((s), (d), (n)))
47#	endif
48#endif
49
50/* use ldap_pvt_strtok instead of strtok or strtok_r! */
51LDAP_F(char *) ldap_pvt_strtok LDAP_P(( char *str,
52	const char *delim, char **pos ));
53
54#ifndef HAVE_STRDUP
55	/* strdup() is missing, declare our own version */
56#	undef strdup
57#	define strdup(s) ber_strdup(s)
58#elif !defined(_WIN32)
59	/* some systems fail to declare strdup */
60	/* Windows does not require this declaration */
61	LDAP_LIBC_F(char *) (strdup)();
62#endif
63
64/*
65 * some systems fail to declare strcasecmp() and strncasecmp()
66 * we need them declared so we can obtain pointers to them
67 */
68
69/* we don't want these declared for Windows or Mingw */
70#ifndef _WIN32
71int (strcasecmp)();
72int (strncasecmp)();
73#endif
74
75#ifndef SAFEMEMCPY
76#	if defined( HAVE_MEMMOVE )
77#		define SAFEMEMCPY( d, s, n ) 	memmove((d), (s), (n))
78#	elif defined( HAVE_BCOPY )
79#		define SAFEMEMCPY( d, s, n ) 	bcopy((s), (d), (n))
80#	else
81		/* nothing left but memcpy() */
82#		define SAFEMEMCPY( d, s, n )	memcpy((d), (s), (n))
83#	endif
84#endif
85
86#define AC_MEMCPY( d, s, n ) (SAFEMEMCPY((d),(s),(n)))
87#define AC_FMEMCPY( d, s, n ) do { \
88		if((n) == 1) *((char*)(d)) = *((char*)(s)); \
89		else AC_MEMCPY( (d), (s), (n) ); \
90	} while(0)
91
92#ifdef NEED_MEMCMP_REPLACEMENT
93	int (lutil_memcmp)(const void *b1, const void *b2, size_t len);
94#define memcmp lutil_memcmp
95#endif
96
97void *(lutil_memrchr)(const void *b, int c, size_t n);
98/* GNU extension (glibc >= 2.1.91), only declared when defined(_GNU_SOURCE) */
99#if defined(HAVE_MEMRCHR) && defined(_GNU_SOURCE)
100#define lutil_memrchr(b, c, n) memrchr(b, c, n)
101#endif /* ! HAVE_MEMRCHR */
102
103#define STRLENOF(s)	(sizeof(s)-1)
104
105#if defined( HAVE_NONPOSIX_STRERROR_R )
106#	define AC_STRERROR_R(e,b,l)		(strerror_r((e), (b), (l)))
107#elif defined( HAVE_STRERROR_R )
108#	define AC_STRERROR_R(e,b,l)		(strerror_r((e), (b), (l)) == 0 ? (b) : "Unknown error")
109#elif defined( HAVE_SYS_ERRLIST )
110#	define AC_STRERROR_R(e,b,l)		((e) > -1 && (e) < sys_nerr \
111							? sys_errlist[(e)] : "Unknown error" )
112#elif defined( HAVE_STRERROR )
113#	define AC_STRERROR_R(e,b,l)		(strerror(e))	/* NOTE: may be NULL */
114#else
115#	define AC_STRERROR_R(e,b,l)		("Unknown error")
116#endif
117
118#endif /* _AC_STRING_H */
119