blob: 47cbcff67f46114455343f9fae4405388738627c [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
Darren Tuckerf78fb542006-08-06 21:25:24 +1000111#include <ctype.h>
112#include <stdlib.h>
Damien Miller62da44f2006-07-24 15:08:35 +1000113#include <string.h>
114
Damien Miller57f39152005-11-24 19:58:19 +1100115#ifdef HAVE_LONG_DOUBLE
116# define LDOUBLE long double
117#else
118# define LDOUBLE double
119#endif
Damien Miller168e6ac2000-07-11 12:23:01 +1000120
Damien Miller57f39152005-11-24 19:58:19 +1100121#ifdef HAVE_LONG_LONG
122# define LLONG long long
123#else
124# define LLONG long
125#endif
Ben Lindstrom116b6bd2001-02-13 14:05:59 +0000126
Damien Miller168e6ac2000-07-11 12:23:01 +1000127/*
128 * dopr(): poor man's version of doprintf
129 */
130
131/* format read states */
132#define DP_S_DEFAULT 0
133#define DP_S_FLAGS 1
134#define DP_S_MIN 2
135#define DP_S_DOT 3
136#define DP_S_MAX 4
137#define DP_S_MOD 5
138#define DP_S_CONV 6
139#define DP_S_DONE 7
140
141/* format flags - Bits */
142#define DP_F_MINUS (1 << 0)
143#define DP_F_PLUS (1 << 1)
144#define DP_F_SPACE (1 << 2)
145#define DP_F_NUM (1 << 3)
146#define DP_F_ZERO (1 << 4)
147#define DP_F_UP (1 << 5)
148#define DP_F_UNSIGNED (1 << 6)
149
150/* Conversion Flags */
Damien Miller57f39152005-11-24 19:58:19 +1100151#define DP_C_SHORT 1
152#define DP_C_LONG 2
153#define DP_C_LDOUBLE 3
154#define DP_C_LLONG 4
Damien Miller168e6ac2000-07-11 12:23:01 +1000155
Damien Miller57f39152005-11-24 19:58:19 +1100156#define char_to_int(p) ((p)- '0')
157#ifndef MAX
158# define MAX(p,q) (((p) >= (q)) ? (p) : (q))
159#endif
Damien Miller168e6ac2000-07-11 12:23:01 +1000160
Damien Miller57f39152005-11-24 19:58:19 +1100161static size_t dopr(char *buffer, size_t maxlen, const char *format,
162 va_list args_in);
163static void fmtstr(char *buffer, size_t *currlen, size_t maxlen,
164 char *value, int flags, int min, int max);
165static void fmtint(char *buffer, size_t *currlen, size_t maxlen,
Darren Tucker9834cab2006-03-19 00:07:07 +1100166 LLONG value, int base, int min, int max, int flags);
Damien Miller57f39152005-11-24 19:58:19 +1100167static void fmtfp(char *buffer, size_t *currlen, size_t maxlen,
168 LDOUBLE fvalue, int min, int max, int flags);
169static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c);
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000170
Damien Miller57f39152005-11-24 19:58:19 +1100171static size_t dopr(char *buffer, size_t maxlen, const char *format, va_list args_in)
Damien Miller13bc0be1999-12-28 10:19:16 +1100172{
Damien Miller57f39152005-11-24 19:58:19 +1100173 char ch;
174 LLONG value;
175 LDOUBLE fvalue;
176 char *strvalue;
177 int min;
178 int max;
179 int state;
180 int flags;
181 int cflags;
182 size_t currlen;
183 va_list args;
Damien Miller13bc0be1999-12-28 10:19:16 +1100184
Damien Miller57f39152005-11-24 19:58:19 +1100185 VA_COPY(args, args_in);
186
187 state = DP_S_DEFAULT;
188 currlen = flags = cflags = min = 0;
189 max = -1;
190 ch = *format++;
191
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000192 while (state != DP_S_DONE) {
Damien Miller57f39152005-11-24 19:58:19 +1100193 if (ch == '\0')
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000194 state = DP_S_DONE;
Damien Miller42b81ff1999-11-26 12:21:24 +1100195
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000196 switch(state) {
Damien Miller31741252003-05-19 00:13:38 +1000197 case DP_S_DEFAULT:
198 if (ch == '%')
199 state = DP_S_FLAGS;
200 else
Damien Miller57f39152005-11-24 19:58:19 +1100201 dopr_outch (buffer, &currlen, maxlen, ch);
Damien Miller31741252003-05-19 00:13:38 +1000202 ch = *format++;
203 break;
204 case DP_S_FLAGS:
205 switch (ch) {
206 case '-':
207 flags |= DP_F_MINUS;
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000208 ch = *format++;
209 break;
Damien Miller31741252003-05-19 00:13:38 +1000210 case '+':
211 flags |= DP_F_PLUS;
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000212 ch = *format++;
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000213 break;
Damien Miller31741252003-05-19 00:13:38 +1000214 case ' ':
215 flags |= DP_F_SPACE;
216 ch = *format++;
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000217 break;
Damien Miller31741252003-05-19 00:13:38 +1000218 case '#':
219 flags |= DP_F_NUM;
220 ch = *format++;
221 break;
222 case '0':
223 flags |= DP_F_ZERO;
224 ch = *format++;
225 break;
226 default:
227 state = DP_S_MIN;
228 break;
229 }
230 break;
231 case DP_S_MIN:
232 if (isdigit((unsigned char)ch)) {
Damien Miller57f39152005-11-24 19:58:19 +1100233 min = 10*min + char_to_int (ch);
Damien Miller31741252003-05-19 00:13:38 +1000234 ch = *format++;
235 } else if (ch == '*') {
236 min = va_arg (args, int);
237 ch = *format++;
238 state = DP_S_DOT;
Damien Miller57f39152005-11-24 19:58:19 +1100239 } else {
Damien Miller31741252003-05-19 00:13:38 +1000240 state = DP_S_DOT;
Damien Miller57f39152005-11-24 19:58:19 +1100241 }
Damien Miller31741252003-05-19 00:13:38 +1000242 break;
243 case DP_S_DOT:
244 if (ch == '.') {
245 state = DP_S_MAX;
246 ch = *format++;
Damien Miller57f39152005-11-24 19:58:19 +1100247 } else {
Damien Miller31741252003-05-19 00:13:38 +1000248 state = DP_S_MOD;
Damien Miller57f39152005-11-24 19:58:19 +1100249 }
Damien Miller31741252003-05-19 00:13:38 +1000250 break;
251 case DP_S_MAX:
252 if (isdigit((unsigned char)ch)) {
253 if (max < 0)
254 max = 0;
Damien Miller57f39152005-11-24 19:58:19 +1100255 max = 10*max + char_to_int (ch);
Damien Miller31741252003-05-19 00:13:38 +1000256 ch = *format++;
257 } else if (ch == '*') {
258 max = va_arg (args, int);
259 ch = *format++;
260 state = DP_S_MOD;
Damien Miller57f39152005-11-24 19:58:19 +1100261 } else {
Damien Miller31741252003-05-19 00:13:38 +1000262 state = DP_S_MOD;
Damien Miller57f39152005-11-24 19:58:19 +1100263 }
Damien Miller31741252003-05-19 00:13:38 +1000264 break;
265 case DP_S_MOD:
266 switch (ch) {
267 case 'h':
268 cflags = DP_C_SHORT;
269 ch = *format++;
270 break;
271 case 'l':
272 cflags = DP_C_LONG;
273 ch = *format++;
Damien Miller57f39152005-11-24 19:58:19 +1100274 if (ch == 'l') { /* It's a long long */
275 cflags = DP_C_LLONG;
Damien Miller31741252003-05-19 00:13:38 +1000276 ch = *format++;
277 }
278 break;
Damien Miller31741252003-05-19 00:13:38 +1000279 case 'L':
280 cflags = DP_C_LDOUBLE;
281 ch = *format++;
282 break;
283 default:
284 break;
285 }
286 state = DP_S_CONV;
287 break;
288 case DP_S_CONV:
289 switch (ch) {
290 case 'd':
291 case 'i':
292 if (cflags == DP_C_SHORT)
Damien Miller57f39152005-11-24 19:58:19 +1100293 value = va_arg (args, int);
Damien Miller31741252003-05-19 00:13:38 +1000294 else if (cflags == DP_C_LONG)
Damien Miller57f39152005-11-24 19:58:19 +1100295 value = va_arg (args, long int);
296 else if (cflags == DP_C_LLONG)
297 value = va_arg (args, LLONG);
Damien Miller31741252003-05-19 00:13:38 +1000298 else
299 value = va_arg (args, int);
Damien Miller57f39152005-11-24 19:58:19 +1100300 fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
Damien Miller31741252003-05-19 00:13:38 +1000301 break;
302 case 'o':
303 flags |= DP_F_UNSIGNED;
304 if (cflags == DP_C_SHORT)
Damien Miller57f39152005-11-24 19:58:19 +1100305 value = va_arg (args, unsigned int);
Damien Miller31741252003-05-19 00:13:38 +1000306 else if (cflags == DP_C_LONG)
Damien Miller57f39152005-11-24 19:58:19 +1100307 value = (long)va_arg (args, unsigned long int);
308 else if (cflags == DP_C_LLONG)
309 value = (long)va_arg (args, unsigned LLONG);
Damien Miller31741252003-05-19 00:13:38 +1000310 else
Damien Miller57f39152005-11-24 19:58:19 +1100311 value = (long)va_arg (args, unsigned int);
312 fmtint (buffer, &currlen, maxlen, value, 8, min, max, flags);
Damien Miller31741252003-05-19 00:13:38 +1000313 break;
314 case 'u':
315 flags |= DP_F_UNSIGNED;
316 if (cflags == DP_C_SHORT)
Damien Miller57f39152005-11-24 19:58:19 +1100317 value = va_arg (args, unsigned int);
Damien Miller31741252003-05-19 00:13:38 +1000318 else if (cflags == DP_C_LONG)
Damien Miller57f39152005-11-24 19:58:19 +1100319 value = (long)va_arg (args, unsigned long int);
320 else if (cflags == DP_C_LLONG)
321 value = (LLONG)va_arg (args, unsigned LLONG);
Damien Miller31741252003-05-19 00:13:38 +1000322 else
Damien Miller57f39152005-11-24 19:58:19 +1100323 value = (long)va_arg (args, unsigned int);
Damien Miller31741252003-05-19 00:13:38 +1000324 fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
325 break;
326 case 'X':
327 flags |= DP_F_UP;
328 case 'x':
329 flags |= DP_F_UNSIGNED;
330 if (cflags == DP_C_SHORT)
Damien Miller57f39152005-11-24 19:58:19 +1100331 value = va_arg (args, unsigned int);
Damien Miller31741252003-05-19 00:13:38 +1000332 else if (cflags == DP_C_LONG)
Damien Miller57f39152005-11-24 19:58:19 +1100333 value = (long)va_arg (args, unsigned long int);
334 else if (cflags == DP_C_LLONG)
335 value = (LLONG)va_arg (args, unsigned LLONG);
Damien Miller31741252003-05-19 00:13:38 +1000336 else
Damien Miller57f39152005-11-24 19:58:19 +1100337 value = (long)va_arg (args, unsigned int);
338 fmtint (buffer, &currlen, maxlen, value, 16, min, max, flags);
Damien Miller31741252003-05-19 00:13:38 +1000339 break;
340 case 'f':
341 if (cflags == DP_C_LDOUBLE)
Damien Miller57f39152005-11-24 19:58:19 +1100342 fvalue = va_arg (args, LDOUBLE);
Damien Miller31741252003-05-19 00:13:38 +1000343 else
Damien Miller57f39152005-11-24 19:58:19 +1100344 fvalue = va_arg (args, double);
Damien Miller31741252003-05-19 00:13:38 +1000345 /* um, floating point? */
Damien Miller57f39152005-11-24 19:58:19 +1100346 fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags);
Damien Miller31741252003-05-19 00:13:38 +1000347 break;
348 case 'E':
349 flags |= DP_F_UP;
350 case 'e':
351 if (cflags == DP_C_LDOUBLE)
Damien Miller57f39152005-11-24 19:58:19 +1100352 fvalue = va_arg (args, LDOUBLE);
Damien Miller31741252003-05-19 00:13:38 +1000353 else
Damien Miller57f39152005-11-24 19:58:19 +1100354 fvalue = va_arg (args, double);
355 fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags);
Damien Miller31741252003-05-19 00:13:38 +1000356 break;
357 case 'G':
358 flags |= DP_F_UP;
359 case 'g':
360 if (cflags == DP_C_LDOUBLE)
Damien Miller57f39152005-11-24 19:58:19 +1100361 fvalue = va_arg (args, LDOUBLE);
Damien Miller31741252003-05-19 00:13:38 +1000362 else
Damien Miller57f39152005-11-24 19:58:19 +1100363 fvalue = va_arg (args, double);
364 fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags);
Damien Miller31741252003-05-19 00:13:38 +1000365 break;
366 case 'c':
Damien Miller57f39152005-11-24 19:58:19 +1100367 dopr_outch (buffer, &currlen, maxlen, va_arg (args, int));
Damien Miller31741252003-05-19 00:13:38 +1000368 break;
369 case 's':
Damien Miller57f39152005-11-24 19:58:19 +1100370 strvalue = va_arg (args, char *);
371 if (!strvalue) strvalue = "(NULL)";
372 if (max == -1) {
373 max = strlen(strvalue);
374 }
375 if (min > 0 && max >= 0 && min > max) max = min;
376 fmtstr (buffer, &currlen, maxlen, strvalue, flags, min, max);
Damien Miller31741252003-05-19 00:13:38 +1000377 break;
378 case 'p':
Damien Miller57f39152005-11-24 19:58:19 +1100379 strvalue = va_arg (args, void *);
380 fmtint (buffer, &currlen, maxlen, (long) strvalue, 16, min, max, flags);
Damien Miller31741252003-05-19 00:13:38 +1000381 break;
382 case 'n':
383 if (cflags == DP_C_SHORT) {
384 short int *num;
Damien Miller57f39152005-11-24 19:58:19 +1100385 num = va_arg (args, short int *);
Damien Miller31741252003-05-19 00:13:38 +1000386 *num = currlen;
387 } else if (cflags == DP_C_LONG) {
388 long int *num;
Damien Miller57f39152005-11-24 19:58:19 +1100389 num = va_arg (args, long int *);
390 *num = (long int)currlen;
391 } else if (cflags == DP_C_LLONG) {
392 LLONG *num;
393 num = va_arg (args, LLONG *);
394 *num = (LLONG)currlen;
Damien Miller31741252003-05-19 00:13:38 +1000395 } else {
396 int *num;
Damien Miller57f39152005-11-24 19:58:19 +1100397 num = va_arg (args, int *);
Damien Miller31741252003-05-19 00:13:38 +1000398 *num = currlen;
399 }
400 break;
401 case '%':
Damien Miller57f39152005-11-24 19:58:19 +1100402 dopr_outch (buffer, &currlen, maxlen, ch);
Damien Miller31741252003-05-19 00:13:38 +1000403 break;
Damien Miller57f39152005-11-24 19:58:19 +1100404 case 'w':
405 /* not supported yet, treat as next char */
Damien Miller31741252003-05-19 00:13:38 +1000406 ch = *format++;
407 break;
Damien Miller57f39152005-11-24 19:58:19 +1100408 default:
409 /* Unknown, skip */
410 break;
Damien Miller31741252003-05-19 00:13:38 +1000411 }
412 ch = *format++;
413 state = DP_S_DEFAULT;
414 flags = cflags = min = 0;
415 max = -1;
416 break;
417 case DP_S_DONE:
418 break;
Damien Miller57f39152005-11-24 19:58:19 +1100419 default:
420 /* hmm? */
Damien Miller31741252003-05-19 00:13:38 +1000421 break; /* some picky compilers need this */
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000422 }
423 }
Damien Miller57f39152005-11-24 19:58:19 +1100424 if (maxlen != 0) {
425 if (currlen < maxlen - 1)
426 buffer[currlen] = '\0';
427 else if (maxlen > 0)
428 buffer[maxlen - 1] = '\0';
429 }
430
431 return currlen;
Damien Miller42b81ff1999-11-26 12:21:24 +1100432}
433
Damien Miller57f39152005-11-24 19:58:19 +1100434static void fmtstr(char *buffer, size_t *currlen, size_t maxlen,
435 char *value, int flags, int min, int max)
Damien Miller42b81ff1999-11-26 12:21:24 +1100436{
Damien Miller57f39152005-11-24 19:58:19 +1100437 int padlen, strln; /* amount to pad */
438 int cnt = 0;
439
440#ifdef DEBUG_SNPRINTF
441 printf("fmtstr min=%d max=%d s=[%s]\n", min, max, value);
442#endif
443 if (value == 0) {
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000444 value = "<NULL>";
Damien Miller57f39152005-11-24 19:58:19 +1100445 }
Damien Miller168e6ac2000-07-11 12:23:01 +1000446
Darren Tucker4127f552004-09-23 21:35:09 +1000447 for (strln = 0; strln < max && value[strln]; ++strln); /* strlen */
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000448 padlen = min - strln;
449 if (padlen < 0)
450 padlen = 0;
451 if (flags & DP_F_MINUS)
452 padlen = -padlen; /* Left Justify */
Damien Miller57f39152005-11-24 19:58:19 +1100453
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000454 while ((padlen > 0) && (cnt < max)) {
Damien Miller57f39152005-11-24 19:58:19 +1100455 dopr_outch (buffer, currlen, maxlen, ' ');
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000456 --padlen;
457 ++cnt;
458 }
459 while (*value && (cnt < max)) {
Damien Miller57f39152005-11-24 19:58:19 +1100460 dopr_outch (buffer, currlen, maxlen, *value++);
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000461 ++cnt;
462 }
463 while ((padlen < 0) && (cnt < max)) {
Damien Miller57f39152005-11-24 19:58:19 +1100464 dopr_outch (buffer, currlen, maxlen, ' ');
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000465 ++padlen;
466 ++cnt;
467 }
Damien Miller42b81ff1999-11-26 12:21:24 +1100468}
469
Damien Miller168e6ac2000-07-11 12:23:01 +1000470/* Have to handle DP_F_NUM (ie 0x and 0 alternates) */
471
Damien Miller57f39152005-11-24 19:58:19 +1100472static void fmtint(char *buffer, size_t *currlen, size_t maxlen,
Darren Tucker9834cab2006-03-19 00:07:07 +1100473 LLONG value, int base, int min, int max, int flags)
Damien Miller42b81ff1999-11-26 12:21:24 +1100474{
Damien Miller57f39152005-11-24 19:58:19 +1100475 int signvalue = 0;
Darren Tucker9834cab2006-03-19 00:07:07 +1100476 unsigned LLONG uvalue;
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000477 char convert[20];
Damien Miller57f39152005-11-24 19:58:19 +1100478 int place = 0;
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000479 int spadlen = 0; /* amount to space pad */
480 int zpadlen = 0; /* amount to zero pad */
Damien Miller57f39152005-11-24 19:58:19 +1100481 int caps = 0;
482
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000483 if (max < 0)
484 max = 0;
Damien Miller57f39152005-11-24 19:58:19 +1100485
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000486 uvalue = value;
Damien Miller57f39152005-11-24 19:58:19 +1100487
488 if(!(flags & DP_F_UNSIGNED)) {
489 if( value < 0 ) {
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000490 signvalue = '-';
491 uvalue = -value;
Damien Miller57f39152005-11-24 19:58:19 +1100492 } else {
493 if (flags & DP_F_PLUS) /* Do a sign (+/i) */
494 signvalue = '+';
495 else if (flags & DP_F_SPACE)
496 signvalue = ' ';
497 }
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000498 }
Damien Miller168e6ac2000-07-11 12:23:01 +1000499
Damien Miller57f39152005-11-24 19:58:19 +1100500 if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
501
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000502 do {
503 convert[place++] =
Damien Miller57f39152005-11-24 19:58:19 +1100504 (caps? "0123456789ABCDEF":"0123456789abcdef")
505 [uvalue % (unsigned)base ];
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000506 uvalue = (uvalue / (unsigned)base );
Damien Miller57f39152005-11-24 19:58:19 +1100507 } while(uvalue && (place < 20));
508 if (place == 20) place--;
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000509 convert[place] = 0;
Damien Miller168e6ac2000-07-11 12:23:01 +1000510
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000511 zpadlen = max - place;
512 spadlen = min - MAX (max, place) - (signvalue ? 1 : 0);
Damien Miller57f39152005-11-24 19:58:19 +1100513 if (zpadlen < 0) zpadlen = 0;
514 if (spadlen < 0) spadlen = 0;
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000515 if (flags & DP_F_ZERO) {
516 zpadlen = MAX(zpadlen, spadlen);
517 spadlen = 0;
518 }
519 if (flags & DP_F_MINUS)
520 spadlen = -spadlen; /* Left Justifty */
Damien Miller168e6ac2000-07-11 12:23:01 +1000521
Damien Miller57f39152005-11-24 19:58:19 +1100522#ifdef DEBUG_SNPRINTF
523 printf("zpad: %d, spad: %d, min: %d, max: %d, place: %d\n",
524 zpadlen, spadlen, min, max, place);
525#endif
526
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000527 /* Spaces */
528 while (spadlen > 0) {
Damien Miller57f39152005-11-24 19:58:19 +1100529 dopr_outch (buffer, currlen, maxlen, ' ');
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000530 --spadlen;
531 }
Damien Miller168e6ac2000-07-11 12:23:01 +1000532
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000533 /* Sign */
534 if (signvalue)
Damien Miller57f39152005-11-24 19:58:19 +1100535 dopr_outch (buffer, currlen, maxlen, signvalue);
Damien Miller168e6ac2000-07-11 12:23:01 +1000536
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000537 /* Zeros */
538 if (zpadlen > 0) {
539 while (zpadlen > 0) {
Damien Miller57f39152005-11-24 19:58:19 +1100540 dopr_outch (buffer, currlen, maxlen, '0');
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000541 --zpadlen;
542 }
543 }
Damien Miller168e6ac2000-07-11 12:23:01 +1000544
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000545 /* Digits */
546 while (place > 0)
Damien Miller57f39152005-11-24 19:58:19 +1100547 dopr_outch (buffer, currlen, maxlen, convert[--place]);
Damien Miller168e6ac2000-07-11 12:23:01 +1000548
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000549 /* Left Justified spaces */
550 while (spadlen < 0) {
551 dopr_outch (buffer, currlen, maxlen, ' ');
552 ++spadlen;
553 }
Damien Miller42b81ff1999-11-26 12:21:24 +1100554}
555
Damien Miller57f39152005-11-24 19:58:19 +1100556static LDOUBLE abs_val(LDOUBLE value)
Damien Miller42b81ff1999-11-26 12:21:24 +1100557{
Damien Miller57f39152005-11-24 19:58:19 +1100558 LDOUBLE result = value;
Damien Miller42b81ff1999-11-26 12:21:24 +1100559
Damien Miller57f39152005-11-24 19:58:19 +1100560 if (value < 0)
561 result = -value;
562
563 return result;
564}
565
566static LDOUBLE POW10(int exp)
567{
568 LDOUBLE result = 1;
569
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000570 while (exp) {
571 result *= 10;
572 exp--;
573 }
Damien Miller168e6ac2000-07-11 12:23:01 +1000574
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000575 return result;
Damien Miller168e6ac2000-07-11 12:23:01 +1000576}
577
Damien Miller57f39152005-11-24 19:58:19 +1100578static LLONG ROUND(LDOUBLE value)
Damien Miller168e6ac2000-07-11 12:23:01 +1000579{
Damien Miller57f39152005-11-24 19:58:19 +1100580 LLONG intpart;
Damien Miller168e6ac2000-07-11 12:23:01 +1000581
Damien Miller57f39152005-11-24 19:58:19 +1100582 intpart = (LLONG)value;
583 value = value - intpart;
584 if (value >= 0.5) intpart++;
585
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000586 return intpart;
Damien Miller168e6ac2000-07-11 12:23:01 +1000587}
588
Damien Miller57f39152005-11-24 19:58:19 +1100589/* a replacement for modf that doesn't need the math library. Should
590 be portable, but slow */
591static double my_modf(double x0, double *iptr)
Damien Miller168e6ac2000-07-11 12:23:01 +1000592{
Damien Miller57f39152005-11-24 19:58:19 +1100593 int i;
594 long l;
595 double x = x0;
596 double f = 1.0;
597
598 for (i=0;i<100;i++) {
599 l = (long)x;
600 if (l <= (x+1) && l >= (x-1)) break;
601 x *= 0.1;
602 f *= 10.0;
603 }
604
605 if (i == 100) {
606 /* yikes! the number is beyond what we can handle. What do we do? */
607 (*iptr) = 0;
608 return 0;
609 }
610
611 if (i != 0) {
612 double i2;
613 double ret;
614
615 ret = my_modf(x0-l*f, &i2);
616 (*iptr) = l*f + i2;
617 return ret;
618 }
619
620 (*iptr) = l;
621 return x - (*iptr);
622}
623
624
625static void fmtfp (char *buffer, size_t *currlen, size_t maxlen,
626 LDOUBLE fvalue, int min, int max, int flags)
627{
628 int signvalue = 0;
629 double ufvalue;
630 char iconvert[311];
631 char fconvert[311];
632 int iplace = 0;
633 int fplace = 0;
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000634 int padlen = 0; /* amount to pad */
Damien Miller57f39152005-11-24 19:58:19 +1100635 int zpadlen = 0;
636 int caps = 0;
637 int idx;
638 double intpart;
639 double fracpart;
640 double temp;
Damien Miller168e6ac2000-07-11 12:23:01 +1000641
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000642 /*
643 * AIX manpage says the default is 0, but Solaris says the default
644 * is 6, and sprintf on AIX defaults to 6
645 */
646 if (max < 0)
647 max = 6;
Damien Miller168e6ac2000-07-11 12:23:01 +1000648
Damien Miller57f39152005-11-24 19:58:19 +1100649 ufvalue = abs_val (fvalue);
Damien Miller168e6ac2000-07-11 12:23:01 +1000650
Damien Miller57f39152005-11-24 19:58:19 +1100651 if (fvalue < 0) {
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000652 signvalue = '-';
Damien Miller57f39152005-11-24 19:58:19 +1100653 } else {
654 if (flags & DP_F_PLUS) { /* Do a sign (+/i) */
655 signvalue = '+';
656 } else {
657 if (flags & DP_F_SPACE)
658 signvalue = ' ';
659 }
660 }
Damien Miller168e6ac2000-07-11 12:23:01 +1000661
Damien Miller57f39152005-11-24 19:58:19 +1100662#if 0
663 if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
664#endif
665
666#if 0
667 if (max == 0) ufvalue += 0.5; /* if max = 0 we must round */
668#endif
Damien Miller168e6ac2000-07-11 12:23:01 +1000669
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000670 /*
Damien Miller57f39152005-11-24 19:58:19 +1100671 * Sorry, we only support 16 digits past the decimal because of our
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000672 * conversion method
673 */
Damien Miller57f39152005-11-24 19:58:19 +1100674 if (max > 16)
675 max = 16;
Damien Miller168e6ac2000-07-11 12:23:01 +1000676
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000677 /* We "cheat" by converting the fractional part to integer by
678 * multiplying by a factor of 10
679 */
Damien Miller168e6ac2000-07-11 12:23:01 +1000680
Damien Miller57f39152005-11-24 19:58:19 +1100681 temp = ufvalue;
682 my_modf(temp, &intpart);
683
684 fracpart = ROUND((POW10(max)) * (ufvalue - intpart));
685
686 if (fracpart >= POW10(max)) {
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000687 intpart++;
Damien Miller57f39152005-11-24 19:58:19 +1100688 fracpart -= POW10(max);
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000689 }
Damien Miller168e6ac2000-07-11 12:23:01 +1000690
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000691 /* Convert integer part */
692 do {
Damien Miller57f39152005-11-24 19:58:19 +1100693 temp = intpart*0.1;
694 my_modf(temp, &intpart);
695 idx = (int) ((temp -intpart +0.05)* 10.0);
696 /* idx = (int) (((double)(temp*0.1) -intpart +0.05) *10.0); */
697 /* printf ("%llf, %f, %x\n", temp, intpart, idx); */
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000698 iconvert[iplace++] =
Damien Miller57f39152005-11-24 19:58:19 +1100699 (caps? "0123456789ABCDEF":"0123456789abcdef")[idx];
700 } while (intpart && (iplace < 311));
701 if (iplace == 311) iplace--;
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000702 iconvert[iplace] = 0;
Damien Miller168e6ac2000-07-11 12:23:01 +1000703
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000704 /* Convert fractional part */
Damien Miller57f39152005-11-24 19:58:19 +1100705 if (fracpart)
706 {
707 do {
708 temp = fracpart*0.1;
709 my_modf(temp, &fracpart);
710 idx = (int) ((temp -fracpart +0.05)* 10.0);
711 /* idx = (int) ((((temp/10) -fracpart) +0.05) *10); */
712 /* printf ("%lf, %lf, %ld\n", temp, fracpart, idx ); */
713 fconvert[fplace++] =
714 (caps? "0123456789ABCDEF":"0123456789abcdef")[idx];
715 } while(fracpart && (fplace < 311));
716 if (fplace == 311) fplace--;
717 }
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000718 fconvert[fplace] = 0;
Damien Miller57f39152005-11-24 19:58:19 +1100719
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000720 /* -1 for decimal point, another -1 if we are printing a sign */
721 padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
722 zpadlen = max - fplace;
Damien Miller57f39152005-11-24 19:58:19 +1100723 if (zpadlen < 0) zpadlen = 0;
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000724 if (padlen < 0)
725 padlen = 0;
726 if (flags & DP_F_MINUS)
727 padlen = -padlen; /* Left Justifty */
Damien Miller57f39152005-11-24 19:58:19 +1100728
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000729 if ((flags & DP_F_ZERO) && (padlen > 0)) {
730 if (signvalue) {
Damien Miller57f39152005-11-24 19:58:19 +1100731 dopr_outch (buffer, currlen, maxlen, signvalue);
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000732 --padlen;
733 signvalue = 0;
734 }
735 while (padlen > 0) {
Damien Miller57f39152005-11-24 19:58:19 +1100736 dopr_outch (buffer, currlen, maxlen, '0');
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000737 --padlen;
738 }
739 }
740 while (padlen > 0) {
Damien Miller57f39152005-11-24 19:58:19 +1100741 dopr_outch (buffer, currlen, maxlen, ' ');
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000742 --padlen;
743 }
744 if (signvalue)
Damien Miller57f39152005-11-24 19:58:19 +1100745 dopr_outch (buffer, currlen, maxlen, signvalue);
746
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000747 while (iplace > 0)
Damien Miller57f39152005-11-24 19:58:19 +1100748 dopr_outch (buffer, currlen, maxlen, iconvert[--iplace]);
749
750#ifdef DEBUG_SNPRINTF
751 printf("fmtfp: fplace=%d zpadlen=%d\n", fplace, zpadlen);
752#endif
Damien Miller168e6ac2000-07-11 12:23:01 +1000753
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000754 /*
Damien Miller57f39152005-11-24 19:58:19 +1100755 * Decimal point. This should probably use locale to find the correct
756 * char to print out.
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000757 */
Damien Miller57f39152005-11-24 19:58:19 +1100758 if (max > 0) {
759 dopr_outch (buffer, currlen, maxlen, '.');
760
761 while (zpadlen > 0) {
762 dopr_outch (buffer, currlen, maxlen, '0');
763 --zpadlen;
764 }
Damien Miller168e6ac2000-07-11 12:23:01 +1000765
Damien Miller57f39152005-11-24 19:58:19 +1100766 while (fplace > 0)
767 dopr_outch (buffer, currlen, maxlen, fconvert[--fplace]);
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000768 }
Damien Miller168e6ac2000-07-11 12:23:01 +1000769
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000770 while (padlen < 0) {
Damien Miller57f39152005-11-24 19:58:19 +1100771 dopr_outch (buffer, currlen, maxlen, ' ');
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000772 ++padlen;
773 }
Damien Miller168e6ac2000-07-11 12:23:01 +1000774}
775
Damien Miller57f39152005-11-24 19:58:19 +1100776static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c)
Damien Miller168e6ac2000-07-11 12:23:01 +1000777{
Damien Miller57f39152005-11-24 19:58:19 +1100778 if (*currlen < maxlen) {
779 buffer[(*currlen)] = c;
780 }
781 (*currlen)++;
Damien Miller168e6ac2000-07-11 12:23:01 +1000782}
783#endif /* !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF) */
784
Damien Miller57f39152005-11-24 19:58:19 +1100785#if !defined(HAVE_VSNPRINTF)
786int vsnprintf (char *str, size_t count, const char *fmt, va_list args)
Damien Miller168e6ac2000-07-11 12:23:01 +1000787{
Damien Miller57f39152005-11-24 19:58:19 +1100788 return dopr(str, count, fmt, args);
Damien Miller168e6ac2000-07-11 12:23:01 +1000789}
Damien Miller57f39152005-11-24 19:58:19 +1100790#endif
Damien Miller168e6ac2000-07-11 12:23:01 +1000791
Damien Miller57f39152005-11-24 19:58:19 +1100792#if !defined(HAVE_SNPRINTF)
Darren Tuckerd40c66c2005-12-17 22:32:03 +1100793int snprintf(char *str, size_t count, SNPRINTF_CONST char *fmt, ...)
Damien Millerc6b3bbe1999-12-13 08:27:33 +1100794{
Damien Miller57f39152005-11-24 19:58:19 +1100795 size_t ret;
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000796 va_list ap;
797
798 va_start(ap, fmt);
Damien Miller57f39152005-11-24 19:58:19 +1100799 ret = vsnprintf(str, count, fmt, ap);
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000800 va_end(ap);
Damien Miller57f39152005-11-24 19:58:19 +1100801 return ret;
Damien Millerc6b3bbe1999-12-13 08:27:33 +1100802}
Damien Miller57f39152005-11-24 19:58:19 +1100803#endif
Damien Millerc6b3bbe1999-12-13 08:27:33 +1100804