blob: c75359c51d4b12a363e5e4ddc399c4db863bcd51 [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
Damien Millere9cf3572001-02-09 12:55:35 +110047#include "includes.h"
48
49RCSID("$Id: bsd-snprintf.c,v 1.2 2001/02/09 01:55:36 djm Exp $");
Damien Miller42b81ff1999-11-26 12:21:24 +110050
Damien Miller168e6ac2000-07-11 12:23:01 +100051#if !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF)
Damien Miller42b81ff1999-11-26 12:21:24 +110052
Damien Miller42b81ff1999-11-26 12:21:24 +110053#include <string.h>
Damien Miller168e6ac2000-07-11 12:23:01 +100054# 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)
Damien Miller42b81ff1999-11-26 12:21:24 +110070#else
Damien Miller168e6ac2000-07-11 12:23:01 +100071# 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
Damien Miller42b81ff1999-11-26 12:21:24 +110081#endif
82
Damien Miller168e6ac2000-07-11 12:23:01 +100083/*int snprintf (char *str, size_t count, const char *fmt, ...);*/
84/*int vsnprintf (char *str, size_t count, const char *fmt, va_list arg);*/
Damien Miller42b81ff1999-11-26 12:21:24 +110085
Damien Miller168e6ac2000-07-11 12:23:01 +100086static 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 );
Damien Miller42b81ff1999-11-26 12:21:24 +110095
Damien Miller168e6ac2000-07-11 12:23:01 +100096/*
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 */
Damien Miller3dfb0dd2000-09-30 09:49:08 +1100120#define DP_C_SHORT 1
121#define DP_C_LONG 2
122#define DP_C_LDOUBLE 3
123#define DP_C_LONG_LONG 4
Damien Miller168e6ac2000-07-11 12:23:01 +1000124
125#define char_to_int(p) (p - '0')
Damien Miller87d29ed2000-08-30 09:21:22 +1100126#ifndef MAX
127# define MAX(p,q) ((p >= q) ? p : q)
128#endif
Damien Miller168e6ac2000-07-11 12:23:01 +1000129
130static void dopr (char *buffer, size_t maxlen, const char *format, va_list args)
Damien Miller13bc0be1999-12-28 10:19:16 +1100131{
Damien Miller168e6ac2000-07-11 12:23:01 +1000132 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++;
Damien Miller13bc0be1999-12-28 10:19:16 +1100147
Damien Miller168e6ac2000-07-11 12:23:01 +1000148 while (state != DP_S_DONE)
149 {
150 if ((ch == '\0') || (currlen >= maxlen))
151 state = DP_S_DONE;
Damien Miller42b81ff1999-11-26 12:21:24 +1100152
Damien Miller168e6ac2000-07-11 12:23:01 +1000153 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:
Damien Miller168e6ac2000-07-11 12:23:01 +1000232 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++;
Damien Miller3dfb0dd2000-09-30 09:49:08 +1100241 if (ch == 'l') {
242 cflags = DP_C_LONG_LONG;
243 ch = *format++;
244 }
Damien Miller168e6ac2000-07-11 12:23:01 +1000245 break;
Damien Miller3dfb0dd2000-09-30 09:49:08 +1100246 case 'q':
247 cflags = DP_C_LONG_LONG;
248 ch = *format++;
249 break;
Damien Miller168e6ac2000-07-11 12:23:01 +1000250 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)
Damien Miller9f4f7552000-11-11 09:03:32 +1100265 value = va_arg (args, int);
Damien Miller168e6ac2000-07-11 12:23:01 +1000266 else if (cflags == DP_C_LONG)
267 value = va_arg (args, long int);
Damien Miller3dfb0dd2000-09-30 09:49:08 +1100268 else if (cflags == DP_C_LONG_LONG)
269 value = va_arg (args, long long);
Damien Miller168e6ac2000-07-11 12:23:01 +1000270 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)
Damien Miller9f4f7552000-11-11 09:03:32 +1100277 value = va_arg (args, unsigned int);
Damien Miller168e6ac2000-07-11 12:23:01 +1000278 else if (cflags == DP_C_LONG)
279 value = va_arg (args, unsigned long int);
Damien Miller3dfb0dd2000-09-30 09:49:08 +1100280 else if (cflags == DP_C_LONG_LONG)
281 value = va_arg (args, unsigned long long);
Damien Miller168e6ac2000-07-11 12:23:01 +1000282 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)
Damien Miller9f4f7552000-11-11 09:03:32 +1100289 value = va_arg (args, unsigned int);
Damien Miller168e6ac2000-07-11 12:23:01 +1000290 else if (cflags == DP_C_LONG)
291 value = va_arg (args, unsigned long int);
Damien Miller3dfb0dd2000-09-30 09:49:08 +1100292 else if (cflags == DP_C_LONG_LONG)
293 value = va_arg (args, unsigned long long);
Damien Miller168e6ac2000-07-11 12:23:01 +1000294 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)
Damien Miller9f4f7552000-11-11 09:03:32 +1100303 value = va_arg (args, unsigned int);
Damien Miller168e6ac2000-07-11 12:23:01 +1000304 else if (cflags == DP_C_LONG)
305 value = va_arg (args, unsigned long int);
Damien Miller3dfb0dd2000-09-30 09:49:08 +1100306 else if (cflags == DP_C_LONG_LONG)
307 value = va_arg (args, unsigned long long);
Damien Miller168e6ac2000-07-11 12:23:01 +1000308 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 }
Damien Miller3dfb0dd2000-09-30 09:49:08 +1100362 else if (cflags == DP_C_LONG_LONG)
363 {
364 long long *num;
365 num = va_arg (args, long long *);
366 *num = currlen;
367 }
Damien Miller168e6ac2000-07-11 12:23:01 +1000368 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';
Damien Miller42b81ff1999-11-26 12:21:24 +1100402}
403
Damien Miller168e6ac2000-07-11 12:23:01 +1000404static void fmtstr (char *buffer, size_t *currlen, size_t maxlen,
405 char *value, int flags, int min, int max)
Damien Miller42b81ff1999-11-26 12:21:24 +1100406{
Damien Miller168e6ac2000-07-11 12:23:01 +1000407 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 }
Damien Miller42b81ff1999-11-26 12:21:24 +1100439}
440
Damien Miller168e6ac2000-07-11 12:23:01 +1000441/* 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)
Damien Miller42b81ff1999-11-26 12:21:24 +1100445{
Damien Miller168e6ac2000-07-11 12:23:01 +1000446 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));
499#endif
500
501 /* Spaces */
502 while (spadlen > 0)
503 {
504 dopr_outch (buffer, currlen, maxlen, ' ');
505 --spadlen;
506 }
507
508 /* Sign */
509 if (signvalue)
510 dopr_outch (buffer, currlen, maxlen, signvalue);
511
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 }
Damien Miller42b81ff1999-11-26 12:21:24 +1100531}
532
Damien Miller168e6ac2000-07-11 12:23:01 +1000533static long double abs_val (long double value)
Damien Miller42b81ff1999-11-26 12:21:24 +1100534{
Damien Miller168e6ac2000-07-11 12:23:01 +1000535 long double result = value;
Damien Miller42b81ff1999-11-26 12:21:24 +1100536
Damien Miller168e6ac2000-07-11 12:23:01 +1000537 if (value < 0)
538 result = -value;
Damien Miller42b81ff1999-11-26 12:21:24 +1100539
Damien Miller168e6ac2000-07-11 12:23:01 +1000540 return result;
Damien Miller42b81ff1999-11-26 12:21:24 +1100541}
Damien Miller42b81ff1999-11-26 12:21:24 +1100542
Damien Miller168e6ac2000-07-11 12:23:01 +1000543static long double pow10 (int exp)
544{
545 long double result = 1;
546
547 while (exp)
548 {
549 result *= 10;
550 exp--;
551 }
552
553 return result;
554}
555
556static long round (long double value)
557{
558 long intpart;
559
560 intpart = value;
561 value = value - intpart;
562 if (value >= 0.5)
563 intpart++;
564
565 return intpart;
566}
567
568static void fmtfp (char *buffer, size_t *currlen, size_t maxlen,
569 long double fvalue, int min, int max, int flags)
570{
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;
708}
709#endif /* !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF) */
710
711#ifndef HAVE_VSNPRINTF
712int vsnprintf (char *str, size_t count, const char *fmt, va_list args)
713{
714 str[0] = 0;
715 dopr(str, count, fmt, args);
716 return(strlen(str));
717}
718#endif /* !HAVE_VSNPRINTF */
719
720#ifndef HAVE_SNPRINTF
721/* VARARGS3 */
722#ifdef HAVE_STDARGS
723int snprintf (char *str,size_t count,const char *fmt,...)
Damien Millerc6b3bbe1999-12-13 08:27:33 +1100724#else
Damien Miller168e6ac2000-07-11 12:23:01 +1000725int snprintf (va_alist) va_dcl
Damien Millerc6b3bbe1999-12-13 08:27:33 +1100726#endif
727{
Damien Miller168e6ac2000-07-11 12:23:01 +1000728#ifndef HAVE_STDARGS
729 char *str;
730 size_t count;
731 char *fmt;
Damien Millerc6b3bbe1999-12-13 08:27:33 +1100732#endif
Damien Miller168e6ac2000-07-11 12:23:01 +1000733 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));
Damien Millerc6b3bbe1999-12-13 08:27:33 +1100742}
Damien Millerc6b3bbe1999-12-13 08:27:33 +1100743
Damien Miller168e6ac2000-07-11 12:23:01 +1000744#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",
Damien Miller3dfb0dd2000-09-30 09:49:08 +1100778 "%lld",
779 "%qd",
Damien Miller168e6ac2000-07-11 12:23:01 +1000780 NULL
781 };
Damien Miller3dfb0dd2000-09-30 09:49:08 +1100782 long long int_nums[] = { -1, 134, 91340, 341, 0203, 0, 9999999 };
Damien Miller168e6ac2000-07-11 12:23:01 +1000783 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 }
802
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);
817}
818#endif /* SNPRINTF_TEST */
819
820#endif /* !HAVE_SNPRINTF */