55
66/* clang-format off */
77#include "c89stringutils_string_extras.h"
8+ #include "c89stringutils_log.h"
89
910#include <stdarg.h>
1011#include <stdio.h>
3132#endif /* ANY_BSD */
3233
3334static int wtf_snprintf (char * buffer , size_t count , const char * format , ...) {
34- int result ;
35+ int rc ;
3536 va_list args ;
3637 va_start (args , format );
3738#if defined(_MSC_VER )
38- result = _vsnprintf_s (buffer , count , _TRUNCATE , format , args );
39+ rc = _vsnprintf_s (buffer , count , _TRUNCATE , format , args );
3940#else
40- result = _vsnprintf (buffer , count , format , args );
41+ rc = _vsnprintf (buffer , count , format , args );
4142#endif
4243 va_end (args );
4344 /* In the case where the string entirely filled the buffer, _vsnprintf will
4445 not null-terminate it, but snprintf must. */
4546 if (count > 0 )
4647 buffer [count - 1 ] = '\0' ;
47- return result ;
48+ return rc ;
4849}
4950
5051static int wtf_vsnprintf (char * buffer , size_t count , const char * format ,
5152 va_list args ) {
52- int result ;
53+ int rc ;
5354#if defined(_MSC_VER )
54- result = _vsnprintf_s (buffer , count , _TRUNCATE , format , args );
55+ rc = _vsnprintf_s (buffer , count , _TRUNCATE , format , args );
5556#else
56- result = _vsnprintf (buffer , count , format , args );
57+ rc = _vsnprintf (buffer , count , format , args );
5758#endif
5859 /* In the case where the string entirely filled the buffer, _vsnprintf will
5960 not null-terminate it, but vsnprintf must. */
6061 if (count > 0 )
6162 buffer [count - 1 ] = '\0' ;
62- return result ;
63+ return rc ;
6364}
6465
6566#define vsnprintf (buffer , count , format , args ) \
@@ -72,8 +73,11 @@ static int wtf_vsnprintf(char *buffer, size_t count, const char *format,
7273
7374#define HAVE_STRNCASECMP_H
7475
75- #define strncasecmp _strnicmp
76- #define strcasecmp _stricmp
76+ int strncasecmp (const char * s1 , const char * s2 , size_t n ) {
77+ return _strnicmp (s1 , s2 , n );
78+ }
79+
80+ int strcasecmp (const char * s1 , const char * s2 ) { return _stricmp (s1 , s2 ); }
7781
7882#endif /* !HAVE_STRNCASECMP_H */
7983
@@ -199,55 +203,79 @@ size_t strerrorlen_s(errno_t errnum) {
199203#define INIT_SZ 128
200204
201205extern int vasprintf (char * * str , const char * fmt , va_list ap ) {
202- int ret ;
206+ int rc ;
203207 va_list ap2 ;
204208 char * string , * newstr ;
205209 size_t len ;
206210
207- if ((string = (char * )malloc (INIT_SZ )) == NULL )
211+ if ((string = (char * )malloc (INIT_SZ )) == NULL ) {
212+ rc = -1 ;
208213 goto fail ;
214+ }
209215
210216 VA_COPY (ap2 , ap );
211- ret = vsnprintf (string , INIT_SZ , fmt , ap2 );
217+ rc = vsnprintf (string , INIT_SZ , fmt , ap2 );
212218 va_end (ap2 );
213- if (ret >= 0 && ret < INIT_SZ ) { /* succeeded with initial alloc */
219+ if (rc >= 0 && rc < INIT_SZ ) { /* succeeded with initial alloc */
214220 * str = string ;
215- } else if (ret == INT_MAX || ret < 0 ) { /* Bad length */
221+ } else if (rc == INT_MAX || rc < 0 ) { /* Bad length */
216222 free (string );
223+ rc = -1 ;
217224 goto fail ;
218225 } else { /* bigger than initial, realloc allowing for nul */
219- len = (size_t )ret + 1 ;
226+ len = (size_t )rc + 1 ;
220227 if ((newstr = (char * )realloc (string , len )) == NULL ) {
221228 free (string );
229+ rc = -1 ;
222230 goto fail ;
223231 }
224232 VA_COPY (ap2 , ap );
225- ret = vsnprintf (newstr , len , fmt , ap2 );
233+ rc = vsnprintf (newstr , len , fmt , ap2 );
226234 va_end (ap2 );
227- if (ret < 0 || (size_t )ret >= len ) { /* failed with realloc'ed string */
235+ if (rc < 0 || (size_t )rc >= len ) { /* failed with realloc'ed string */
228236 free (newstr );
237+ rc = -1 ;
229238 goto fail ;
230239 }
231240 * str = newstr ;
232241 }
233- return ret ;
242+ return rc ;
234243
235244fail :
245+ if (rc != 0 ) {
246+ #ifdef _MSC_VER
247+ char errbuf [256 ];
248+ strerror_s (errbuf , sizeof (errbuf ), errno );
249+ LOG_DEBUG ("vasprintf failed with rc=%d, error=%s\n" , rc , errbuf );
250+ #else
251+ LOG_DEBUG ("vasprintf failed with rc=%d, error=%s\n" , rc , strerror (errno ));
252+ #endif
253+ }
236254 * str = NULL ;
237255 errno = ENOMEM ;
238- return -1 ;
256+ return rc ;
239257}
240258
241259extern int asprintf (char * * str , const char * fmt , ...) {
242260 va_list ap ;
243- int ret ;
261+ int rc ;
244262
245263 * str = NULL ;
246264 va_start (ap , fmt );
247- ret = vasprintf (str , fmt , ap );
265+ rc = vasprintf (str , fmt , ap );
248266 va_end (ap );
249267
250- return ret ;
268+ if (rc < 0 ) {
269+ #ifdef _MSC_VER
270+ char errbuf [256 ];
271+ strerror_s (errbuf , sizeof (errbuf ), errno );
272+ LOG_DEBUG ("asprintf failed with rc=%d, error=%s\n" , rc , errbuf );
273+ #else
274+ LOG_DEBUG ("asprintf failed with rc=%d, error=%s\n" , rc , strerror (errno ));
275+ #endif
276+ }
277+
278+ return rc ;
251279}
252280
253281#endif /* !HAVE_ASPRINTF */
0 commit comments