blob: 59fefbf258d95a7e8fc4d270c46983c227c2bf2e [file] [log] [blame]
Damien Miller168e6ac2000-07-11 12:23:01 +10001/**************************************************************
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...
Damien Miller42b81ff1999-11-26 12:21:24 +11007 *
Damien Miller168e6ac2000-07-11 12:23:01 +10008 * snprintf() is used instead of sprintf() as it does limit checks
9 * for string length. This covers a nasty loophole.
Damien Miller42b81ff1999-11-26 12:21:24 +110010 *
Damien Miller168e6ac2000-07-11 12:23:01 +100011 * The other functions are there to prevent NULL pointers from
12 * causing nast effects.
Damien Miller42b81ff1999-11-26 12:21:24 +110013 *
Damien Miller168e6ac2000-07-11 12:23:01 +100014 * 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 *
Damien Miller3dfb0dd2000-09-30 09:49:08 +110041 * 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.
Damien Miller168e6ac2000-07-11 12:23:01 +100045 **************************************************************/
Damien Miller42b81ff1999-11-26 12:21:24 +110046
47#include "config.h"
48
Damien Miller168e6ac2000-07-11 12:23:01 +100049#if !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF)
Damien Miller42b81ff1999-11-26 12:21:24 +110050
Damien Miller42b81ff1999-11-26 12:21:24 +110051#include <string.h>
Damien Miller168e6ac2000-07-11 12:23:01 +100052# include <ctype.h>
53#include <sys/types.h>
54
55/* Define this as a fall through, HAVE_STDARG_H is probably already set */
56
57#define HAVE_VARARGS_H
58
59/* varargs declarations: */
60
61#if defined(HAVE_STDARG_H)
62# include <stdarg.h>
63# define HAVE_STDARGS /* let's hope that works everywhere (mj) */
64# define VA_LOCAL_DECL va_list ap
65# define VA_START(f) va_start(ap, f)
66# define VA_SHIFT(v,t) ; /* no-op for ANSI */
67# define VA_END va_end(ap)
Damien Miller42b81ff1999-11-26 12:21:24 +110068#else
Damien Miller168e6ac2000-07-11 12:23:01 +100069# if defined(HAVE_VARARGS_H)
70# include <varargs.h>
71# undef HAVE_STDARGS
72# define VA_LOCAL_DECL va_list ap
73# define VA_START(f) va_start(ap) /* f is ignored! */
74# define VA_SHIFT(v,t) v = va_arg(ap,t)
75# define VA_END va_end(ap)
76# else
77/*XX ** NO VARARGS ** XX*/
78# endif
Damien Miller42b81ff1999-11-26 12:21:24 +110079#endif
80
Damien Miller168e6ac2000-07-11 12:23:01 +100081/*int snprintf (char *str, size_t count, const char *fmt, ...);*/
82/*int vsnprintf (char *str, size_t count, const char *fmt, va_list arg);*/
Damien Miller42b81ff1999-11-26 12:21:24 +110083
Damien Miller168e6ac2000-07-11 12:23:01 +100084static void dopr (char *buffer, size_t maxlen, const char *format,
85 va_list args);
86static void fmtstr (char *buffer, size_t *currlen, size_t maxlen,
87 char *value, int flags, int min, int max);
88static void fmtint (char *buffer, size_t *currlen, size_t maxlen,
89 long value, int base, int min, int max, int flags);
90static void fmtfp (char *buffer, size_t *currlen, size_t maxlen,
91 long double fvalue, int min, int max, int flags);
92static void dopr_outch (char *buffer, size_t *currlen, size_t maxlen, char c );
Damien Miller42b81ff1999-11-26 12:21:24 +110093
Damien Miller168e6ac2000-07-11 12:23:01 +100094/*
95 * dopr(): poor man's version of doprintf
96 */
97
98/* format read states */
99#define DP_S_DEFAULT 0
100#define DP_S_FLAGS 1
101#define DP_S_MIN 2
102#define DP_S_DOT 3
103#define DP_S_MAX 4
104#define DP_S_MOD 5
105#define DP_S_CONV 6
106#define DP_S_DONE 7
107
108/* format flags - Bits */
109#define DP_F_MINUS (1 << 0)
110#define DP_F_PLUS (1 << 1)
111#define DP_F_SPACE (1 << 2)
112#define DP_F_NUM (1 << 3)
113#define DP_F_ZERO (1 << 4)
114#define DP_F_UP (1 << 5)
115#define DP_F_UNSIGNED (1 << 6)
116
117/* Conversion Flags */
Damien Miller3dfb0dd2000-09-30 09:49:08 +1100118#define DP_C_SHORT 1
119#define DP_C_LONG 2
120#define DP_C_LDOUBLE 3
121#define DP_C_LONG_LONG 4
Damien Miller168e6ac2000-07-11 12:23:01 +1000122
123#define char_to_int(p) (p - '0')
Damien Miller87d29ed2000-08-30 09:21:22 +1100124#ifndef MAX
125# define MAX(p,q) ((p >= q) ? p : q)
126#endif
Damien Miller168e6ac2000-07-11 12:23:01 +1000127
128static void dopr (char *buffer, size_t maxlen, const char *format, va_list args)
Damien Miller13bc0be1999-12-28 10:19:16 +1100129{
Damien Miller168e6ac2000-07-11 12:23:01 +1000130 char ch;
131 long value;
132 long double fvalue;
133 char *strvalue;
134 int min;
135 int max;
136 int state;
137 int flags;
138 int cflags;
139 size_t currlen;
140
141 state = DP_S_DEFAULT;
142 currlen = flags = cflags = min = 0;
143 max = -1;
144 ch = *format++;
Damien Miller13bc0be1999-12-28 10:19:16 +1100145
Damien Miller168e6ac2000-07-11 12:23:01 +1000146 while (state != DP_S_DONE)
147 {
148 if ((ch == '\0') || (currlen >= maxlen))
149 state = DP_S_DONE;
Damien Miller42b81ff1999-11-26 12:21:24 +1100150
Damien Miller168e6ac2000-07-11 12:23:01 +1000151 switch(state)
152 {
153 case DP_S_DEFAULT:
154 if (ch == '%')
155 state = DP_S_FLAGS;
156 else
157 dopr_outch (buffer, &currlen, maxlen, ch);
158 ch = *format++;
159 break;
160 case DP_S_FLAGS:
161 switch (ch)
162 {
163 case '-':
164 flags |= DP_F_MINUS;
165 ch = *format++;
166 break;
167 case '+':
168 flags |= DP_F_PLUS;
169 ch = *format++;
170 break;
171 case ' ':
172 flags |= DP_F_SPACE;
173 ch = *format++;
174 break;
175 case '#':
176 flags |= DP_F_NUM;
177 ch = *format++;
178 break;
179 case '0':
180 flags |= DP_F_ZERO;
181 ch = *format++;
182 break;
183 default:
184 state = DP_S_MIN;
185 break;
186 }
187 break;
188 case DP_S_MIN:
189 if (isdigit((unsigned char)ch))
190 {
191 min = 10*min + char_to_int (ch);
192 ch = *format++;
193 }
194 else if (ch == '*')
195 {
196 min = va_arg (args, int);
197 ch = *format++;
198 state = DP_S_DOT;
199 }
200 else
201 state = DP_S_DOT;
202 break;
203 case DP_S_DOT:
204 if (ch == '.')
205 {
206 state = DP_S_MAX;
207 ch = *format++;
208 }
209 else
210 state = DP_S_MOD;
211 break;
212 case DP_S_MAX:
213 if (isdigit((unsigned char)ch))
214 {
215 if (max < 0)
216 max = 0;
217 max = 10*max + char_to_int (ch);
218 ch = *format++;
219 }
220 else if (ch == '*')
221 {
222 max = va_arg (args, int);
223 ch = *format++;
224 state = DP_S_MOD;
225 }
226 else
227 state = DP_S_MOD;
228 break;
229 case DP_S_MOD:
Damien Miller168e6ac2000-07-11 12:23:01 +1000230 switch (ch)
231 {
232 case 'h':
233 cflags = DP_C_SHORT;
234 ch = *format++;
235 break;
236 case 'l':
237 cflags = DP_C_LONG;
238 ch = *format++;
Damien Miller3dfb0dd2000-09-30 09:49:08 +1100239 if (ch == 'l') {
240 cflags = DP_C_LONG_LONG;
241 ch = *format++;
242 }
Damien Miller168e6ac2000-07-11 12:23:01 +1000243 break;
Damien Miller3dfb0dd2000-09-30 09:49:08 +1100244 case 'q':
245 cflags = DP_C_LONG_LONG;
246 ch = *format++;
247 break;
Damien Miller168e6ac2000-07-11 12:23:01 +1000248 case 'L':
249 cflags = DP_C_LDOUBLE;
250 ch = *format++;
251 break;
252 default:
253 break;
254 }
255 state = DP_S_CONV;
256 break;
257 case DP_S_CONV:
258 switch (ch)
259 {
260 case 'd':
261 case 'i':
262 if (cflags == DP_C_SHORT)
Damien Miller9f4f7552000-11-11 09:03:32 +1100263 value = va_arg (args, int);
Damien Miller168e6ac2000-07-11 12:23:01 +1000264 else if (cflags == DP_C_LONG)
265 value = va_arg (args, long int);
Damien Miller3dfb0dd2000-09-30 09:49:08 +1100266 else if (cflags == DP_C_LONG_LONG)
267 value = va_arg (args, long long);
Damien Miller168e6ac2000-07-11 12:23:01 +1000268 else
269 value = va_arg (args, int);
270 fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
271 break;
272 case 'o':
273 flags |= DP_F_UNSIGNED;
274 if (cflags == DP_C_SHORT)
Damien Miller9f4f7552000-11-11 09:03:32 +1100275 value = va_arg (args, unsigned int);
Damien Miller168e6ac2000-07-11 12:23:01 +1000276 else if (cflags == DP_C_LONG)
277 value = va_arg (args, unsigned long int);
Damien Miller3dfb0dd2000-09-30 09:49:08 +1100278 else if (cflags == DP_C_LONG_LONG)
279 value = va_arg (args, unsigned long long);
Damien Miller168e6ac2000-07-11 12:23:01 +1000280 else
281 value = va_arg (args, unsigned int);
282 fmtint (buffer, &currlen, maxlen, value, 8, min, max, flags);
283 break;
284 case 'u':
285 flags |= DP_F_UNSIGNED;
286 if (cflags == DP_C_SHORT)
Damien Miller9f4f7552000-11-11 09:03:32 +1100287 value = va_arg (args, unsigned int);
Damien Miller168e6ac2000-07-11 12:23:01 +1000288 else if (cflags == DP_C_LONG)
289 value = va_arg (args, unsigned long int);
Damien Miller3dfb0dd2000-09-30 09:49:08 +1100290 else if (cflags == DP_C_LONG_LONG)
291 value = va_arg (args, unsigned long long);
Damien Miller168e6ac2000-07-11 12:23:01 +1000292 else
293 value = va_arg (args, unsigned int);
294 fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
295 break;
296 case 'X':
297 flags |= DP_F_UP;
298 case 'x':
299 flags |= DP_F_UNSIGNED;
300 if (cflags == DP_C_SHORT)
Damien Miller9f4f7552000-11-11 09:03:32 +1100301 value = va_arg (args, unsigned int);
Damien Miller168e6ac2000-07-11 12:23:01 +1000302 else if (cflags == DP_C_LONG)
303 value = va_arg (args, unsigned long int);
Damien Miller3dfb0dd2000-09-30 09:49:08 +1100304 else if (cflags == DP_C_LONG_LONG)
305 value = va_arg (args, unsigned long long);
Damien Miller168e6ac2000-07-11 12:23:01 +1000306 else
307 value = va_arg (args, unsigned int);
308 fmtint (buffer, &currlen, maxlen, value, 16, min, max, flags);
309 break;
310 case 'f':
311 if (cflags == DP_C_LDOUBLE)
312 fvalue = va_arg (args, long double);
313 else
314 fvalue = va_arg (args, double);
315 /* um, floating point? */
316 fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags);
317 break;
318 case 'E':
319 flags |= DP_F_UP;
320 case 'e':
321 if (cflags == DP_C_LDOUBLE)
322 fvalue = va_arg (args, long double);
323 else
324 fvalue = va_arg (args, double);
325 break;
326 case 'G':
327 flags |= DP_F_UP;
328 case 'g':
329 if (cflags == DP_C_LDOUBLE)
330 fvalue = va_arg (args, long double);
331 else
332 fvalue = va_arg (args, double);
333 break;
334 case 'c':
335 dopr_outch (buffer, &currlen, maxlen, va_arg (args, int));
336 break;
337 case 's':
338 strvalue = va_arg (args, char *);
339 if (max < 0)
340 max = maxlen; /* ie, no max */
341 fmtstr (buffer, &currlen, maxlen, strvalue, flags, min, max);
342 break;
343 case 'p':
344 strvalue = va_arg (args, void *);
345 fmtint (buffer, &currlen, maxlen, (long) strvalue, 16, min, max, flags);
346 break;
347 case 'n':
348 if (cflags == DP_C_SHORT)
349 {
350 short int *num;
351 num = va_arg (args, short int *);
352 *num = currlen;
353 }
354 else if (cflags == DP_C_LONG)
355 {
356 long int *num;
357 num = va_arg (args, long int *);
358 *num = currlen;
359 }
Damien Miller3dfb0dd2000-09-30 09:49:08 +1100360 else if (cflags == DP_C_LONG_LONG)
361 {
362 long long *num;
363 num = va_arg (args, long long *);
364 *num = currlen;
365 }
Damien Miller168e6ac2000-07-11 12:23:01 +1000366 else
367 {
368 int *num;
369 num = va_arg (args, int *);
370 *num = currlen;
371 }
372 break;
373 case '%':
374 dopr_outch (buffer, &currlen, maxlen, ch);
375 break;
376 case 'w':
377 /* not supported yet, treat as next char */
378 ch = *format++;
379 break;
380 default:
381 /* Unknown, skip */
382 break;
383 }
384 ch = *format++;
385 state = DP_S_DEFAULT;
386 flags = cflags = min = 0;
387 max = -1;
388 break;
389 case DP_S_DONE:
390 break;
391 default:
392 /* hmm? */
393 break; /* some picky compilers need this */
394 }
395 }
396 if (currlen < maxlen - 1)
397 buffer[currlen] = '\0';
398 else
399 buffer[maxlen - 1] = '\0';
Damien Miller42b81ff1999-11-26 12:21:24 +1100400}
401
Damien Miller168e6ac2000-07-11 12:23:01 +1000402static void fmtstr (char *buffer, size_t *currlen, size_t maxlen,
403 char *value, int flags, int min, int max)
Damien Miller42b81ff1999-11-26 12:21:24 +1100404{
Damien Miller168e6ac2000-07-11 12:23:01 +1000405 int padlen, strln; /* amount to pad */
406 int cnt = 0;
407
408 if (value == 0)
409 {
410 value = "<NULL>";
411 }
412
413 for (strln = 0; value[strln]; ++strln); /* strlen */
414 padlen = min - strln;
415 if (padlen < 0)
416 padlen = 0;
417 if (flags & DP_F_MINUS)
418 padlen = -padlen; /* Left Justify */
419
420 while ((padlen > 0) && (cnt < max))
421 {
422 dopr_outch (buffer, currlen, maxlen, ' ');
423 --padlen;
424 ++cnt;
425 }
426 while (*value && (cnt < max))
427 {
428 dopr_outch (buffer, currlen, maxlen, *value++);
429 ++cnt;
430 }
431 while ((padlen < 0) && (cnt < max))
432 {
433 dopr_outch (buffer, currlen, maxlen, ' ');
434 ++padlen;
435 ++cnt;
436 }
Damien Miller42b81ff1999-11-26 12:21:24 +1100437}
438
Damien Miller168e6ac2000-07-11 12:23:01 +1000439/* Have to handle DP_F_NUM (ie 0x and 0 alternates) */
440
441static void fmtint (char *buffer, size_t *currlen, size_t maxlen,
442 long value, int base, int min, int max, int flags)
Damien Miller42b81ff1999-11-26 12:21:24 +1100443{
Damien Miller168e6ac2000-07-11 12:23:01 +1000444 int signvalue = 0;
445 unsigned long uvalue;
446 char convert[20];
447 int place = 0;
448 int spadlen = 0; /* amount to space pad */
449 int zpadlen = 0; /* amount to zero pad */
450 int caps = 0;
451
452 if (max < 0)
453 max = 0;
454
455 uvalue = value;
456
457 if(!(flags & DP_F_UNSIGNED))
458 {
459 if( value < 0 ) {
460 signvalue = '-';
461 uvalue = -value;
462 }
463 else
464 if (flags & DP_F_PLUS) /* Do a sign (+/i) */
465 signvalue = '+';
466 else
467 if (flags & DP_F_SPACE)
468 signvalue = ' ';
469 }
470
471 if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
472
473 do {
474 convert[place++] =
475 (caps? "0123456789ABCDEF":"0123456789abcdef")
476 [uvalue % (unsigned)base ];
477 uvalue = (uvalue / (unsigned)base );
478 } while(uvalue && (place < 20));
479 if (place == 20) place--;
480 convert[place] = 0;
481
482 zpadlen = max - place;
483 spadlen = min - MAX (max, place) - (signvalue ? 1 : 0);
484 if (zpadlen < 0) zpadlen = 0;
485 if (spadlen < 0) spadlen = 0;
486 if (flags & DP_F_ZERO)
487 {
488 zpadlen = MAX(zpadlen, spadlen);
489 spadlen = 0;
490 }
491 if (flags & DP_F_MINUS)
492 spadlen = -spadlen; /* Left Justifty */
493
494#ifdef DEBUG_SNPRINTF
495 dprint (1, (debugfile, "zpad: %d, spad: %d, min: %d, max: %d, place: %d\n",
496 zpadlen, spadlen, min, max, place));
497#endif
498
499 /* Spaces */
500 while (spadlen > 0)
501 {
502 dopr_outch (buffer, currlen, maxlen, ' ');
503 --spadlen;
504 }
505
506 /* Sign */
507 if (signvalue)
508 dopr_outch (buffer, currlen, maxlen, signvalue);
509
510 /* Zeros */
511 if (zpadlen > 0)
512 {
513 while (zpadlen > 0)
514 {
515 dopr_outch (buffer, currlen, maxlen, '0');
516 --zpadlen;
517 }
518 }
519
520 /* Digits */
521 while (place > 0)
522 dopr_outch (buffer, currlen, maxlen, convert[--place]);
523
524 /* Left Justified spaces */
525 while (spadlen < 0) {
526 dopr_outch (buffer, currlen, maxlen, ' ');
527 ++spadlen;
528 }
Damien Miller42b81ff1999-11-26 12:21:24 +1100529}
530
Damien Miller168e6ac2000-07-11 12:23:01 +1000531static long double abs_val (long double value)
Damien Miller42b81ff1999-11-26 12:21:24 +1100532{
Damien Miller168e6ac2000-07-11 12:23:01 +1000533 long double result = value;
Damien Miller42b81ff1999-11-26 12:21:24 +1100534
Damien Miller168e6ac2000-07-11 12:23:01 +1000535 if (value < 0)
536 result = -value;
Damien Miller42b81ff1999-11-26 12:21:24 +1100537
Damien Miller168e6ac2000-07-11 12:23:01 +1000538 return result;
Damien Miller42b81ff1999-11-26 12:21:24 +1100539}
Damien Miller42b81ff1999-11-26 12:21:24 +1100540
Damien Miller168e6ac2000-07-11 12:23:01 +1000541static long double pow10 (int exp)
542{
543 long double result = 1;
544
545 while (exp)
546 {
547 result *= 10;
548 exp--;
549 }
550
551 return result;
552}
553
554static long round (long double value)
555{
556 long intpart;
557
558 intpart = value;
559 value = value - intpart;
560 if (value >= 0.5)
561 intpart++;
562
563 return intpart;
564}
565
566static void fmtfp (char *buffer, size_t *currlen, size_t maxlen,
567 long double fvalue, int min, int max, int flags)
568{
569 int signvalue = 0;
570 long double ufvalue;
571 char iconvert[20];
572 char fconvert[20];
573 int iplace = 0;
574 int fplace = 0;
575 int padlen = 0; /* amount to pad */
576 int zpadlen = 0;
577 int caps = 0;
578 long intpart;
579 long fracpart;
580
581 /*
582 * AIX manpage says the default is 0, but Solaris says the default
583 * is 6, and sprintf on AIX defaults to 6
584 */
585 if (max < 0)
586 max = 6;
587
588 ufvalue = abs_val (fvalue);
589
590 if (fvalue < 0)
591 signvalue = '-';
592 else
593 if (flags & DP_F_PLUS) /* Do a sign (+/i) */
594 signvalue = '+';
595 else
596 if (flags & DP_F_SPACE)
597 signvalue = ' ';
598
599#if 0
600 if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
601#endif
602
603 intpart = ufvalue;
604
605 /*
606 * Sorry, we only support 9 digits past the decimal because of our
607 * conversion method
608 */
609 if (max > 9)
610 max = 9;
611
612 /* We "cheat" by converting the fractional part to integer by
613 * multiplying by a factor of 10
614 */
615 fracpart = round ((pow10 (max)) * (ufvalue - intpart));
616
617 if (fracpart >= pow10 (max))
618 {
619 intpart++;
620 fracpart -= pow10 (max);
621 }
622
623#ifdef DEBUG_SNPRINTF
624 dprint (1, (debugfile, "fmtfp: %f =? %d.%d\n", fvalue, intpart, fracpart));
625#endif
626
627 /* Convert integer part */
628 do {
629 iconvert[iplace++] =
630 (caps? "0123456789ABCDEF":"0123456789abcdef")[intpart % 10];
631 intpart = (intpart / 10);
632 } while(intpart && (iplace < 20));
633 if (iplace == 20) iplace--;
634 iconvert[iplace] = 0;
635
636 /* Convert fractional part */
637 do {
638 fconvert[fplace++] =
639 (caps? "0123456789ABCDEF":"0123456789abcdef")[fracpart % 10];
640 fracpart = (fracpart / 10);
641 } while(fracpart && (fplace < 20));
642 if (fplace == 20) fplace--;
643 fconvert[fplace] = 0;
644
645 /* -1 for decimal point, another -1 if we are printing a sign */
646 padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
647 zpadlen = max - fplace;
648 if (zpadlen < 0)
649 zpadlen = 0;
650 if (padlen < 0)
651 padlen = 0;
652 if (flags & DP_F_MINUS)
653 padlen = -padlen; /* Left Justifty */
654
655 if ((flags & DP_F_ZERO) && (padlen > 0))
656 {
657 if (signvalue)
658 {
659 dopr_outch (buffer, currlen, maxlen, signvalue);
660 --padlen;
661 signvalue = 0;
662 }
663 while (padlen > 0)
664 {
665 dopr_outch (buffer, currlen, maxlen, '0');
666 --padlen;
667 }
668 }
669 while (padlen > 0)
670 {
671 dopr_outch (buffer, currlen, maxlen, ' ');
672 --padlen;
673 }
674 if (signvalue)
675 dopr_outch (buffer, currlen, maxlen, signvalue);
676
677 while (iplace > 0)
678 dopr_outch (buffer, currlen, maxlen, iconvert[--iplace]);
679
680 /*
681 * Decimal point. This should probably use locale to find the correct
682 * char to print out.
683 */
684 dopr_outch (buffer, currlen, maxlen, '.');
685
686 while (fplace > 0)
687 dopr_outch (buffer, currlen, maxlen, fconvert[--fplace]);
688
689 while (zpadlen > 0)
690 {
691 dopr_outch (buffer, currlen, maxlen, '0');
692 --zpadlen;
693 }
694
695 while (padlen < 0)
696 {
697 dopr_outch (buffer, currlen, maxlen, ' ');
698 ++padlen;
699 }
700}
701
702static void dopr_outch (char *buffer, size_t *currlen, size_t maxlen, char c)
703{
704 if (*currlen < maxlen)
705 buffer[(*currlen)++] = c;
706}
707#endif /* !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF) */
708
709#ifndef HAVE_VSNPRINTF
710int vsnprintf (char *str, size_t count, const char *fmt, va_list args)
711{
712 str[0] = 0;
713 dopr(str, count, fmt, args);
714 return(strlen(str));
715}
716#endif /* !HAVE_VSNPRINTF */
717
718#ifndef HAVE_SNPRINTF
719/* VARARGS3 */
720#ifdef HAVE_STDARGS
721int snprintf (char *str,size_t count,const char *fmt,...)
Damien Millerc6b3bbe1999-12-13 08:27:33 +1100722#else
Damien Miller168e6ac2000-07-11 12:23:01 +1000723int snprintf (va_alist) va_dcl
Damien Millerc6b3bbe1999-12-13 08:27:33 +1100724#endif
725{
Damien Miller168e6ac2000-07-11 12:23:01 +1000726#ifndef HAVE_STDARGS
727 char *str;
728 size_t count;
729 char *fmt;
Damien Millerc6b3bbe1999-12-13 08:27:33 +1100730#endif
Damien Miller168e6ac2000-07-11 12:23:01 +1000731 VA_LOCAL_DECL;
732
733 VA_START (fmt);
734 VA_SHIFT (str, char *);
735 VA_SHIFT (count, size_t );
736 VA_SHIFT (fmt, char *);
737 (void) vsnprintf(str, count, fmt, ap);
738 VA_END;
739 return(strlen(str));
Damien Millerc6b3bbe1999-12-13 08:27:33 +1100740}
Damien Millerc6b3bbe1999-12-13 08:27:33 +1100741
Damien Miller168e6ac2000-07-11 12:23:01 +1000742#ifdef TEST_SNPRINTF
743#ifndef LONG_STRING
744#define LONG_STRING 1024
745#endif
746int main (void)
747{
748 char buf1[LONG_STRING];
749 char buf2[LONG_STRING];
750 char *fp_fmt[] = {
751 "%-1.5f",
752 "%1.5f",
753 "%123.9f",
754 "%10.5f",
755 "% 10.5f",
756 "%+22.9f",
757 "%+4.9f",
758 "%01.3f",
759 "%4f",
760 "%3.1f",
761 "%3.2f",
762 NULL
763 };
764 double fp_nums[] = { -1.5, 134.21, 91340.2, 341.1234, 0203.9, 0.96, 0.996,
765 0.9996, 1.996, 4.136, 0};
766 char *int_fmt[] = {
767 "%-1.5d",
768 "%1.5d",
769 "%123.9d",
770 "%5.5d",
771 "%10.5d",
772 "% 10.5d",
773 "%+22.33d",
774 "%01.3d",
775 "%4d",
Damien Miller3dfb0dd2000-09-30 09:49:08 +1100776 "%lld",
777 "%qd",
Damien Miller168e6ac2000-07-11 12:23:01 +1000778 NULL
779 };
Damien Miller3dfb0dd2000-09-30 09:49:08 +1100780 long long int_nums[] = { -1, 134, 91340, 341, 0203, 0, 9999999 };
Damien Miller168e6ac2000-07-11 12:23:01 +1000781 int x, y;
782 int fail = 0;
783 int num = 0;
784
785 printf ("Testing snprintf format codes against system sprintf...\n");
786
787 for (x = 0; fp_fmt[x] != NULL ; x++)
788 for (y = 0; fp_nums[y] != 0 ; y++)
789 {
790 snprintf (buf1, sizeof (buf1), fp_fmt[x], fp_nums[y]);
791 sprintf (buf2, fp_fmt[x], fp_nums[y]);
792 if (strcmp (buf1, buf2))
793 {
794 printf("snprintf doesn't match Format: %s\n\tsnprintf = %s\n\tsprintf = %s\n",
795 fp_fmt[x], buf1, buf2);
796 fail++;
797 }
798 num++;
799 }
800
801 for (x = 0; int_fmt[x] != NULL ; x++)
802 for (y = 0; int_nums[y] != 0 ; y++)
803 {
804 snprintf (buf1, sizeof (buf1), int_fmt[x], int_nums[y]);
805 sprintf (buf2, int_fmt[x], int_nums[y]);
806 if (strcmp (buf1, buf2))
807 {
808 printf("snprintf doesn't match Format: %s\n\tsnprintf = %s\n\tsprintf = %s\n",
809 int_fmt[x], buf1, buf2);
810 fail++;
811 }
812 num++;
813 }
814 printf ("%d tests failed out of %d.\n", fail, num);
815}
816#endif /* SNPRINTF_TEST */
817
818#endif /* !HAVE_SNPRINTF */