blob: 41d2be23899c14414f28397a307626702cae8d5e [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 { \
Darren Tucker07877ca2007-01-24 00:07:29 +1100171 if (pos + 1 >= INT_MAX) { \
Damien Miller742cc1c2007-01-14 21:20:30 +1100172 errno = ERANGE; \
173 return -1; \
Darren Tucker07877ca2007-01-24 00:07:29 +1100174 } \
Damien Miller742cc1c2007-01-14 21:20:30 +1100175 if (pos < buflen) \
176 buf[pos] = thechar; \
Darren Tucker07877ca2007-01-24 00:07:29 +1100177 (pos)++; \
Damien Miller742cc1c2007-01-14 21:20:30 +1100178 } while (0)
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000179
Damien Miller742cc1c2007-01-14 21:20:30 +1100180static int dopr(char *buffer, size_t maxlen, const char *format,
181 va_list args_in);
182static int fmtstr(char *buffer, size_t *currlen, size_t maxlen,
183 char *value, int flags, int min, int max);
184static int fmtint(char *buffer, size_t *currlen, size_t maxlen,
185 LLONG value, int base, int min, int max, int flags);
186static int fmtfp(char *buffer, size_t *currlen, size_t maxlen,
187 LDOUBLE fvalue, int min, int max, int flags);
188
189static int
190dopr(char *buffer, size_t maxlen, const char *format, va_list args_in)
Damien Miller13bc0be1999-12-28 10:19:16 +1100191{
Damien Miller57f39152005-11-24 19:58:19 +1100192 char ch;
193 LLONG value;
194 LDOUBLE fvalue;
195 char *strvalue;
196 int min;
197 int max;
198 int state;
199 int flags;
200 int cflags;
201 size_t currlen;
202 va_list args;
Damien Miller13bc0be1999-12-28 10:19:16 +1100203
Damien Miller57f39152005-11-24 19:58:19 +1100204 VA_COPY(args, args_in);
205
206 state = DP_S_DEFAULT;
207 currlen = flags = cflags = min = 0;
208 max = -1;
209 ch = *format++;
210
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000211 while (state != DP_S_DONE) {
Damien Miller57f39152005-11-24 19:58:19 +1100212 if (ch == '\0')
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000213 state = DP_S_DONE;
Damien Miller42b81ff1999-11-26 12:21:24 +1100214
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000215 switch(state) {
Damien Miller31741252003-05-19 00:13:38 +1000216 case DP_S_DEFAULT:
217 if (ch == '%')
218 state = DP_S_FLAGS;
Damien Miller742cc1c2007-01-14 21:20:30 +1100219 else
220 DOPR_OUTCH(buffer, currlen, maxlen, ch);
Damien Miller31741252003-05-19 00:13:38 +1000221 ch = *format++;
222 break;
223 case DP_S_FLAGS:
224 switch (ch) {
225 case '-':
226 flags |= DP_F_MINUS;
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000227 ch = *format++;
228 break;
Damien Miller31741252003-05-19 00:13:38 +1000229 case '+':
230 flags |= DP_F_PLUS;
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000231 ch = *format++;
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000232 break;
Damien Miller31741252003-05-19 00:13:38 +1000233 case ' ':
234 flags |= DP_F_SPACE;
235 ch = *format++;
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000236 break;
Damien Miller31741252003-05-19 00:13:38 +1000237 case '#':
238 flags |= DP_F_NUM;
239 ch = *format++;
240 break;
241 case '0':
242 flags |= DP_F_ZERO;
243 ch = *format++;
244 break;
245 default:
246 state = DP_S_MIN;
247 break;
248 }
249 break;
250 case DP_S_MIN:
251 if (isdigit((unsigned char)ch)) {
Damien Miller57f39152005-11-24 19:58:19 +1100252 min = 10*min + char_to_int (ch);
Damien Miller31741252003-05-19 00:13:38 +1000253 ch = *format++;
254 } else if (ch == '*') {
255 min = va_arg (args, int);
256 ch = *format++;
257 state = DP_S_DOT;
Damien Miller57f39152005-11-24 19:58:19 +1100258 } else {
Damien Miller31741252003-05-19 00:13:38 +1000259 state = DP_S_DOT;
Damien Miller57f39152005-11-24 19:58:19 +1100260 }
Damien Miller31741252003-05-19 00:13:38 +1000261 break;
262 case DP_S_DOT:
263 if (ch == '.') {
264 state = DP_S_MAX;
265 ch = *format++;
Damien Miller57f39152005-11-24 19:58:19 +1100266 } else {
Damien Miller31741252003-05-19 00:13:38 +1000267 state = DP_S_MOD;
Damien Miller57f39152005-11-24 19:58:19 +1100268 }
Damien Miller31741252003-05-19 00:13:38 +1000269 break;
270 case DP_S_MAX:
271 if (isdigit((unsigned char)ch)) {
272 if (max < 0)
273 max = 0;
Damien Miller57f39152005-11-24 19:58:19 +1100274 max = 10*max + char_to_int (ch);
Damien Miller31741252003-05-19 00:13:38 +1000275 ch = *format++;
276 } else if (ch == '*') {
277 max = va_arg (args, int);
278 ch = *format++;
279 state = DP_S_MOD;
Damien Miller57f39152005-11-24 19:58:19 +1100280 } else {
Damien Miller31741252003-05-19 00:13:38 +1000281 state = DP_S_MOD;
Damien Miller57f39152005-11-24 19:58:19 +1100282 }
Damien Miller31741252003-05-19 00:13:38 +1000283 break;
284 case DP_S_MOD:
285 switch (ch) {
286 case 'h':
287 cflags = DP_C_SHORT;
288 ch = *format++;
289 break;
290 case 'l':
291 cflags = DP_C_LONG;
292 ch = *format++;
Damien Miller57f39152005-11-24 19:58:19 +1100293 if (ch == 'l') { /* It's a long long */
294 cflags = DP_C_LLONG;
Damien Miller31741252003-05-19 00:13:38 +1000295 ch = *format++;
296 }
297 break;
Damien Miller31741252003-05-19 00:13:38 +1000298 case 'L':
299 cflags = DP_C_LDOUBLE;
300 ch = *format++;
301 break;
302 default:
303 break;
304 }
305 state = DP_S_CONV;
306 break;
307 case DP_S_CONV:
308 switch (ch) {
309 case 'd':
310 case 'i':
311 if (cflags == DP_C_SHORT)
Damien Miller57f39152005-11-24 19:58:19 +1100312 value = va_arg (args, int);
Damien Miller31741252003-05-19 00:13:38 +1000313 else if (cflags == DP_C_LONG)
Damien Miller57f39152005-11-24 19:58:19 +1100314 value = va_arg (args, long int);
315 else if (cflags == DP_C_LLONG)
316 value = va_arg (args, LLONG);
Damien Miller31741252003-05-19 00:13:38 +1000317 else
318 value = va_arg (args, int);
Damien Miller742cc1c2007-01-14 21:20:30 +1100319 if (fmtint(buffer, &currlen, maxlen,
320 value, 10, min, max, flags) == -1)
321 return -1;
Damien Miller31741252003-05-19 00:13:38 +1000322 break;
323 case 'o':
324 flags |= DP_F_UNSIGNED;
325 if (cflags == DP_C_SHORT)
Damien Miller57f39152005-11-24 19:58:19 +1100326 value = va_arg (args, unsigned int);
Damien Miller31741252003-05-19 00:13:38 +1000327 else if (cflags == DP_C_LONG)
Damien Miller57f39152005-11-24 19:58:19 +1100328 value = (long)va_arg (args, unsigned long int);
329 else if (cflags == DP_C_LLONG)
330 value = (long)va_arg (args, unsigned LLONG);
Damien Miller31741252003-05-19 00:13:38 +1000331 else
Damien Miller57f39152005-11-24 19:58:19 +1100332 value = (long)va_arg (args, unsigned int);
Damien Miller742cc1c2007-01-14 21:20:30 +1100333 if (fmtint(buffer, &currlen, maxlen, value,
334 8, min, max, flags) == -1)
335 return -1;
Damien Miller31741252003-05-19 00:13:38 +1000336 break;
337 case 'u':
338 flags |= DP_F_UNSIGNED;
339 if (cflags == DP_C_SHORT)
Damien Miller57f39152005-11-24 19:58:19 +1100340 value = va_arg (args, unsigned int);
Damien Miller31741252003-05-19 00:13:38 +1000341 else if (cflags == DP_C_LONG)
Damien Miller57f39152005-11-24 19:58:19 +1100342 value = (long)va_arg (args, unsigned long int);
343 else if (cflags == DP_C_LLONG)
344 value = (LLONG)va_arg (args, unsigned LLONG);
Damien Miller31741252003-05-19 00:13:38 +1000345 else
Damien Miller57f39152005-11-24 19:58:19 +1100346 value = (long)va_arg (args, unsigned int);
Damien Miller742cc1c2007-01-14 21:20:30 +1100347 if (fmtint(buffer, &currlen, maxlen, value,
348 10, min, max, flags) == -1)
349 return -1;
Damien Miller31741252003-05-19 00:13:38 +1000350 break;
351 case 'X':
352 flags |= DP_F_UP;
353 case 'x':
354 flags |= DP_F_UNSIGNED;
355 if (cflags == DP_C_SHORT)
Damien Miller57f39152005-11-24 19:58:19 +1100356 value = va_arg (args, unsigned int);
Damien Miller31741252003-05-19 00:13:38 +1000357 else if (cflags == DP_C_LONG)
Damien Miller57f39152005-11-24 19:58:19 +1100358 value = (long)va_arg (args, unsigned long int);
359 else if (cflags == DP_C_LLONG)
360 value = (LLONG)va_arg (args, unsigned LLONG);
Damien Miller31741252003-05-19 00:13:38 +1000361 else
Damien Miller57f39152005-11-24 19:58:19 +1100362 value = (long)va_arg (args, unsigned int);
Damien Miller742cc1c2007-01-14 21:20:30 +1100363 if (fmtint(buffer, &currlen, maxlen, value,
364 16, min, max, flags) == -1)
365 return -1;
Damien Miller31741252003-05-19 00:13:38 +1000366 break;
367 case 'f':
368 if (cflags == DP_C_LDOUBLE)
Damien Miller57f39152005-11-24 19:58:19 +1100369 fvalue = va_arg (args, LDOUBLE);
Damien Miller31741252003-05-19 00:13:38 +1000370 else
Damien Miller57f39152005-11-24 19:58:19 +1100371 fvalue = va_arg (args, double);
Damien Miller742cc1c2007-01-14 21:20:30 +1100372 if (fmtfp(buffer, &currlen, maxlen, fvalue,
373 min, max, flags) == -1)
374 return -1;
Damien Miller31741252003-05-19 00:13:38 +1000375 break;
376 case 'E':
377 flags |= DP_F_UP;
378 case 'e':
379 if (cflags == DP_C_LDOUBLE)
Damien Miller57f39152005-11-24 19:58:19 +1100380 fvalue = va_arg (args, LDOUBLE);
Damien Miller31741252003-05-19 00:13:38 +1000381 else
Damien Miller57f39152005-11-24 19:58:19 +1100382 fvalue = va_arg (args, double);
Damien Miller742cc1c2007-01-14 21:20:30 +1100383 if (fmtfp(buffer, &currlen, maxlen, fvalue,
384 min, max, flags) == -1)
385 return -1;
Damien Miller31741252003-05-19 00:13:38 +1000386 break;
387 case 'G':
388 flags |= DP_F_UP;
389 case 'g':
390 if (cflags == DP_C_LDOUBLE)
Damien Miller57f39152005-11-24 19:58:19 +1100391 fvalue = va_arg (args, LDOUBLE);
Damien Miller31741252003-05-19 00:13:38 +1000392 else
Damien Miller57f39152005-11-24 19:58:19 +1100393 fvalue = va_arg (args, double);
Damien Miller742cc1c2007-01-14 21:20:30 +1100394 if (fmtfp(buffer, &currlen, maxlen, fvalue,
395 min, max, flags) == -1)
396 return -1;
Damien Miller31741252003-05-19 00:13:38 +1000397 break;
398 case 'c':
Damien Miller742cc1c2007-01-14 21:20:30 +1100399 DOPR_OUTCH(buffer, currlen, maxlen,
400 va_arg (args, int));
Damien Miller31741252003-05-19 00:13:38 +1000401 break;
402 case 's':
Damien Miller57f39152005-11-24 19:58:19 +1100403 strvalue = va_arg (args, char *);
404 if (!strvalue) strvalue = "(NULL)";
405 if (max == -1) {
406 max = strlen(strvalue);
407 }
408 if (min > 0 && max >= 0 && min > max) max = min;
Damien Miller742cc1c2007-01-14 21:20:30 +1100409 if (fmtstr(buffer, &currlen, maxlen,
410 strvalue, flags, min, max) == -1)
411 return -1;
Damien Miller31741252003-05-19 00:13:38 +1000412 break;
413 case 'p':
Damien Miller57f39152005-11-24 19:58:19 +1100414 strvalue = va_arg (args, void *);
Damien Miller742cc1c2007-01-14 21:20:30 +1100415 if (fmtint(buffer, &currlen, maxlen,
416 (long) strvalue, 16, min, max, flags) == -1)
417 return -1;
Damien Miller31741252003-05-19 00:13:38 +1000418 break;
419 case 'n':
420 if (cflags == DP_C_SHORT) {
421 short int *num;
Damien Miller57f39152005-11-24 19:58:19 +1100422 num = va_arg (args, short int *);
Damien Miller31741252003-05-19 00:13:38 +1000423 *num = currlen;
424 } else if (cflags == DP_C_LONG) {
425 long int *num;
Damien Miller57f39152005-11-24 19:58:19 +1100426 num = va_arg (args, long int *);
427 *num = (long int)currlen;
428 } else if (cflags == DP_C_LLONG) {
429 LLONG *num;
430 num = va_arg (args, LLONG *);
431 *num = (LLONG)currlen;
Damien Miller31741252003-05-19 00:13:38 +1000432 } else {
433 int *num;
Damien Miller57f39152005-11-24 19:58:19 +1100434 num = va_arg (args, int *);
Damien Miller31741252003-05-19 00:13:38 +1000435 *num = currlen;
436 }
437 break;
438 case '%':
Damien Miller742cc1c2007-01-14 21:20:30 +1100439 DOPR_OUTCH(buffer, currlen, maxlen, ch);
Damien Miller31741252003-05-19 00:13:38 +1000440 break;
Damien Miller57f39152005-11-24 19:58:19 +1100441 case 'w':
442 /* not supported yet, treat as next char */
Damien Miller31741252003-05-19 00:13:38 +1000443 ch = *format++;
444 break;
Damien Miller57f39152005-11-24 19:58:19 +1100445 default:
446 /* Unknown, skip */
447 break;
Damien Miller31741252003-05-19 00:13:38 +1000448 }
449 ch = *format++;
450 state = DP_S_DEFAULT;
451 flags = cflags = min = 0;
452 max = -1;
453 break;
454 case DP_S_DONE:
455 break;
Damien Miller57f39152005-11-24 19:58:19 +1100456 default:
457 /* hmm? */
Damien Miller31741252003-05-19 00:13:38 +1000458 break; /* some picky compilers need this */
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000459 }
460 }
Damien Miller57f39152005-11-24 19:58:19 +1100461 if (maxlen != 0) {
462 if (currlen < maxlen - 1)
463 buffer[currlen] = '\0';
464 else if (maxlen > 0)
465 buffer[maxlen - 1] = '\0';
466 }
467
Damien Miller742cc1c2007-01-14 21:20:30 +1100468 return currlen < INT_MAX ? (int)currlen : -1;
Damien Miller42b81ff1999-11-26 12:21:24 +1100469}
470
Damien Miller742cc1c2007-01-14 21:20:30 +1100471static int
472fmtstr(char *buffer, size_t *currlen, size_t maxlen,
473 char *value, int flags, int min, int max)
Damien Miller42b81ff1999-11-26 12:21:24 +1100474{
Damien Miller57f39152005-11-24 19:58:19 +1100475 int padlen, strln; /* amount to pad */
476 int cnt = 0;
477
478#ifdef DEBUG_SNPRINTF
479 printf("fmtstr min=%d max=%d s=[%s]\n", min, max, value);
480#endif
481 if (value == 0) {
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000482 value = "<NULL>";
Damien Miller57f39152005-11-24 19:58:19 +1100483 }
Damien Miller168e6ac2000-07-11 12:23:01 +1000484
Darren Tucker4127f552004-09-23 21:35:09 +1000485 for (strln = 0; strln < max && value[strln]; ++strln); /* strlen */
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000486 padlen = min - strln;
487 if (padlen < 0)
488 padlen = 0;
489 if (flags & DP_F_MINUS)
490 padlen = -padlen; /* Left Justify */
Damien Miller57f39152005-11-24 19:58:19 +1100491
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000492 while ((padlen > 0) && (cnt < max)) {
Damien Miller742cc1c2007-01-14 21:20:30 +1100493 DOPR_OUTCH(buffer, *currlen, maxlen, ' ');
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000494 --padlen;
495 ++cnt;
496 }
497 while (*value && (cnt < max)) {
Darren Tucker07877ca2007-01-24 00:07:29 +1100498 DOPR_OUTCH(buffer, *currlen, maxlen, *value);
499 *value++;
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000500 ++cnt;
501 }
502 while ((padlen < 0) && (cnt < max)) {
Damien Miller742cc1c2007-01-14 21:20:30 +1100503 DOPR_OUTCH(buffer, *currlen, maxlen, ' ');
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000504 ++padlen;
505 ++cnt;
506 }
Damien Miller742cc1c2007-01-14 21:20:30 +1100507 return 0;
Damien Miller42b81ff1999-11-26 12:21:24 +1100508}
509
Damien Miller168e6ac2000-07-11 12:23:01 +1000510/* Have to handle DP_F_NUM (ie 0x and 0 alternates) */
511
Damien Miller742cc1c2007-01-14 21:20:30 +1100512static int
513fmtint(char *buffer, size_t *currlen, size_t maxlen,
Darren Tucker9834cab2006-03-19 00:07:07 +1100514 LLONG value, int base, int min, int max, int flags)
Damien Miller42b81ff1999-11-26 12:21:24 +1100515{
Damien Miller57f39152005-11-24 19:58:19 +1100516 int signvalue = 0;
Darren Tucker9834cab2006-03-19 00:07:07 +1100517 unsigned LLONG uvalue;
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000518 char convert[20];
Damien Miller57f39152005-11-24 19:58:19 +1100519 int place = 0;
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000520 int spadlen = 0; /* amount to space pad */
521 int zpadlen = 0; /* amount to zero pad */
Damien Miller57f39152005-11-24 19:58:19 +1100522 int caps = 0;
523
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000524 if (max < 0)
525 max = 0;
Damien Miller57f39152005-11-24 19:58:19 +1100526
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000527 uvalue = value;
Damien Miller57f39152005-11-24 19:58:19 +1100528
529 if(!(flags & DP_F_UNSIGNED)) {
530 if( value < 0 ) {
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000531 signvalue = '-';
532 uvalue = -value;
Damien Miller57f39152005-11-24 19:58:19 +1100533 } else {
534 if (flags & DP_F_PLUS) /* Do a sign (+/i) */
535 signvalue = '+';
536 else if (flags & DP_F_SPACE)
537 signvalue = ' ';
538 }
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000539 }
Damien Miller168e6ac2000-07-11 12:23:01 +1000540
Damien Miller57f39152005-11-24 19:58:19 +1100541 if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
542
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000543 do {
544 convert[place++] =
Damien Miller57f39152005-11-24 19:58:19 +1100545 (caps? "0123456789ABCDEF":"0123456789abcdef")
546 [uvalue % (unsigned)base ];
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000547 uvalue = (uvalue / (unsigned)base );
Damien Miller57f39152005-11-24 19:58:19 +1100548 } while(uvalue && (place < 20));
549 if (place == 20) place--;
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000550 convert[place] = 0;
Damien Miller168e6ac2000-07-11 12:23:01 +1000551
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000552 zpadlen = max - place;
553 spadlen = min - MAX (max, place) - (signvalue ? 1 : 0);
Damien Miller57f39152005-11-24 19:58:19 +1100554 if (zpadlen < 0) zpadlen = 0;
555 if (spadlen < 0) spadlen = 0;
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000556 if (flags & DP_F_ZERO) {
557 zpadlen = MAX(zpadlen, spadlen);
558 spadlen = 0;
559 }
560 if (flags & DP_F_MINUS)
561 spadlen = -spadlen; /* Left Justifty */
Damien Miller168e6ac2000-07-11 12:23:01 +1000562
Damien Miller57f39152005-11-24 19:58:19 +1100563#ifdef DEBUG_SNPRINTF
564 printf("zpad: %d, spad: %d, min: %d, max: %d, place: %d\n",
565 zpadlen, spadlen, min, max, place);
566#endif
567
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000568 /* Spaces */
569 while (spadlen > 0) {
Damien Miller742cc1c2007-01-14 21:20:30 +1100570 DOPR_OUTCH(buffer, *currlen, maxlen, ' ');
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000571 --spadlen;
572 }
Damien Miller168e6ac2000-07-11 12:23:01 +1000573
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000574 /* Sign */
575 if (signvalue)
Damien Miller742cc1c2007-01-14 21:20:30 +1100576 DOPR_OUTCH(buffer, *currlen, maxlen, signvalue);
Damien Miller168e6ac2000-07-11 12:23:01 +1000577
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000578 /* Zeros */
579 if (zpadlen > 0) {
580 while (zpadlen > 0) {
Damien Miller742cc1c2007-01-14 21:20:30 +1100581 DOPR_OUTCH(buffer, *currlen, maxlen, '0');
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000582 --zpadlen;
583 }
584 }
Damien Miller168e6ac2000-07-11 12:23:01 +1000585
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000586 /* Digits */
Darren Tucker07877ca2007-01-24 00:07:29 +1100587 while (place > 0) {
588 --place;
589 DOPR_OUTCH(buffer, *currlen, maxlen, convert[place]);
590 }
Damien Miller168e6ac2000-07-11 12:23:01 +1000591
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000592 /* Left Justified spaces */
593 while (spadlen < 0) {
Damien Miller742cc1c2007-01-14 21:20:30 +1100594 DOPR_OUTCH(buffer, *currlen, maxlen, ' ');
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000595 ++spadlen;
596 }
Damien Miller742cc1c2007-01-14 21:20:30 +1100597 return 0;
Damien Miller42b81ff1999-11-26 12:21:24 +1100598}
599
Damien Miller57f39152005-11-24 19:58:19 +1100600static LDOUBLE abs_val(LDOUBLE value)
Damien Miller42b81ff1999-11-26 12:21:24 +1100601{
Damien Miller57f39152005-11-24 19:58:19 +1100602 LDOUBLE result = value;
Damien Miller42b81ff1999-11-26 12:21:24 +1100603
Damien Miller57f39152005-11-24 19:58:19 +1100604 if (value < 0)
605 result = -value;
606
607 return result;
608}
609
Damien Miller742cc1c2007-01-14 21:20:30 +1100610static LDOUBLE POW10(int val)
Damien Miller57f39152005-11-24 19:58:19 +1100611{
612 LDOUBLE result = 1;
613
Damien Miller742cc1c2007-01-14 21:20:30 +1100614 while (val) {
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000615 result *= 10;
Damien Miller742cc1c2007-01-14 21:20:30 +1100616 val--;
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000617 }
Damien Miller168e6ac2000-07-11 12:23:01 +1000618
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000619 return result;
Damien Miller168e6ac2000-07-11 12:23:01 +1000620}
621
Damien Miller57f39152005-11-24 19:58:19 +1100622static LLONG ROUND(LDOUBLE value)
Damien Miller168e6ac2000-07-11 12:23:01 +1000623{
Damien Miller57f39152005-11-24 19:58:19 +1100624 LLONG intpart;
Damien Miller168e6ac2000-07-11 12:23:01 +1000625
Damien Miller57f39152005-11-24 19:58:19 +1100626 intpart = (LLONG)value;
627 value = value - intpart;
628 if (value >= 0.5) intpart++;
629
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000630 return intpart;
Damien Miller168e6ac2000-07-11 12:23:01 +1000631}
632
Damien Miller57f39152005-11-24 19:58:19 +1100633/* a replacement for modf that doesn't need the math library. Should
634 be portable, but slow */
635static double my_modf(double x0, double *iptr)
Damien Miller168e6ac2000-07-11 12:23:01 +1000636{
Damien Miller57f39152005-11-24 19:58:19 +1100637 int i;
638 long l;
639 double x = x0;
640 double f = 1.0;
641
642 for (i=0;i<100;i++) {
643 l = (long)x;
644 if (l <= (x+1) && l >= (x-1)) break;
645 x *= 0.1;
646 f *= 10.0;
647 }
648
649 if (i == 100) {
Damien Miller742cc1c2007-01-14 21:20:30 +1100650 /*
651 * yikes! the number is beyond what we can handle.
652 * What do we do?
653 */
Damien Miller57f39152005-11-24 19:58:19 +1100654 (*iptr) = 0;
655 return 0;
656 }
657
658 if (i != 0) {
659 double i2;
660 double ret;
661
662 ret = my_modf(x0-l*f, &i2);
663 (*iptr) = l*f + i2;
664 return ret;
665 }
666
667 (*iptr) = l;
668 return x - (*iptr);
669}
670
671
Damien Miller742cc1c2007-01-14 21:20:30 +1100672static int
673fmtfp (char *buffer, size_t *currlen, size_t maxlen,
674 LDOUBLE fvalue, int min, int max, int flags)
Damien Miller57f39152005-11-24 19:58:19 +1100675{
676 int signvalue = 0;
677 double ufvalue;
678 char iconvert[311];
679 char fconvert[311];
680 int iplace = 0;
681 int fplace = 0;
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000682 int padlen = 0; /* amount to pad */
Damien Miller57f39152005-11-24 19:58:19 +1100683 int zpadlen = 0;
684 int caps = 0;
685 int idx;
686 double intpart;
687 double fracpart;
688 double temp;
Damien Miller168e6ac2000-07-11 12:23:01 +1000689
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000690 /*
691 * AIX manpage says the default is 0, but Solaris says the default
692 * is 6, and sprintf on AIX defaults to 6
693 */
694 if (max < 0)
695 max = 6;
Damien Miller168e6ac2000-07-11 12:23:01 +1000696
Damien Miller57f39152005-11-24 19:58:19 +1100697 ufvalue = abs_val (fvalue);
Damien Miller168e6ac2000-07-11 12:23:01 +1000698
Damien Miller57f39152005-11-24 19:58:19 +1100699 if (fvalue < 0) {
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000700 signvalue = '-';
Damien Miller57f39152005-11-24 19:58:19 +1100701 } else {
702 if (flags & DP_F_PLUS) { /* Do a sign (+/i) */
703 signvalue = '+';
704 } else {
705 if (flags & DP_F_SPACE)
706 signvalue = ' ';
707 }
708 }
Damien Miller168e6ac2000-07-11 12:23:01 +1000709
Damien Miller57f39152005-11-24 19:58:19 +1100710#if 0
711 if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
712#endif
713
714#if 0
715 if (max == 0) ufvalue += 0.5; /* if max = 0 we must round */
716#endif
Damien Miller168e6ac2000-07-11 12:23:01 +1000717
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000718 /*
Damien Miller57f39152005-11-24 19:58:19 +1100719 * Sorry, we only support 16 digits past the decimal because of our
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000720 * conversion method
721 */
Damien Miller57f39152005-11-24 19:58:19 +1100722 if (max > 16)
723 max = 16;
Damien Miller168e6ac2000-07-11 12:23:01 +1000724
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000725 /* We "cheat" by converting the fractional part to integer by
726 * multiplying by a factor of 10
727 */
Damien Miller168e6ac2000-07-11 12:23:01 +1000728
Damien Miller57f39152005-11-24 19:58:19 +1100729 temp = ufvalue;
730 my_modf(temp, &intpart);
731
732 fracpart = ROUND((POW10(max)) * (ufvalue - intpart));
733
734 if (fracpart >= POW10(max)) {
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000735 intpart++;
Damien Miller57f39152005-11-24 19:58:19 +1100736 fracpart -= POW10(max);
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000737 }
Damien Miller168e6ac2000-07-11 12:23:01 +1000738
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000739 /* Convert integer part */
740 do {
Damien Miller57f39152005-11-24 19:58:19 +1100741 temp = intpart*0.1;
742 my_modf(temp, &intpart);
743 idx = (int) ((temp -intpart +0.05)* 10.0);
744 /* idx = (int) (((double)(temp*0.1) -intpart +0.05) *10.0); */
745 /* printf ("%llf, %f, %x\n", temp, intpart, idx); */
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000746 iconvert[iplace++] =
Damien Miller57f39152005-11-24 19:58:19 +1100747 (caps? "0123456789ABCDEF":"0123456789abcdef")[idx];
748 } while (intpart && (iplace < 311));
749 if (iplace == 311) iplace--;
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000750 iconvert[iplace] = 0;
Damien Miller168e6ac2000-07-11 12:23:01 +1000751
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000752 /* Convert fractional part */
Damien Miller57f39152005-11-24 19:58:19 +1100753 if (fracpart)
754 {
755 do {
756 temp = fracpart*0.1;
757 my_modf(temp, &fracpart);
758 idx = (int) ((temp -fracpart +0.05)* 10.0);
759 /* idx = (int) ((((temp/10) -fracpart) +0.05) *10); */
760 /* printf ("%lf, %lf, %ld\n", temp, fracpart, idx ); */
761 fconvert[fplace++] =
762 (caps? "0123456789ABCDEF":"0123456789abcdef")[idx];
763 } while(fracpart && (fplace < 311));
764 if (fplace == 311) fplace--;
765 }
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000766 fconvert[fplace] = 0;
Damien Miller57f39152005-11-24 19:58:19 +1100767
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000768 /* -1 for decimal point, another -1 if we are printing a sign */
769 padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
770 zpadlen = max - fplace;
Damien Miller57f39152005-11-24 19:58:19 +1100771 if (zpadlen < 0) zpadlen = 0;
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000772 if (padlen < 0)
773 padlen = 0;
774 if (flags & DP_F_MINUS)
775 padlen = -padlen; /* Left Justifty */
Damien Miller57f39152005-11-24 19:58:19 +1100776
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000777 if ((flags & DP_F_ZERO) && (padlen > 0)) {
778 if (signvalue) {
Damien Miller742cc1c2007-01-14 21:20:30 +1100779 DOPR_OUTCH(buffer, *currlen, maxlen, signvalue);
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000780 --padlen;
781 signvalue = 0;
782 }
783 while (padlen > 0) {
Damien Miller742cc1c2007-01-14 21:20:30 +1100784 DOPR_OUTCH(buffer, *currlen, maxlen, '0');
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000785 --padlen;
786 }
787 }
788 while (padlen > 0) {
Damien Miller742cc1c2007-01-14 21:20:30 +1100789 DOPR_OUTCH(buffer, *currlen, maxlen, ' ');
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000790 --padlen;
791 }
792 if (signvalue)
Damien Miller742cc1c2007-01-14 21:20:30 +1100793 DOPR_OUTCH(buffer, *currlen, maxlen, signvalue);
Damien Miller57f39152005-11-24 19:58:19 +1100794
Darren Tucker07877ca2007-01-24 00:07:29 +1100795 while (iplace > 0) {
796 --iplace;
797 DOPR_OUTCH(buffer, *currlen, maxlen, iconvert[iplace]);
798 }
Damien Miller57f39152005-11-24 19:58:19 +1100799
800#ifdef DEBUG_SNPRINTF
801 printf("fmtfp: fplace=%d zpadlen=%d\n", fplace, zpadlen);
802#endif
Damien Miller168e6ac2000-07-11 12:23:01 +1000803
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000804 /*
Damien Miller57f39152005-11-24 19:58:19 +1100805 * Decimal point. This should probably use locale to find the correct
806 * char to print out.
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000807 */
Damien Miller57f39152005-11-24 19:58:19 +1100808 if (max > 0) {
Damien Miller742cc1c2007-01-14 21:20:30 +1100809 DOPR_OUTCH(buffer, *currlen, maxlen, '.');
Damien Miller57f39152005-11-24 19:58:19 +1100810
811 while (zpadlen > 0) {
Damien Miller742cc1c2007-01-14 21:20:30 +1100812 DOPR_OUTCH(buffer, *currlen, maxlen, '0');
Damien Miller57f39152005-11-24 19:58:19 +1100813 --zpadlen;
814 }
Damien Miller168e6ac2000-07-11 12:23:01 +1000815
Darren Tucker07877ca2007-01-24 00:07:29 +1100816 while (fplace > 0) {
817 --fplace;
818 DOPR_OUTCH(buffer, *currlen, maxlen, fconvert[fplace]);
819 }
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000820 }
Damien Miller168e6ac2000-07-11 12:23:01 +1000821
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000822 while (padlen < 0) {
Damien Miller742cc1c2007-01-14 21:20:30 +1100823 DOPR_OUTCH(buffer, *currlen, maxlen, ' ');
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000824 ++padlen;
825 }
Damien Miller742cc1c2007-01-14 21:20:30 +1100826 return 0;
Damien Miller168e6ac2000-07-11 12:23:01 +1000827}
828#endif /* !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF) */
829
Damien Miller57f39152005-11-24 19:58:19 +1100830#if !defined(HAVE_VSNPRINTF)
Darren Tucker07877ca2007-01-24 00:07:29 +1100831int
Damien Miller742cc1c2007-01-14 21:20:30 +1100832vsnprintf (char *str, size_t count, const char *fmt, va_list args)
Damien Miller168e6ac2000-07-11 12:23:01 +1000833{
Damien Miller57f39152005-11-24 19:58:19 +1100834 return dopr(str, count, fmt, args);
Damien Miller168e6ac2000-07-11 12:23:01 +1000835}
Damien Miller57f39152005-11-24 19:58:19 +1100836#endif
Damien Miller168e6ac2000-07-11 12:23:01 +1000837
Damien Miller57f39152005-11-24 19:58:19 +1100838#if !defined(HAVE_SNPRINTF)
Darren Tucker07877ca2007-01-24 00:07:29 +1100839int
840snprintf(char *str, size_t count, SNPRINTF_CONST char *fmt, ...)
Damien Millerc6b3bbe1999-12-13 08:27:33 +1100841{
Damien Miller57f39152005-11-24 19:58:19 +1100842 size_t ret;
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000843 va_list ap;
844
845 va_start(ap, fmt);
Damien Miller57f39152005-11-24 19:58:19 +1100846 ret = vsnprintf(str, count, fmt, ap);
Ben Lindstrom6c92dab2001-02-13 02:18:50 +0000847 va_end(ap);
Damien Miller57f39152005-11-24 19:58:19 +1100848 return ret;
Damien Millerc6b3bbe1999-12-13 08:27:33 +1100849}
Damien Miller57f39152005-11-24 19:58:19 +1100850#endif