Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
VSNPRINTF_RETURN.cpp
Go to the documentation of this file.
1// checking vsnprintf() return value
2
3
25#include <stddef.h>
26#include <stdarg.h>
27#include <stdio.h>
28
29#include "config.h"
30
31
32#ifdef _RWSTD_NO_VSNPRINTF
33# if !defined (_MSC_VER)
34
35extern "C" int vsnprintf (char*, size_t, const char*, va_list);
36
37# else // if defined (_MSC_VER)
38
39extern "C" int _vsnprintf (char*, size_t, const char*, va_list);
40
41# define vsnprintf _vsnprintf
42
43# endif // MSVC
44
45#endif // _RWSTD_NO_VSNPRINTF
46
47
48int my_snprintf (char *buf, size_t size, const char *fmat, ...)
49{
50 va_list va;
51 va_start (va, fmat);
52
53 const int ret = vsnprintf (buf, size, fmat, va);
54
55 va_end (va);
56
57 return ret;
58}
59
60
61int test_vsnprintf (int success)
62{
63 char buf [] = "0123456789";
64
65 const char data [] = "abcdef";
66
67 const size_t bufsize = success ? sizeof buf : 4U;
68 const size_t datasize = sizeof data;
69
70 const int ret = my_snprintf (buf, bufsize, "%s", data);
71
72 // 7.19.6.12, p3 of C99:
73 //
74 // The vsnprintf function returns the number of characters that would
75 // have been written had n been sufficiently large, not counting the
76 // terminating null character, or a negative value if an encoding error
77 // occurred. Thus, the null-terminated output has been completely
78 // written if and only if the returned value is nonnegative and less
79 // than n.
80
81 if (success) {
82 if (datasize - 1 == ret) {
83 // expected behavior, no output
84 return 0;
85 }
86 else if (datasize == ret) {
87 printf ("#define _RWSTD_NO_VSNPRINTF_NUL "
88 "/* return value includes NUL on success */\n");
89 return -1;
90 }
91 else if (ret < 0) {
92 printf ("#define _RWSTD_NO_VSNPRINTF_POS "
93 "/* return value negative on success */\n");
94 return 1;
95 }
96 else {
97 printf ("#define _RWSTD_NO_VSNPRINTF_VALID "
98 "/* return value bogus on success */\n");
99 return 1;
100 }
101 }
102 else {
103 if (datasize - 1 == ret) {
104 // expected behavior, no output
105 return 0;
106 }
107 else if (ret < 0) {
108 // HP-UX and Windows are known to return -1 on buffer overflow
109 printf ("#define _RWSTD_NO_VSNPRINTF_VFLOW_VALID "
110 "/* return value negative on overflow */\n");
111 return 1;
112 }
113 else if (3 == ret) {
114 // IRIX and Tru64 UNIX are known to return the number
115 // of bytes actually written which is useless for
116 // determing the size of the buffer
117 printf ("#define _RWSTD_NO_VSNPRINTF_VFLOW_VALID "
118 "/* returns the number of non-NUL bytes written */\n");
119 return 1;
120 }
121 else if (4 == ret) {
122 printf ("#define _RWSTD_NO_VSNPRINTF_VFLOW_VALID "
123 "/* returns the number of bytes written */\n");
124 return 1;
125 }
126 else {
127 printf ("#define _RWSTD_NO_VSNPRINTF_VFLOW_VALID "
128 "/* return value bogus on overflow */\n");
129 return 1;
130 }
131 }
132}
133
134
135int main ()
136{
137 test_vsnprintf (0);
138 test_vsnprintf (1);
139
140 return 0;
141}
#define buf
Definition 17.names.cpp:452
const char *const data[]
IMPORT_C SInt32 printf(const Char *,...)
int my_snprintf(char *buf, size_t size, const char *fmat,...)
int test_vsnprintf(int success)
int main()
#define va_end(ignore)
#define va_start(va, name)