1#include <config.h>
|
1/* snprintf - compatibility implementation of snprintf, vsnprintf 2 * 3 * Copyright (c) 2013, NLnet Labs. All rights reserved. 4 * 5 * This software is open source. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * Redistributions of source code must retain the above copyright notice, 12 * this list of conditions and the following disclaimer. 13 * 14 * Redistributions in binary form must reproduce the above copyright notice, 15 * this list of conditions and the following disclaimer in the documentation 16 * and/or other materials provided with the distribution. 17 * 18 * Neither the name of the NLNET LABS nor the names of its contributors may 19 * be used to endorse or promote products derived from this software without 20 * specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 26 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 28 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 29 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 31 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 32 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 */ |
34
|
3#ifndef HAVE_SNPRINTF
4
|
35#include "config.h" 36#include <stdio.h> |
37#include <ctype.h>
|
6#include <sys/types.h>
|
38#include <string.h> 39#include <stdarg.h> 40#include <stdlib.h> 41#include <errno.h> 42#ifdef HAVE_STDINT_H 43#include <stdint.h> 44#endif |
45
|
8/* Define this as a fall through, HAVE_STDARG_H is probably already set */
|
46/* for test */ 47/* #define SNPRINTF_TEST 1 */ 48#ifdef SNPRINTF_TEST 49#define snprintf my_snprintf 50#define vsnprintf my_vsnprintf 51#endif /* SNPRINTF_TEST */ |
52
|
10#define HAVE_VARARGS_H
|
53int snprintf(char* str, size_t size, const char* format, ...); 54int vsnprintf(char* str, size_t size, const char* format, va_list arg); |
55
|
12/**************************************************************
13 * Original:
14 * Patrick Powell Tue Apr 11 09:48:21 PDT 1995
15 * A bombproof version of doprnt (dopr) included.
16 * Sigh. This sort of thing is always nasty do deal with. Note that
17 * the version here does not include floating point...
|
56/** 57 * Very portable snprintf implementation, limited in functionality, 58 * esp. for %[capital] %[nonportable] and so on. Reduced float functionality, 59 * mostly in formatting and range (e+-16), for %f and %g. |
60 *
|
19 * snprintf() is used instead of sprintf() as it does limit checks
20 * for string length. This covers a nasty loophole.
21 *
22 * The other functions are there to prevent NULL pointers from
23 * causing nast effects.
24 *
25 * More Recently:
26 * Brandon Long (blong@fiction.net) 9/15/96 for mutt 0.43
27 * This was ugly. It is still ugly. I opted out of floating point
28 * numbers, but the formatter understands just about everything
29 * from the normal C string format, at least as far as I can tell from
30 * the Solaris 2.5 printf(3S) man page.
31 *
32 * Brandon Long (blong@fiction.net) 10/22/97 for mutt 0.87.1
33 * Ok, added some minimal floating point support, which means this
34 * probably requires libm on most operating systems. Don't yet
35 * support the exponent (e,E) and sigfig (g,G). Also, fmtint()
36 * was pretty badly broken, it just wasn't being exercised in ways
37 * which showed it, so that's been fixed. Also, formated the code
38 * to mutt conventions, and removed dead code left over from the
39 * original. Also, there is now a builtin-test, just compile with:
40 * gcc -DTEST_SNPRINTF -o snprintf snprintf.c -lm
41 * and run snprintf for results.
42 *
43 * Wouter Wijngaards(wouter@nlnetlabs.nl) 2/09/2010 for unbound.
44 * Limited support for %g. Does not do the exponents for the before-dot.
45 *
46 **************************************************************/
|
61 * %s, %d, %u, %i, %x, %c, %n and %% are fully supported. 62 * This includes width, precision, flags 0- +, and *(arg for wid,prec). 63 * %f, %g, %m, %p have reduced support, support for wid,prec,flags,*, but 64 * less floating point range, no %e formatting for %g. 65 */ 66int snprintf(char* str, size_t size, const char* format, ...) 67{ 68 int r; 69 va_list args; 70 va_start(args, format); 71 r = vsnprintf(str, size, format, args); 72 va_end(args); 73 return r; 74} |
75
|
76/** add padding to string */ 77static void 78print_pad(char** at, size_t* left, int* ret, char p, int num) 79{ 80 while(num--) { 81 if(*left > 1) { 82 *(*at)++ = p; 83 (*left)--; 84 } 85 (*ret)++; 86 } 87} |
88
|
49/* varargs declarations: */
|
89/** get negative symbol, 0 if none */ 90static char 91get_negsign(int negative, int plus, int space) 92{ 93 if(negative) 94 return '-'; 95 if(plus) 96 return '+'; 97 if(space) 98 return ' '; 99 return 0; 100} |
101
|
51#if defined(HAVE_STDARG_H)
52# include <stdarg.h>
53# define HAVE_STDARGS /* let's hope that works everywhere (mj) */
54# define VA_LOCAL_DECL va_list ap
55# define VA_START(f) va_start(ap, f)
56# define VA_SHIFT(v,t) ; /* no-op for ANSI */
57# define VA_END va_end(ap)
58#else
59# if defined(HAVE_VARARGS_H)
60# include <varargs.h>
61# undef HAVE_STDARGS
62# define VA_LOCAL_DECL va_list ap
63# define VA_START(f) va_start(ap) /* f is ignored! */
64# define VA_SHIFT(v,t) v = va_arg(ap,t)
65# define VA_END va_end(ap)
66# else
67/*XX ** NO VARARGS ** XX*/
68# endif
69#endif
|
102#define PRINT_DEC_BUFSZ 32 /* 20 is enough for 64 bit decimals */ 103/** print decimal into buffer, returns length */ 104static int 105print_dec(char* buf, int max, unsigned int value) 106{ 107 int i = 0; 108 if(value == 0) { 109 if(max > 0) { 110 buf[0] = '0'; 111 i = 1; 112 } 113 } else while(value && i < max) { 114 buf[i++] = '0' + value % 10; 115 value /= 10; 116 } 117 return i; 118} |
119
|
71int snprintf (char *str, size_t count, const char *fmt, ...);
72int vsnprintf (char *str, size_t count, const char *fmt, va_list arg);
|
120/** print long decimal into buffer, returns length */ 121static int 122print_dec_l(char* buf, int max, unsigned long value) 123{ 124 int i = 0; 125 if(value == 0) { 126 if(max > 0) { 127 buf[0] = '0'; 128 i = 1; 129 } 130 } else while(value && i < max) { 131 buf[i++] = '0' + value % 10; 132 value /= 10; 133 } 134 return i; 135} |
136
|
74static void dopr (char *buffer, size_t maxlen, const char *format,
75 va_list args);
76static void fmtstr (char *buffer, size_t *currlen, size_t maxlen,
77 char *value, int flags, int min, int max);
78static void fmtint (char *buffer, size_t *currlen, size_t maxlen,
79 long value, int base, int min, int max, int flags);
80static void fmtfp (char *buffer, size_t *currlen, size_t maxlen,
81 long double fvalue, int min, int max, int flags, int conv);
82static void dopr_outch (char *buffer, size_t *currlen, size_t maxlen, char c );
|
137/** print long decimal into buffer, returns length */ 138static int 139print_dec_ll(char* buf, int max, unsigned long long value) 140{ 141 int i = 0; 142 if(value == 0) { 143 if(max > 0) { 144 buf[0] = '0'; 145 i = 1; 146 } 147 } else while(value && i < max) { 148 buf[i++] = '0' + value % 10; 149 value /= 10; 150 } 151 return i; 152} |
153
|
84int vsnprintf (char *str, size_t count, const char *fmt, va_list args)
|
154/** print hex into buffer, returns length */ 155static int 156print_hex(char* buf, int max, unsigned int value) |
157{
|
86 str[0] = 0;
87 dopr(str, count, fmt, args);
88 return(strlen(str));
|
158 const char* h = "0123456789abcdef"; 159 int i = 0; 160 if(value == 0) { 161 if(max > 0) { 162 buf[0] = '0'; 163 i = 1; 164 } 165 } else while(value && i < max) { 166 buf[i++] = h[value & 0x0f]; 167 value >>= 4; 168 } 169 return i; |
170} 171
|
91/* VARARGS3 */
92#ifdef HAVE_STDARGS
93int snprintf (char *str,size_t count,const char *fmt,...)
94#else
95int snprintf (va_alist) va_dcl
96#endif
|
172/** print long hex into buffer, returns length */ 173static int 174print_hex_l(char* buf, int max, unsigned long value) |
175{
|
98#ifndef HAVE_STDARGS
99 char *str;
100 size_t count;
101 char *fmt;
102#endif
103 VA_LOCAL_DECL;
104
105 VA_START (fmt);
106 VA_SHIFT (str, char *);
107 VA_SHIFT (count, size_t );
108 VA_SHIFT (fmt, char *);
109 (void) vsnprintf(str, count, fmt, ap);
110 VA_END;
111 return(strlen(str));
|
176 const char* h = "0123456789abcdef"; 177 int i = 0; 178 if(value == 0) { 179 if(max > 0) { 180 buf[0] = '0'; 181 i = 1; 182 } 183 } else while(value && i < max) { 184 buf[i++] = h[value & 0x0f]; 185 value >>= 4; 186 } 187 return i; |
188} 189
|
114/*
115 * dopr(): poor man's version of doprintf
116 */
|
190/** print long long hex into buffer, returns length */ 191static int 192print_hex_ll(char* buf, int max, unsigned long long value) 193{ 194 const char* h = "0123456789abcdef"; 195 int i = 0; 196 if(value == 0) { 197 if(max > 0) { 198 buf[0] = '0'; 199 i = 1; 200 } 201 } else while(value && i < max) { 202 buf[i++] = h[value & 0x0f]; 203 value >>= 4; 204 } 205 return i; 206} |
207
|
118/* format read states */
119#define DP_S_DEFAULT 0
120#define DP_S_FLAGS 1
121#define DP_S_MIN 2
122#define DP_S_DOT 3
123#define DP_S_MAX 4
124#define DP_S_MOD 5
125#define DP_S_CONV 6
126#define DP_S_DONE 7
|
208/** copy string into result, reversed */ 209static void 210spool_str_rev(char** at, size_t* left, int* ret, const char* buf, int len) 211{ 212 int i = len; 213 while(i) { 214 if(*left > 1) { 215 *(*at)++ = buf[--i]; 216 (*left)--; 217 } else --i; 218 (*ret)++; 219 } 220} |
221
|
128/* format flags - Bits */
129#define DP_F_MINUS 1
130#define DP_F_PLUS 2
131#define DP_F_SPACE 4
132#define DP_F_NUM 8
133#define DP_F_ZERO 16
134#define DP_F_UP 32
|
222/** copy string into result */ 223static void 224spool_str(char** at, size_t* left, int* ret, const char* buf, int len) 225{ 226 int i; 227 for(i=0; i<len; i++) { 228 if(*left > 1) { 229 *(*at)++ = buf[i]; 230 (*left)--; 231 } 232 (*ret)++; 233 } 234} |
235
|
136/* Conversion Flags */
137#define DP_C_SHORT 1
138#define DP_C_LONG 2
139#define DP_C_LDOUBLE 3
|
236/** print number formatted */ 237static void 238print_num(char** at, size_t* left, int* ret, int minw, int precision, 239 int prgiven, int zeropad, int minus, int plus, int space, 240 int zero, int negative, char* buf, int len) 241{ 242 int w = len; /* excludes minus sign */ 243 char s = get_negsign(negative, plus, space); 244 if(minus) { 245 /* left adjust the number into the field, space padding */ 246 /* calc numw = [sign][zeroes][number] */ 247 int numw = w; 248 if(precision == 0 && zero) numw = 0; 249 if(numw < precision) numw = precision; 250 if(s) numw++; |
251
|
141#define char_to_int(p) (p - '0')
142#ifndef MAX
143#define MAX(p,q) ((p >= q) ? p : q)
144#endif
|
252 /* sign */ 253 if(s) print_pad(at, left, ret, s, 1); |
254
|
146static void dopr (char *buffer, size_t maxlen, const char *format, va_list args)
147{
148 char ch;
149 long value;
150 long double fvalue;
151 char *strvalue;
152 int min;
153 int max;
154 int state;
155 int flags;
156 int cflags;
157 size_t currlen;
158
159 state = DP_S_DEFAULT;
160 currlen = flags = cflags = min = 0;
161 max = -1;
162 ch = *format++;
|
255 /* number */ 256 if(precision == 0 && zero) { 257 /* "" for the number */ 258 } else { 259 if(w < precision) 260 print_pad(at, left, ret, '0', precision - w); 261 spool_str_rev(at, left, ret, buf, len); 262 } 263 /* spaces */ 264 if(numw < minw) 265 print_pad(at, left, ret, ' ', minw - numw); 266 } else { 267 /* pad on the left of the number */ 268 /* calculate numw has width of [sign][zeroes][number] */ 269 int numw = w; 270 if(precision == 0 && zero) numw = 0; 271 if(numw < precision) numw = precision; 272 if(!prgiven && zeropad && numw < minw) numw = minw; 273 else if(s) numw++; |
274
|
164 while (state != DP_S_DONE)
165 {
166 if ((ch == '\0') || (currlen >= maxlen))
167 state = DP_S_DONE;
|
275 /* pad with spaces */ 276 if(numw < minw) 277 print_pad(at, left, ret, ' ', minw - numw); 278 /* print sign (and one less zeropad if so) */ 279 if(s) { 280 print_pad(at, left, ret, s, 1); 281 numw--; 282 } 283 /* pad with zeroes */ 284 if(w < numw) 285 print_pad(at, left, ret, '0', numw - w); 286 if(precision == 0 && zero) 287 return; 288 /* print the characters for the value */ 289 spool_str_rev(at, left, ret, buf, len); 290 } 291} |
292
|
169 switch(state)
170 {
171 case DP_S_DEFAULT:
172 if (ch == '%')
173 state = DP_S_FLAGS;
174 else
175 dopr_outch (buffer, &currlen, maxlen, ch);
176 ch = *format++;
177 break;
178 case DP_S_FLAGS:
179 switch (ch)
180 {
181 case '-':
182 flags |= DP_F_MINUS;
183 ch = *format++;
184 break;
185 case '+':
186 flags |= DP_F_PLUS;
187 ch = *format++;
188 break;
189 case ' ':
190 flags |= DP_F_SPACE;
191 ch = *format++;
192 break;
193 case '#':
194 flags |= DP_F_NUM;
195 ch = *format++;
196 break;
197 case '0':
198 flags |= DP_F_ZERO;
199 ch = *format++;
200 break;
201 default:
202 state = DP_S_MIN;
203 break;
204 }
205 break;
206 case DP_S_MIN:
207 if (isdigit(ch))
208 {
209 min = 10*min + char_to_int (ch);
210 ch = *format++;
211 }
212 else if (ch == '*')
213 {
214 min = va_arg (args, int);
215 ch = *format++;
216 state = DP_S_DOT;
217 }
218 else
219 state = DP_S_DOT;
220 break;
221 case DP_S_DOT:
222 if (ch == '.')
223 {
224 state = DP_S_MAX;
225 ch = *format++;
226 }
227 else
228 state = DP_S_MOD;
229 break;
230 case DP_S_MAX:
231 if (isdigit(ch))
232 {
233 if (max < 0)
234 max = 0;
235 max = 10*max + char_to_int (ch);
236 ch = *format++;
237 }
238 else if (ch == '*')
239 {
240 max = va_arg (args, int);
241 ch = *format++;
242 state = DP_S_MOD;
243 }
244 else
245 state = DP_S_MOD;
246 break;
247 case DP_S_MOD:
248 /* Currently, we don't support Long Long, bummer */
249 switch (ch)
250 {
251 case 'h':
252 cflags = DP_C_SHORT;
253 ch = *format++;
254 break;
255 case 'l':
256 cflags = DP_C_LONG;
257 ch = *format++;
258 break;
259 case 'L':
260 cflags = DP_C_LDOUBLE;
261 ch = *format++;
262 break;
263 default:
264 break;
265 }
266 state = DP_S_CONV;
267 break;
268 case DP_S_CONV:
269 switch (ch)
270 {
271 case 'd':
272 case 'i':
273 if (cflags == DP_C_SHORT)
274 value = va_arg (args, int);
275 else if (cflags == DP_C_LONG)
276 value = va_arg (args, long int);
277 else
278 value = va_arg (args, int);
279 fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
280 break;
281 case 'o':
282 flags &= ~DP_F_PLUS;
283 if (cflags == DP_C_SHORT)
284 value = va_arg (args, unsigned int);
285 else if (cflags == DP_C_LONG)
286 value = va_arg (args, unsigned long int);
287 else
288 value = va_arg (args, unsigned int);
289 fmtint (buffer, &currlen, maxlen, value, 8, min, max, flags);
290 break;
291 case 'u':
292 flags &= ~DP_F_PLUS;
293 if (cflags == DP_C_SHORT)
294 value = va_arg (args, unsigned int);
295 else if (cflags == DP_C_LONG)
296 value = va_arg (args, unsigned long int);
297 else
298 value = va_arg (args, unsigned int);
299 fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
300 break;
301 case 'X':
302 flags |= DP_F_UP;
303 case 'x':
304 flags &= ~DP_F_PLUS;
305 if (cflags == DP_C_SHORT)
306 value = va_arg (args, unsigned int);
307 else if (cflags == DP_C_LONG)
308 value = va_arg (args, unsigned long int);
309 else
310 value = va_arg (args, unsigned int);
311 fmtint (buffer, &currlen, maxlen, value, 16, min, max, flags);
312 break;
313 case 'f':
314 if (cflags == DP_C_LDOUBLE)
315 fvalue = va_arg (args, long double);
316 else
317 fvalue = va_arg (args, double);
318 /* um, floating point? */
319 fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags, 'f');
320 break;
321 case 'E':
322 flags |= DP_F_UP;
323 case 'e':
324 if (cflags == DP_C_LDOUBLE)
325 fvalue = va_arg (args, long double);
326 else
327 fvalue = va_arg (args, double);
328 break;
329 case 'G':
330 flags |= DP_F_UP;
331 case 'g':
332 if (cflags == DP_C_LDOUBLE)
333 fvalue = va_arg (args, long double);
334 else
335 fvalue = va_arg (args, double);
336 fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags, 'g');
337 break;
338 case 'c':
339 dopr_outch (buffer, &currlen, maxlen, va_arg (args, int));
340 break;
341 case 's':
342 strvalue = va_arg (args, char *);
343 if (max < 0)
344 max = maxlen; /* ie, no max */
345 fmtstr (buffer, &currlen, maxlen, strvalue, flags, min, max);
346 break;
347 case 'p':
348 strvalue = va_arg (args, void *);
349 fmtint (buffer, &currlen, maxlen, (long) strvalue, 16, min, max, flags);
350 break;
351 case 'n':
352 if (cflags == DP_C_SHORT)
353 {
354 short int *num;
355 num = va_arg (args, short int *);
356 *num = currlen;
357 }
358 else if (cflags == DP_C_LONG)
359 {
360 long int *num;
361 num = va_arg (args, long int *);
362 *num = currlen;
363 }
364 else
365 {
366 int *num;
367 num = va_arg (args, int *);
368 *num = currlen;
369 }
370 break;
371 case '%':
372 dopr_outch (buffer, &currlen, maxlen, ch);
373 break;
374 case 'w':
375 /* not supported yet, treat as next char */
376 ch = *format++;
377 break;
378 default:
379 /* Unknown, skip */
380 break;
381 }
382 ch = *format++;
383 state = DP_S_DEFAULT;
384 flags = cflags = min = 0;
385 max = -1;
386 break;
387 case DP_S_DONE:
388 break;
389 default:
390 /* hmm? */
391 break; /* some picky compilers need this */
392 }
393 }
394 if (currlen < maxlen - 1)
395 buffer[currlen] = '\0';
396 else
397 buffer[maxlen - 1] = '\0';
|
293/** print %d and %i */ 294static void 295print_num_d(char** at, size_t* left, int* ret, int value, 296 int minw, int precision, int prgiven, int zeropad, int minus, 297 int plus, int space) 298{ 299 char buf[PRINT_DEC_BUFSZ]; 300 int negative = (value < 0); 301 int zero = (value == 0); 302 int len = print_dec(buf, (int)sizeof(buf), 303 (unsigned int)(negative?-value:value)); 304 print_num(at, left, ret, minw, precision, prgiven, zeropad, minus, 305 plus, space, zero, negative, buf, len); |
306} 307
|
400static void fmtstr (char *buffer, size_t *currlen, size_t maxlen,
401 char *value, int flags, int min, int max)
|
308/** print %ld and %li */ 309static void 310print_num_ld(char** at, size_t* left, int* ret, long value, 311 int minw, int precision, int prgiven, int zeropad, int minus, 312 int plus, int space) |
313{
|
403 int padlen, strln; /* amount to pad */
404 int cnt = 0;
405
406 if (value == 0)
407 {
408 value = "<NULL>";
409 }
|
314 char buf[PRINT_DEC_BUFSZ]; 315 int negative = (value < 0); 316 int zero = (value == 0); 317 int len = print_dec_l(buf, (int)sizeof(buf), 318 (unsigned long)(negative?-value:value)); 319 print_num(at, left, ret, minw, precision, prgiven, zeropad, minus, 320 plus, space, zero, negative, buf, len); 321} |
322
|
411 for (strln = 0; value[strln]; ++strln); /* strlen */
412 padlen = min - strln;
413 if (padlen < 0)
414 padlen = 0;
415 if (flags & DP_F_MINUS)
416 padlen = -padlen; /* Left Justify */
|
323/** print %lld and %lli */ 324static void 325print_num_lld(char** at, size_t* left, int* ret, long long value, 326 int minw, int precision, int prgiven, int zeropad, int minus, 327 int plus, int space) 328{ 329 char buf[PRINT_DEC_BUFSZ]; 330 int negative = (value < 0); 331 int zero = (value == 0); 332 int len = print_dec_ll(buf, (int)sizeof(buf), 333 (unsigned long long)(negative?-value:value)); 334 print_num(at, left, ret, minw, precision, prgiven, zeropad, minus, 335 plus, space, zero, negative, buf, len); 336} |
337
|
418 while ((padlen > 0) && (cnt < max))
419 {
420 dopr_outch (buffer, currlen, maxlen, ' ');
421 --padlen;
422 ++cnt;
423 }
424 while (*value && (cnt < max))
425 {
426 dopr_outch (buffer, currlen, maxlen, *value++);
427 ++cnt;
428 }
429 while ((padlen < 0) && (cnt < max))
430 {
431 dopr_outch (buffer, currlen, maxlen, ' ');
432 ++padlen;
433 ++cnt;
434 }
|
338/** print %u */ 339static void 340print_num_u(char** at, size_t* left, int* ret, unsigned int value, 341 int minw, int precision, int prgiven, int zeropad, int minus, 342 int plus, int space) 343{ 344 char buf[PRINT_DEC_BUFSZ]; 345 int negative = 0; 346 int zero = (value == 0); 347 int len = print_dec(buf, (int)sizeof(buf), value); 348 print_num(at, left, ret, minw, precision, prgiven, zeropad, minus, 349 plus, space, zero, negative, buf, len); |
350} 351
|
437/* Have to handle DP_F_NUM (ie 0x and 0 alternates) */
|
352/** print %lu */ 353static void 354print_num_lu(char** at, size_t* left, int* ret, unsigned long value, 355 int minw, int precision, int prgiven, int zeropad, int minus, 356 int plus, int space) 357{ 358 char buf[PRINT_DEC_BUFSZ]; 359 int negative = 0; 360 int zero = (value == 0); 361 int len = print_dec_l(buf, (int)sizeof(buf), value); 362 print_num(at, left, ret, minw, precision, prgiven, zeropad, minus, 363 plus, space, zero, negative, buf, len); 364} |
365
|
439static void fmtint (char *buffer, size_t *currlen, size_t maxlen,
440 long value, int base, int min, int max, int flags)
|
366/** print %llu */ 367static void 368print_num_llu(char** at, size_t* left, int* ret, unsigned long long value, 369 int minw, int precision, int prgiven, int zeropad, int minus, 370 int plus, int space) |
371{
|
442 int signvalue = 0;
443 unsigned long uvalue;
444 char convert[20];
445 int place = 0;
446 int spadlen = 0; /* amount to space pad */
447 int zpadlen = 0; /* amount to zero pad */
448 int caps = 0;
449
450 if (max < 0)
451 max = 0;
|
372 char buf[PRINT_DEC_BUFSZ]; 373 int negative = 0; 374 int zero = (value == 0); 375 int len = print_dec_ll(buf, (int)sizeof(buf), value); 376 print_num(at, left, ret, minw, precision, prgiven, zeropad, minus, 377 plus, space, zero, negative, buf, len); 378} |
379
|
453 uvalue = value;
454 if( value < 0 ) {
455 signvalue = '-';
456 uvalue = -value;
457 }
458 else
459 if (flags & DP_F_PLUS) /* Do a sign (+/i) */
460 signvalue = '+';
461 else
462 if (flags & DP_F_SPACE)
463 signvalue = ' ';
|
380/** print %x */ 381static void 382print_num_x(char** at, size_t* left, int* ret, unsigned int value, 383 int minw, int precision, int prgiven, int zeropad, int minus, 384 int plus, int space) 385{ 386 char buf[PRINT_DEC_BUFSZ]; 387 int negative = 0; 388 int zero = (value == 0); 389 int len = print_hex(buf, (int)sizeof(buf), value); 390 print_num(at, left, ret, minw, precision, prgiven, zeropad, minus, 391 plus, space, zero, negative, buf, len); 392} |
393
|
465 if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
|
394/** print %lx */ 395static void 396print_num_lx(char** at, size_t* left, int* ret, unsigned long value, 397 int minw, int precision, int prgiven, int zeropad, int minus, 398 int plus, int space) 399{ 400 char buf[PRINT_DEC_BUFSZ]; 401 int negative = 0; 402 int zero = (value == 0); 403 int len = print_hex_l(buf, (int)sizeof(buf), value); 404 print_num(at, left, ret, minw, precision, prgiven, zeropad, minus, 405 plus, space, zero, negative, buf, len); 406} |
407
|
467 do {
468 convert[place++] =
469 (caps? "0123456789ABCDEF":"0123456789abcdef")
470 [uvalue % (unsigned)base ];
471 uvalue = (uvalue / (unsigned)base );
472 } while(uvalue && (place < 20));
473 if (place == 20) place--;
474 convert[place] = 0;
|
408/** print %llx */ 409static void 410print_num_llx(char** at, size_t* left, int* ret, unsigned long long value, 411 int minw, int precision, int prgiven, int zeropad, int minus, 412 int plus, int space) 413{ 414 char buf[PRINT_DEC_BUFSZ]; 415 int negative = 0; 416 int zero = (value == 0); 417 int len = print_hex_ll(buf, (int)sizeof(buf), value); 418 print_num(at, left, ret, minw, precision, prgiven, zeropad, minus, 419 plus, space, zero, negative, buf, len); 420} |
421
|
476 zpadlen = max - place;
477 spadlen = min - MAX (max, place) - (signvalue ? 1 : 0);
478 if (zpadlen < 0) zpadlen = 0;
479 if (spadlen < 0) spadlen = 0;
480 if (flags & DP_F_ZERO)
481 {
482 zpadlen = MAX(zpadlen, spadlen);
483 spadlen = 0;
484 }
485 if (flags & DP_F_MINUS)
486 spadlen = -spadlen; /* Left Justifty */
487
488#ifdef DEBUG_SNPRINTF
489 dprint (1, (debugfile, "zpad: %d, spad: %d, min: %d, max: %d, place: %d\n",
490 zpadlen, spadlen, min, max, place));
|
422/** print %llp */ 423static void 424print_num_llp(char** at, size_t* left, int* ret, void* value, 425 int minw, int precision, int prgiven, int zeropad, int minus, 426 int plus, int space) 427{ 428 char buf[PRINT_DEC_BUFSZ]; 429 int negative = 0; 430 int zero = (value == 0); 431#if defined(UINTPTR_MAX) && defined(UINT32_MAX) && (UINTPTR_MAX == UINT32_MAX) 432 /* avoid warning about upcast on 32bit systems */ 433 unsigned long long llvalue = (unsigned long)value; 434#else 435 unsigned long long llvalue = (unsigned long long)value; |
436#endif
|
437 int len = print_hex_ll(buf, (int)sizeof(buf), llvalue); 438 if(zero) { 439 buf[0]=')'; 440 buf[1]='l'; 441 buf[2]='i'; 442 buf[3]='n'; 443 buf[4]='('; 444 len = 5; 445 } else { 446 /* put '0x' in front of the (reversed) buffer result */ 447 if(len < PRINT_DEC_BUFSZ) 448 buf[len++] = 'x'; 449 if(len < PRINT_DEC_BUFSZ) 450 buf[len++] = '0'; 451 } 452 print_num(at, left, ret, minw, precision, prgiven, zeropad, minus, 453 plus, space, zero, negative, buf, len); 454} |
455
|
493 /* Spaces */
494 while (spadlen > 0)
495 {
496 dopr_outch (buffer, currlen, maxlen, ' ');
497 --spadlen;
498 }
|
456#define PRINT_FLOAT_BUFSZ 64 /* xx.yy with 20.20 about the max */ 457/** spool remainder after the decimal point to buffer, in reverse */ 458static int 459print_remainder(char* buf, int max, double r, int prec) 460{ 461 unsigned long long cap = 1; 462 unsigned long long value; 463 int len, i; 464 if(prec > 19) prec = 19; /* max we can do */ 465 if(max < prec) return 0; 466 for(i=0; i<prec; i++) { 467 cap *= 10; 468 } 469 r *= (double)cap; 470 value = (unsigned long long)r; 471 /* see if we need to round up */ 472 if(((unsigned long long)((r - (double)value)*10.0)) >= 5) { 473 value++; 474 /* that might carry to numbers before the comma, if so, 475 * just ignore that rounding. failure because 64bitprintout */ 476 if(value >= cap) 477 value = cap-1; 478 } 479 len = print_dec_ll(buf, max, value); 480 while(len < prec) { /* pad with zeroes, e.g. if 0.0012 */ 481 buf[len++] = '0'; 482 } 483 if(len < max) 484 buf[len++] = '.'; 485 return len; 486} |
487
|
500 /* Sign */
501 if (signvalue)
502 dopr_outch (buffer, currlen, maxlen, signvalue);
|
488/** spool floating point to buffer */ 489static int 490print_float(char* buf, int max, double value, int prec) 491{ 492 /* as xxx.xxx if prec==0, no '.', with prec decimals after . */ 493 /* no conversion for NAN and INF, because we do not want to require 494 linking with -lm. */ 495 /* Thus, the conversions use 64bit integers to convert the numbers, 496 * which makes 19 digits before and after the decimal point the max */ 497 unsigned long long whole = (unsigned long long)value; 498 double remain = value - (double)whole; 499 int len = 0; 500 if(prec != 0) 501 len = print_remainder(buf, max, remain, prec); 502 len += print_dec_ll(buf+len, max-len, whole); 503 return len; 504} |
505
|
504 /* Zeros */
505 if (zpadlen > 0)
506 {
507 while (zpadlen > 0)
508 {
509 dopr_outch (buffer, currlen, maxlen, '0');
510 --zpadlen;
511 }
512 }
513
514 /* Digits */
515 while (place > 0)
516 dopr_outch (buffer, currlen, maxlen, convert[--place]);
517
518 /* Left Justified spaces */
519 while (spadlen < 0) {
520 dopr_outch (buffer, currlen, maxlen, ' ');
521 ++spadlen;
522 }
|
506/** print %f */ 507static void 508print_num_f(char** at, size_t* left, int* ret, double value, 509 int minw, int precision, int prgiven, int zeropad, int minus, 510 int plus, int space) 511{ 512 char buf[PRINT_FLOAT_BUFSZ]; 513 int negative = (value < 0); 514 int zero = 0; 515 int len; 516 if(!prgiven) precision = 6; 517 len = print_float(buf, (int)sizeof(buf), negative?-value:value, 518 precision); 519 print_num(at, left, ret, minw, 1, 0, zeropad, minus, 520 plus, space, zero, negative, buf, len); |
521} 522
|
525static long double abs_val (long double value)
|
523/* rudimentary %g support */ 524static int 525print_float_g(char* buf, int max, double value, int prec) |
526{
|
527 long double result = value;
|
527 unsigned long long whole = (unsigned long long)value; 528 double remain = value - (double)whole; 529 int before = 0; 530 int len = 0; |
531
|
529 if (value < 0)
530 result = -value;
|
532 /* number of digits before the decimal point */ 533 while(whole > 0) { 534 before++; 535 whole /= 10; 536 } 537 whole = (unsigned long long)value; |
538
|
532 return result;
|
539 if(prec > before && remain != 0.0) { 540 /* see if the last decimals are zero, if so, skip them */ 541 len = print_remainder(buf, max, remain, prec-before); 542 while(len > 0 && buf[0]=='0') { 543 memmove(buf, buf+1, --len); 544 } 545 } 546 len += print_dec_ll(buf+len, max-len, whole); 547 return len; |
548} 549
|
535static long double compat_pow10 (int exp)
|
550 551/** print %g */ 552static void 553print_num_g(char** at, size_t* left, int* ret, double value, 554 int minw, int precision, int prgiven, int zeropad, int minus, 555 int plus, int space) |
556{
|
537 long double result = 1;
|
557 char buf[PRINT_FLOAT_BUFSZ]; 558 int negative = (value < 0); 559 int zero = 0; 560 int len; 561 if(!prgiven) precision = 6; 562 if(precision == 0) precision = 1; 563 len = print_float_g(buf, (int)sizeof(buf), negative?-value:value, 564 precision); 565 print_num(at, left, ret, minw, 1, 0, zeropad, minus, 566 plus, space, zero, negative, buf, len); 567} |
568
|
539 while (exp)
540 {
541 result *= 10;
542 exp--;
543 }
544
545 return result;
|
569 570/** strnlen (compat implementation) */ 571static int 572my_strnlen(const char* s, int max) 573{ 574 int i; 575 for(i=0; i<max; i++) 576 if(s[i]==0) 577 return i; 578 return max; |
579} 580
|
548static long compat_round (long double value)
|
581/** print %s */ 582static void 583print_str(char** at, size_t* left, int* ret, char* s, 584 int minw, int precision, int prgiven, int minus) |
585{
|
550 long intpart;
|
586 int w; 587 /* with prec: no more than x characters from this string, stop at 0 */ 588 if(prgiven) 589 w = my_strnlen(s, precision); 590 else w = (int)strlen(s); /* up to the nul */ 591 if(w < minw && !minus) 592 print_pad(at, left, ret, ' ', minw - w); 593 spool_str(at, left, ret, s, w); 594 if(w < minw && minus) 595 print_pad(at, left, ret, ' ', minw - w); 596} |
597
|
552 intpart = value;
553 value = value - intpart;
554 if (value >= 0.5)
555 intpart++;
556
557 return intpart;
|
598/** print %c */ 599static void 600print_char(char** at, size_t* left, int* ret, int c, 601 int minw, int minus) 602{ 603 if(1 < minw && !minus) 604 print_pad(at, left, ret, ' ', minw - 1); 605 print_pad(at, left, ret, c, 1); 606 if(1 < minw && minus) 607 print_pad(at, left, ret, ' ', minw - 1); |
608} 609
|
560static void fmtfp (char *buffer, size_t *currlen, size_t maxlen,
561 long double fvalue, int min, int max, int flags, int conv)
|
610 611/** 612 * Print to string. 613 * str: string buffer for result. result will be null terminated. 614 * size: size of the buffer. null is put inside buffer. 615 * format: printf format string. 616 * arg: '...' arguments to print. 617 * returns number of characters. a null is printed after this. 618 * return number of bytes that would have been written 619 * if the buffer had been large enough. 620 * 621 * supported format specifiers: 622 * %s, %u, %d, %x, %i, %f, %g, %c, %p, %n. 623 * length: l, ll (for d, u, x). 624 * precision: 6.6d (for d, u, x) 625 * %f, %g precisions, 0.3f 626 * %20s, '.*s' 627 * and %%. 628 */ 629int vsnprintf(char* str, size_t size, const char* format, va_list arg) |
630{
|
563 int signvalue = 0;
564 long double ufvalue;
565 char iconvert[20];
566 char fconvert[20];
567 int iplace = 0;
568 int fplace = 0;
569 int padlen = 0; /* amount to pad */
570 int zpadlen = 0;
571 int caps = 0;
572 long intpart;
573 long fracpart;
574
575 /*
576 * AIX manpage says the default is 0, but Solaris says the default
577 * is 6, and sprintf on AIX defaults to 6
578 */
579 if (max < 0)
580 max = 6;
|
631 char* at = str; 632 size_t left = size; 633 int ret = 0; 634 const char* fmt = format; 635 int conv, minw, precision, prgiven, zeropad, minus, plus, space, length; 636 while(*fmt) { 637 /* copy string before % */ 638 while(*fmt && *fmt!='%') { 639 if(left > 1) { 640 *at++ = *fmt++; 641 left--; 642 } else fmt++; 643 ret++; 644 } 645 646 /* see if we are at end */ 647 if(!*fmt) break; |
648
|
582 ufvalue = abs_val (fvalue);
|
649 /* fetch next argument % designation from format string */ 650 fmt++; /* skip the '%' */ |
651
|
584 if (fvalue < 0)
585 signvalue = '-';
586 else
587 if (flags & DP_F_PLUS) /* Do a sign (+/i) */
588 signvalue = '+';
589 else
590 if (flags & DP_F_SPACE)
591 signvalue = ' ';
|
652 /********************************/ 653 /* get the argument designation */ 654 /********************************/ 655 /* we must do this vararg stuff inside this function for 656 * portability. Hence, get_designation, and print_designation 657 * are not their own functions. */ |
658
|
593#if 0
594 if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
595#endif
|
659 /* printout designation: 660 * conversion specifier: x, d, u, s, c, n, m, p 661 * flags: # not supported 662 * 0 zeropad (on the left) 663 * - left adjust (right by default) 664 * ' ' printspace for positive number (in - position). 665 * + alwayssign 666 * fieldwidth: [1-9][0-9]* minimum field width. 667 * if this is * then type int next argument specifies the minwidth. 668 * if this is negative, the - flag is set (with positive width). 669 * precision: period[digits]*, %.2x. 670 * if this is * then type int next argument specifies the precision. 671 * just '.' or negative value means precision=0. 672 * this is mindigits to print for d, i, u, x 673 * this is aftercomma digits for f 674 * this is max number significant digits for g 675 * maxnumber characters to be printed for s 676 * length: 0-none (int), 1-l (long), 2-ll (long long) 677 * notsupported: hh (char), h (short), L (long double), q, j, z, t 678 * Does not support %m$ and *m$ argument designation as array indices. 679 * Does not support %#x 680 * 681 */ 682 minw = 0; 683 precision = 1; 684 prgiven = 0; 685 zeropad = 0; 686 minus = 0; 687 plus = 0; 688 space = 0; 689 length = 0; |
690
|
597 intpart = ufvalue;
|
691 /* get flags in any order */ 692 for(;;) { 693 if(*fmt == '0') 694 zeropad = 1; 695 else if(*fmt == '-') 696 minus = 1; 697 else if(*fmt == '+') 698 plus = 1; 699 else if(*fmt == ' ') 700 space = 1; 701 else break; 702 fmt++; 703 } |
704
|
599 /*
600 * Sorry, we only support 9 digits past the decimal because of our
601 * conversion method
602 */
603 if (max > 9)
604 max = 9;
|
705 /* field width */ 706 if(*fmt == '*') { 707 fmt++; /* skip char */ 708 minw = va_arg(arg, int); 709 if(minw < 0) { 710 minus = 1; 711 minw = -minw; 712 } 713 } else while(*fmt >= '0' && *fmt <= '9') { 714 minw = minw*10 + (*fmt++)-'0'; 715 } |
716
|
606 /* We "cheat" by converting the fractional part to integer by
607 * multiplying by a factor of 10
608 */
609 fracpart = compat_round ((compat_pow10 (max)) * (ufvalue - intpart));
|
717 /* precision */ 718 if(*fmt == '.') { 719 fmt++; /* skip period */ 720 prgiven = 1; 721 precision = 0; 722 if(*fmt == '*') { 723 fmt++; /* skip char */ 724 precision = va_arg(arg, int); 725 if(precision < 0) 726 precision = 0; 727 } else while(*fmt >= '0' && *fmt <= '9') { 728 precision = precision*10 + (*fmt++)-'0'; 729 } 730 } |
731
|
611 if (fracpart >= compat_pow10 (max))
612 {
613 intpart++;
614 fracpart -= compat_pow10 (max);
615 }
|
732 /* length */ 733 if(*fmt == 'l') { 734 fmt++; /* skip char */ 735 length = 1; 736 if(*fmt == 'l') { 737 fmt++; /* skip char */ 738 length = 2; 739 } 740 } |
741
|
617#ifdef DEBUG_SNPRINTF
618 dprint (1, (debugfile, "fmtfp: %f =? %d.%d\n", fvalue, intpart, fracpart));
619#endif
|
742 /* get the conversion */ 743 if(!*fmt) conv = 0; 744 else conv = *fmt++; |
745
|
621 /* Convert integer part */
622 do {
623 iconvert[iplace++] =
624 (caps? "0123456789ABCDEF":"0123456789abcdef")[intpart % 10];
625 intpart = (intpart / 10);
626 } while(intpart && (iplace < 20));
627 if (iplace == 20) iplace--;
628 iconvert[iplace] = 0;
|
746 /***********************************/ 747 /* print that argument designation */ 748 /***********************************/ 749 switch(conv) { 750 case 'i': 751 case 'd': 752 if(length == 0) 753 print_num_d(&at, &left, &ret, va_arg(arg, int), 754 minw, precision, prgiven, zeropad, minus, plus, space); 755 else if(length == 1) 756 print_num_ld(&at, &left, &ret, va_arg(arg, long), 757 minw, precision, prgiven, zeropad, minus, plus, space); 758 else if(length == 2) 759 print_num_lld(&at, &left, &ret, 760 va_arg(arg, long long), 761 minw, precision, prgiven, zeropad, minus, plus, space); 762 break; 763 case 'u': 764 if(length == 0) 765 print_num_u(&at, &left, &ret, 766 va_arg(arg, unsigned int), 767 minw, precision, prgiven, zeropad, minus, plus, space); 768 else if(length == 1) 769 print_num_lu(&at, &left, &ret, 770 va_arg(arg, unsigned long), 771 minw, precision, prgiven, zeropad, minus, plus, space); 772 else if(length == 2) 773 print_num_llu(&at, &left, &ret, 774 va_arg(arg, unsigned long long), 775 minw, precision, prgiven, zeropad, minus, plus, space); 776 break; 777 case 'x': 778 if(length == 0) 779 print_num_x(&at, &left, &ret, 780 va_arg(arg, unsigned int), 781 minw, precision, prgiven, zeropad, minus, plus, space); 782 else if(length == 1) 783 print_num_lx(&at, &left, &ret, 784 va_arg(arg, unsigned long), 785 minw, precision, prgiven, zeropad, minus, plus, space); 786 else if(length == 2) 787 print_num_llx(&at, &left, &ret, 788 va_arg(arg, unsigned long long), 789 minw, precision, prgiven, zeropad, minus, plus, space); 790 break; 791 case 's': 792 print_str(&at, &left, &ret, va_arg(arg, char*), 793 minw, precision, prgiven, minus); 794 break; 795 case 'c': 796 print_char(&at, &left, &ret, va_arg(arg, int), 797 minw, minus); 798 break; 799 case 'n': 800 *va_arg(arg, int*) = ret; 801 break; 802 case 'm': 803 print_str(&at, &left, &ret, strerror(errno), 804 minw, precision, prgiven, minus); 805 break; 806 case 'p': 807 print_num_llp(&at, &left, &ret, va_arg(arg, void*), 808 minw, precision, prgiven, zeropad, minus, plus, space); 809 break; 810 case '%': 811 print_pad(&at, &left, &ret, '%', 1); 812 break; 813 case 'f': 814 print_num_f(&at, &left, &ret, va_arg(arg, double), 815 minw, precision, prgiven, zeropad, minus, plus, space); 816 break; 817 case 'g': 818 print_num_g(&at, &left, &ret, va_arg(arg, double), 819 minw, precision, prgiven, zeropad, minus, plus, space); 820 break; 821 /* unknown */ 822 default: 823 case 0: break; 824 } 825 } |
826
|
630 /* Convert fractional part */
631 do {
632 fconvert[fplace++] =
633 (caps? "0123456789ABCDEF":"0123456789abcdef")[fracpart % 10];
634 fracpart = (fracpart / 10);
635 if(conv == 'g' && fplace == 1 && fconvert[0] == '0') {
636 fplace = 0; /* skip trailing zeroes for %g */
637 zpadlen ++;
638 }
639 } while(fracpart && (fplace < 20));
640 if (fplace == 20) fplace--;
641 fconvert[fplace] = 0;
|
827 /* zero terminate */ 828 if(left > 0) 829 *at = 0; 830 return ret; 831} |
832
|
643 if(conv == 'f') {
644 /* -1 for decimal point, another -1 if we are printing a sign */
645 padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
646 zpadlen = max - fplace;
647 } else if(conv == 'g') {
648 /* zpadlen contains number of trailing zeroes removed */
649 padlen = min - iplace - (max-zpadlen) - 1 - ((signvalue) ? 1 : 0);
650 if(fplace == 0) {
651 padlen += 1; /* add the decimal dot suppressed */
652 zpadlen = 0;
653 } else zpadlen = (max-zpadlen) - fplace;
654 }
655 if (zpadlen < 0)
656 zpadlen = 0;
657 if (padlen < 0)
658 padlen = 0;
659 if (flags & DP_F_MINUS)
660 padlen = -padlen; /* Left Justifty */
|
833#ifdef SNPRINTF_TEST |
834
|
662 if ((flags & DP_F_ZERO) && (padlen > 0))
663 {
664 if (signvalue)
665 {
666 dopr_outch (buffer, currlen, maxlen, signvalue);
667 --padlen;
668 signvalue = 0;
669 }
670 while (padlen > 0)
671 {
672 dopr_outch (buffer, currlen, maxlen, '0');
673 --padlen;
674 }
675 }
676 while (padlen > 0)
677 {
678 dopr_outch (buffer, currlen, maxlen, ' ');
679 --padlen;
680 }
681 if (signvalue)
682 dopr_outch (buffer, currlen, maxlen, signvalue);
|
835/** do tests */ 836#undef snprintf 837#define DOTEST(bufsz, result, retval, ...) do { \ 838 char buf[bufsz]; \ 839 printf("now test %s\n", #__VA_ARGS__); \ 840 int r=my_snprintf(buf, sizeof(buf), __VA_ARGS__); \ 841 if(r != retval || strcmp(buf, result) != 0) { \ 842 printf("error test(%s) was \"%s\":%d\n", \ 843 ""#bufsz", "#result", "#retval", "#__VA_ARGS__, \ 844 buf, r); \ 845 exit(1); \ 846 } \ 847 r=snprintf(buf, sizeof(buf), __VA_ARGS__); \ 848 if(r != retval || strcmp(buf, result) != 0) { \ 849 printf("error test(%s) differs with system, \"%s\":%d\n", \ 850 ""#bufsz", "#result", "#retval", "#__VA_ARGS__, \ 851 buf, r); \ 852 exit(1); \ 853 } \ 854 printf("test(\"%s\":%d) passed\n", buf, r); \ 855 } while(0); |
856
|
684 while (iplace > 0)
685 dopr_outch (buffer, currlen, maxlen, iconvert[--iplace]);
|
857/** test program */ 858int main(void) 859{ 860 int x = 0; |
861
|
687 /* for %g do not output decimal point if no fraction is present */
688 if(conv == 'f' || (conv == 'g' && fplace > 0)) {
689 /*
690 * Decimal point. This should probably use locale to find the correct
691 * char to print out.
692 */
693 dopr_outch (buffer, currlen, maxlen, '.');
694 }
|
862 /* bufsize, expectedstring, expectedretval, snprintf arguments */ 863 DOTEST(1024, "hello", 5, "hello"); 864 DOTEST(1024, "h", 1, "h"); 865 /* warning from gcc for format string, but it does work 866 * DOTEST(1024, "", 0, ""); */ |
867
|
696 while (zpadlen > 0)
697 {
698 dopr_outch (buffer, currlen, maxlen, '0');
699 --zpadlen;
700 }
|
868 DOTEST(3, "he", 5, "hello"); 869 DOTEST(1, "", 7, "%d", 7823089); |
870
|
702 while (fplace > 0)
703 dopr_outch (buffer, currlen, maxlen, fconvert[--fplace]);
|
871 /* test positive numbers */ 872 DOTEST(1024, "0", 1, "%d", 0); 873 DOTEST(1024, "1", 1, "%d", 1); 874 DOTEST(1024, "9", 1, "%d", 9); 875 DOTEST(1024, "15", 2, "%d", 15); 876 DOTEST(1024, "ab15cd", 6, "ab%dcd", 15); 877 DOTEST(1024, "167", 3, "%d", 167); 878 DOTEST(1024, "7823089", 7, "%d", 7823089); 879 DOTEST(1024, " 12", 3, "%3d", 12); 880 DOTEST(1024, "012", 3, "%.3d", 12); 881 DOTEST(1024, "012", 3, "%3.3d", 12); 882 DOTEST(1024, "012", 3, "%03d", 12); 883 DOTEST(1024, " 012", 4, "%4.3d", 12); 884 DOTEST(1024, "", 0, "%.0d", 0); |
885
|
705 while (padlen < 0)
706 {
707 dopr_outch (buffer, currlen, maxlen, ' ');
708 ++padlen;
709 }
710}
|
886 /* test negative numbers */ 887 DOTEST(1024, "-1", 2, "%d", -1); 888 DOTEST(1024, "-12", 3, "%3d", -12); 889 DOTEST(1024, " -2", 3, "%3d", -2); 890 DOTEST(1024, "-012", 4, "%.3d", -12); 891 DOTEST(1024, "-012", 4, "%3.3d", -12); 892 DOTEST(1024, "-012", 4, "%4.3d", -12); 893 DOTEST(1024, " -012", 5, "%5.3d", -12); 894 DOTEST(1024, "-12", 3, "%03d", -12); 895 DOTEST(1024, "-02", 3, "%03d", -2); 896 DOTEST(1024, "-15", 3, "%d", -15); 897 DOTEST(1024, "-7307", 5, "%d", -7307); 898 DOTEST(1024, "-12 ", 5, "%-5d", -12); 899 DOTEST(1024, "-00012", 6, "%-.5d", -12); |
900
|
712static void dopr_outch (char *buffer, size_t *currlen, size_t maxlen, char c)
713{
714 if (*currlen < maxlen)
715 buffer[(*currlen)++] = c;
716}
|
901 /* test + and space flags */ 902 DOTEST(1024, "+12", 3, "%+d", 12); 903 DOTEST(1024, " 12", 3, "% d", 12); |
904
|
718#ifdef TEST_SNPRINTF
719#ifndef LONG_STRING
720#define LONG_STRING 1024
721#endif
722int main (void)
723{
724 char buf1[LONG_STRING];
725 char buf2[LONG_STRING];
726 char *fp_fmt[] = {
727 "%-1.5f",
728 "%1.5f",
729 "%123.9f",
730 "%10.5f",
731 "% 10.5f",
732 "%+22.9f",
733 "%+4.9f",
734 "%01.3f",
735 "%4f",
736 "%3.1f",
737 "%3.2f",
738 NULL
739 };
740 double fp_nums[] = { -1.5, 134.21, 91340.2, 341.1234, 0203.9, 0.96, 0.996,
741 0.9996, 1.996, 4.136, 0};
742 char *int_fmt[] = {
743 "%-1.5d",
744 "%1.5d",
745 "%123.9d",
746 "%5.5d",
747 "%10.5d",
748 "% 10.5d",
749 "%+22.33d",
750 "%01.3d",
751 "%4d",
752 NULL
753 };
754 long int_nums[] = { -1, 134, 91340, 341, 0203, 0};
755 int x, y;
756 int fail = 0;
757 int num = 0;
|
905 /* test %u */ 906 DOTEST(1024, "12", 2, "%u", 12); 907 DOTEST(1024, "0", 1, "%u", 0); 908 DOTEST(1024, "4294967295", 10, "%u", 0xffffffff); |
909
|
759 printf ("Testing snprintf format codes against system sprintf...\n");
|
910 /* test %x */ 911 DOTEST(1024, "0", 1, "%x", 0); 912 DOTEST(1024, "c", 1, "%x", 12); 913 DOTEST(1024, "12ab34cd", 8, "%x", 0x12ab34cd); |
914
|
761 for (x = 0; fp_fmt[x] != NULL ; x++)
762 for (y = 0; fp_nums[y] != 0 ; y++)
763 {
764 snprintf (buf1, sizeof (buf1), fp_fmt[x], fp_nums[y]);
765 sprintf (buf2, fp_fmt[x], fp_nums[y]);
766 if (strcmp (buf1, buf2))
767 {
768 printf("snprintf doesn't match Format: %s\n\tsnprintf = %s\n\tsprintf = %s\n",
769 fp_fmt[x], buf1, buf2);
770 fail++;
771 }
772 num++;
773 }
|
915 /* test %llu, %lld */ 916 DOTEST(1024, "18446744073709551615", 20, "%llu", 917 (long long)0xffffffffffffffff); 918 DOTEST(1024, "-9223372036854775808", 20, "%lld", 919 (long long)0x8000000000000000); 920 DOTEST(1024, "9223372036854775808", 19, "%llu", 921 (long long)0x8000000000000000); |
922
|
775 for (x = 0; int_fmt[x] != NULL ; x++)
776 for (y = 0; int_nums[y] != 0 ; y++)
777 {
778 snprintf (buf1, sizeof (buf1), int_fmt[x], int_nums[y]);
779 sprintf (buf2, int_fmt[x], int_nums[y]);
780 if (strcmp (buf1, buf2))
781 {
782 printf("snprintf doesn't match Format: %s\n\tsnprintf = %s\n\tsprintf = %s\n",
783 int_fmt[x], buf1, buf2);
784 fail++;
785 }
786 num++;
787 }
788 printf ("%d tests failed out of %d.\n", fail, num);
|
923 /* test %s */ 924 DOTEST(1024, "hello", 5, "%s", "hello"); 925 DOTEST(1024, " hello", 10, "%10s", "hello"); 926 DOTEST(1024, "hello ", 10, "%-10s", "hello"); 927 DOTEST(1024, "he", 2, "%.2s", "hello"); 928 DOTEST(1024, " he", 4, "%4.2s", "hello"); 929 DOTEST(1024, " h", 4, "%4.2s", "h"); 930 931 /* test %c */ 932 DOTEST(1024, "a", 1, "%c", 'a'); 933 /* warning from gcc for format string, but it does work 934 DOTEST(1024, " a", 5, "%5c", 'a'); 935 DOTEST(1024, "a", 1, "%.0c", 'a'); */ 936 937 /* test %n */ 938 DOTEST(1024, "hello", 5, "hello%n", &x); 939 if(x != 5) { printf("the %%n failed\n"); exit(1); } 940 941 /* test %m */ 942 errno = 0; 943 DOTEST(1024, "Success", 7, "%m"); 944 945 /* test %p */ 946 DOTEST(1024, "0x10", 4, "%p", (void*)0x10); 947 DOTEST(1024, "(nil)", 5, "%p", (void*)0x0); 948 949 /* test %% */ 950 DOTEST(1024, "%", 1, "%%"); 951 952 /* test %f */ 953 DOTEST(1024, "0.000000", 8, "%f", 0.0); 954 DOTEST(1024, "0.00", 4, "%.2f", 0.0); 955 /* differs, "-0.00" DOTEST(1024, "0.00", 4, "%.2f", -0.0); */ 956 DOTEST(1024, "234.00", 6, "%.2f", 234.005); 957 DOTEST(1024, "8973497.1246", 12, "%.4f", 8973497.12456); 958 DOTEST(1024, "-12.000000", 10, "%f", -12.0); 959 DOTEST(1024, "6", 1, "%.0f", 6.0); 960 961 DOTEST(1024, "6", 1, "%g", 6.0); 962 DOTEST(1024, "6.1", 3, "%g", 6.1); 963 DOTEST(1024, "6.15", 4, "%g", 6.15); 964 965 /* These format strings are from the code of NSD, Unbound, ldns */ 966 967 DOTEST(1024, "abcdef", 6, "%s", "abcdef"); 968 DOTEST(1024, "005", 3, "%03u", 5); 969 DOTEST(1024, "12345", 5, "%03u", 12345); 970 DOTEST(1024, "5", 1, "%d", 5); 971 DOTEST(1024, "(nil)", 5, "%p", NULL); 972 DOTEST(1024, "12345", 5, "%ld", (long)12345); 973 DOTEST(1024, "12345", 5, "%lu", (long)12345); 974 DOTEST(1024, " 12345", 12, "%12u", (unsigned)12345); 975 DOTEST(1024, "12345", 5, "%u", (unsigned)12345); 976 DOTEST(1024, "12345", 5, "%llu", (unsigned long long)12345); 977 DOTEST(1024, "12345", 5, "%x", 0x12345); 978 DOTEST(1024, "12345", 5, "%llx", (long long)0x12345); 979 DOTEST(1024, "012345", 6, "%6.6d", 12345); 980 DOTEST(1024, "012345", 6, "%6.6u", 12345); 981 DOTEST(1024, "1234.54", 7, "%g", 1234.54); 982 DOTEST(1024, "123456789.54", 12, "%.12g", 123456789.54); 983 DOTEST(1024, "3456789123456.54", 16, "%.16g", 3456789123456.54); 984 /* %24g does not work with 24 digits, not enough accuracy, 985 * the first 16 digits are correct */ 986 DOTEST(1024, "12345", 5, "%3.3d", 12345); 987 DOTEST(1024, "000", 3, "%3.3d", 0); 988 DOTEST(1024, "001", 3, "%3.3d", 1); 989 DOTEST(1024, "012", 3, "%3.3d", 12); 990 DOTEST(1024, "-012", 4, "%3.3d", -12); 991 DOTEST(1024, "he", 2, "%.2s", "hello"); 992 DOTEST(1024, "helloworld", 10, "%s%s", "hello", "world"); 993 DOTEST(1024, "he", 2, "%.*s", 2, "hello"); 994 DOTEST(1024, " hello", 7, "%*s", 7, "hello"); 995 DOTEST(1024, "hello ", 7, "%*s", -7, "hello"); 996 DOTEST(1024, "0", 1, "%c", '0'); 997 DOTEST(1024, "A", 1, "%c", 'A'); 998 DOTEST(1024, "", 1, "%c", 0); 999 DOTEST(1024, "\010", 1, "%c", 8); 1000 DOTEST(1024, "%", 1, "%%"); 1001 DOTEST(1024, "0a", 2, "%02x", 0x0a); 1002 DOTEST(1024, "bd", 2, "%02x", 0xbd); 1003 DOTEST(1024, "12", 2, "%02ld", (long)12); 1004 DOTEST(1024, "02", 2, "%02ld", (long)2); 1005 DOTEST(1024, "02", 2, "%02u", (unsigned)2); 1006 DOTEST(1024, "765432", 6, "%05u", (unsigned)765432); 1007 DOTEST(1024, "10.234", 6, "%0.3f", 10.23421); 1008 DOTEST(1024, "123456.234", 10, "%0.3f", 123456.23421); 1009 DOTEST(1024, "123456789.234", 13, "%0.3f", 123456789.23421); 1010 DOTEST(1024, "123456.23", 9, "%.2f", 123456.23421); 1011 DOTEST(1024, "123456", 6, "%.0f", 123456.23421); 1012 DOTEST(1024, "0123", 4, "%.4x", 0x0123); 1013 DOTEST(1024, "00000123", 8, "%.8x", 0x0123); 1014 DOTEST(1024, "ffeb0cde", 8, "%.8x", 0xffeb0cde); 1015 DOTEST(1024, " 987654321", 10, "%10lu", (unsigned long)987654321); 1016 DOTEST(1024, " 987654321", 12, "%12lu", (unsigned long)987654321); 1017 DOTEST(1024, "987654321", 9, "%i", 987654321); 1018 DOTEST(1024, "-87654321", 9, "%i", -87654321); 1019 DOTEST(1024, "hello ", 16, "%-16s", "hello"); 1020 DOTEST(1024, " ", 16, "%-16s", ""); 1021 DOTEST(1024, "a ", 16, "%-16s", "a"); 1022 DOTEST(1024, "foobarfoobar ", 16, "%-16s", "foobarfoobar"); 1023 DOTEST(1024, "foobarfoobarfoobar", 18, "%-16s", "foobarfoobarfoobar"); 1024 1025 /* combined expressions */ 1026 DOTEST(1024, "foo 1.0 size 512 edns", 21, 1027 "foo %s size %d %s%s", "1.0", 512, "", "edns"); 1028 DOTEST(15, "foo 1.0 size 5", 21, 1029 "foo %s size %d %s%s", "1.0", 512, "", "edns"); 1030 DOTEST(1024, "packet 1203ceff id", 18, 1031 "packet %2.2x%2.2x%2.2x%2.2x id", 0x12, 0x03, 0xce, 0xff); 1032 DOTEST(1024, "/tmp/testbound_123abcd.tmp", 26, "/tmp/testbound_%u%s%s.tmp", 123, "ab", "cd"); 1033 1034 return 0; |
1035} 1036#endif /* SNPRINTF_TEST */
|
791
792#endif /* !HAVE_SNPRINTF */
|
|