blob: 04651e1d438be8e8702d4dc0753e48f90f725358 [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>
Darren Tucker08432d52006-09-09 15:59:43 +1000112#include <stdarg.h>
Darren Tuckerf78fb542006-08-06 21:25:24 +1000113#include <stdlib.h>
Damien Miller62da44f2006-07-24 15:08:35 +1000114#include <string.h>
115
Damien Miller57f39152005-11-24 19:58:19 +1100116#ifdef HAVE_LONG_DOUBLE
117# define LDOUBLE long double
118#else
119# define LDOUBLE double
120#endif
Damien Miller168e6ac2000-07-11 12:23:01 +1000121
Damien Miller57f39152005-11-24 19:58:19 +1100122#ifdef HAVE_LONG_LONG
123# define LLONG long long
124#else
125# define LLONG long
126#endif
Ben Lindstrom116b6bd2001-02-13 14:05:59 +0000127
Damien Miller168e6ac2000-07-11 12:23:01 +1000128/*
129 * dopr(): poor man's version of doprintf
130 */
131
132/* format read states */
133#define DP_S_DEFAULT 0
134#define DP_S_FLAGS 1
135#define DP_S_MIN 2
136#define DP_S_DOT 3
137#define DP_S_MAX 4
138#define DP_S_MOD 5
139#define DP_S_CONV 6
140#define DP_S_DONE 7
141
142/* format flags - Bits */
143#define DP_F_MINUS (1 << 0)
144#define DP_F_PLUS (1 << 1)
145#define DP_F_SPACE (1 << 2)
146#define DP_F_NUM (1 << 3)
147#define DP_F_ZERO (1 << 4)
148#define DP_F_UP (1 << 5)
149#define DP_F_UNSIGNED (1 << 6)
150
151/* Conversion Flags */
Damien Miller57f39152005-11-24 19:58:19 +1100152#define DP_C_SHORT 1
153#define DP_C_LONG 2
154#define DP_C_LDOUBLE 3
155#define DP_C_LLONG 4
Damien Miller168e6ac2000-07-11 12:23:01 +1000156
Damien Miller57f39152005-11-24 19:58:19 +1100157#define char_to_int(p) ((p)- '0')
158#ifndef MAX
159# define MAX(p,q) (((p) >= (q)) ? (p) : (q))
160#endif
Damien Miller168e6ac2000-07-11 12:23:01 +1000161
Damien Miller57f39152005-11-24 19:58:19 +1100162static size_t dopr(char *buffer, size_t maxlen, const char *format,
163 va_list args_in);
164static void fmtstr(char *buffer, size_t *currlen, size_t maxlen,
165 char *value, int flags, int min, int max);
166static void fmtint(char *buffer, size_t *currlen, size_t maxlen,
Darren Tucker9834cab2006-03-19 00:07:07 +1100167 LLONG value, int base, int min, int max, int flags);
Damien Miller57f39152005-11-24 19:58:19 +1100168static void fmtfp(char *buffer, size_t *currlen, size_t maxlen,
169 LDOUBLE fvalue, int min, int max, int flags);
170static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c);
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000171
Damien Miller57f39152005-11-24 19:58:19 +1100172static size_t dopr(char *buffer, size_t maxlen, const char *format, va_list args_in)
Damien Miller13bc0be1999-12-28 10:19:16 +1100173{
Damien Miller57f39152005-11-24 19:58:19 +1100174 char ch;
175 LLONG value;
176 LDOUBLE fvalue;
177 char *strvalue;
178 int min;
179 int max;
180 int state;
181 int flags;
182 int cflags;
183 size_t currlen;
184 va_list args;
Damien Miller13bc0be1999-12-28 10:19:16 +1100185
Damien Miller57f39152005-11-24 19:58:19 +1100186 VA_COPY(args, args_in);
187
188 state = DP_S_DEFAULT;
189 currlen = flags = cflags = min = 0;
190 max = -1;
191 ch = *format++;
192
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000193 while (state != DP_S_DONE) {
Damien Miller57f39152005-11-24 19:58:19 +1100194 if (ch == '\0')
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000195 state = DP_S_DONE;
Damien Miller42b81ff1999-11-26 12:21:24 +1100196
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000197 switch(state) {
Damien Miller31741252003-05-19 00:13:38 +1000198 case DP_S_DEFAULT:
199 if (ch == '%')
200 state = DP_S_FLAGS;
201 else
Damien Miller57f39152005-11-24 19:58:19 +1100202 dopr_outch (buffer, &currlen, maxlen, ch);
Damien Miller31741252003-05-19 00:13:38 +1000203 ch = *format++;
204 break;
205 case DP_S_FLAGS:
206 switch (ch) {
207 case '-':
208 flags |= DP_F_MINUS;
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000209 ch = *format++;
210 break;
Damien Miller31741252003-05-19 00:13:38 +1000211 case '+':
212 flags |= DP_F_PLUS;
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000213 ch = *format++;
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000214 break;
Damien Miller31741252003-05-19 00:13:38 +1000215 case ' ':
216 flags |= DP_F_SPACE;
217 ch = *format++;
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000218 break;
Damien Miller31741252003-05-19 00:13:38 +1000219 case '#':
220 flags |= DP_F_NUM;
221 ch = *format++;
222 break;
223 case '0':
224 flags |= DP_F_ZERO;
225 ch = *format++;
226 break;
227 default:
228 state = DP_S_MIN;
229 break;
230 }
231 break;
232 case DP_S_MIN:
233 if (isdigit((unsigned char)ch)) {
Damien Miller57f39152005-11-24 19:58:19 +1100234 min = 10*min + char_to_int (ch);
Damien Miller31741252003-05-19 00:13:38 +1000235 ch = *format++;
236 } else if (ch == '*') {
237 min = va_arg (args, int);
238 ch = *format++;
239 state = DP_S_DOT;
Damien Miller57f39152005-11-24 19:58:19 +1100240 } else {
Damien Miller31741252003-05-19 00:13:38 +1000241 state = DP_S_DOT;
Damien Miller57f39152005-11-24 19:58:19 +1100242 }
Damien Miller31741252003-05-19 00:13:38 +1000243 break;
244 case DP_S_DOT:
245 if (ch == '.') {
246 state = DP_S_MAX;
247 ch = *format++;
Damien Miller57f39152005-11-24 19:58:19 +1100248 } else {
Damien Miller31741252003-05-19 00:13:38 +1000249 state = DP_S_MOD;
Damien Miller57f39152005-11-24 19:58:19 +1100250 }
Damien Miller31741252003-05-19 00:13:38 +1000251 break;
252 case DP_S_MAX:
253 if (isdigit((unsigned char)ch)) {
254 if (max < 0)
255 max = 0;
Damien Miller57f39152005-11-24 19:58:19 +1100256 max = 10*max + char_to_int (ch);
Damien Miller31741252003-05-19 00:13:38 +1000257 ch = *format++;
258 } else if (ch == '*') {
259 max = va_arg (args, int);
260 ch = *format++;
261 state = DP_S_MOD;
Damien Miller57f39152005-11-24 19:58:19 +1100262 } else {
Damien Miller31741252003-05-19 00:13:38 +1000263 state = DP_S_MOD;
Damien Miller57f39152005-11-24 19:58:19 +1100264 }
Damien Miller31741252003-05-19 00:13:38 +1000265 break;
266 case DP_S_MOD:
267 switch (ch) {
268 case 'h':
269 cflags = DP_C_SHORT;
270 ch = *format++;
271 break;
272 case 'l':
273 cflags = DP_C_LONG;
274 ch = *format++;
Damien Miller57f39152005-11-24 19:58:19 +1100275 if (ch == 'l') { /* It's a long long */
276 cflags = DP_C_LLONG;
Damien Miller31741252003-05-19 00:13:38 +1000277 ch = *format++;
278 }
279 break;
Damien Miller31741252003-05-19 00:13:38 +1000280 case 'L':
281 cflags = DP_C_LDOUBLE;
282 ch = *format++;
283 break;
284 default:
285 break;
286 }
287 state = DP_S_CONV;
288 break;
289 case DP_S_CONV:
290 switch (ch) {
291 case 'd':
292 case 'i':
293 if (cflags == DP_C_SHORT)
Damien Miller57f39152005-11-24 19:58:19 +1100294 value = va_arg (args, int);
Damien Miller31741252003-05-19 00:13:38 +1000295 else if (cflags == DP_C_LONG)
Damien Miller57f39152005-11-24 19:58:19 +1100296 value = va_arg (args, long int);
297 else if (cflags == DP_C_LLONG)
298 value = va_arg (args, LLONG);
Damien Miller31741252003-05-19 00:13:38 +1000299 else
300 value = va_arg (args, int);
Damien Miller57f39152005-11-24 19:58:19 +1100301 fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
Damien Miller31741252003-05-19 00:13:38 +1000302 break;
303 case 'o':
304 flags |= DP_F_UNSIGNED;
305 if (cflags == DP_C_SHORT)
Damien Miller57f39152005-11-24 19:58:19 +1100306 value = va_arg (args, unsigned int);
Damien Miller31741252003-05-19 00:13:38 +1000307 else if (cflags == DP_C_LONG)
Damien Miller57f39152005-11-24 19:58:19 +1100308 value = (long)va_arg (args, unsigned long int);
309 else if (cflags == DP_C_LLONG)
310 value = (long)va_arg (args, unsigned LLONG);
Damien Miller31741252003-05-19 00:13:38 +1000311 else
Damien Miller57f39152005-11-24 19:58:19 +1100312 value = (long)va_arg (args, unsigned int);
313 fmtint (buffer, &currlen, maxlen, value, 8, min, max, flags);
Damien Miller31741252003-05-19 00:13:38 +1000314 break;
315 case 'u':
316 flags |= DP_F_UNSIGNED;
317 if (cflags == DP_C_SHORT)
Damien Miller57f39152005-11-24 19:58:19 +1100318 value = va_arg (args, unsigned int);
Damien Miller31741252003-05-19 00:13:38 +1000319 else if (cflags == DP_C_LONG)
Damien Miller57f39152005-11-24 19:58:19 +1100320 value = (long)va_arg (args, unsigned long int);
321 else if (cflags == DP_C_LLONG)
322 value = (LLONG)va_arg (args, unsigned LLONG);
Damien Miller31741252003-05-19 00:13:38 +1000323 else
Damien Miller57f39152005-11-24 19:58:19 +1100324 value = (long)va_arg (args, unsigned int);
Damien Miller31741252003-05-19 00:13:38 +1000325 fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
326 break;
327 case 'X':
328 flags |= DP_F_UP;
329 case 'x':
330 flags |= DP_F_UNSIGNED;
331 if (cflags == DP_C_SHORT)
Damien Miller57f39152005-11-24 19:58:19 +1100332 value = va_arg (args, unsigned int);
Damien Miller31741252003-05-19 00:13:38 +1000333 else if (cflags == DP_C_LONG)
Damien Miller57f39152005-11-24 19:58:19 +1100334 value = (long)va_arg (args, unsigned long int);
335 else if (cflags == DP_C_LLONG)
336 value = (LLONG)va_arg (args, unsigned LLONG);
Damien Miller31741252003-05-19 00:13:38 +1000337 else
Damien Miller57f39152005-11-24 19:58:19 +1100338 value = (long)va_arg (args, unsigned int);
339 fmtint (buffer, &currlen, maxlen, value, 16, min, max, flags);
Damien Miller31741252003-05-19 00:13:38 +1000340 break;
341 case 'f':
342 if (cflags == DP_C_LDOUBLE)
Damien Miller57f39152005-11-24 19:58:19 +1100343 fvalue = va_arg (args, LDOUBLE);
Damien Miller31741252003-05-19 00:13:38 +1000344 else
Damien Miller57f39152005-11-24 19:58:19 +1100345 fvalue = va_arg (args, double);
Damien Miller31741252003-05-19 00:13:38 +1000346 /* um, floating point? */
Damien Miller57f39152005-11-24 19:58:19 +1100347 fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags);
Damien Miller31741252003-05-19 00:13:38 +1000348 break;
349 case 'E':
350 flags |= DP_F_UP;
351 case 'e':
352 if (cflags == DP_C_LDOUBLE)
Damien Miller57f39152005-11-24 19:58:19 +1100353 fvalue = va_arg (args, LDOUBLE);
Damien Miller31741252003-05-19 00:13:38 +1000354 else
Damien Miller57f39152005-11-24 19:58:19 +1100355 fvalue = va_arg (args, double);
356 fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags);
Damien Miller31741252003-05-19 00:13:38 +1000357 break;
358 case 'G':
359 flags |= DP_F_UP;
360 case 'g':
361 if (cflags == DP_C_LDOUBLE)
Damien Miller57f39152005-11-24 19:58:19 +1100362 fvalue = va_arg (args, LDOUBLE);
Damien Miller31741252003-05-19 00:13:38 +1000363 else
Damien Miller57f39152005-11-24 19:58:19 +1100364 fvalue = va_arg (args, double);
365 fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags);
Damien Miller31741252003-05-19 00:13:38 +1000366 break;
367 case 'c':
Damien Miller57f39152005-11-24 19:58:19 +1100368 dopr_outch (buffer, &currlen, maxlen, va_arg (args, int));
Damien Miller31741252003-05-19 00:13:38 +1000369 break;
370 case 's':
Damien Miller57f39152005-11-24 19:58:19 +1100371 strvalue = va_arg (args, char *);
372 if (!strvalue) strvalue = "(NULL)";
373 if (max == -1) {
374 max = strlen(strvalue);
375 }
376 if (min > 0 && max >= 0 && min > max) max = min;
377 fmtstr (buffer, &currlen, maxlen, strvalue, flags, min, max);
Damien Miller31741252003-05-19 00:13:38 +1000378 break;
379 case 'p':
Damien Miller57f39152005-11-24 19:58:19 +1100380 strvalue = va_arg (args, void *);
381 fmtint (buffer, &currlen, maxlen, (long) strvalue, 16, min, max, flags);
Damien Miller31741252003-05-19 00:13:38 +1000382 break;
383 case 'n':
384 if (cflags == DP_C_SHORT) {
385 short int *num;
Damien Miller57f39152005-11-24 19:58:19 +1100386 num = va_arg (args, short int *);
Damien Miller31741252003-05-19 00:13:38 +1000387 *num = currlen;
388 } else if (cflags == DP_C_LONG) {
389 long int *num;
Damien Miller57f39152005-11-24 19:58:19 +1100390 num = va_arg (args, long int *);
391 *num = (long int)currlen;
392 } else if (cflags == DP_C_LLONG) {
393 LLONG *num;
394 num = va_arg (args, LLONG *);
395 *num = (LLONG)currlen;
Damien Miller31741252003-05-19 00:13:38 +1000396 } else {
397 int *num;
Damien Miller57f39152005-11-24 19:58:19 +1100398 num = va_arg (args, int *);
Damien Miller31741252003-05-19 00:13:38 +1000399 *num = currlen;
400 }
401 break;
402 case '%':
Damien Miller57f39152005-11-24 19:58:19 +1100403 dopr_outch (buffer, &currlen, maxlen, ch);
Damien Miller31741252003-05-19 00:13:38 +1000404 break;
Damien Miller57f39152005-11-24 19:58:19 +1100405 case 'w':
406 /* not supported yet, treat as next char */
Damien Miller31741252003-05-19 00:13:38 +1000407 ch = *format++;
408 break;
Damien Miller57f39152005-11-24 19:58:19 +1100409 default:
410 /* Unknown, skip */
411 break;
Damien Miller31741252003-05-19 00:13:38 +1000412 }
413 ch = *format++;
414 state = DP_S_DEFAULT;
415 flags = cflags = min = 0;
416 max = -1;
417 break;
418 case DP_S_DONE:
419 break;
Damien Miller57f39152005-11-24 19:58:19 +1100420 default:
421 /* hmm? */
Damien Miller31741252003-05-19 00:13:38 +1000422 break; /* some picky compilers need this */
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000423 }
424 }
Damien Miller57f39152005-11-24 19:58:19 +1100425 if (maxlen != 0) {
426 if (currlen < maxlen - 1)
427 buffer[currlen] = '\0';
428 else if (maxlen > 0)
429 buffer[maxlen - 1] = '\0';
430 }
431
432 return currlen;
Damien Miller42b81ff1999-11-26 12:21:24 +1100433}
434
Damien Miller57f39152005-11-24 19:58:19 +1100435static void fmtstr(char *buffer, size_t *currlen, size_t maxlen,
436 char *value, int flags, int min, int max)
Damien Miller42b81ff1999-11-26 12:21:24 +1100437{
Damien Miller57f39152005-11-24 19:58:19 +1100438 int padlen, strln; /* amount to pad */
439 int cnt = 0;
440
441#ifdef DEBUG_SNPRINTF
442 printf("fmtstr min=%d max=%d s=[%s]\n", min, max, value);
443#endif
444 if (value == 0) {
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000445 value = "<NULL>";
Damien Miller57f39152005-11-24 19:58:19 +1100446 }
Damien Miller168e6ac2000-07-11 12:23:01 +1000447
Darren Tucker4127f552004-09-23 21:35:09 +1000448 for (strln = 0; strln < max && value[strln]; ++strln); /* strlen */
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000449 padlen = min - strln;
450 if (padlen < 0)
451 padlen = 0;
452 if (flags & DP_F_MINUS)
453 padlen = -padlen; /* Left Justify */
Damien Miller57f39152005-11-24 19:58:19 +1100454
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000455 while ((padlen > 0) && (cnt < max)) {
Damien Miller57f39152005-11-24 19:58:19 +1100456 dopr_outch (buffer, currlen, maxlen, ' ');
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000457 --padlen;
458 ++cnt;
459 }
460 while (*value && (cnt < max)) {
Damien Miller57f39152005-11-24 19:58:19 +1100461 dopr_outch (buffer, currlen, maxlen, *value++);
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000462 ++cnt;
463 }
464 while ((padlen < 0) && (cnt < max)) {
Damien Miller57f39152005-11-24 19:58:19 +1100465 dopr_outch (buffer, currlen, maxlen, ' ');
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000466 ++padlen;
467 ++cnt;
468 }
Damien Miller42b81ff1999-11-26 12:21:24 +1100469}
470
Damien Miller168e6ac2000-07-11 12:23:01 +1000471/* Have to handle DP_F_NUM (ie 0x and 0 alternates) */
472
Damien Miller57f39152005-11-24 19:58:19 +1100473static void fmtint(char *buffer, size_t *currlen, size_t maxlen,
Darren Tucker9834cab2006-03-19 00:07:07 +1100474 LLONG value, int base, int min, int max, int flags)
Damien Miller42b81ff1999-11-26 12:21:24 +1100475{
Damien Miller57f39152005-11-24 19:58:19 +1100476 int signvalue = 0;
Darren Tucker9834cab2006-03-19 00:07:07 +1100477 unsigned LLONG uvalue;
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000478 char convert[20];
Damien Miller57f39152005-11-24 19:58:19 +1100479 int place = 0;
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000480 int spadlen = 0; /* amount to space pad */
481 int zpadlen = 0; /* amount to zero pad */
Damien Miller57f39152005-11-24 19:58:19 +1100482 int caps = 0;
483
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000484 if (max < 0)
485 max = 0;
Damien Miller57f39152005-11-24 19:58:19 +1100486
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000487 uvalue = value;
Damien Miller57f39152005-11-24 19:58:19 +1100488
489 if(!(flags & DP_F_UNSIGNED)) {
490 if( value < 0 ) {
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000491 signvalue = '-';
492 uvalue = -value;
Damien Miller57f39152005-11-24 19:58:19 +1100493 } else {
494 if (flags & DP_F_PLUS) /* Do a sign (+/i) */
495 signvalue = '+';
496 else if (flags & DP_F_SPACE)
497 signvalue = ' ';
498 }
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000499 }
Damien Miller168e6ac2000-07-11 12:23:01 +1000500
Damien Miller57f39152005-11-24 19:58:19 +1100501 if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
502
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000503 do {
504 convert[place++] =
Damien Miller57f39152005-11-24 19:58:19 +1100505 (caps? "0123456789ABCDEF":"0123456789abcdef")
506 [uvalue % (unsigned)base ];
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000507 uvalue = (uvalue / (unsigned)base );
Damien Miller57f39152005-11-24 19:58:19 +1100508 } while(uvalue && (place < 20));
509 if (place == 20) place--;
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000510 convert[place] = 0;
Damien Miller168e6ac2000-07-11 12:23:01 +1000511
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000512 zpadlen = max - place;
513 spadlen = min - MAX (max, place) - (signvalue ? 1 : 0);
Damien Miller57f39152005-11-24 19:58:19 +1100514 if (zpadlen < 0) zpadlen = 0;
515 if (spadlen < 0) spadlen = 0;
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000516 if (flags & DP_F_ZERO) {
517 zpadlen = MAX(zpadlen, spadlen);
518 spadlen = 0;
519 }
520 if (flags & DP_F_MINUS)
521 spadlen = -spadlen; /* Left Justifty */
Damien Miller168e6ac2000-07-11 12:23:01 +1000522
Damien Miller57f39152005-11-24 19:58:19 +1100523#ifdef DEBUG_SNPRINTF
524 printf("zpad: %d, spad: %d, min: %d, max: %d, place: %d\n",
525 zpadlen, spadlen, min, max, place);
526#endif
527
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000528 /* Spaces */
529 while (spadlen > 0) {
Damien Miller57f39152005-11-24 19:58:19 +1100530 dopr_outch (buffer, currlen, maxlen, ' ');
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000531 --spadlen;
532 }
Damien Miller168e6ac2000-07-11 12:23:01 +1000533
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000534 /* Sign */
535 if (signvalue)
Damien Miller57f39152005-11-24 19:58:19 +1100536 dopr_outch (buffer, currlen, maxlen, signvalue);
Damien Miller168e6ac2000-07-11 12:23:01 +1000537
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000538 /* Zeros */
539 if (zpadlen > 0) {
540 while (zpadlen > 0) {
Damien Miller57f39152005-11-24 19:58:19 +1100541 dopr_outch (buffer, currlen, maxlen, '0');
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000542 --zpadlen;
543 }
544 }
Damien Miller168e6ac2000-07-11 12:23:01 +1000545
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000546 /* Digits */
547 while (place > 0)
Damien Miller57f39152005-11-24 19:58:19 +1100548 dopr_outch (buffer, currlen, maxlen, convert[--place]);
Damien Miller168e6ac2000-07-11 12:23:01 +1000549
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000550 /* Left Justified spaces */
551 while (spadlen < 0) {
552 dopr_outch (buffer, currlen, maxlen, ' ');
553 ++spadlen;
554 }
Damien Miller42b81ff1999-11-26 12:21:24 +1100555}
556
Damien Miller57f39152005-11-24 19:58:19 +1100557static LDOUBLE abs_val(LDOUBLE value)
Damien Miller42b81ff1999-11-26 12:21:24 +1100558{
Damien Miller57f39152005-11-24 19:58:19 +1100559 LDOUBLE result = value;
Damien Miller42b81ff1999-11-26 12:21:24 +1100560
Damien Miller57f39152005-11-24 19:58:19 +1100561 if (value < 0)
562 result = -value;
563
564 return result;
565}
566
567static LDOUBLE POW10(int exp)
568{
569 LDOUBLE result = 1;
570
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000571 while (exp) {
572 result *= 10;
573 exp--;
574 }
Damien Miller168e6ac2000-07-11 12:23:01 +1000575
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000576 return result;
Damien Miller168e6ac2000-07-11 12:23:01 +1000577}
578
Damien Miller57f39152005-11-24 19:58:19 +1100579static LLONG ROUND(LDOUBLE value)
Damien Miller168e6ac2000-07-11 12:23:01 +1000580{
Damien Miller57f39152005-11-24 19:58:19 +1100581 LLONG intpart;
Damien Miller168e6ac2000-07-11 12:23:01 +1000582
Damien Miller57f39152005-11-24 19:58:19 +1100583 intpart = (LLONG)value;
584 value = value - intpart;
585 if (value >= 0.5) intpart++;
586
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000587 return intpart;
Damien Miller168e6ac2000-07-11 12:23:01 +1000588}
589
Damien Miller57f39152005-11-24 19:58:19 +1100590/* a replacement for modf that doesn't need the math library. Should
591 be portable, but slow */
592static double my_modf(double x0, double *iptr)
Damien Miller168e6ac2000-07-11 12:23:01 +1000593{
Damien Miller57f39152005-11-24 19:58:19 +1100594 int i;
595 long l;
596 double x = x0;
597 double f = 1.0;
598
599 for (i=0;i<100;i++) {
600 l = (long)x;
601 if (l <= (x+1) && l >= (x-1)) break;
602 x *= 0.1;
603 f *= 10.0;
604 }
605
606 if (i == 100) {
607 /* yikes! the number is beyond what we can handle. What do we do? */
608 (*iptr) = 0;
609 return 0;
610 }
611
612 if (i != 0) {
613 double i2;
614 double ret;
615
616 ret = my_modf(x0-l*f, &i2);
617 (*iptr) = l*f + i2;
618 return ret;
619 }
620
621 (*iptr) = l;
622 return x - (*iptr);
623}
624
625
626static void fmtfp (char *buffer, size_t *currlen, size_t maxlen,
627 LDOUBLE fvalue, int min, int max, int flags)
628{
629 int signvalue = 0;
630 double ufvalue;
631 char iconvert[311];
632 char fconvert[311];
633 int iplace = 0;
634 int fplace = 0;
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000635 int padlen = 0; /* amount to pad */
Damien Miller57f39152005-11-24 19:58:19 +1100636 int zpadlen = 0;
637 int caps = 0;
638 int idx;
639 double intpart;
640 double fracpart;
641 double temp;
Damien Miller168e6ac2000-07-11 12:23:01 +1000642
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000643 /*
644 * AIX manpage says the default is 0, but Solaris says the default
645 * is 6, and sprintf on AIX defaults to 6
646 */
647 if (max < 0)
648 max = 6;
Damien Miller168e6ac2000-07-11 12:23:01 +1000649
Damien Miller57f39152005-11-24 19:58:19 +1100650 ufvalue = abs_val (fvalue);
Damien Miller168e6ac2000-07-11 12:23:01 +1000651
Damien Miller57f39152005-11-24 19:58:19 +1100652 if (fvalue < 0) {
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000653 signvalue = '-';
Damien Miller57f39152005-11-24 19:58:19 +1100654 } else {
655 if (flags & DP_F_PLUS) { /* Do a sign (+/i) */
656 signvalue = '+';
657 } else {
658 if (flags & DP_F_SPACE)
659 signvalue = ' ';
660 }
661 }
Damien Miller168e6ac2000-07-11 12:23:01 +1000662
Damien Miller57f39152005-11-24 19:58:19 +1100663#if 0
664 if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
665#endif
666
667#if 0
668 if (max == 0) ufvalue += 0.5; /* if max = 0 we must round */
669#endif
Damien Miller168e6ac2000-07-11 12:23:01 +1000670
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000671 /*
Damien Miller57f39152005-11-24 19:58:19 +1100672 * Sorry, we only support 16 digits past the decimal because of our
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000673 * conversion method
674 */
Damien Miller57f39152005-11-24 19:58:19 +1100675 if (max > 16)
676 max = 16;
Damien Miller168e6ac2000-07-11 12:23:01 +1000677
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000678 /* We "cheat" by converting the fractional part to integer by
679 * multiplying by a factor of 10
680 */
Damien Miller168e6ac2000-07-11 12:23:01 +1000681
Damien Miller57f39152005-11-24 19:58:19 +1100682 temp = ufvalue;
683 my_modf(temp, &intpart);
684
685 fracpart = ROUND((POW10(max)) * (ufvalue - intpart));
686
687 if (fracpart >= POW10(max)) {
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000688 intpart++;
Damien Miller57f39152005-11-24 19:58:19 +1100689 fracpart -= POW10(max);
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000690 }
Damien Miller168e6ac2000-07-11 12:23:01 +1000691
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000692 /* Convert integer part */
693 do {
Damien Miller57f39152005-11-24 19:58:19 +1100694 temp = intpart*0.1;
695 my_modf(temp, &intpart);
696 idx = (int) ((temp -intpart +0.05)* 10.0);
697 /* idx = (int) (((double)(temp*0.1) -intpart +0.05) *10.0); */
698 /* printf ("%llf, %f, %x\n", temp, intpart, idx); */
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000699 iconvert[iplace++] =
Damien Miller57f39152005-11-24 19:58:19 +1100700 (caps? "0123456789ABCDEF":"0123456789abcdef")[idx];
701 } while (intpart && (iplace < 311));
702 if (iplace == 311) iplace--;
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000703 iconvert[iplace] = 0;
Damien Miller168e6ac2000-07-11 12:23:01 +1000704
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000705 /* Convert fractional part */
Damien Miller57f39152005-11-24 19:58:19 +1100706 if (fracpart)
707 {
708 do {
709 temp = fracpart*0.1;
710 my_modf(temp, &fracpart);
711 idx = (int) ((temp -fracpart +0.05)* 10.0);
712 /* idx = (int) ((((temp/10) -fracpart) +0.05) *10); */
713 /* printf ("%lf, %lf, %ld\n", temp, fracpart, idx ); */
714 fconvert[fplace++] =
715 (caps? "0123456789ABCDEF":"0123456789abcdef")[idx];
716 } while(fracpart && (fplace < 311));
717 if (fplace == 311) fplace--;
718 }
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000719 fconvert[fplace] = 0;
Damien Miller57f39152005-11-24 19:58:19 +1100720
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000721 /* -1 for decimal point, another -1 if we are printing a sign */
722 padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
723 zpadlen = max - fplace;
Damien Miller57f39152005-11-24 19:58:19 +1100724 if (zpadlen < 0) zpadlen = 0;
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000725 if (padlen < 0)
726 padlen = 0;
727 if (flags & DP_F_MINUS)
728 padlen = -padlen; /* Left Justifty */
Damien Miller57f39152005-11-24 19:58:19 +1100729
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000730 if ((flags & DP_F_ZERO) && (padlen > 0)) {
731 if (signvalue) {
Damien Miller57f39152005-11-24 19:58:19 +1100732 dopr_outch (buffer, currlen, maxlen, signvalue);
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000733 --padlen;
734 signvalue = 0;
735 }
736 while (padlen > 0) {
Damien Miller57f39152005-11-24 19:58:19 +1100737 dopr_outch (buffer, currlen, maxlen, '0');
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000738 --padlen;
739 }
740 }
741 while (padlen > 0) {
Damien Miller57f39152005-11-24 19:58:19 +1100742 dopr_outch (buffer, currlen, maxlen, ' ');
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000743 --padlen;
744 }
745 if (signvalue)
Damien Miller57f39152005-11-24 19:58:19 +1100746 dopr_outch (buffer, currlen, maxlen, signvalue);
747
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000748 while (iplace > 0)
Damien Miller57f39152005-11-24 19:58:19 +1100749 dopr_outch (buffer, currlen, maxlen, iconvert[--iplace]);
750
751#ifdef DEBUG_SNPRINTF
752 printf("fmtfp: fplace=%d zpadlen=%d\n", fplace, zpadlen);
753#endif
Damien Miller168e6ac2000-07-11 12:23:01 +1000754
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000755 /*
Damien Miller57f39152005-11-24 19:58:19 +1100756 * Decimal point. This should probably use locale to find the correct
757 * char to print out.
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000758 */
Damien Miller57f39152005-11-24 19:58:19 +1100759 if (max > 0) {
760 dopr_outch (buffer, currlen, maxlen, '.');
761
762 while (zpadlen > 0) {
763 dopr_outch (buffer, currlen, maxlen, '0');
764 --zpadlen;
765 }
Damien Miller168e6ac2000-07-11 12:23:01 +1000766
Damien Miller57f39152005-11-24 19:58:19 +1100767 while (fplace > 0)
768 dopr_outch (buffer, currlen, maxlen, fconvert[--fplace]);
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000769 }
Damien Miller168e6ac2000-07-11 12:23:01 +1000770
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000771 while (padlen < 0) {
Damien Miller57f39152005-11-24 19:58:19 +1100772 dopr_outch (buffer, currlen, maxlen, ' ');
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000773 ++padlen;
774 }
Damien Miller168e6ac2000-07-11 12:23:01 +1000775}
776
Damien Miller57f39152005-11-24 19:58:19 +1100777static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c)
Damien Miller168e6ac2000-07-11 12:23:01 +1000778{
Damien Miller57f39152005-11-24 19:58:19 +1100779 if (*currlen < maxlen) {
780 buffer[(*currlen)] = c;
781 }
782 (*currlen)++;
Damien Miller168e6ac2000-07-11 12:23:01 +1000783}
784#endif /* !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF) */
785
Damien Miller57f39152005-11-24 19:58:19 +1100786#if !defined(HAVE_VSNPRINTF)
787int vsnprintf (char *str, size_t count, const char *fmt, va_list args)
Damien Miller168e6ac2000-07-11 12:23:01 +1000788{
Damien Miller57f39152005-11-24 19:58:19 +1100789 return dopr(str, count, fmt, args);
Damien Miller168e6ac2000-07-11 12:23:01 +1000790}
Damien Miller57f39152005-11-24 19:58:19 +1100791#endif
Damien Miller168e6ac2000-07-11 12:23:01 +1000792
Damien Miller57f39152005-11-24 19:58:19 +1100793#if !defined(HAVE_SNPRINTF)
Darren Tuckerd40c66c2005-12-17 22:32:03 +1100794int snprintf(char *str, size_t count, SNPRINTF_CONST char *fmt, ...)
Damien Millerc6b3bbe1999-12-13 08:27:33 +1100795{
Damien Miller57f39152005-11-24 19:58:19 +1100796 size_t ret;
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000797 va_list ap;
798
799 va_start(ap, fmt);
Damien Miller57f39152005-11-24 19:58:19 +1100800 ret = vsnprintf(str, count, fmt, ap);
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000801 va_end(ap);
Damien Miller57f39152005-11-24 19:58:19 +1100802 return ret;
Damien Millerc6b3bbe1999-12-13 08:27:33 +1100803}
Damien Miller57f39152005-11-24 19:58:19 +1100804#endif
Damien Millerc6b3bbe1999-12-13 08:27:33 +1100805