xref: /illumos-gate/usr/src/head/string.h (revision a2cd9e1884647e1e412c282879881873b71c84df)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2014 Garrett D'Amore <garrett@damore.org>
24  * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
25  * Copyright 2020 Joyent, Inc.
26  */
27 
28 /*	Copyright (c) 1988 AT&T	*/
29 /*	  All Rights Reserved	*/
30 
31 #ifndef _STRING_H
32 #define	_STRING_H
33 
34 #include <iso/string_iso.h>
35 
36 /*
37  * Allow global visibility for symbols defined in
38  * C++ "std" namespace in <iso/string_iso.h>.
39  */
40 #if __cplusplus >= 199711L
41 using std::size_t;
42 using std::memchr;
43 using std::memcmp;
44 using std::memcpy;
45 using std::memmove;
46 using std::memset;
47 using std::strcat;
48 using std::strchr;
49 using std::strcmp;
50 using std::strcoll;
51 using std::strcpy;
52 using std::strcspn;
53 using std::strerror;
54 using std::strlen;
55 using std::strncat;
56 using std::strncmp;
57 using std::strncpy;
58 using std::strpbrk;
59 using std::strrchr;
60 using std::strspn;
61 using std::strstr;
62 using std::strtok;
63 using std::strxfrm;
64 #endif
65 
66 #ifdef	__cplusplus
67 extern "C" {
68 #endif
69 
70 #if defined(__EXTENSIONS__) || \
71 	(!defined(_STRICT_STDC) && !defined(__XOPEN_OR_POSIX)) || \
72 	defined(_XPG6) || defined(_REENTRANT)
73 extern int strerror_r(int, char *, size_t);
74 #endif
75 
76 #if defined(__EXTENSIONS__) || \
77 	(!defined(_STRICT_STDC) && !defined(__XOPEN_OR_POSIX)) || \
78 	(_POSIX_C_SOURCE - 0 >= 199506L) || defined(_REENTRANT)
79 extern char *strtok_r(char *_RESTRICT_KYWD, const char *_RESTRICT_KYWD,
80 	char **_RESTRICT_KYWD);
81 #endif
82 
83 #if defined(__EXTENSIONS__) || !defined(_STRICT_STDC) || \
84 	defined(__XOPEN_OR_POSIX)
85 extern void *memccpy(void *_RESTRICT_KYWD, const void *_RESTRICT_KYWD,
86 		int, size_t);
87 #endif
88 
89 #if !defined(_STRICT_SYMBOLS) || defined(_XPG7)
90 
91 extern char *stpcpy(char *_RESTRICT_KYWD, const char *_RESTRICT_KYWD);
92 extern char *stpncpy(char *_RESTRICT_KYWD, const char *_RESTRICT_KYWD, size_t);
93 extern char *strndup(const char *, size_t);
94 extern size_t strnlen(const char *, size_t);
95 extern char *strsignal(int);
96 
97 #ifndef	_LOCALE_T
98 #define	_LOCALE_T
99 typedef struct _locale *locale_t;
100 #endif
101 
102 extern int strcoll_l(const char *, const char *, locale_t);
103 extern size_t strxfrm_l(char *_RESTRICT_KYWD, const char *_RESTRICT_KYWD,
104     size_t, locale_t);
105 extern int strcasecmp_l(const char *, const char *, locale_t);
106 extern int strncasecmp_l(const char *, const char *, size_t, locale_t);
107 extern char *strerror_l(int, locale_t);
108 
109 #endif /* defined(_STRICT_SYMBOLS) || defined(_XPG7) */
110 
111 #if !defined(_STRICT_SYMBOLS)
112 
113 /* Note that some of these are also declared in strings.h for XPG4_2+ */
114 extern void explicit_bzero(void *, size_t);
115 extern int uucopy(const void *_RESTRICT_KYWD, void *_RESTRICT_KYWD, size_t);
116 extern int uucopystr(const void *_RESTRICT_KYWD, void *_RESTRICT_KYWD, size_t);
117 extern int ffs(int);
118 extern int ffsl(long);
119 extern int ffsll(long long);
120 extern int fls(int);
121 extern int flsl(long);
122 extern int flsll(long long);
123 extern void *memmem(const void *, size_t, const void *, size_t);
124 extern void *memrchr(const void *, int, size_t);
125 extern char *strcasestr(const char *, const char *);
126 extern char *strnstr(const char *, const char *, size_t);
127 extern size_t strlcpy(char *, const char *, size_t);
128 extern size_t strlcat(char *, const char *, size_t);
129 extern char *strsep(char **stringp, const char *delim);
130 extern char *strchrnul(const char *, int);
131 extern char *strcasestr_l(const char *, const char *, locale_t);
132 extern int strcasecmp(const char *, const char *);
133 extern int strncasecmp(const char *, const char *, size_t);
134 #endif /* !defined(__STRICT_SYMBOLS) */
135 
136 #if defined(__EXTENSIONS__) || \
137 	(!defined(_STRICT_STDC) && !defined(__XOPEN_OR_POSIX)) || \
138 	defined(_XPG4_2)
139 extern char *strdup(const char *);
140 #endif
141 
142 #if defined(__EXTENSIONS__) || \
143 	(!defined(_STRICT_STDC) && !defined(__XOPEN_OR_POSIX))
144 
145 #if defined(__GNUC__)
146 
147 #define	strdupa(s)							\
148 	(__extension__(							\
149 	{								\
150 	char *__str = (char *)(s);					\
151 	strcpy((char *)__builtin_alloca(strlen(__str) + 1), __str);	\
152 	}))
153 
154 #define	strndupa(s, n)							\
155 	(__extension__(							\
156 	{								\
157 	char *__str = (char *)(s);					\
158 	size_t __len = strnlen(__str, (n));				\
159 	(__str = strncpy((char *)__builtin_alloca(__len + 1),		\
160 	    __str, __len),						\
161 	__str[__len] = '\0', __str);					\
162 	}))
163 
164 #else	/* __GNUC__ */
165 
166 #if defined(unix)	/* excludes c99 */
167 /*
168  * Studio C currently can't do the gcc-style inlining,
169  * so we use thread-local storage instead.
170  */
171 extern void *__builtin_alloca(size_t);
172 extern __thread char *__strdupa_str;
173 extern __thread size_t __strdupa_len;
174 
175 #define	strdupa(s)							\
176 	(__strdupa_str = (char *)(s),					\
177 	strcpy((char *)__builtin_alloca(strlen(__strdupa_str) + 1),	\
178 	    __strdupa_str))
179 
180 #define	strndupa(s, n)							\
181 	(__strdupa_str = (char *)(s),					\
182 	__strdupa_len = strnlen(__strdupa_str, (n)),			\
183 	__strdupa_str = strncpy((char *)__builtin_alloca(__strdupa_len + 1), \
184 	    __strdupa_str, __strdupa_len),				\
185 	__strdupa_str[__strdupa_len] = '\0', __strdupa_str)
186 #endif	/* unix */
187 
188 #endif	/* __GNUC__ */
189 #endif	/* __EXTENSIONS__ ... */
190 
191 #ifdef	__cplusplus
192 }
193 #endif
194 
195 #endif	/* _STRING_H */
196