blob: c30cd1223348bd3539460a7f4640f5fe49780f0e [file] [log] [blame]
Damien Millerf18462f2003-04-01 21:31:56 +10001/*
2 * Copyright Patrick Powell 1995
3 * This code is based on code written by Patrick Powell (papowell@astart.com)
4 * It may be used for any purpose as long as this notice remains intact
5 * on all source code distributions
6 */
7
Damien Miller168e6ac2000-07-11 12:23:01 +10008/**************************************************************
9 * Original:
10 * Patrick Powell Tue Apr 11 09:48:21 PDT 1995
11 * A bombproof version of doprnt (dopr) included.
12 * Sigh. This sort of thing is always nasty do deal with. Note that
13 * the version here does not include floating point...
Damien Miller42b81ff1999-11-26 12:21:24 +110014 *
Damien Miller168e6ac2000-07-11 12:23:01 +100015 * snprintf() is used instead of sprintf() as it does limit checks
16 * for string length. This covers a nasty loophole.
Damien Miller42b81ff1999-11-26 12:21:24 +110017 *
Damien Miller168e6ac2000-07-11 12:23:01 +100018 * The other functions are there to prevent NULL pointers from
19 * causing nast effects.
Damien Miller42b81ff1999-11-26 12:21:24 +110020 *
Damien Miller168e6ac2000-07-11 12:23:01 +100021 * More Recently:
22 * Brandon Long <blong@fiction.net> 9/15/96 for mutt 0.43
23 * This was ugly. It is still ugly. I opted out of floating point
24 * numbers, but the formatter understands just about everything
25 * from the normal C string format, at least as far as I can tell from
26 * the Solaris 2.5 printf(3S) man page.
27 *
28 * Brandon Long <blong@fiction.net> 10/22/97 for mutt 0.87.1
29 * Ok, added some minimal floating point support, which means this
30 * probably requires libm on most operating systems. Don't yet
31 * support the exponent (e,E) and sigfig (g,G). Also, fmtint()
32 * was pretty badly broken, it just wasn't being exercised in ways
33 * which showed it, so that's been fixed. Also, formated the code
34 * to mutt conventions, and removed dead code left over from the
35 * original. Also, there is now a builtin-test, just compile with:
36 * gcc -DTEST_SNPRINTF -o snprintf snprintf.c -lm
37 * and run snprintf for results.
38 *
39 * Thomas Roessler <roessler@guug.de> 01/27/98 for mutt 0.89i
40 * The PGP code was using unsigned hexadecimal formats.
41 * Unfortunately, unsigned formats simply didn't work.
42 *
43 * Michael Elkins <me@cs.hmc.edu> 03/05/98 for mutt 0.90.8
44 * The original code assumed that both snprintf() and vsnprintf() were
45 * missing. Some systems only have snprintf() but not vsnprintf(), so
46 * the code is now broken down under HAVE_SNPRINTF and HAVE_VSNPRINTF.
47 *
Damien Miller57f39152005-11-24 19:58:19 +110048 * Andrew Tridgell (tridge@samba.org) Oct 1998
49 * fixed handling of %.0f
50 * added test for HAVE_LONG_DOUBLE
Ben Lindstrom6c92dab2001-02-13 02:18:50 +000051 *
Damien Miller57f39152005-11-24 19:58:19 +110052 * tridge@samba.org, idra@samba.org, April 2001
53 * got rid of fcvt code (twas buggy and made testing harder)
54 * added C99 semantics
55 *
56 * date: 2002/12/19 19:56:31; author: herb; state: Exp; lines: +2 -0
57 * actually print args for %g and %e
58 *
59 * date: 2002/06/03 13:37:52; author: jmcd; state: Exp; lines: +8 -0
60 * Since includes.h isn't included here, VA_COPY has to be defined here. I don't
61 * see any include file that is guaranteed to be here, so I'm defining it
62 * locally. Fixes AIX and Solaris builds.
63 *
64 * date: 2002/06/03 03:07:24; author: tridge; state: Exp; lines: +5 -13
65 * put the ifdef for HAVE_VA_COPY in one place rather than in lots of
66 * functions
67 *
68 * date: 2002/05/17 14:51:22; author: jmcd; state: Exp; lines: +21 -4
69 * Fix usage of va_list passed as an arg. Use __va_copy before using it
70 * when it exists.
71 *
72 * date: 2002/04/16 22:38:04; author: idra; state: Exp; lines: +20 -14
73 * Fix incorrect zpadlen handling in fmtfp.
74 * Thanks to Ollie Oldham <ollie.oldham@metro-optix.com> for spotting it.
75 * few mods to make it easier to compile the tests.
76 * addedd the "Ollie" test to the floating point ones.
77 *
78 * Martin Pool (mbp@samba.org) April 2003
79 * Remove NO_CONFIG_H so that the test case can be built within a source
80 * tree with less trouble.
81 * Remove unnecessary SAFE_FREE() definition.
82 *
83 * Martin Pool (mbp@samba.org) May 2003
84 * Put in a prototype for dummy_snprintf() to quiet compiler warnings.
85 *
86 * Move #endif to make sure VA_COPY, LDOUBLE, etc are defined even
87 * if the C library has some snprintf functions already.
Damien Miller168e6ac2000-07-11 12:23:01 +100088 **************************************************************/
Damien Miller42b81ff1999-11-26 12:21:24 +110089
Damien Millere9cf3572001-02-09 12:55:35 +110090#include "includes.h"
91
Ben Lindstrom63941f92001-02-25 23:20:40 +000092#if defined(BROKEN_SNPRINTF) /* For those with broken snprintf() */
93# undef HAVE_SNPRINTF
94# undef HAVE_VSNPRINTF
95#endif
Damien Miller42b81ff1999-11-26 12:21:24 +110096
Damien Miller57f39152005-11-24 19:58:19 +110097#ifndef VA_COPY
98# ifdef HAVE_VA_COPY
99# define VA_COPY(dest, src) va_copy(dest, src)
100# else
101# ifdef HAVE___VA_COPY
102# define VA_COPY(dest, src) __va_copy(dest, src)
103# else
104# define VA_COPY(dest, src) (dest) = (src)
105# endif
106# endif
107#endif
108
Damien Miller168e6ac2000-07-11 12:23:01 +1000109#if !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF)
Damien Miller42b81ff1999-11-26 12:21:24 +1100110
Damien Miller57f39152005-11-24 19:58:19 +1100111#ifdef HAVE_LONG_DOUBLE
112# define LDOUBLE long double
113#else
114# define LDOUBLE double
115#endif
Damien Miller168e6ac2000-07-11 12:23:01 +1000116
Damien Miller57f39152005-11-24 19:58:19 +1100117#ifdef HAVE_LONG_LONG
118# define LLONG long long
119#else
120# define LLONG long
121#endif
Ben Lindstrom116b6bd2001-02-13 14:05:59 +0000122
Damien Miller168e6ac2000-07-11 12:23:01 +1000123/*
124 * dopr(): poor man's version of doprintf
125 */
126
127/* format read states */
128#define DP_S_DEFAULT 0
129#define DP_S_FLAGS 1
130#define DP_S_MIN 2
131#define DP_S_DOT 3
132#define DP_S_MAX 4
133#define DP_S_MOD 5
134#define DP_S_CONV 6
135#define DP_S_DONE 7
136
137/* format flags - Bits */
138#define DP_F_MINUS (1 << 0)
139#define DP_F_PLUS (1 << 1)
140#define DP_F_SPACE (1 << 2)
141#define DP_F_NUM (1 << 3)
142#define DP_F_ZERO (1 << 4)
143#define DP_F_UP (1 << 5)
144#define DP_F_UNSIGNED (1 << 6)
145
146/* Conversion Flags */
Damien Miller57f39152005-11-24 19:58:19 +1100147#define DP_C_SHORT 1
148#define DP_C_LONG 2
149#define DP_C_LDOUBLE 3
150#define DP_C_LLONG 4
Damien Miller168e6ac2000-07-11 12:23:01 +1000151
Damien Miller57f39152005-11-24 19:58:19 +1100152#define char_to_int(p) ((p)- '0')
153#ifndef MAX
154# define MAX(p,q) (((p) >= (q)) ? (p) : (q))
155#endif
Damien Miller168e6ac2000-07-11 12:23:01 +1000156
Damien Miller57f39152005-11-24 19:58:19 +1100157static size_t dopr(char *buffer, size_t maxlen, const char *format,
158 va_list args_in);
159static void fmtstr(char *buffer, size_t *currlen, size_t maxlen,
160 char *value, int flags, int min, int max);
161static void fmtint(char *buffer, size_t *currlen, size_t maxlen,
Darren Tucker9834cab2006-03-19 00:07:07 +1100162 LLONG value, int base, int min, int max, int flags);
Damien Miller57f39152005-11-24 19:58:19 +1100163static void fmtfp(char *buffer, size_t *currlen, size_t maxlen,
164 LDOUBLE fvalue, int min, int max, int flags);
165static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c);
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000166
Damien Miller57f39152005-11-24 19:58:19 +1100167static size_t dopr(char *buffer, size_t maxlen, const char *format, va_list args_in)
Damien Miller13bc0be1999-12-28 10:19:16 +1100168{
Damien Miller57f39152005-11-24 19:58:19 +1100169 char ch;
170 LLONG value;
171 LDOUBLE fvalue;
172 char *strvalue;
173 int min;
174 int max;
175 int state;
176 int flags;
177 int cflags;
178 size_t currlen;
179 va_list args;
Damien Miller13bc0be1999-12-28 10:19:16 +1100180
Damien Miller57f39152005-11-24 19:58:19 +1100181 VA_COPY(args, args_in);
182
183 state = DP_S_DEFAULT;
184 currlen = flags = cflags = min = 0;
185 max = -1;
186 ch = *format++;
187
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000188 while (state != DP_S_DONE) {
Damien Miller57f39152005-11-24 19:58:19 +1100189 if (ch == '\0')
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000190 state = DP_S_DONE;
Damien Miller42b81ff1999-11-26 12:21:24 +1100191
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000192 switch(state) {
Damien Miller31741252003-05-19 00:13:38 +1000193 case DP_S_DEFAULT:
194 if (ch == '%')
195 state = DP_S_FLAGS;
196 else
Damien Miller57f39152005-11-24 19:58:19 +1100197 dopr_outch (buffer, &currlen, maxlen, ch);
Damien Miller31741252003-05-19 00:13:38 +1000198 ch = *format++;
199 break;
200 case DP_S_FLAGS:
201 switch (ch) {
202 case '-':
203 flags |= DP_F_MINUS;
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000204 ch = *format++;
205 break;
Damien Miller31741252003-05-19 00:13:38 +1000206 case '+':
207 flags |= DP_F_PLUS;
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000208 ch = *format++;
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000209 break;
Damien Miller31741252003-05-19 00:13:38 +1000210 case ' ':
211 flags |= DP_F_SPACE;
212 ch = *format++;
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000213 break;
Damien Miller31741252003-05-19 00:13:38 +1000214 case '#':
215 flags |= DP_F_NUM;
216 ch = *format++;
217 break;
218 case '0':
219 flags |= DP_F_ZERO;
220 ch = *format++;
221 break;
222 default:
223 state = DP_S_MIN;
224 break;
225 }
226 break;
227 case DP_S_MIN:
228 if (isdigit((unsigned char)ch)) {
Damien Miller57f39152005-11-24 19:58:19 +1100229 min = 10*min + char_to_int (ch);
Damien Miller31741252003-05-19 00:13:38 +1000230 ch = *format++;
231 } else if (ch == '*') {
232 min = va_arg (args, int);
233 ch = *format++;
234 state = DP_S_DOT;
Damien Miller57f39152005-11-24 19:58:19 +1100235 } else {
Damien Miller31741252003-05-19 00:13:38 +1000236 state = DP_S_DOT;
Damien Miller57f39152005-11-24 19:58:19 +1100237 }
Damien Miller31741252003-05-19 00:13:38 +1000238 break;
239 case DP_S_DOT:
240 if (ch == '.') {
241 state = DP_S_MAX;
242 ch = *format++;
Damien Miller57f39152005-11-24 19:58:19 +1100243 } else {
Damien Miller31741252003-05-19 00:13:38 +1000244 state = DP_S_MOD;
Damien Miller57f39152005-11-24 19:58:19 +1100245 }
Damien Miller31741252003-05-19 00:13:38 +1000246 break;
247 case DP_S_MAX:
248 if (isdigit((unsigned char)ch)) {
249 if (max < 0)
250 max = 0;
Damien Miller57f39152005-11-24 19:58:19 +1100251 max = 10*max + char_to_int (ch);
Damien Miller31741252003-05-19 00:13:38 +1000252 ch = *format++;
253 } else if (ch == '*') {
254 max = va_arg (args, int);
255 ch = *format++;
256 state = DP_S_MOD;
Damien Miller57f39152005-11-24 19:58:19 +1100257 } else {
Damien Miller31741252003-05-19 00:13:38 +1000258 state = DP_S_MOD;
Damien Miller57f39152005-11-24 19:58:19 +1100259 }
Damien Miller31741252003-05-19 00:13:38 +1000260 break;
261 case DP_S_MOD:
262 switch (ch) {
263 case 'h':
264 cflags = DP_C_SHORT;
265 ch = *format++;
266 break;
267 case 'l':
268 cflags = DP_C_LONG;
269 ch = *format++;
Damien Miller57f39152005-11-24 19:58:19 +1100270 if (ch == 'l') { /* It's a long long */
271 cflags = DP_C_LLONG;
Damien Miller31741252003-05-19 00:13:38 +1000272 ch = *format++;
273 }
274 break;
Damien Miller31741252003-05-19 00:13:38 +1000275 case 'L':
276 cflags = DP_C_LDOUBLE;
277 ch = *format++;
278 break;
279 default:
280 break;
281 }
282 state = DP_S_CONV;
283 break;
284 case DP_S_CONV:
285 switch (ch) {
286 case 'd':
287 case 'i':
288 if (cflags == DP_C_SHORT)
Damien Miller57f39152005-11-24 19:58:19 +1100289 value = va_arg (args, int);
Damien Miller31741252003-05-19 00:13:38 +1000290 else if (cflags == DP_C_LONG)
Damien Miller57f39152005-11-24 19:58:19 +1100291 value = va_arg (args, long int);
292 else if (cflags == DP_C_LLONG)
293 value = va_arg (args, LLONG);
Damien Miller31741252003-05-19 00:13:38 +1000294 else
295 value = va_arg (args, int);
Damien Miller57f39152005-11-24 19:58:19 +1100296 fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
Damien Miller31741252003-05-19 00:13:38 +1000297 break;
298 case 'o':
299 flags |= DP_F_UNSIGNED;
300 if (cflags == DP_C_SHORT)
Damien Miller57f39152005-11-24 19:58:19 +1100301 value = va_arg (args, unsigned int);
Damien Miller31741252003-05-19 00:13:38 +1000302 else if (cflags == DP_C_LONG)
Damien Miller57f39152005-11-24 19:58:19 +1100303 value = (long)va_arg (args, unsigned long int);
304 else if (cflags == DP_C_LLONG)
305 value = (long)va_arg (args, unsigned LLONG);
Damien Miller31741252003-05-19 00:13:38 +1000306 else
Damien Miller57f39152005-11-24 19:58:19 +1100307 value = (long)va_arg (args, unsigned int);
308 fmtint (buffer, &currlen, maxlen, value, 8, min, max, flags);
Damien Miller31741252003-05-19 00:13:38 +1000309 break;
310 case 'u':
311 flags |= DP_F_UNSIGNED;
312 if (cflags == DP_C_SHORT)
Damien Miller57f39152005-11-24 19:58:19 +1100313 value = va_arg (args, unsigned int);
Damien Miller31741252003-05-19 00:13:38 +1000314 else if (cflags == DP_C_LONG)
Damien Miller57f39152005-11-24 19:58:19 +1100315 value = (long)va_arg (args, unsigned long int);
316 else if (cflags == DP_C_LLONG)
317 value = (LLONG)va_arg (args, unsigned LLONG);
Damien Miller31741252003-05-19 00:13:38 +1000318 else
Damien Miller57f39152005-11-24 19:58:19 +1100319 value = (long)va_arg (args, unsigned int);
Damien Miller31741252003-05-19 00:13:38 +1000320 fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
321 break;
322 case 'X':
323 flags |= DP_F_UP;
324 case 'x':
325 flags |= DP_F_UNSIGNED;
326 if (cflags == DP_C_SHORT)
Damien Miller57f39152005-11-24 19:58:19 +1100327 value = va_arg (args, unsigned int);
Damien Miller31741252003-05-19 00:13:38 +1000328 else if (cflags == DP_C_LONG)
Damien Miller57f39152005-11-24 19:58:19 +1100329 value = (long)va_arg (args, unsigned long int);
330 else if (cflags == DP_C_LLONG)
331 value = (LLONG)va_arg (args, unsigned LLONG);
Damien Miller31741252003-05-19 00:13:38 +1000332 else
Damien Miller57f39152005-11-24 19:58:19 +1100333 value = (long)va_arg (args, unsigned int);
334 fmtint (buffer, &currlen, maxlen, value, 16, min, max, flags);
Damien Miller31741252003-05-19 00:13:38 +1000335 break;
336 case 'f':
337 if (cflags == DP_C_LDOUBLE)
Damien Miller57f39152005-11-24 19:58:19 +1100338 fvalue = va_arg (args, LDOUBLE);
Damien Miller31741252003-05-19 00:13:38 +1000339 else
Damien Miller57f39152005-11-24 19:58:19 +1100340 fvalue = va_arg (args, double);
Damien Miller31741252003-05-19 00:13:38 +1000341 /* um, floating point? */
Damien Miller57f39152005-11-24 19:58:19 +1100342 fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags);
Damien Miller31741252003-05-19 00:13:38 +1000343 break;
344 case 'E':
345 flags |= DP_F_UP;
346 case 'e':
347 if (cflags == DP_C_LDOUBLE)
Damien Miller57f39152005-11-24 19:58:19 +1100348 fvalue = va_arg (args, LDOUBLE);
Damien Miller31741252003-05-19 00:13:38 +1000349 else
Damien Miller57f39152005-11-24 19:58:19 +1100350 fvalue = va_arg (args, double);
351 fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags);
Damien Miller31741252003-05-19 00:13:38 +1000352 break;
353 case 'G':
354 flags |= DP_F_UP;
355 case 'g':
356 if (cflags == DP_C_LDOUBLE)
Damien Miller57f39152005-11-24 19:58:19 +1100357 fvalue = va_arg (args, LDOUBLE);
Damien Miller31741252003-05-19 00:13:38 +1000358 else
Damien Miller57f39152005-11-24 19:58:19 +1100359 fvalue = va_arg (args, double);
360 fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags);
Damien Miller31741252003-05-19 00:13:38 +1000361 break;
362 case 'c':
Damien Miller57f39152005-11-24 19:58:19 +1100363 dopr_outch (buffer, &currlen, maxlen, va_arg (args, int));
Damien Miller31741252003-05-19 00:13:38 +1000364 break;
365 case 's':
Damien Miller57f39152005-11-24 19:58:19 +1100366 strvalue = va_arg (args, char *);
367 if (!strvalue) strvalue = "(NULL)";
368 if (max == -1) {
369 max = strlen(strvalue);
370 }
371 if (min > 0 && max >= 0 && min > max) max = min;
372 fmtstr (buffer, &currlen, maxlen, strvalue, flags, min, max);
Damien Miller31741252003-05-19 00:13:38 +1000373 break;
374 case 'p':
Damien Miller57f39152005-11-24 19:58:19 +1100375 strvalue = va_arg (args, void *);
376 fmtint (buffer, &currlen, maxlen, (long) strvalue, 16, min, max, flags);
Damien Miller31741252003-05-19 00:13:38 +1000377 break;
378 case 'n':
379 if (cflags == DP_C_SHORT) {
380 short int *num;
Damien Miller57f39152005-11-24 19:58:19 +1100381 num = va_arg (args, short int *);
Damien Miller31741252003-05-19 00:13:38 +1000382 *num = currlen;
383 } else if (cflags == DP_C_LONG) {
384 long int *num;
Damien Miller57f39152005-11-24 19:58:19 +1100385 num = va_arg (args, long int *);
386 *num = (long int)currlen;
387 } else if (cflags == DP_C_LLONG) {
388 LLONG *num;
389 num = va_arg (args, LLONG *);
390 *num = (LLONG)currlen;
Damien Miller31741252003-05-19 00:13:38 +1000391 } else {
392 int *num;
Damien Miller57f39152005-11-24 19:58:19 +1100393 num = va_arg (args, int *);
Damien Miller31741252003-05-19 00:13:38 +1000394 *num = currlen;
395 }
396 break;
397 case '%':
Damien Miller57f39152005-11-24 19:58:19 +1100398 dopr_outch (buffer, &currlen, maxlen, ch);
Damien Miller31741252003-05-19 00:13:38 +1000399 break;
Damien Miller57f39152005-11-24 19:58:19 +1100400 case 'w':
401 /* not supported yet, treat as next char */
Damien Miller31741252003-05-19 00:13:38 +1000402 ch = *format++;
403 break;
Damien Miller57f39152005-11-24 19:58:19 +1100404 default:
405 /* Unknown, skip */
406 break;
Damien Miller31741252003-05-19 00:13:38 +1000407 }
408 ch = *format++;
409 state = DP_S_DEFAULT;
410 flags = cflags = min = 0;
411 max = -1;
412 break;
413 case DP_S_DONE:
414 break;
Damien Miller57f39152005-11-24 19:58:19 +1100415 default:
416 /* hmm? */
Damien Miller31741252003-05-19 00:13:38 +1000417 break; /* some picky compilers need this */
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000418 }
419 }
Damien Miller57f39152005-11-24 19:58:19 +1100420 if (maxlen != 0) {
421 if (currlen < maxlen - 1)
422 buffer[currlen] = '\0';
423 else if (maxlen > 0)
424 buffer[maxlen - 1] = '\0';
425 }
426
427 return currlen;
Damien Miller42b81ff1999-11-26 12:21:24 +1100428}
429
Damien Miller57f39152005-11-24 19:58:19 +1100430static void fmtstr(char *buffer, size_t *currlen, size_t maxlen,
431 char *value, int flags, int min, int max)
Damien Miller42b81ff1999-11-26 12:21:24 +1100432{
Damien Miller57f39152005-11-24 19:58:19 +1100433 int padlen, strln; /* amount to pad */
434 int cnt = 0;
435
436#ifdef DEBUG_SNPRINTF
437 printf("fmtstr min=%d max=%d s=[%s]\n", min, max, value);
438#endif
439 if (value == 0) {
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000440 value = "<NULL>";
Damien Miller57f39152005-11-24 19:58:19 +1100441 }
Damien Miller168e6ac2000-07-11 12:23:01 +1000442
Darren Tucker4127f552004-09-23 21:35:09 +1000443 for (strln = 0; strln < max && value[strln]; ++strln); /* strlen */
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000444 padlen = min - strln;
445 if (padlen < 0)
446 padlen = 0;
447 if (flags & DP_F_MINUS)
448 padlen = -padlen; /* Left Justify */
Damien Miller57f39152005-11-24 19:58:19 +1100449
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000450 while ((padlen > 0) && (cnt < max)) {
Damien Miller57f39152005-11-24 19:58:19 +1100451 dopr_outch (buffer, currlen, maxlen, ' ');
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000452 --padlen;
453 ++cnt;
454 }
455 while (*value && (cnt < max)) {
Damien Miller57f39152005-11-24 19:58:19 +1100456 dopr_outch (buffer, currlen, maxlen, *value++);
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000457 ++cnt;
458 }
459 while ((padlen < 0) && (cnt < max)) {
Damien Miller57f39152005-11-24 19:58:19 +1100460 dopr_outch (buffer, currlen, maxlen, ' ');
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000461 ++padlen;
462 ++cnt;
463 }
Damien Miller42b81ff1999-11-26 12:21:24 +1100464}
465
Damien Miller168e6ac2000-07-11 12:23:01 +1000466/* Have to handle DP_F_NUM (ie 0x and 0 alternates) */
467
Damien Miller57f39152005-11-24 19:58:19 +1100468static void fmtint(char *buffer, size_t *currlen, size_t maxlen,
Darren Tucker9834cab2006-03-19 00:07:07 +1100469 LLONG value, int base, int min, int max, int flags)
Damien Miller42b81ff1999-11-26 12:21:24 +1100470{
Damien Miller57f39152005-11-24 19:58:19 +1100471 int signvalue = 0;
Darren Tucker9834cab2006-03-19 00:07:07 +1100472 unsigned LLONG uvalue;
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000473 char convert[20];
Damien Miller57f39152005-11-24 19:58:19 +1100474 int place = 0;
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000475 int spadlen = 0; /* amount to space pad */
476 int zpadlen = 0; /* amount to zero pad */
Damien Miller57f39152005-11-24 19:58:19 +1100477 int caps = 0;
478
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000479 if (max < 0)
480 max = 0;
Damien Miller57f39152005-11-24 19:58:19 +1100481
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000482 uvalue = value;
Damien Miller57f39152005-11-24 19:58:19 +1100483
484 if(!(flags & DP_F_UNSIGNED)) {
485 if( value < 0 ) {
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000486 signvalue = '-';
487 uvalue = -value;
Damien Miller57f39152005-11-24 19:58:19 +1100488 } else {
489 if (flags & DP_F_PLUS) /* Do a sign (+/i) */
490 signvalue = '+';
491 else if (flags & DP_F_SPACE)
492 signvalue = ' ';
493 }
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000494 }
Damien Miller168e6ac2000-07-11 12:23:01 +1000495
Damien Miller57f39152005-11-24 19:58:19 +1100496 if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
497
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000498 do {
499 convert[place++] =
Damien Miller57f39152005-11-24 19:58:19 +1100500 (caps? "0123456789ABCDEF":"0123456789abcdef")
501 [uvalue % (unsigned)base ];
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000502 uvalue = (uvalue / (unsigned)base );
Damien Miller57f39152005-11-24 19:58:19 +1100503 } while(uvalue && (place < 20));
504 if (place == 20) place--;
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000505 convert[place] = 0;
Damien Miller168e6ac2000-07-11 12:23:01 +1000506
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000507 zpadlen = max - place;
508 spadlen = min - MAX (max, place) - (signvalue ? 1 : 0);
Damien Miller57f39152005-11-24 19:58:19 +1100509 if (zpadlen < 0) zpadlen = 0;
510 if (spadlen < 0) spadlen = 0;
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000511 if (flags & DP_F_ZERO) {
512 zpadlen = MAX(zpadlen, spadlen);
513 spadlen = 0;
514 }
515 if (flags & DP_F_MINUS)
516 spadlen = -spadlen; /* Left Justifty */
Damien Miller168e6ac2000-07-11 12:23:01 +1000517
Damien Miller57f39152005-11-24 19:58:19 +1100518#ifdef DEBUG_SNPRINTF
519 printf("zpad: %d, spad: %d, min: %d, max: %d, place: %d\n",
520 zpadlen, spadlen, min, max, place);
521#endif
522
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000523 /* Spaces */
524 while (spadlen > 0) {
Damien Miller57f39152005-11-24 19:58:19 +1100525 dopr_outch (buffer, currlen, maxlen, ' ');
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000526 --spadlen;
527 }
Damien Miller168e6ac2000-07-11 12:23:01 +1000528
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000529 /* Sign */
530 if (signvalue)
Damien Miller57f39152005-11-24 19:58:19 +1100531 dopr_outch (buffer, currlen, maxlen, signvalue);
Damien Miller168e6ac2000-07-11 12:23:01 +1000532
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000533 /* Zeros */
534 if (zpadlen > 0) {
535 while (zpadlen > 0) {
Damien Miller57f39152005-11-24 19:58:19 +1100536 dopr_outch (buffer, currlen, maxlen, '0');
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000537 --zpadlen;
538 }
539 }
Damien Miller168e6ac2000-07-11 12:23:01 +1000540
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000541 /* Digits */
542 while (place > 0)
Damien Miller57f39152005-11-24 19:58:19 +1100543 dopr_outch (buffer, currlen, maxlen, convert[--place]);
Damien Miller168e6ac2000-07-11 12:23:01 +1000544
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000545 /* Left Justified spaces */
546 while (spadlen < 0) {
547 dopr_outch (buffer, currlen, maxlen, ' ');
548 ++spadlen;
549 }
Damien Miller42b81ff1999-11-26 12:21:24 +1100550}
551
Damien Miller57f39152005-11-24 19:58:19 +1100552static LDOUBLE abs_val(LDOUBLE value)
Damien Miller42b81ff1999-11-26 12:21:24 +1100553{
Damien Miller57f39152005-11-24 19:58:19 +1100554 LDOUBLE result = value;
Damien Miller42b81ff1999-11-26 12:21:24 +1100555
Damien Miller57f39152005-11-24 19:58:19 +1100556 if (value < 0)
557 result = -value;
558
559 return result;
560}
561
562static LDOUBLE POW10(int exp)
563{
564 LDOUBLE result = 1;
565
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000566 while (exp) {
567 result *= 10;
568 exp--;
569 }
Damien Miller168e6ac2000-07-11 12:23:01 +1000570
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000571 return result;
Damien Miller168e6ac2000-07-11 12:23:01 +1000572}
573
Damien Miller57f39152005-11-24 19:58:19 +1100574static LLONG ROUND(LDOUBLE value)
Damien Miller168e6ac2000-07-11 12:23:01 +1000575{
Damien Miller57f39152005-11-24 19:58:19 +1100576 LLONG intpart;
Damien Miller168e6ac2000-07-11 12:23:01 +1000577
Damien Miller57f39152005-11-24 19:58:19 +1100578 intpart = (LLONG)value;
579 value = value - intpart;
580 if (value >= 0.5) intpart++;
581
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000582 return intpart;
Damien Miller168e6ac2000-07-11 12:23:01 +1000583}
584
Damien Miller57f39152005-11-24 19:58:19 +1100585/* a replacement for modf that doesn't need the math library. Should
586 be portable, but slow */
587static double my_modf(double x0, double *iptr)
Damien Miller168e6ac2000-07-11 12:23:01 +1000588{
Damien Miller57f39152005-11-24 19:58:19 +1100589 int i;
590 long l;
591 double x = x0;
592 double f = 1.0;
593
594 for (i=0;i<100;i++) {
595 l = (long)x;
596 if (l <= (x+1) && l >= (x-1)) break;
597 x *= 0.1;
598 f *= 10.0;
599 }
600
601 if (i == 100) {
602 /* yikes! the number is beyond what we can handle. What do we do? */
603 (*iptr) = 0;
604 return 0;
605 }
606
607 if (i != 0) {
608 double i2;
609 double ret;
610
611 ret = my_modf(x0-l*f, &i2);
612 (*iptr) = l*f + i2;
613 return ret;
614 }
615
616 (*iptr) = l;
617 return x - (*iptr);
618}
619
620
621static void fmtfp (char *buffer, size_t *currlen, size_t maxlen,
622 LDOUBLE fvalue, int min, int max, int flags)
623{
624 int signvalue = 0;
625 double ufvalue;
626 char iconvert[311];
627 char fconvert[311];
628 int iplace = 0;
629 int fplace = 0;
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000630 int padlen = 0; /* amount to pad */
Damien Miller57f39152005-11-24 19:58:19 +1100631 int zpadlen = 0;
632 int caps = 0;
633 int idx;
634 double intpart;
635 double fracpart;
636 double temp;
Damien Miller168e6ac2000-07-11 12:23:01 +1000637
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000638 /*
639 * AIX manpage says the default is 0, but Solaris says the default
640 * is 6, and sprintf on AIX defaults to 6
641 */
642 if (max < 0)
643 max = 6;
Damien Miller168e6ac2000-07-11 12:23:01 +1000644
Damien Miller57f39152005-11-24 19:58:19 +1100645 ufvalue = abs_val (fvalue);
Damien Miller168e6ac2000-07-11 12:23:01 +1000646
Damien Miller57f39152005-11-24 19:58:19 +1100647 if (fvalue < 0) {
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000648 signvalue = '-';
Damien Miller57f39152005-11-24 19:58:19 +1100649 } else {
650 if (flags & DP_F_PLUS) { /* Do a sign (+/i) */
651 signvalue = '+';
652 } else {
653 if (flags & DP_F_SPACE)
654 signvalue = ' ';
655 }
656 }
Damien Miller168e6ac2000-07-11 12:23:01 +1000657
Damien Miller57f39152005-11-24 19:58:19 +1100658#if 0
659 if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
660#endif
661
662#if 0
663 if (max == 0) ufvalue += 0.5; /* if max = 0 we must round */
664#endif
Damien Miller168e6ac2000-07-11 12:23:01 +1000665
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000666 /*
Damien Miller57f39152005-11-24 19:58:19 +1100667 * Sorry, we only support 16 digits past the decimal because of our
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000668 * conversion method
669 */
Damien Miller57f39152005-11-24 19:58:19 +1100670 if (max > 16)
671 max = 16;
Damien Miller168e6ac2000-07-11 12:23:01 +1000672
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000673 /* We "cheat" by converting the fractional part to integer by
674 * multiplying by a factor of 10
675 */
Damien Miller168e6ac2000-07-11 12:23:01 +1000676
Damien Miller57f39152005-11-24 19:58:19 +1100677 temp = ufvalue;
678 my_modf(temp, &intpart);
679
680 fracpart = ROUND((POW10(max)) * (ufvalue - intpart));
681
682 if (fracpart >= POW10(max)) {
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000683 intpart++;
Damien Miller57f39152005-11-24 19:58:19 +1100684 fracpart -= POW10(max);
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000685 }
Damien Miller168e6ac2000-07-11 12:23:01 +1000686
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000687 /* Convert integer part */
688 do {
Damien Miller57f39152005-11-24 19:58:19 +1100689 temp = intpart*0.1;
690 my_modf(temp, &intpart);
691 idx = (int) ((temp -intpart +0.05)* 10.0);
692 /* idx = (int) (((double)(temp*0.1) -intpart +0.05) *10.0); */
693 /* printf ("%llf, %f, %x\n", temp, intpart, idx); */
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000694 iconvert[iplace++] =
Damien Miller57f39152005-11-24 19:58:19 +1100695 (caps? "0123456789ABCDEF":"0123456789abcdef")[idx];
696 } while (intpart && (iplace < 311));
697 if (iplace == 311) iplace--;
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000698 iconvert[iplace] = 0;
Damien Miller168e6ac2000-07-11 12:23:01 +1000699
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000700 /* Convert fractional part */
Damien Miller57f39152005-11-24 19:58:19 +1100701 if (fracpart)
702 {
703 do {
704 temp = fracpart*0.1;
705 my_modf(temp, &fracpart);
706 idx = (int) ((temp -fracpart +0.05)* 10.0);
707 /* idx = (int) ((((temp/10) -fracpart) +0.05) *10); */
708 /* printf ("%lf, %lf, %ld\n", temp, fracpart, idx ); */
709 fconvert[fplace++] =
710 (caps? "0123456789ABCDEF":"0123456789abcdef")[idx];
711 } while(fracpart && (fplace < 311));
712 if (fplace == 311) fplace--;
713 }
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000714 fconvert[fplace] = 0;
Damien Miller57f39152005-11-24 19:58:19 +1100715
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000716 /* -1 for decimal point, another -1 if we are printing a sign */
717 padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
718 zpadlen = max - fplace;
Damien Miller57f39152005-11-24 19:58:19 +1100719 if (zpadlen < 0) zpadlen = 0;
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000720 if (padlen < 0)
721 padlen = 0;
722 if (flags & DP_F_MINUS)
723 padlen = -padlen; /* Left Justifty */
Damien Miller57f39152005-11-24 19:58:19 +1100724
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000725 if ((flags & DP_F_ZERO) && (padlen > 0)) {
726 if (signvalue) {
Damien Miller57f39152005-11-24 19:58:19 +1100727 dopr_outch (buffer, currlen, maxlen, signvalue);
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000728 --padlen;
729 signvalue = 0;
730 }
731 while (padlen > 0) {
Damien Miller57f39152005-11-24 19:58:19 +1100732 dopr_outch (buffer, currlen, maxlen, '0');
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000733 --padlen;
734 }
735 }
736 while (padlen > 0) {
Damien Miller57f39152005-11-24 19:58:19 +1100737 dopr_outch (buffer, currlen, maxlen, ' ');
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000738 --padlen;
739 }
740 if (signvalue)
Damien Miller57f39152005-11-24 19:58:19 +1100741 dopr_outch (buffer, currlen, maxlen, signvalue);
742
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000743 while (iplace > 0)
Damien Miller57f39152005-11-24 19:58:19 +1100744 dopr_outch (buffer, currlen, maxlen, iconvert[--iplace]);
745
746#ifdef DEBUG_SNPRINTF
747 printf("fmtfp: fplace=%d zpadlen=%d\n", fplace, zpadlen);
748#endif
Damien Miller168e6ac2000-07-11 12:23:01 +1000749
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000750 /*
Damien Miller57f39152005-11-24 19:58:19 +1100751 * Decimal point. This should probably use locale to find the correct
752 * char to print out.
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000753 */
Damien Miller57f39152005-11-24 19:58:19 +1100754 if (max > 0) {
755 dopr_outch (buffer, currlen, maxlen, '.');
756
757 while (zpadlen > 0) {
758 dopr_outch (buffer, currlen, maxlen, '0');
759 --zpadlen;
760 }
Damien Miller168e6ac2000-07-11 12:23:01 +1000761
Damien Miller57f39152005-11-24 19:58:19 +1100762 while (fplace > 0)
763 dopr_outch (buffer, currlen, maxlen, fconvert[--fplace]);
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000764 }
Damien Miller168e6ac2000-07-11 12:23:01 +1000765
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000766 while (padlen < 0) {
Damien Miller57f39152005-11-24 19:58:19 +1100767 dopr_outch (buffer, currlen, maxlen, ' ');
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000768 ++padlen;
769 }
Damien Miller168e6ac2000-07-11 12:23:01 +1000770}
771
Damien Miller57f39152005-11-24 19:58:19 +1100772static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c)
Damien Miller168e6ac2000-07-11 12:23:01 +1000773{
Damien Miller57f39152005-11-24 19:58:19 +1100774 if (*currlen < maxlen) {
775 buffer[(*currlen)] = c;
776 }
777 (*currlen)++;
Damien Miller168e6ac2000-07-11 12:23:01 +1000778}
779#endif /* !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF) */
780
Damien Miller57f39152005-11-24 19:58:19 +1100781#if !defined(HAVE_VSNPRINTF)
782int vsnprintf (char *str, size_t count, const char *fmt, va_list args)
Damien Miller168e6ac2000-07-11 12:23:01 +1000783{
Damien Miller57f39152005-11-24 19:58:19 +1100784 return dopr(str, count, fmt, args);
Damien Miller168e6ac2000-07-11 12:23:01 +1000785}
Damien Miller57f39152005-11-24 19:58:19 +1100786#endif
Damien Miller168e6ac2000-07-11 12:23:01 +1000787
Damien Miller57f39152005-11-24 19:58:19 +1100788#if !defined(HAVE_SNPRINTF)
Darren Tuckerd40c66c2005-12-17 22:32:03 +1100789int snprintf(char *str, size_t count, SNPRINTF_CONST char *fmt, ...)
Damien Millerc6b3bbe1999-12-13 08:27:33 +1100790{
Damien Miller57f39152005-11-24 19:58:19 +1100791 size_t ret;
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000792 va_list ap;
793
794 va_start(ap, fmt);
Damien Miller57f39152005-11-24 19:58:19 +1100795 ret = vsnprintf(str, count, fmt, ap);
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000796 va_end(ap);
Damien Miller57f39152005-11-24 19:58:19 +1100797 return ret;
Damien Millerc6b3bbe1999-12-13 08:27:33 +1100798}
Damien Miller57f39152005-11-24 19:58:19 +1100799#endif
Damien Millerc6b3bbe1999-12-13 08:27:33 +1100800