blob: cefb1d1add510e5dac3d69233b7a61ed05040711 [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 Miller742cc1c2007-01-14 21:20:30 +110088 *
89 * Damien Miller (djm@mindrot.org) Jan 2007
90 * Fix integer overflows in return value.
91 * Make formatting quite a bit faster by inlining dopr_outch()
92 *
Damien Miller168e6ac2000-07-11 12:23:01 +100093 **************************************************************/
Damien Miller42b81ff1999-11-26 12:21:24 +110094
Damien Millere9cf3572001-02-09 12:55:35 +110095#include "includes.h"
96
Ben Lindstrom63941f92001-02-25 23:20:40 +000097#if defined(BROKEN_SNPRINTF) /* For those with broken snprintf() */
98# undef HAVE_SNPRINTF
99# undef HAVE_VSNPRINTF
100#endif
Damien Miller42b81ff1999-11-26 12:21:24 +1100101
Damien Miller57f39152005-11-24 19:58:19 +1100102#ifndef VA_COPY
103# ifdef HAVE_VA_COPY
104# define VA_COPY(dest, src) va_copy(dest, src)
105# else
106# ifdef HAVE___VA_COPY
107# define VA_COPY(dest, src) __va_copy(dest, src)
108# else
109# define VA_COPY(dest, src) (dest) = (src)
110# endif
111# endif
112#endif
113
Damien Miller168e6ac2000-07-11 12:23:01 +1000114#if !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF)
Damien Miller42b81ff1999-11-26 12:21:24 +1100115
Darren Tuckerf78fb542006-08-06 21:25:24 +1000116#include <ctype.h>
Darren Tucker08432d52006-09-09 15:59:43 +1000117#include <stdarg.h>
Darren Tuckerf78fb542006-08-06 21:25:24 +1000118#include <stdlib.h>
Damien Miller62da44f2006-07-24 15:08:35 +1000119#include <string.h>
Damien Miller742cc1c2007-01-14 21:20:30 +1100120#include <limits.h>
121#include <errno.h>
Damien Miller62da44f2006-07-24 15:08:35 +1000122
Damien Miller57f39152005-11-24 19:58:19 +1100123#ifdef HAVE_LONG_DOUBLE
124# define LDOUBLE long double
125#else
126# define LDOUBLE double
127#endif
Damien Miller168e6ac2000-07-11 12:23:01 +1000128
Damien Miller57f39152005-11-24 19:58:19 +1100129#ifdef HAVE_LONG_LONG
130# define LLONG long long
131#else
132# define LLONG long
133#endif
Ben Lindstrom116b6bd2001-02-13 14:05:59 +0000134
Damien Miller168e6ac2000-07-11 12:23:01 +1000135/*
136 * dopr(): poor man's version of doprintf
137 */
138
139/* format read states */
140#define DP_S_DEFAULT 0
141#define DP_S_FLAGS 1
142#define DP_S_MIN 2
143#define DP_S_DOT 3
144#define DP_S_MAX 4
145#define DP_S_MOD 5
146#define DP_S_CONV 6
147#define DP_S_DONE 7
148
149/* format flags - Bits */
150#define DP_F_MINUS (1 << 0)
151#define DP_F_PLUS (1 << 1)
152#define DP_F_SPACE (1 << 2)
153#define DP_F_NUM (1 << 3)
154#define DP_F_ZERO (1 << 4)
155#define DP_F_UP (1 << 5)
156#define DP_F_UNSIGNED (1 << 6)
157
158/* Conversion Flags */
Damien Miller57f39152005-11-24 19:58:19 +1100159#define DP_C_SHORT 1
160#define DP_C_LONG 2
161#define DP_C_LDOUBLE 3
162#define DP_C_LLONG 4
Damien Miller168e6ac2000-07-11 12:23:01 +1000163
Damien Miller57f39152005-11-24 19:58:19 +1100164#define char_to_int(p) ((p)- '0')
165#ifndef MAX
166# define MAX(p,q) (((p) >= (q)) ? (p) : (q))
167#endif
Damien Miller168e6ac2000-07-11 12:23:01 +1000168
Damien Miller742cc1c2007-01-14 21:20:30 +1100169#define DOPR_OUTCH(buf, pos, buflen, thechar) \
170 do { \
171 if (++pos >= INT_MAX) { \
172 errno = ERANGE; \
173 return -1; \
174 if (pos < buflen) \
175 buf[pos] = thechar; \
176 } \
177 } while (0)
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000178
Damien Miller742cc1c2007-01-14 21:20:30 +1100179static int dopr(char *buffer, size_t maxlen, const char *format,
180 va_list args_in);
181static int fmtstr(char *buffer, size_t *currlen, size_t maxlen,
182 char *value, int flags, int min, int max);
183static int fmtint(char *buffer, size_t *currlen, size_t maxlen,
184 LLONG value, int base, int min, int max, int flags);
185static int fmtfp(char *buffer, size_t *currlen, size_t maxlen,
186 LDOUBLE fvalue, int min, int max, int flags);
187
188static int
189dopr(char *buffer, size_t maxlen, const char *format, va_list args_in)
Damien Miller13bc0be1999-12-28 10:19:16 +1100190{
Damien Miller57f39152005-11-24 19:58:19 +1100191 char ch;
192 LLONG value;
193 LDOUBLE fvalue;
194 char *strvalue;
195 int min;
196 int max;
197 int state;
198 int flags;
199 int cflags;
200 size_t currlen;
201 va_list args;
Damien Miller13bc0be1999-12-28 10:19:16 +1100202
Damien Miller57f39152005-11-24 19:58:19 +1100203 VA_COPY(args, args_in);
204
205 state = DP_S_DEFAULT;
206 currlen = flags = cflags = min = 0;
207 max = -1;
208 ch = *format++;
209
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000210 while (state != DP_S_DONE) {
Damien Miller57f39152005-11-24 19:58:19 +1100211 if (ch == '\0')
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000212 state = DP_S_DONE;
Damien Miller42b81ff1999-11-26 12:21:24 +1100213
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000214 switch(state) {
Damien Miller31741252003-05-19 00:13:38 +1000215 case DP_S_DEFAULT:
216 if (ch == '%')
217 state = DP_S_FLAGS;
Damien Miller742cc1c2007-01-14 21:20:30 +1100218 else
219 DOPR_OUTCH(buffer, currlen, maxlen, ch);
Damien Miller31741252003-05-19 00:13:38 +1000220 ch = *format++;
221 break;
222 case DP_S_FLAGS:
223 switch (ch) {
224 case '-':
225 flags |= DP_F_MINUS;
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000226 ch = *format++;
227 break;
Damien Miller31741252003-05-19 00:13:38 +1000228 case '+':
229 flags |= DP_F_PLUS;
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000230 ch = *format++;
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000231 break;
Damien Miller31741252003-05-19 00:13:38 +1000232 case ' ':
233 flags |= DP_F_SPACE;
234 ch = *format++;
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000235 break;
Damien Miller31741252003-05-19 00:13:38 +1000236 case '#':
237 flags |= DP_F_NUM;
238 ch = *format++;
239 break;
240 case '0':
241 flags |= DP_F_ZERO;
242 ch = *format++;
243 break;
244 default:
245 state = DP_S_MIN;
246 break;
247 }
248 break;
249 case DP_S_MIN:
250 if (isdigit((unsigned char)ch)) {
Damien Miller57f39152005-11-24 19:58:19 +1100251 min = 10*min + char_to_int (ch);
Damien Miller31741252003-05-19 00:13:38 +1000252 ch = *format++;
253 } else if (ch == '*') {
254 min = va_arg (args, int);
255 ch = *format++;
256 state = DP_S_DOT;
Damien Miller57f39152005-11-24 19:58:19 +1100257 } else {
Damien Miller31741252003-05-19 00:13:38 +1000258 state = DP_S_DOT;
Damien Miller57f39152005-11-24 19:58:19 +1100259 }
Damien Miller31741252003-05-19 00:13:38 +1000260 break;
261 case DP_S_DOT:
262 if (ch == '.') {
263 state = DP_S_MAX;
264 ch = *format++;
Damien Miller57f39152005-11-24 19:58:19 +1100265 } else {
Damien Miller31741252003-05-19 00:13:38 +1000266 state = DP_S_MOD;
Damien Miller57f39152005-11-24 19:58:19 +1100267 }
Damien Miller31741252003-05-19 00:13:38 +1000268 break;
269 case DP_S_MAX:
270 if (isdigit((unsigned char)ch)) {
271 if (max < 0)
272 max = 0;
Damien Miller57f39152005-11-24 19:58:19 +1100273 max = 10*max + char_to_int (ch);
Damien Miller31741252003-05-19 00:13:38 +1000274 ch = *format++;
275 } else if (ch == '*') {
276 max = va_arg (args, int);
277 ch = *format++;
278 state = DP_S_MOD;
Damien Miller57f39152005-11-24 19:58:19 +1100279 } else {
Damien Miller31741252003-05-19 00:13:38 +1000280 state = DP_S_MOD;
Damien Miller57f39152005-11-24 19:58:19 +1100281 }
Damien Miller31741252003-05-19 00:13:38 +1000282 break;
283 case DP_S_MOD:
284 switch (ch) {
285 case 'h':
286 cflags = DP_C_SHORT;
287 ch = *format++;
288 break;
289 case 'l':
290 cflags = DP_C_LONG;
291 ch = *format++;
Damien Miller57f39152005-11-24 19:58:19 +1100292 if (ch == 'l') { /* It's a long long */
293 cflags = DP_C_LLONG;
Damien Miller31741252003-05-19 00:13:38 +1000294 ch = *format++;
295 }
296 break;
Damien Miller31741252003-05-19 00:13:38 +1000297 case 'L':
298 cflags = DP_C_LDOUBLE;
299 ch = *format++;
300 break;
301 default:
302 break;
303 }
304 state = DP_S_CONV;
305 break;
306 case DP_S_CONV:
307 switch (ch) {
308 case 'd':
309 case 'i':
310 if (cflags == DP_C_SHORT)
Damien Miller57f39152005-11-24 19:58:19 +1100311 value = va_arg (args, int);
Damien Miller31741252003-05-19 00:13:38 +1000312 else if (cflags == DP_C_LONG)
Damien Miller57f39152005-11-24 19:58:19 +1100313 value = va_arg (args, long int);
314 else if (cflags == DP_C_LLONG)
315 value = va_arg (args, LLONG);
Damien Miller31741252003-05-19 00:13:38 +1000316 else
317 value = va_arg (args, int);
Damien Miller742cc1c2007-01-14 21:20:30 +1100318 if (fmtint(buffer, &currlen, maxlen,
319 value, 10, min, max, flags) == -1)
320 return -1;
Damien Miller31741252003-05-19 00:13:38 +1000321 break;
322 case 'o':
323 flags |= DP_F_UNSIGNED;
324 if (cflags == DP_C_SHORT)
Damien Miller57f39152005-11-24 19:58:19 +1100325 value = va_arg (args, unsigned int);
Damien Miller31741252003-05-19 00:13:38 +1000326 else if (cflags == DP_C_LONG)
Damien Miller57f39152005-11-24 19:58:19 +1100327 value = (long)va_arg (args, unsigned long int);
328 else if (cflags == DP_C_LLONG)
329 value = (long)va_arg (args, unsigned LLONG);
Damien Miller31741252003-05-19 00:13:38 +1000330 else
Damien Miller57f39152005-11-24 19:58:19 +1100331 value = (long)va_arg (args, unsigned int);
Damien Miller742cc1c2007-01-14 21:20:30 +1100332 if (fmtint(buffer, &currlen, maxlen, value,
333 8, min, max, flags) == -1)
334 return -1;
Damien Miller31741252003-05-19 00:13:38 +1000335 break;
336 case 'u':
337 flags |= DP_F_UNSIGNED;
338 if (cflags == DP_C_SHORT)
Damien Miller57f39152005-11-24 19:58:19 +1100339 value = va_arg (args, unsigned int);
Damien Miller31741252003-05-19 00:13:38 +1000340 else if (cflags == DP_C_LONG)
Damien Miller57f39152005-11-24 19:58:19 +1100341 value = (long)va_arg (args, unsigned long int);
342 else if (cflags == DP_C_LLONG)
343 value = (LLONG)va_arg (args, unsigned LLONG);
Damien Miller31741252003-05-19 00:13:38 +1000344 else
Damien Miller57f39152005-11-24 19:58:19 +1100345 value = (long)va_arg (args, unsigned int);
Damien Miller742cc1c2007-01-14 21:20:30 +1100346 if (fmtint(buffer, &currlen, maxlen, value,
347 10, min, max, flags) == -1)
348 return -1;
Damien Miller31741252003-05-19 00:13:38 +1000349 break;
350 case 'X':
351 flags |= DP_F_UP;
352 case 'x':
353 flags |= DP_F_UNSIGNED;
354 if (cflags == DP_C_SHORT)
Damien Miller57f39152005-11-24 19:58:19 +1100355 value = va_arg (args, unsigned int);
Damien Miller31741252003-05-19 00:13:38 +1000356 else if (cflags == DP_C_LONG)
Damien Miller57f39152005-11-24 19:58:19 +1100357 value = (long)va_arg (args, unsigned long int);
358 else if (cflags == DP_C_LLONG)
359 value = (LLONG)va_arg (args, unsigned LLONG);
Damien Miller31741252003-05-19 00:13:38 +1000360 else
Damien Miller57f39152005-11-24 19:58:19 +1100361 value = (long)va_arg (args, unsigned int);
Damien Miller742cc1c2007-01-14 21:20:30 +1100362 if (fmtint(buffer, &currlen, maxlen, value,
363 16, min, max, flags) == -1)
364 return -1;
Damien Miller31741252003-05-19 00:13:38 +1000365 break;
366 case 'f':
367 if (cflags == DP_C_LDOUBLE)
Damien Miller57f39152005-11-24 19:58:19 +1100368 fvalue = va_arg (args, LDOUBLE);
Damien Miller31741252003-05-19 00:13:38 +1000369 else
Damien Miller57f39152005-11-24 19:58:19 +1100370 fvalue = va_arg (args, double);
Damien Miller742cc1c2007-01-14 21:20:30 +1100371 if (fmtfp(buffer, &currlen, maxlen, fvalue,
372 min, max, flags) == -1)
373 return -1;
Damien Miller31741252003-05-19 00:13:38 +1000374 break;
375 case 'E':
376 flags |= DP_F_UP;
377 case 'e':
378 if (cflags == DP_C_LDOUBLE)
Damien Miller57f39152005-11-24 19:58:19 +1100379 fvalue = va_arg (args, LDOUBLE);
Damien Miller31741252003-05-19 00:13:38 +1000380 else
Damien Miller57f39152005-11-24 19:58:19 +1100381 fvalue = va_arg (args, double);
Damien Miller742cc1c2007-01-14 21:20:30 +1100382 if (fmtfp(buffer, &currlen, maxlen, fvalue,
383 min, max, flags) == -1)
384 return -1;
Damien Miller31741252003-05-19 00:13:38 +1000385 break;
386 case 'G':
387 flags |= DP_F_UP;
388 case 'g':
389 if (cflags == DP_C_LDOUBLE)
Damien Miller57f39152005-11-24 19:58:19 +1100390 fvalue = va_arg (args, LDOUBLE);
Damien Miller31741252003-05-19 00:13:38 +1000391 else
Damien Miller57f39152005-11-24 19:58:19 +1100392 fvalue = va_arg (args, double);
Damien Miller742cc1c2007-01-14 21:20:30 +1100393 if (fmtfp(buffer, &currlen, maxlen, fvalue,
394 min, max, flags) == -1)
395 return -1;
Damien Miller31741252003-05-19 00:13:38 +1000396 break;
397 case 'c':
Damien Miller742cc1c2007-01-14 21:20:30 +1100398 DOPR_OUTCH(buffer, currlen, maxlen,
399 va_arg (args, int));
Damien Miller31741252003-05-19 00:13:38 +1000400 break;
401 case 's':
Damien Miller57f39152005-11-24 19:58:19 +1100402 strvalue = va_arg (args, char *);
403 if (!strvalue) strvalue = "(NULL)";
404 if (max == -1) {
405 max = strlen(strvalue);
406 }
407 if (min > 0 && max >= 0 && min > max) max = min;
Damien Miller742cc1c2007-01-14 21:20:30 +1100408 if (fmtstr(buffer, &currlen, maxlen,
409 strvalue, flags, min, max) == -1)
410 return -1;
Damien Miller31741252003-05-19 00:13:38 +1000411 break;
412 case 'p':
Damien Miller57f39152005-11-24 19:58:19 +1100413 strvalue = va_arg (args, void *);
Damien Miller742cc1c2007-01-14 21:20:30 +1100414 if (fmtint(buffer, &currlen, maxlen,
415 (long) strvalue, 16, min, max, flags) == -1)
416 return -1;
Damien Miller31741252003-05-19 00:13:38 +1000417 break;
418 case 'n':
419 if (cflags == DP_C_SHORT) {
420 short int *num;
Damien Miller57f39152005-11-24 19:58:19 +1100421 num = va_arg (args, short int *);
Damien Miller31741252003-05-19 00:13:38 +1000422 *num = currlen;
423 } else if (cflags == DP_C_LONG) {
424 long int *num;
Damien Miller57f39152005-11-24 19:58:19 +1100425 num = va_arg (args, long int *);
426 *num = (long int)currlen;
427 } else if (cflags == DP_C_LLONG) {
428 LLONG *num;
429 num = va_arg (args, LLONG *);
430 *num = (LLONG)currlen;
Damien Miller31741252003-05-19 00:13:38 +1000431 } else {
432 int *num;
Damien Miller57f39152005-11-24 19:58:19 +1100433 num = va_arg (args, int *);
Damien Miller31741252003-05-19 00:13:38 +1000434 *num = currlen;
435 }
436 break;
437 case '%':
Damien Miller742cc1c2007-01-14 21:20:30 +1100438 DOPR_OUTCH(buffer, currlen, maxlen, ch);
Damien Miller31741252003-05-19 00:13:38 +1000439 break;
Damien Miller57f39152005-11-24 19:58:19 +1100440 case 'w':
441 /* not supported yet, treat as next char */
Damien Miller31741252003-05-19 00:13:38 +1000442 ch = *format++;
443 break;
Damien Miller57f39152005-11-24 19:58:19 +1100444 default:
445 /* Unknown, skip */
446 break;
Damien Miller31741252003-05-19 00:13:38 +1000447 }
448 ch = *format++;
449 state = DP_S_DEFAULT;
450 flags = cflags = min = 0;
451 max = -1;
452 break;
453 case DP_S_DONE:
454 break;
Damien Miller57f39152005-11-24 19:58:19 +1100455 default:
456 /* hmm? */
Damien Miller31741252003-05-19 00:13:38 +1000457 break; /* some picky compilers need this */
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000458 }
459 }
Damien Miller57f39152005-11-24 19:58:19 +1100460 if (maxlen != 0) {
461 if (currlen < maxlen - 1)
462 buffer[currlen] = '\0';
463 else if (maxlen > 0)
464 buffer[maxlen - 1] = '\0';
465 }
466
Damien Miller742cc1c2007-01-14 21:20:30 +1100467 return currlen < INT_MAX ? (int)currlen : -1;
Damien Miller42b81ff1999-11-26 12:21:24 +1100468}
469
Damien Miller742cc1c2007-01-14 21:20:30 +1100470static int
471fmtstr(char *buffer, size_t *currlen, size_t maxlen,
472 char *value, int flags, int min, int max)
Damien Miller42b81ff1999-11-26 12:21:24 +1100473{
Damien Miller57f39152005-11-24 19:58:19 +1100474 int padlen, strln; /* amount to pad */
475 int cnt = 0;
476
477#ifdef DEBUG_SNPRINTF
478 printf("fmtstr min=%d max=%d s=[%s]\n", min, max, value);
479#endif
480 if (value == 0) {
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000481 value = "<NULL>";
Damien Miller57f39152005-11-24 19:58:19 +1100482 }
Damien Miller168e6ac2000-07-11 12:23:01 +1000483
Darren Tucker4127f552004-09-23 21:35:09 +1000484 for (strln = 0; strln < max && value[strln]; ++strln); /* strlen */
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000485 padlen = min - strln;
486 if (padlen < 0)
487 padlen = 0;
488 if (flags & DP_F_MINUS)
489 padlen = -padlen; /* Left Justify */
Damien Miller57f39152005-11-24 19:58:19 +1100490
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000491 while ((padlen > 0) && (cnt < max)) {
Damien Miller742cc1c2007-01-14 21:20:30 +1100492 DOPR_OUTCH(buffer, *currlen, maxlen, ' ');
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000493 --padlen;
494 ++cnt;
495 }
496 while (*value && (cnt < max)) {
Damien Miller742cc1c2007-01-14 21:20:30 +1100497 DOPR_OUTCH(buffer, *currlen, maxlen, *value++);
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000498 ++cnt;
499 }
500 while ((padlen < 0) && (cnt < max)) {
Damien Miller742cc1c2007-01-14 21:20:30 +1100501 DOPR_OUTCH(buffer, *currlen, maxlen, ' ');
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000502 ++padlen;
503 ++cnt;
504 }
Damien Miller742cc1c2007-01-14 21:20:30 +1100505 return 0;
Damien Miller42b81ff1999-11-26 12:21:24 +1100506}
507
Damien Miller168e6ac2000-07-11 12:23:01 +1000508/* Have to handle DP_F_NUM (ie 0x and 0 alternates) */
509
Damien Miller742cc1c2007-01-14 21:20:30 +1100510static int
511fmtint(char *buffer, size_t *currlen, size_t maxlen,
Darren Tucker9834cab2006-03-19 00:07:07 +1100512 LLONG value, int base, int min, int max, int flags)
Damien Miller42b81ff1999-11-26 12:21:24 +1100513{
Damien Miller57f39152005-11-24 19:58:19 +1100514 int signvalue = 0;
Darren Tucker9834cab2006-03-19 00:07:07 +1100515 unsigned LLONG uvalue;
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000516 char convert[20];
Damien Miller57f39152005-11-24 19:58:19 +1100517 int place = 0;
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000518 int spadlen = 0; /* amount to space pad */
519 int zpadlen = 0; /* amount to zero pad */
Damien Miller57f39152005-11-24 19:58:19 +1100520 int caps = 0;
521
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000522 if (max < 0)
523 max = 0;
Damien Miller57f39152005-11-24 19:58:19 +1100524
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000525 uvalue = value;
Damien Miller57f39152005-11-24 19:58:19 +1100526
527 if(!(flags & DP_F_UNSIGNED)) {
528 if( value < 0 ) {
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000529 signvalue = '-';
530 uvalue = -value;
Damien Miller57f39152005-11-24 19:58:19 +1100531 } else {
532 if (flags & DP_F_PLUS) /* Do a sign (+/i) */
533 signvalue = '+';
534 else if (flags & DP_F_SPACE)
535 signvalue = ' ';
536 }
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000537 }
Damien Miller168e6ac2000-07-11 12:23:01 +1000538
Damien Miller57f39152005-11-24 19:58:19 +1100539 if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
540
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000541 do {
542 convert[place++] =
Damien Miller57f39152005-11-24 19:58:19 +1100543 (caps? "0123456789ABCDEF":"0123456789abcdef")
544 [uvalue % (unsigned)base ];
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000545 uvalue = (uvalue / (unsigned)base );
Damien Miller57f39152005-11-24 19:58:19 +1100546 } while(uvalue && (place < 20));
547 if (place == 20) place--;
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000548 convert[place] = 0;
Damien Miller168e6ac2000-07-11 12:23:01 +1000549
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000550 zpadlen = max - place;
551 spadlen = min - MAX (max, place) - (signvalue ? 1 : 0);
Damien Miller57f39152005-11-24 19:58:19 +1100552 if (zpadlen < 0) zpadlen = 0;
553 if (spadlen < 0) spadlen = 0;
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000554 if (flags & DP_F_ZERO) {
555 zpadlen = MAX(zpadlen, spadlen);
556 spadlen = 0;
557 }
558 if (flags & DP_F_MINUS)
559 spadlen = -spadlen; /* Left Justifty */
Damien Miller168e6ac2000-07-11 12:23:01 +1000560
Damien Miller57f39152005-11-24 19:58:19 +1100561#ifdef DEBUG_SNPRINTF
562 printf("zpad: %d, spad: %d, min: %d, max: %d, place: %d\n",
563 zpadlen, spadlen, min, max, place);
564#endif
565
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000566 /* Spaces */
567 while (spadlen > 0) {
Damien Miller742cc1c2007-01-14 21:20:30 +1100568 DOPR_OUTCH(buffer, *currlen, maxlen, ' ');
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000569 --spadlen;
570 }
Damien Miller168e6ac2000-07-11 12:23:01 +1000571
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000572 /* Sign */
573 if (signvalue)
Damien Miller742cc1c2007-01-14 21:20:30 +1100574 DOPR_OUTCH(buffer, *currlen, maxlen, signvalue);
Damien Miller168e6ac2000-07-11 12:23:01 +1000575
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000576 /* Zeros */
577 if (zpadlen > 0) {
578 while (zpadlen > 0) {
Damien Miller742cc1c2007-01-14 21:20:30 +1100579 DOPR_OUTCH(buffer, *currlen, maxlen, '0');
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000580 --zpadlen;
581 }
582 }
Damien Miller168e6ac2000-07-11 12:23:01 +1000583
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000584 /* Digits */
585 while (place > 0)
Damien Miller742cc1c2007-01-14 21:20:30 +1100586 DOPR_OUTCH(buffer, *currlen, maxlen, convert[--place]);
Damien Miller168e6ac2000-07-11 12:23:01 +1000587
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000588 /* Left Justified spaces */
589 while (spadlen < 0) {
Damien Miller742cc1c2007-01-14 21:20:30 +1100590 DOPR_OUTCH(buffer, *currlen, maxlen, ' ');
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000591 ++spadlen;
592 }
Damien Miller742cc1c2007-01-14 21:20:30 +1100593 return 0;
Damien Miller42b81ff1999-11-26 12:21:24 +1100594}
595
Damien Miller57f39152005-11-24 19:58:19 +1100596static LDOUBLE abs_val(LDOUBLE value)
Damien Miller42b81ff1999-11-26 12:21:24 +1100597{
Damien Miller57f39152005-11-24 19:58:19 +1100598 LDOUBLE result = value;
Damien Miller42b81ff1999-11-26 12:21:24 +1100599
Damien Miller57f39152005-11-24 19:58:19 +1100600 if (value < 0)
601 result = -value;
602
603 return result;
604}
605
Damien Miller742cc1c2007-01-14 21:20:30 +1100606static LDOUBLE POW10(int val)
Damien Miller57f39152005-11-24 19:58:19 +1100607{
608 LDOUBLE result = 1;
609
Damien Miller742cc1c2007-01-14 21:20:30 +1100610 while (val) {
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000611 result *= 10;
Damien Miller742cc1c2007-01-14 21:20:30 +1100612 val--;
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000613 }
Damien Miller168e6ac2000-07-11 12:23:01 +1000614
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000615 return result;
Damien Miller168e6ac2000-07-11 12:23:01 +1000616}
617
Damien Miller57f39152005-11-24 19:58:19 +1100618static LLONG ROUND(LDOUBLE value)
Damien Miller168e6ac2000-07-11 12:23:01 +1000619{
Damien Miller57f39152005-11-24 19:58:19 +1100620 LLONG intpart;
Damien Miller168e6ac2000-07-11 12:23:01 +1000621
Damien Miller57f39152005-11-24 19:58:19 +1100622 intpart = (LLONG)value;
623 value = value - intpart;
624 if (value >= 0.5) intpart++;
625
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000626 return intpart;
Damien Miller168e6ac2000-07-11 12:23:01 +1000627}
628
Damien Miller57f39152005-11-24 19:58:19 +1100629/* a replacement for modf that doesn't need the math library. Should
630 be portable, but slow */
631static double my_modf(double x0, double *iptr)
Damien Miller168e6ac2000-07-11 12:23:01 +1000632{
Damien Miller57f39152005-11-24 19:58:19 +1100633 int i;
634 long l;
635 double x = x0;
636 double f = 1.0;
637
638 for (i=0;i<100;i++) {
639 l = (long)x;
640 if (l <= (x+1) && l >= (x-1)) break;
641 x *= 0.1;
642 f *= 10.0;
643 }
644
645 if (i == 100) {
Damien Miller742cc1c2007-01-14 21:20:30 +1100646 /*
647 * yikes! the number is beyond what we can handle.
648 * What do we do?
649 */
Damien Miller57f39152005-11-24 19:58:19 +1100650 (*iptr) = 0;
651 return 0;
652 }
653
654 if (i != 0) {
655 double i2;
656 double ret;
657
658 ret = my_modf(x0-l*f, &i2);
659 (*iptr) = l*f + i2;
660 return ret;
661 }
662
663 (*iptr) = l;
664 return x - (*iptr);
665}
666
667
Damien Miller742cc1c2007-01-14 21:20:30 +1100668static int
669fmtfp (char *buffer, size_t *currlen, size_t maxlen,
670 LDOUBLE fvalue, int min, int max, int flags)
Damien Miller57f39152005-11-24 19:58:19 +1100671{
672 int signvalue = 0;
673 double ufvalue;
674 char iconvert[311];
675 char fconvert[311];
676 int iplace = 0;
677 int fplace = 0;
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000678 int padlen = 0; /* amount to pad */
Damien Miller57f39152005-11-24 19:58:19 +1100679 int zpadlen = 0;
680 int caps = 0;
681 int idx;
682 double intpart;
683 double fracpart;
684 double temp;
Damien Miller168e6ac2000-07-11 12:23:01 +1000685
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000686 /*
687 * AIX manpage says the default is 0, but Solaris says the default
688 * is 6, and sprintf on AIX defaults to 6
689 */
690 if (max < 0)
691 max = 6;
Damien Miller168e6ac2000-07-11 12:23:01 +1000692
Damien Miller57f39152005-11-24 19:58:19 +1100693 ufvalue = abs_val (fvalue);
Damien Miller168e6ac2000-07-11 12:23:01 +1000694
Damien Miller57f39152005-11-24 19:58:19 +1100695 if (fvalue < 0) {
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000696 signvalue = '-';
Damien Miller57f39152005-11-24 19:58:19 +1100697 } else {
698 if (flags & DP_F_PLUS) { /* Do a sign (+/i) */
699 signvalue = '+';
700 } else {
701 if (flags & DP_F_SPACE)
702 signvalue = ' ';
703 }
704 }
Damien Miller168e6ac2000-07-11 12:23:01 +1000705
Damien Miller57f39152005-11-24 19:58:19 +1100706#if 0
707 if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
708#endif
709
710#if 0
711 if (max == 0) ufvalue += 0.5; /* if max = 0 we must round */
712#endif
Damien Miller168e6ac2000-07-11 12:23:01 +1000713
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000714 /*
Damien Miller57f39152005-11-24 19:58:19 +1100715 * Sorry, we only support 16 digits past the decimal because of our
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000716 * conversion method
717 */
Damien Miller57f39152005-11-24 19:58:19 +1100718 if (max > 16)
719 max = 16;
Damien Miller168e6ac2000-07-11 12:23:01 +1000720
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000721 /* We "cheat" by converting the fractional part to integer by
722 * multiplying by a factor of 10
723 */
Damien Miller168e6ac2000-07-11 12:23:01 +1000724
Damien Miller57f39152005-11-24 19:58:19 +1100725 temp = ufvalue;
726 my_modf(temp, &intpart);
727
728 fracpart = ROUND((POW10(max)) * (ufvalue - intpart));
729
730 if (fracpart >= POW10(max)) {
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000731 intpart++;
Damien Miller57f39152005-11-24 19:58:19 +1100732 fracpart -= POW10(max);
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000733 }
Damien Miller168e6ac2000-07-11 12:23:01 +1000734
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000735 /* Convert integer part */
736 do {
Damien Miller57f39152005-11-24 19:58:19 +1100737 temp = intpart*0.1;
738 my_modf(temp, &intpart);
739 idx = (int) ((temp -intpart +0.05)* 10.0);
740 /* idx = (int) (((double)(temp*0.1) -intpart +0.05) *10.0); */
741 /* printf ("%llf, %f, %x\n", temp, intpart, idx); */
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000742 iconvert[iplace++] =
Damien Miller57f39152005-11-24 19:58:19 +1100743 (caps? "0123456789ABCDEF":"0123456789abcdef")[idx];
744 } while (intpart && (iplace < 311));
745 if (iplace == 311) iplace--;
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000746 iconvert[iplace] = 0;
Damien Miller168e6ac2000-07-11 12:23:01 +1000747
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000748 /* Convert fractional part */
Damien Miller57f39152005-11-24 19:58:19 +1100749 if (fracpart)
750 {
751 do {
752 temp = fracpart*0.1;
753 my_modf(temp, &fracpart);
754 idx = (int) ((temp -fracpart +0.05)* 10.0);
755 /* idx = (int) ((((temp/10) -fracpart) +0.05) *10); */
756 /* printf ("%lf, %lf, %ld\n", temp, fracpart, idx ); */
757 fconvert[fplace++] =
758 (caps? "0123456789ABCDEF":"0123456789abcdef")[idx];
759 } while(fracpart && (fplace < 311));
760 if (fplace == 311) fplace--;
761 }
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000762 fconvert[fplace] = 0;
Damien Miller57f39152005-11-24 19:58:19 +1100763
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000764 /* -1 for decimal point, another -1 if we are printing a sign */
765 padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
766 zpadlen = max - fplace;
Damien Miller57f39152005-11-24 19:58:19 +1100767 if (zpadlen < 0) zpadlen = 0;
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000768 if (padlen < 0)
769 padlen = 0;
770 if (flags & DP_F_MINUS)
771 padlen = -padlen; /* Left Justifty */
Damien Miller57f39152005-11-24 19:58:19 +1100772
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000773 if ((flags & DP_F_ZERO) && (padlen > 0)) {
774 if (signvalue) {
Damien Miller742cc1c2007-01-14 21:20:30 +1100775 DOPR_OUTCH(buffer, *currlen, maxlen, signvalue);
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000776 --padlen;
777 signvalue = 0;
778 }
779 while (padlen > 0) {
Damien Miller742cc1c2007-01-14 21:20:30 +1100780 DOPR_OUTCH(buffer, *currlen, maxlen, '0');
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000781 --padlen;
782 }
783 }
784 while (padlen > 0) {
Damien Miller742cc1c2007-01-14 21:20:30 +1100785 DOPR_OUTCH(buffer, *currlen, maxlen, ' ');
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000786 --padlen;
787 }
788 if (signvalue)
Damien Miller742cc1c2007-01-14 21:20:30 +1100789 DOPR_OUTCH(buffer, *currlen, maxlen, signvalue);
Damien Miller57f39152005-11-24 19:58:19 +1100790
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000791 while (iplace > 0)
Damien Miller742cc1c2007-01-14 21:20:30 +1100792 DOPR_OUTCH(buffer, *currlen, maxlen, iconvert[--iplace]);
Damien Miller57f39152005-11-24 19:58:19 +1100793
794#ifdef DEBUG_SNPRINTF
795 printf("fmtfp: fplace=%d zpadlen=%d\n", fplace, zpadlen);
796#endif
Damien Miller168e6ac2000-07-11 12:23:01 +1000797
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000798 /*
Damien Miller57f39152005-11-24 19:58:19 +1100799 * Decimal point. This should probably use locale to find the correct
800 * char to print out.
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000801 */
Damien Miller57f39152005-11-24 19:58:19 +1100802 if (max > 0) {
Damien Miller742cc1c2007-01-14 21:20:30 +1100803 DOPR_OUTCH(buffer, *currlen, maxlen, '.');
Damien Miller57f39152005-11-24 19:58:19 +1100804
805 while (zpadlen > 0) {
Damien Miller742cc1c2007-01-14 21:20:30 +1100806 DOPR_OUTCH(buffer, *currlen, maxlen, '0');
Damien Miller57f39152005-11-24 19:58:19 +1100807 --zpadlen;
808 }
Damien Miller168e6ac2000-07-11 12:23:01 +1000809
Damien Miller57f39152005-11-24 19:58:19 +1100810 while (fplace > 0)
Damien Miller742cc1c2007-01-14 21:20:30 +1100811 DOPR_OUTCH(buffer, *currlen, maxlen,
812 fconvert[--fplace]);
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000813 }
Damien Miller168e6ac2000-07-11 12:23:01 +1000814
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000815 while (padlen < 0) {
Damien Miller742cc1c2007-01-14 21:20:30 +1100816 DOPR_OUTCH(buffer, *currlen, maxlen, ' ');
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000817 ++padlen;
818 }
Damien Miller742cc1c2007-01-14 21:20:30 +1100819 return 0;
Damien Miller168e6ac2000-07-11 12:23:01 +1000820}
821#endif /* !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF) */
822
Damien Miller57f39152005-11-24 19:58:19 +1100823#if !defined(HAVE_VSNPRINTF)
Damien Miller742cc1c2007-01-14 21:20:30 +1100824static int
825vsnprintf (char *str, size_t count, const char *fmt, va_list args)
Damien Miller168e6ac2000-07-11 12:23:01 +1000826{
Damien Miller57f39152005-11-24 19:58:19 +1100827 return dopr(str, count, fmt, args);
Damien Miller168e6ac2000-07-11 12:23:01 +1000828}
Damien Miller57f39152005-11-24 19:58:19 +1100829#endif
Damien Miller168e6ac2000-07-11 12:23:01 +1000830
Damien Miller57f39152005-11-24 19:58:19 +1100831#if !defined(HAVE_SNPRINTF)
Damien Miller742cc1c2007-01-14 21:20:30 +1100832static int
833snprintf(char *str, size_t count, const char *fmt, ...)
Damien Millerc6b3bbe1999-12-13 08:27:33 +1100834{
Damien Miller57f39152005-11-24 19:58:19 +1100835 size_t ret;
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000836 va_list ap;
837
838 va_start(ap, fmt);
Damien Miller57f39152005-11-24 19:58:19 +1100839 ret = vsnprintf(str, count, fmt, ap);
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000840 va_end(ap);
Damien Miller57f39152005-11-24 19:58:19 +1100841 return ret;
Damien Millerc6b3bbe1999-12-13 08:27:33 +1100842}
Damien Miller57f39152005-11-24 19:58:19 +1100843#endif