]> andersk Git - openssh.git/blame - openbsd-compat/bsd-snprintf.c
- (djm) Add CVS Id's to files that we have missed
[openssh.git] / openbsd-compat / bsd-snprintf.c
CommitLineData
2d9a148e 1/**************************************************************
2 * Original:
3 * Patrick Powell Tue Apr 11 09:48:21 PDT 1995
4 * A bombproof version of doprnt (dopr) included.
5 * Sigh. This sort of thing is always nasty do deal with. Note that
6 * the version here does not include floating point...
dad3b556 7 *
2d9a148e 8 * snprintf() is used instead of sprintf() as it does limit checks
9 * for string length. This covers a nasty loophole.
dad3b556 10 *
2d9a148e 11 * The other functions are there to prevent NULL pointers from
12 * causing nast effects.
dad3b556 13 *
2d9a148e 14 * More Recently:
15 * Brandon Long <blong@fiction.net> 9/15/96 for mutt 0.43
16 * This was ugly. It is still ugly. I opted out of floating point
17 * numbers, but the formatter understands just about everything
18 * from the normal C string format, at least as far as I can tell from
19 * the Solaris 2.5 printf(3S) man page.
20 *
21 * Brandon Long <blong@fiction.net> 10/22/97 for mutt 0.87.1
22 * Ok, added some minimal floating point support, which means this
23 * probably requires libm on most operating systems. Don't yet
24 * support the exponent (e,E) and sigfig (g,G). Also, fmtint()
25 * was pretty badly broken, it just wasn't being exercised in ways
26 * which showed it, so that's been fixed. Also, formated the code
27 * to mutt conventions, and removed dead code left over from the
28 * original. Also, there is now a builtin-test, just compile with:
29 * gcc -DTEST_SNPRINTF -o snprintf snprintf.c -lm
30 * and run snprintf for results.
31 *
32 * Thomas Roessler <roessler@guug.de> 01/27/98 for mutt 0.89i
33 * The PGP code was using unsigned hexadecimal formats.
34 * Unfortunately, unsigned formats simply didn't work.
35 *
36 * Michael Elkins <me@cs.hmc.edu> 03/05/98 for mutt 0.90.8
37 * The original code assumed that both snprintf() and vsnprintf() were
38 * missing. Some systems only have snprintf() but not vsnprintf(), so
39 * the code is now broken down under HAVE_SNPRINTF and HAVE_VSNPRINTF.
40 *
c73b8e3a 41 * Ben Lindstrom <mouring@pconline.com> 09/27/00 for OpenSSH
42 * Welcome to the world of %lld and %qd support. With other
43 * long long support. This is needed for sftp-server to work
44 * right.
2d9a148e 45 **************************************************************/
dad3b556 46
0b202697 47#include "includes.h"
48
49RCSID("$Id$");
dad3b556 50
2d9a148e 51#if !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF)
dad3b556 52
dad3b556 53#include <string.h>
2d9a148e 54# include <ctype.h>
55#include <sys/types.h>
56
57/* Define this as a fall through, HAVE_STDARG_H is probably already set */
58
59#define HAVE_VARARGS_H
60
61/* varargs declarations: */
62
63#if defined(HAVE_STDARG_H)
64# include <stdarg.h>
65# define HAVE_STDARGS /* let's hope that works everywhere (mj) */
66# define VA_LOCAL_DECL va_list ap
67# define VA_START(f) va_start(ap, f)
68# define VA_SHIFT(v,t) ; /* no-op for ANSI */
69# define VA_END va_end(ap)
dad3b556 70#else
2d9a148e 71# if defined(HAVE_VARARGS_H)
72# include <varargs.h>
73# undef HAVE_STDARGS
74# define VA_LOCAL_DECL va_list ap
75# define VA_START(f) va_start(ap) /* f is ignored! */
76# define VA_SHIFT(v,t) v = va_arg(ap,t)
77# define VA_END va_end(ap)
78# else
79/*XX ** NO VARARGS ** XX*/
80# endif
dad3b556 81#endif
dad3b556 82
2d9a148e 83/*int snprintf (char *str, size_t count, const char *fmt, ...);*/
84/*int vsnprintf (char *str, size_t count, const char *fmt, va_list arg);*/
85
86static void dopr (char *buffer, size_t maxlen, const char *format,
87 va_list args);
88static void fmtstr (char *buffer, size_t *currlen, size_t maxlen,
89 char *value, int flags, int min, int max);
90static void fmtint (char *buffer, size_t *currlen, size_t maxlen,
91 long value, int base, int min, int max, int flags);
92static void fmtfp (char *buffer, size_t *currlen, size_t maxlen,
93 long double fvalue, int min, int max, int flags);
94static void dopr_outch (char *buffer, size_t *currlen, size_t maxlen, char c );
95
96/*
97 * dopr(): poor man's version of doprintf
98 */
99
100/* format read states */
101#define DP_S_DEFAULT 0
102#define DP_S_FLAGS 1
103#define DP_S_MIN 2
104#define DP_S_DOT 3
105#define DP_S_MAX 4
106#define DP_S_MOD 5
107#define DP_S_CONV 6
108#define DP_S_DONE 7
109
110/* format flags - Bits */
111#define DP_F_MINUS (1 << 0)
112#define DP_F_PLUS (1 << 1)
113#define DP_F_SPACE (1 << 2)
114#define DP_F_NUM (1 << 3)
115#define DP_F_ZERO (1 << 4)
116#define DP_F_UP (1 << 5)
117#define DP_F_UNSIGNED (1 << 6)
118
119/* Conversion Flags */
c73b8e3a 120#define DP_C_SHORT 1
121#define DP_C_LONG 2
122#define DP_C_LDOUBLE 3
123#define DP_C_LONG_LONG 4
2d9a148e 124
125#define char_to_int(p) (p - '0')
0b65b628 126#ifndef MAX
127# define MAX(p,q) ((p >= q) ? p : q)
128#endif
2d9a148e 129
130static void dopr (char *buffer, size_t maxlen, const char *format, va_list args)
131{
132 char ch;
133 long value;
134 long double fvalue;
135 char *strvalue;
136 int min;
137 int max;
138 int state;
139 int flags;
140 int cflags;
141 size_t currlen;
142
143 state = DP_S_DEFAULT;
144 currlen = flags = cflags = min = 0;
145 max = -1;
146 ch = *format++;
147
148 while (state != DP_S_DONE)
149 {
150 if ((ch == '\0') || (currlen >= maxlen))
151 state = DP_S_DONE;
152
153 switch(state)
154 {
155 case DP_S_DEFAULT:
156 if (ch == '%')
157 state = DP_S_FLAGS;
158 else
159 dopr_outch (buffer, &currlen, maxlen, ch);
160 ch = *format++;
161 break;
162 case DP_S_FLAGS:
163 switch (ch)
164 {
165 case '-':
166 flags |= DP_F_MINUS;
167 ch = *format++;
168 break;
169 case '+':
170 flags |= DP_F_PLUS;
171 ch = *format++;
172 break;
173 case ' ':
174 flags |= DP_F_SPACE;
175 ch = *format++;
176 break;
177 case '#':
178 flags |= DP_F_NUM;
179 ch = *format++;
180 break;
181 case '0':
182 flags |= DP_F_ZERO;
183 ch = *format++;
184 break;
185 default:
186 state = DP_S_MIN;
187 break;
188 }
189 break;
190 case DP_S_MIN:
191 if (isdigit((unsigned char)ch))
192 {
193 min = 10*min + char_to_int (ch);
194 ch = *format++;
195 }
196 else if (ch == '*')
197 {
198 min = va_arg (args, int);
199 ch = *format++;
200 state = DP_S_DOT;
201 }
202 else
203 state = DP_S_DOT;
204 break;
205 case DP_S_DOT:
206 if (ch == '.')
207 {
208 state = DP_S_MAX;
209 ch = *format++;
210 }
211 else
212 state = DP_S_MOD;
213 break;
214 case DP_S_MAX:
215 if (isdigit((unsigned char)ch))
216 {
217 if (max < 0)
218 max = 0;
219 max = 10*max + char_to_int (ch);
220 ch = *format++;
221 }
222 else if (ch == '*')
223 {
224 max = va_arg (args, int);
225 ch = *format++;
226 state = DP_S_MOD;
227 }
228 else
229 state = DP_S_MOD;
230 break;
231 case DP_S_MOD:
2d9a148e 232 switch (ch)
233 {
234 case 'h':
235 cflags = DP_C_SHORT;
236 ch = *format++;
237 break;
238 case 'l':
239 cflags = DP_C_LONG;
240 ch = *format++;
c73b8e3a 241 if (ch == 'l') {
242 cflags = DP_C_LONG_LONG;
243 ch = *format++;
244 }
2d9a148e 245 break;
c73b8e3a 246 case 'q':
247 cflags = DP_C_LONG_LONG;
248 ch = *format++;
249 break;
2d9a148e 250 case 'L':
251 cflags = DP_C_LDOUBLE;
252 ch = *format++;
253 break;
254 default:
255 break;
256 }
257 state = DP_S_CONV;
258 break;
259 case DP_S_CONV:
260 switch (ch)
261 {
262 case 'd':
263 case 'i':
264 if (cflags == DP_C_SHORT)
027bf205 265 value = va_arg (args, int);
2d9a148e 266 else if (cflags == DP_C_LONG)
267 value = va_arg (args, long int);
c73b8e3a 268 else if (cflags == DP_C_LONG_LONG)
269 value = va_arg (args, long long);
2d9a148e 270 else
271 value = va_arg (args, int);
272 fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
273 break;
274 case 'o':
275 flags |= DP_F_UNSIGNED;
276 if (cflags == DP_C_SHORT)
027bf205 277 value = va_arg (args, unsigned int);
2d9a148e 278 else if (cflags == DP_C_LONG)
279 value = va_arg (args, unsigned long int);
c73b8e3a 280 else if (cflags == DP_C_LONG_LONG)
281 value = va_arg (args, unsigned long long);
2d9a148e 282 else
283 value = va_arg (args, unsigned int);
284 fmtint (buffer, &currlen, maxlen, value, 8, min, max, flags);
285 break;
286 case 'u':
287 flags |= DP_F_UNSIGNED;
288 if (cflags == DP_C_SHORT)
027bf205 289 value = va_arg (args, unsigned int);
2d9a148e 290 else if (cflags == DP_C_LONG)
291 value = va_arg (args, unsigned long int);
c73b8e3a 292 else if (cflags == DP_C_LONG_LONG)
293 value = va_arg (args, unsigned long long);
2d9a148e 294 else
295 value = va_arg (args, unsigned int);
296 fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
297 break;
298 case 'X':
299 flags |= DP_F_UP;
300 case 'x':
301 flags |= DP_F_UNSIGNED;
302 if (cflags == DP_C_SHORT)
027bf205 303 value = va_arg (args, unsigned int);
2d9a148e 304 else if (cflags == DP_C_LONG)
305 value = va_arg (args, unsigned long int);
c73b8e3a 306 else if (cflags == DP_C_LONG_LONG)
307 value = va_arg (args, unsigned long long);
2d9a148e 308 else
309 value = va_arg (args, unsigned int);
310 fmtint (buffer, &currlen, maxlen, value, 16, min, max, flags);
311 break;
312 case 'f':
313 if (cflags == DP_C_LDOUBLE)
314 fvalue = va_arg (args, long double);
315 else
316 fvalue = va_arg (args, double);
317 /* um, floating point? */
318 fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags);
319 break;
320 case 'E':
321 flags |= DP_F_UP;
322 case 'e':
323 if (cflags == DP_C_LDOUBLE)
324 fvalue = va_arg (args, long double);
325 else
326 fvalue = va_arg (args, double);
327 break;
328 case 'G':
329 flags |= DP_F_UP;
330 case 'g':
331 if (cflags == DP_C_LDOUBLE)
332 fvalue = va_arg (args, long double);
333 else
334 fvalue = va_arg (args, double);
335 break;
336 case 'c':
337 dopr_outch (buffer, &currlen, maxlen, va_arg (args, int));
338 break;
339 case 's':
340 strvalue = va_arg (args, char *);
341 if (max < 0)
342 max = maxlen; /* ie, no max */
343 fmtstr (buffer, &currlen, maxlen, strvalue, flags, min, max);
344 break;
345 case 'p':
346 strvalue = va_arg (args, void *);
347 fmtint (buffer, &currlen, maxlen, (long) strvalue, 16, min, max, flags);
348 break;
349 case 'n':
350 if (cflags == DP_C_SHORT)
351 {
352 short int *num;
353 num = va_arg (args, short int *);
354 *num = currlen;
355 }
356 else if (cflags == DP_C_LONG)
357 {
358 long int *num;
359 num = va_arg (args, long int *);
360 *num = currlen;
361 }
c73b8e3a 362 else if (cflags == DP_C_LONG_LONG)
363 {
364 long long *num;
365 num = va_arg (args, long long *);
366 *num = currlen;
367 }
2d9a148e 368 else
369 {
370 int *num;
371 num = va_arg (args, int *);
372 *num = currlen;
373 }
374 break;
375 case '%':
376 dopr_outch (buffer, &currlen, maxlen, ch);
377 break;
378 case 'w':
379 /* not supported yet, treat as next char */
380 ch = *format++;
381 break;
382 default:
383 /* Unknown, skip */
384 break;
385 }
386 ch = *format++;
387 state = DP_S_DEFAULT;
388 flags = cflags = min = 0;
389 max = -1;
390 break;
391 case DP_S_DONE:
392 break;
393 default:
394 /* hmm? */
395 break; /* some picky compilers need this */
396 }
397 }
398 if (currlen < maxlen - 1)
399 buffer[currlen] = '\0';
400 else
401 buffer[maxlen - 1] = '\0';
402}
403
404static void fmtstr (char *buffer, size_t *currlen, size_t maxlen,
405 char *value, int flags, int min, int max)
406{
407 int padlen, strln; /* amount to pad */
408 int cnt = 0;
409
410 if (value == 0)
411 {
412 value = "<NULL>";
413 }
414
415 for (strln = 0; value[strln]; ++strln); /* strlen */
416 padlen = min - strln;
417 if (padlen < 0)
418 padlen = 0;
419 if (flags & DP_F_MINUS)
420 padlen = -padlen; /* Left Justify */
421
422 while ((padlen > 0) && (cnt < max))
423 {
424 dopr_outch (buffer, currlen, maxlen, ' ');
425 --padlen;
426 ++cnt;
427 }
428 while (*value && (cnt < max))
429 {
430 dopr_outch (buffer, currlen, maxlen, *value++);
431 ++cnt;
432 }
433 while ((padlen < 0) && (cnt < max))
434 {
435 dopr_outch (buffer, currlen, maxlen, ' ');
436 ++padlen;
437 ++cnt;
438 }
439}
440
441/* Have to handle DP_F_NUM (ie 0x and 0 alternates) */
442
443static void fmtint (char *buffer, size_t *currlen, size_t maxlen,
444 long value, int base, int min, int max, int flags)
445{
446 int signvalue = 0;
447 unsigned long uvalue;
448 char convert[20];
449 int place = 0;
450 int spadlen = 0; /* amount to space pad */
451 int zpadlen = 0; /* amount to zero pad */
452 int caps = 0;
453
454 if (max < 0)
455 max = 0;
456
457 uvalue = value;
458
459 if(!(flags & DP_F_UNSIGNED))
460 {
461 if( value < 0 ) {
462 signvalue = '-';
463 uvalue = -value;
464 }
465 else
466 if (flags & DP_F_PLUS) /* Do a sign (+/i) */
467 signvalue = '+';
468 else
469 if (flags & DP_F_SPACE)
470 signvalue = ' ';
471 }
472
473 if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
474
475 do {
476 convert[place++] =
477 (caps? "0123456789ABCDEF":"0123456789abcdef")
478 [uvalue % (unsigned)base ];
479 uvalue = (uvalue / (unsigned)base );
480 } while(uvalue && (place < 20));
481 if (place == 20) place--;
482 convert[place] = 0;
483
484 zpadlen = max - place;
485 spadlen = min - MAX (max, place) - (signvalue ? 1 : 0);
486 if (zpadlen < 0) zpadlen = 0;
487 if (spadlen < 0) spadlen = 0;
488 if (flags & DP_F_ZERO)
489 {
490 zpadlen = MAX(zpadlen, spadlen);
491 spadlen = 0;
492 }
493 if (flags & DP_F_MINUS)
494 spadlen = -spadlen; /* Left Justifty */
495
496#ifdef DEBUG_SNPRINTF
497 dprint (1, (debugfile, "zpad: %d, spad: %d, min: %d, max: %d, place: %d\n",
498 zpadlen, spadlen, min, max, place));
dad3b556 499#endif
500
2d9a148e 501 /* Spaces */
502 while (spadlen > 0)
503 {
504 dopr_outch (buffer, currlen, maxlen, ' ');
505 --spadlen;
506 }
dad3b556 507
2d9a148e 508 /* Sign */
509 if (signvalue)
510 dopr_outch (buffer, currlen, maxlen, signvalue);
dad3b556 511
2d9a148e 512 /* Zeros */
513 if (zpadlen > 0)
514 {
515 while (zpadlen > 0)
516 {
517 dopr_outch (buffer, currlen, maxlen, '0');
518 --zpadlen;
519 }
520 }
521
522 /* Digits */
523 while (place > 0)
524 dopr_outch (buffer, currlen, maxlen, convert[--place]);
525
526 /* Left Justified spaces */
527 while (spadlen < 0) {
528 dopr_outch (buffer, currlen, maxlen, ' ');
529 ++spadlen;
530 }
531}
532
533static long double abs_val (long double value)
b6019d68 534{
2d9a148e 535 long double result = value;
536
537 if (value < 0)
538 result = -value;
539
540 return result;
b6019d68 541}
b6019d68 542
2d9a148e 543static long double pow10 (int exp)
dad3b556 544{
2d9a148e 545 long double result = 1;
546
547 while (exp)
548 {
549 result *= 10;
550 exp--;
551 }
552
553 return result;
dad3b556 554}
555
2d9a148e 556static long round (long double value)
dad3b556 557{
2d9a148e 558 long intpart;
559
560 intpart = value;
561 value = value - intpart;
562 if (value >= 0.5)
563 intpart++;
564
565 return intpart;
dad3b556 566}
567
2d9a148e 568static void fmtfp (char *buffer, size_t *currlen, size_t maxlen,
569 long double fvalue, int min, int max, int flags)
dad3b556 570{
2d9a148e 571 int signvalue = 0;
572 long double ufvalue;
573 char iconvert[20];
574 char fconvert[20];
575 int iplace = 0;
576 int fplace = 0;
577 int padlen = 0; /* amount to pad */
578 int zpadlen = 0;
579 int caps = 0;
580 long intpart;
581 long fracpart;
582
583 /*
584 * AIX manpage says the default is 0, but Solaris says the default
585 * is 6, and sprintf on AIX defaults to 6
586 */
587 if (max < 0)
588 max = 6;
589
590 ufvalue = abs_val (fvalue);
591
592 if (fvalue < 0)
593 signvalue = '-';
594 else
595 if (flags & DP_F_PLUS) /* Do a sign (+/i) */
596 signvalue = '+';
597 else
598 if (flags & DP_F_SPACE)
599 signvalue = ' ';
600
601#if 0
602 if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
603#endif
604
605 intpart = ufvalue;
606
607 /*
608 * Sorry, we only support 9 digits past the decimal because of our
609 * conversion method
610 */
611 if (max > 9)
612 max = 9;
613
614 /* We "cheat" by converting the fractional part to integer by
615 * multiplying by a factor of 10
616 */
617 fracpart = round ((pow10 (max)) * (ufvalue - intpart));
618
619 if (fracpart >= pow10 (max))
620 {
621 intpart++;
622 fracpart -= pow10 (max);
623 }
624
625#ifdef DEBUG_SNPRINTF
626 dprint (1, (debugfile, "fmtfp: %f =? %d.%d\n", fvalue, intpart, fracpart));
627#endif
628
629 /* Convert integer part */
630 do {
631 iconvert[iplace++] =
632 (caps? "0123456789ABCDEF":"0123456789abcdef")[intpart % 10];
633 intpart = (intpart / 10);
634 } while(intpart && (iplace < 20));
635 if (iplace == 20) iplace--;
636 iconvert[iplace] = 0;
637
638 /* Convert fractional part */
639 do {
640 fconvert[fplace++] =
641 (caps? "0123456789ABCDEF":"0123456789abcdef")[fracpart % 10];
642 fracpart = (fracpart / 10);
643 } while(fracpart && (fplace < 20));
644 if (fplace == 20) fplace--;
645 fconvert[fplace] = 0;
646
647 /* -1 for decimal point, another -1 if we are printing a sign */
648 padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
649 zpadlen = max - fplace;
650 if (zpadlen < 0)
651 zpadlen = 0;
652 if (padlen < 0)
653 padlen = 0;
654 if (flags & DP_F_MINUS)
655 padlen = -padlen; /* Left Justifty */
656
657 if ((flags & DP_F_ZERO) && (padlen > 0))
658 {
659 if (signvalue)
660 {
661 dopr_outch (buffer, currlen, maxlen, signvalue);
662 --padlen;
663 signvalue = 0;
664 }
665 while (padlen > 0)
666 {
667 dopr_outch (buffer, currlen, maxlen, '0');
668 --padlen;
669 }
670 }
671 while (padlen > 0)
672 {
673 dopr_outch (buffer, currlen, maxlen, ' ');
674 --padlen;
675 }
676 if (signvalue)
677 dopr_outch (buffer, currlen, maxlen, signvalue);
678
679 while (iplace > 0)
680 dopr_outch (buffer, currlen, maxlen, iconvert[--iplace]);
681
682 /*
683 * Decimal point. This should probably use locale to find the correct
684 * char to print out.
685 */
686 dopr_outch (buffer, currlen, maxlen, '.');
687
688 while (fplace > 0)
689 dopr_outch (buffer, currlen, maxlen, fconvert[--fplace]);
690
691 while (zpadlen > 0)
692 {
693 dopr_outch (buffer, currlen, maxlen, '0');
694 --zpadlen;
695 }
696
697 while (padlen < 0)
698 {
699 dopr_outch (buffer, currlen, maxlen, ' ');
700 ++padlen;
701 }
702}
703
704static void dopr_outch (char *buffer, size_t *currlen, size_t maxlen, char c)
705{
706 if (*currlen < maxlen)
707 buffer[(*currlen)++] = c;
dad3b556 708}
2d9a148e 709#endif /* !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF) */
dad3b556 710
2d9a148e 711#ifndef HAVE_VSNPRINTF
712int vsnprintf (char *str, size_t count, const char *fmt, va_list args)
dad3b556 713{
2d9a148e 714 str[0] = 0;
715 dopr(str, count, fmt, args);
716 return(strlen(str));
dad3b556 717}
2d9a148e 718#endif /* !HAVE_VSNPRINTF */
dad3b556 719
2d9a148e 720#ifndef HAVE_SNPRINTF
721/* VARARGS3 */
722#ifdef HAVE_STDARGS
723int snprintf (char *str,size_t count,const char *fmt,...)
847e8865 724#else
2d9a148e 725int snprintf (va_alist) va_dcl
847e8865 726#endif
727{
2d9a148e 728#ifndef HAVE_STDARGS
729 char *str;
730 size_t count;
731 char *fmt;
847e8865 732#endif
2d9a148e 733 VA_LOCAL_DECL;
734
735 VA_START (fmt);
736 VA_SHIFT (str, char *);
737 VA_SHIFT (count, size_t );
738 VA_SHIFT (fmt, char *);
739 (void) vsnprintf(str, count, fmt, ap);
740 VA_END;
741 return(strlen(str));
742}
743
744#ifdef TEST_SNPRINTF
745#ifndef LONG_STRING
746#define LONG_STRING 1024
747#endif
748int main (void)
749{
750 char buf1[LONG_STRING];
751 char buf2[LONG_STRING];
752 char *fp_fmt[] = {
753 "%-1.5f",
754 "%1.5f",
755 "%123.9f",
756 "%10.5f",
757 "% 10.5f",
758 "%+22.9f",
759 "%+4.9f",
760 "%01.3f",
761 "%4f",
762 "%3.1f",
763 "%3.2f",
764 NULL
765 };
766 double fp_nums[] = { -1.5, 134.21, 91340.2, 341.1234, 0203.9, 0.96, 0.996,
767 0.9996, 1.996, 4.136, 0};
768 char *int_fmt[] = {
769 "%-1.5d",
770 "%1.5d",
771 "%123.9d",
772 "%5.5d",
773 "%10.5d",
774 "% 10.5d",
775 "%+22.33d",
776 "%01.3d",
777 "%4d",
c73b8e3a 778 "%lld",
779 "%qd",
2d9a148e 780 NULL
781 };
c73b8e3a 782 long long int_nums[] = { -1, 134, 91340, 341, 0203, 0, 9999999 };
2d9a148e 783 int x, y;
784 int fail = 0;
785 int num = 0;
786
787 printf ("Testing snprintf format codes against system sprintf...\n");
788
789 for (x = 0; fp_fmt[x] != NULL ; x++)
790 for (y = 0; fp_nums[y] != 0 ; y++)
791 {
792 snprintf (buf1, sizeof (buf1), fp_fmt[x], fp_nums[y]);
793 sprintf (buf2, fp_fmt[x], fp_nums[y]);
794 if (strcmp (buf1, buf2))
795 {
796 printf("snprintf doesn't match Format: %s\n\tsnprintf = %s\n\tsprintf = %s\n",
797 fp_fmt[x], buf1, buf2);
798 fail++;
799 }
800 num++;
801 }
847e8865 802
2d9a148e 803 for (x = 0; int_fmt[x] != NULL ; x++)
804 for (y = 0; int_nums[y] != 0 ; y++)
805 {
806 snprintf (buf1, sizeof (buf1), int_fmt[x], int_nums[y]);
807 sprintf (buf2, int_fmt[x], int_nums[y]);
808 if (strcmp (buf1, buf2))
809 {
810 printf("snprintf doesn't match Format: %s\n\tsnprintf = %s\n\tsprintf = %s\n",
811 int_fmt[x], buf1, buf2);
812 fail++;
813 }
814 num++;
815 }
816 printf ("%d tests failed out of %d.\n", fail, num);
847e8865 817}
2d9a148e 818#endif /* SNPRINTF_TEST */
847e8865 819
2d9a148e 820#endif /* !HAVE_SNPRINTF */
This page took 0.286401 seconds and 5 git commands to generate.