77#define C89STRINGUTILS_STRING_EXTRAS_H
88
99#include <string.h>
10+ #include <stdarg.h>
11+ #include <stdio.h>
1012
1113#if defined(__FreeBSD__ ) || defined(__NetBSD__ ) || defined(__OpenBSD__ ) || \
1214 defined(__bsdi__ ) || defined(__DragonFly__ ) || defined(BSD )
3537
3638# include <sys/param.h>
3739
38- # if _BSD_SOURCE || _XOPEN_SOURCE >= 500 || _ISOC99_SOURCE || _POSIX_C_SOURCE >= 200112L || defined( ANY_BSD )
40+ # if _BSD_SOURCE || _XOPEN_SOURCE >= 500 || _ISOC99_SOURCE || _POSIX_C_SOURCE >= 200112L
3941# define HAVE_SNPRINTF_H
42+ # endif /* _BSD_SOURCE || _XOPEN_SOURCE >= 500 || _ISOC99_SOURCE || _POSIX_C_SOURCE >= 200112L */
43+ # if defined(_GNU_SOURCE ) || defined(ANY_BSD )
4044# define HAVE_STRCASESTR_H
41- # endif /* _BSD_SOURCE || _XOPEN_SOURCE >= 500 || _ISOC99_SOURCE || _POSIX_C_SOURCE >= 200112L || defined(ANY_BSD) */
45+ # endif /* defined(_GNU_SOURCE) || defined(ANY_BSD) */
4246
4347# if defined(__APPLE__ ) && defined(__MACH__ )
4448# define HAVE_SNPRINTF_H
5155
5256#endif /* defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) */
5357
54- #if defined(__STDC_VERSION__ ) && __STDC_VERSION__ >= 201112L && defined(__STDC_LIB_EXT1__ ) && \
55- __STDC_WANT_LIB_EXT1__ || defined(__APPLE__ ) || defined(__APPLE_CC__ )
58+ #if defined(__STDC_LIB_EXT1__ ) && __STDC_WANT_LIB_EXT1__
5659
5760# define HAVE_STRERRORLEN_S
5861
5962#else
6063
6164# if !defined(__APPLE__ ) && !defined(__APPLE_CC__ )
62- typedef int errno_t ;
65+ typedef int errno_t ;
6366# endif /* !defined(__APPLE__) && !defined(__APPLE_CC__) */
6467
65- #endif /* defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L && defined(__STDC_LIB_EXT1__) && \
66- __STDC_WANT_LIB_EXT1__ || defined(__APPLE__) || defined(__APPLE_CC__) */
68+ # if defined(__linux__ ) || defined(linux ) || defined(__linux ) || defined(ANY_BSD )
69+ # define strerror_s strerror_r
70+ # define HAVE_STRERRORLEN_S
71+ # endif /* defined(__linux__) || defined(linux) || defined(__linux) || defined(ANY_BSD) */
72+
73+ #endif /* defined(__STDC_LIB_EXT1__) && __STDC_WANT_LIB_EXT1__ */
6774
6875#ifndef _MSC_VER
6976# define HAVE_STRINGS_H
7481# define HAVE_ASPRINTF
7582#endif /* defined(ANY_BSD) || defined(__APPLE__) && defined(__MACH__) || defined(_GNU_SOURCE) || defined(_BSD_SOURCE) */
7683
77- #include <stdarg.h>
78- #include <stdio.h>
79- #include <string.h>
80-
8184#ifdef HAVE_STRINGS_H
8285# include <strings.h>
8386#endif /* HAVE_STRINGS_H */
8487
8588#if !defined(HAVE_SNPRINTF_H ) && defined(C89STRINGUTILS_IMPLEMENTATION )
86-
89+ #define HAVE_SNPRINTF_H
8790/*
8891 * `snprintf`, `vsnprintf`, `strnstr` taken from:
8992 * https://chromium.googlesource.com/chromium/blink/+/5cedd2fd208daf119b9ea47c7c1e22d760a586eb/Source/wtf/StringExtras.h
@@ -133,7 +136,7 @@ extern int strncasecmp(const char *, const char *, size_t);
133136extern int strcasecmp (const char * , const char * );
134137
135138#ifdef C89STRINGUTILS_IMPLEMENTATION
136-
139+ #define HAVE_STRNCASECMP_H
137140#define strncasecmp _strnicmp
138141
139142#define strcasecmp _stricmp
@@ -147,6 +150,7 @@ extern int strcasecmp(const char *, const char *);
147150extern char * strnstr (const char * , const char * , size_t );
148151
149152#ifdef C89STRINGUTILS_IMPLEMENTATION
153+ #define HAVE_STRNSTR
150154char * strnstr (const char * buffer , const char * target , size_t bufferLength ) {
151155 /*
152156 Find the first occurrence of find in s, where the search is limited to the
@@ -182,6 +186,7 @@ char *strnstr(const char *buffer, const char *target, size_t bufferLength) {
182186extern char * strcasestr (const char * , const char * );
183187
184188#ifdef C89STRINGUTILS_IMPLEMENTATION
189+ #define HAVE_STRCASESTR_H
185190
186191/* `strcasestr` from MUSL */
187192
@@ -198,6 +203,10 @@ char *strcasestr(const char *h, const char *n)
198203
199204#ifndef HAVE_STRERRORLEN_S
200205
206+ extern size_t strerrorlen_s (errno_t );
207+
208+ #ifdef C89STRINGUTILS_IMPLEMENTATION
209+ #define HAVE_STRERRORLEN_S
201210/* MIT licensed function from Safe C Library */
202211
203212size_t strerrorlen_s (errno_t errnum )
@@ -246,91 +255,8 @@ size_t strerrorlen_s(errno_t errnum)
246255 }
247256}
248257
249- #endif /* !HAVE_STRERRORLEN_S */
250-
251- #ifndef HAVE_ASPRINTF
252-
253- extern int vasprintf (char * * str , const char * fmt , va_list ap );
254-
255- extern int asprintf (char * * str , const char * fmt , ...);
256-
257- #ifdef C89STRINGUTILS_IMPLEMENTATION
258-
259- #include <errno.h>
260- #include <limits.h> /* for INT_MAX */
261- #include <stdlib.h>
262-
263- #ifndef VA_COPY
264- # if defined(HAVE_VA_COPY ) || defined(va_copy )
265- # define VA_COPY (dest , src ) va_copy(dest, src)
266- # else
267- # ifdef HAVE___VA_COPY
268- # define VA_COPY (dest , src ) __va_copy(dest, src)
269- # else
270- # define VA_COPY (dest , src ) (dest) = (src)
271- # endif
272- # endif
273- #endif /* ! VA_COPY */
274-
275- #define INIT_SZ 128
276-
277- extern int
278- vasprintf (char * * str , const char * fmt , va_list ap )
279- {
280- int ret ;
281- va_list ap2 ;
282- char * string , * newstr ;
283- size_t len ;
284-
285- if ((string = (char * ) malloc (INIT_SZ )) == NULL )
286- goto fail ;
287-
288- VA_COPY (ap2 , ap );
289- ret = vsnprintf (string , INIT_SZ , fmt , ap2 );
290- va_end (ap2 );
291- if (ret >= 0 && ret < INIT_SZ ) { /* succeeded with initial alloc */
292- * str = string ;
293- } else if (ret == INT_MAX || ret < 0 ) { /* Bad length */
294- free (string );
295- goto fail ;
296- } else { /* bigger than initial, realloc allowing for nul */
297- len = (size_t )ret + 1 ;
298- if ((newstr = (char * )realloc (string , len )) == NULL ) {
299- free (string );
300- goto fail ;
301- }
302- VA_COPY (ap2 , ap );
303- ret = vsnprintf (newstr , len , fmt , ap2 );
304- va_end (ap2 );
305- if (ret < 0 || (size_t )ret >= len ) { /* failed with realloc'ed string */
306- free (newstr );
307- goto fail ;
308- }
309- * str = newstr ;
310- }
311- return ret ;
312-
313- fail :
314- * str = NULL ;
315- errno = ENOMEM ;
316- return -1 ;
317- }
318-
319- extern int asprintf (char * * str , const char * fmt , ...)
320- {
321- va_list ap ;
322- int ret ;
323-
324- * str = NULL ;
325- va_start (ap , fmt );
326- ret = vasprintf (str , fmt , ap );
327- va_end (ap );
328-
329- return ret ;
330- }
331-
332258#endif /* C89STRINGUTILS_IMPLEMENTATION */
333259
334- #endif /* !HAVE_ASPRINTF */
260+ #endif /* !HAVE_STRERRORLEN_S */
335261
336262#endif /* ! C89STRINGUTILS_STRING_EXTRAS_H */
0 commit comments