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