Skip to content

Commit 035ff24

Browse files
committed
Conform to C89—and my own quality standards—for MSVC (2005,2022,2026), DOS (OpenWatcom), gcc, and clang
1 parent abef559 commit 035ff24

2 files changed

Lines changed: 19 additions & 13 deletions

File tree

c89stringutils/c89stringutils_export_pregen.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
#ifndef C89STRINGUTILS_EXPORT_H
33
#define C89STRINGUTILS_EXPORT_H
44

5+
#ifdef __cplusplus
6+
extern "C" {
7+
#endif /* __cplusplus */
8+
59
#ifdef C89STRINGUTILS_STATIC_DEFINE
610
#define C89STRINGUTILS_EXPORT
711
#define C89STRINGUTILS_NO_EXPORT
@@ -14,7 +18,6 @@
1418
#else
1519
#define C89STRINGUTILS_EXPORT
1620
#endif
17-
#endif
1821
#else
1922
/* We are using this library */
2023
#if defined(BUILD_SHARED)
@@ -23,6 +26,7 @@
2326
#define C89STRINGUTILS_EXPORT
2427
#endif
2528
#endif
29+
#endif
2630

2731
#ifndef C89STRINGUTILS_NO_EXPORT
2832
#define C89STRINGUTILS_NO_EXPORT
@@ -53,4 +57,8 @@
5357
#endif
5458
#endif
5559

60+
#ifdef __cplusplus
61+
}
62+
#endif /* __cplusplus */
63+
5664
#endif /* C89STRINGUTILS_EXPORT_H */

c89stringutils/c89stringutils_string_extras.c

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,6 @@
2626
*
2727
* SPDX-License-Identifier: BSD-2-Clause
2828
*/
29-
#ifdef OLD_MSVC
30-
31-
#define snprintf _snprintf
32-
#define vsnprintf _vsnprintf
33-
34-
#else
3529

3630
#ifdef ANY_BSD
3731
#define _vsnprintf vsnprintf
@@ -41,7 +35,11 @@ static int wtf_snprintf(char *buffer, size_t count, const char *format, ...) {
4135
int result;
4236
va_list args;
4337
va_start(args, format);
38+
#if defined(_MSC_VER)
39+
result = _vsnprintf_s(buffer, count, _TRUNCATE, format, args);
40+
#else
4441
result = _vsnprintf(buffer, count, format, args);
42+
#endif
4543
va_end(args);
4644
/* In the case where the string entirely filled the buffer, _vsnprintf will
4745
not null-terminate it, but snprintf must. */
@@ -52,23 +50,23 @@ static int wtf_snprintf(char *buffer, size_t count, const char *format, ...) {
5250

5351
static int wtf_vsnprintf(char *buffer, size_t count, const char *format,
5452
va_list args) {
55-
int result = _vsnprintf(buffer, count, format, args);
53+
int result;
54+
#if defined(_MSC_VER)
55+
result = _vsnprintf_s(buffer, count, _TRUNCATE, format, args);
56+
#else
57+
result = _vsnprintf(buffer, count, format, args);
58+
#endif
5659
/* In the case where the string entirely filled the buffer, _vsnprintf will
5760
not null-terminate it, but vsnprintf must. */
5861
if (count > 0)
5962
buffer[count - 1] = '\0';
6063
return result;
6164
}
6265

63-
/* Work around a difference in Microsoft's implementation of vsnprintf, where
64-
vsnprintf does not null terminate the buffer. WebKit can rely on the null
65-
termination. Microsoft's implementation is fixed in VS 2015. */
6666
#define vsnprintf(buffer, count, format, args) \
6767
wtf_vsnprintf(buffer, count, format, args)
6868
#define snprintf wtf_snprintf
6969

70-
#endif /* OLD_MSVC */
71-
7270
#endif /* !HAVE_SNPRINTF_H */
7371

7472
#ifndef HAVE_STRNCASECMP_H

0 commit comments

Comments
 (0)