comparison src/lib/strfuncs.c @ 859:c4754c5770f3 HEAD

We weren't using va_list properly, especially gcc/PowerPC didn't like it.
author Timo Sirainen <tss@iki.fi>
date Fri, 27 Dec 2002 18:02:25 +0200
parents 7f41a4b33975
children 21ffcce83c70
comparison
equal deleted inserted replaced
858:8f4bd02461e0 859:c4754c5770f3
95 int i_snprintf(char *dest, size_t max_chars, const char *format, ...) 95 int i_snprintf(char *dest, size_t max_chars, const char *format, ...)
96 { 96 {
97 #ifndef HAVE_VSNPRINTF 97 #ifndef HAVE_VSNPRINTF
98 char *buf; 98 char *buf;
99 #endif 99 #endif
100 va_list args; 100 va_list args, args2;
101 ssize_t len; 101 ssize_t len;
102 int ret; 102 int ret;
103 103
104 i_assert(max_chars < INT_MAX); 104 i_assert(max_chars < INT_MAX);
105 105
106 t_push(); 106 t_push();
107 107
108 va_start(args, format); 108 va_start(args, format);
109 VA_COPY(args2, args);
110
109 format = printf_string_fix_format(format); 111 format = printf_string_fix_format(format);
110 len = printf_string_upper_bound(format, args); 112 len = printf_string_upper_bound(format, args);
111 va_end(args);
112 113
113 i_assert(len >= 0); 114 i_assert(len >= 0);
114 115
115 #ifdef HAVE_VSNPRINTF 116 #ifdef HAVE_VSNPRINTF
116 len = vsnprintf(dest, max_chars, format, args); 117 len = vsnprintf(dest, max_chars, format, args2);
117 #else 118 #else
118 buf = t_buffer_get(len); 119 buf = t_buffer_get(len);
119 len = vsprintf(buf, format, args); 120 len = vsprintf(buf, format, args2);
120 #endif 121 #endif
122 va_end(args);
123
121 if (len < 0) { 124 if (len < 0) {
122 /* some error occured */ 125 /* some error occured */
123 len = 0; 126 len = 0;
124 ret = -1; 127 ret = -1;
125 } else if ((size_t)len >= max_chars) { 128 } else if ((size_t)len >= max_chars) {
211 } 214 }
212 215
213 char *p_strdup_vprintf(Pool pool, const char *format, va_list args) 216 char *p_strdup_vprintf(Pool pool, const char *format, va_list args)
214 { 217 {
215 char *ret; 218 char *ret;
219 va_list args2;
216 size_t len; 220 size_t len;
217 221
218 i_assert(format != NULL); 222 i_assert(format != NULL);
219 223
220 if (pool != data_stack_pool) 224 if (pool != data_stack_pool)
221 t_push(); 225 t_push();
222 226
227 VA_COPY(args2, args);
228
223 format = printf_string_fix_format(format); 229 format = printf_string_fix_format(format);
224 230
225 len = printf_string_upper_bound(format, args); 231 len = printf_string_upper_bound(format, args);
226 ret = p_malloc(pool, len); 232 ret = p_malloc(pool, len);
227 233
228 #ifdef HAVE_VSNPRINTF 234 #ifdef HAVE_VSNPRINTF
229 vsnprintf(ret, len, format, args); 235 vsnprintf(ret, len, format, args2);
230 #else 236 #else
231 vsprintf(ret, format, args); 237 vsprintf(ret, format, args2);
232 #endif 238 #endif
233 if (pool != data_stack_pool) 239 if (pool != data_stack_pool)
234 t_pop(); 240 t_pop();
235 return ret; 241 return ret;
236 } 242 }