Damien Miller | f18462f | 2003-04-01 21:31:56 +1000 | [diff] [blame] | 1 | /* |
| 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 Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 8 | /************************************************************** |
| 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 Miller | 42b81ff | 1999-11-26 12:21:24 +1100 | [diff] [blame] | 14 | * |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 15 | * snprintf() is used instead of sprintf() as it does limit checks |
| 16 | * for string length. This covers a nasty loophole. |
Damien Miller | 42b81ff | 1999-11-26 12:21:24 +1100 | [diff] [blame] | 17 | * |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 18 | * The other functions are there to prevent NULL pointers from |
| 19 | * causing nast effects. |
Damien Miller | 42b81ff | 1999-11-26 12:21:24 +1100 | [diff] [blame] | 20 | * |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 21 | * 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 Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 48 | * Andrew Tridgell (tridge@samba.org) Oct 1998 |
| 49 | * fixed handling of %.0f |
| 50 | * added test for HAVE_LONG_DOUBLE |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 51 | * |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 52 | * 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 Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame^] | 88 | * |
| 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 Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 93 | **************************************************************/ |
Damien Miller | 42b81ff | 1999-11-26 12:21:24 +1100 | [diff] [blame] | 94 | |
Damien Miller | e9cf357 | 2001-02-09 12:55:35 +1100 | [diff] [blame] | 95 | #include "includes.h" |
| 96 | |
Ben Lindstrom | 63941f9 | 2001-02-25 23:20:40 +0000 | [diff] [blame] | 97 | #if defined(BROKEN_SNPRINTF) /* For those with broken snprintf() */ |
| 98 | # undef HAVE_SNPRINTF |
| 99 | # undef HAVE_VSNPRINTF |
| 100 | #endif |
Damien Miller | 42b81ff | 1999-11-26 12:21:24 +1100 | [diff] [blame] | 101 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 102 | #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 Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 114 | #if !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF) |
Damien Miller | 42b81ff | 1999-11-26 12:21:24 +1100 | [diff] [blame] | 115 | |
Darren Tucker | f78fb54 | 2006-08-06 21:25:24 +1000 | [diff] [blame] | 116 | #include <ctype.h> |
Darren Tucker | 08432d5 | 2006-09-09 15:59:43 +1000 | [diff] [blame] | 117 | #include <stdarg.h> |
Darren Tucker | f78fb54 | 2006-08-06 21:25:24 +1000 | [diff] [blame] | 118 | #include <stdlib.h> |
Damien Miller | 62da44f | 2006-07-24 15:08:35 +1000 | [diff] [blame] | 119 | #include <string.h> |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame^] | 120 | #include <limits.h> |
| 121 | #include <errno.h> |
Damien Miller | 62da44f | 2006-07-24 15:08:35 +1000 | [diff] [blame] | 122 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 123 | #ifdef HAVE_LONG_DOUBLE |
| 124 | # define LDOUBLE long double |
| 125 | #else |
| 126 | # define LDOUBLE double |
| 127 | #endif |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 128 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 129 | #ifdef HAVE_LONG_LONG |
| 130 | # define LLONG long long |
| 131 | #else |
| 132 | # define LLONG long |
| 133 | #endif |
Ben Lindstrom | 116b6bd | 2001-02-13 14:05:59 +0000 | [diff] [blame] | 134 | |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 135 | /* |
| 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 Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 159 | #define DP_C_SHORT 1 |
| 160 | #define DP_C_LONG 2 |
| 161 | #define DP_C_LDOUBLE 3 |
| 162 | #define DP_C_LLONG 4 |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 163 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 164 | #define char_to_int(p) ((p)- '0') |
| 165 | #ifndef MAX |
| 166 | # define MAX(p,q) (((p) >= (q)) ? (p) : (q)) |
| 167 | #endif |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 168 | |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame^] | 169 | #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 Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 178 | |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame^] | 179 | static int dopr(char *buffer, size_t maxlen, const char *format, |
| 180 | va_list args_in); |
| 181 | static int fmtstr(char *buffer, size_t *currlen, size_t maxlen, |
| 182 | char *value, int flags, int min, int max); |
| 183 | static int fmtint(char *buffer, size_t *currlen, size_t maxlen, |
| 184 | LLONG value, int base, int min, int max, int flags); |
| 185 | static int fmtfp(char *buffer, size_t *currlen, size_t maxlen, |
| 186 | LDOUBLE fvalue, int min, int max, int flags); |
| 187 | |
| 188 | static int |
| 189 | dopr(char *buffer, size_t maxlen, const char *format, va_list args_in) |
Damien Miller | 13bc0be | 1999-12-28 10:19:16 +1100 | [diff] [blame] | 190 | { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 191 | 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 Miller | 13bc0be | 1999-12-28 10:19:16 +1100 | [diff] [blame] | 202 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 203 | 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 Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 210 | while (state != DP_S_DONE) { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 211 | if (ch == '\0') |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 212 | state = DP_S_DONE; |
Damien Miller | 42b81ff | 1999-11-26 12:21:24 +1100 | [diff] [blame] | 213 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 214 | switch(state) { |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 215 | case DP_S_DEFAULT: |
| 216 | if (ch == '%') |
| 217 | state = DP_S_FLAGS; |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame^] | 218 | else |
| 219 | DOPR_OUTCH(buffer, currlen, maxlen, ch); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 220 | ch = *format++; |
| 221 | break; |
| 222 | case DP_S_FLAGS: |
| 223 | switch (ch) { |
| 224 | case '-': |
| 225 | flags |= DP_F_MINUS; |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 226 | ch = *format++; |
| 227 | break; |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 228 | case '+': |
| 229 | flags |= DP_F_PLUS; |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 230 | ch = *format++; |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 231 | break; |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 232 | case ' ': |
| 233 | flags |= DP_F_SPACE; |
| 234 | ch = *format++; |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 235 | break; |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 236 | 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 Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 251 | min = 10*min + char_to_int (ch); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 252 | ch = *format++; |
| 253 | } else if (ch == '*') { |
| 254 | min = va_arg (args, int); |
| 255 | ch = *format++; |
| 256 | state = DP_S_DOT; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 257 | } else { |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 258 | state = DP_S_DOT; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 259 | } |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 260 | break; |
| 261 | case DP_S_DOT: |
| 262 | if (ch == '.') { |
| 263 | state = DP_S_MAX; |
| 264 | ch = *format++; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 265 | } else { |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 266 | state = DP_S_MOD; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 267 | } |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 268 | break; |
| 269 | case DP_S_MAX: |
| 270 | if (isdigit((unsigned char)ch)) { |
| 271 | if (max < 0) |
| 272 | max = 0; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 273 | max = 10*max + char_to_int (ch); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 274 | ch = *format++; |
| 275 | } else if (ch == '*') { |
| 276 | max = va_arg (args, int); |
| 277 | ch = *format++; |
| 278 | state = DP_S_MOD; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 279 | } else { |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 280 | state = DP_S_MOD; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 281 | } |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 282 | 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 Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 292 | if (ch == 'l') { /* It's a long long */ |
| 293 | cflags = DP_C_LLONG; |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 294 | ch = *format++; |
| 295 | } |
| 296 | break; |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 297 | 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 Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 311 | value = va_arg (args, int); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 312 | else if (cflags == DP_C_LONG) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 313 | value = va_arg (args, long int); |
| 314 | else if (cflags == DP_C_LLONG) |
| 315 | value = va_arg (args, LLONG); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 316 | else |
| 317 | value = va_arg (args, int); |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame^] | 318 | if (fmtint(buffer, &currlen, maxlen, |
| 319 | value, 10, min, max, flags) == -1) |
| 320 | return -1; |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 321 | break; |
| 322 | case 'o': |
| 323 | flags |= DP_F_UNSIGNED; |
| 324 | if (cflags == DP_C_SHORT) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 325 | value = va_arg (args, unsigned int); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 326 | else if (cflags == DP_C_LONG) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 327 | value = (long)va_arg (args, unsigned long int); |
| 328 | else if (cflags == DP_C_LLONG) |
| 329 | value = (long)va_arg (args, unsigned LLONG); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 330 | else |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 331 | value = (long)va_arg (args, unsigned int); |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame^] | 332 | if (fmtint(buffer, &currlen, maxlen, value, |
| 333 | 8, min, max, flags) == -1) |
| 334 | return -1; |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 335 | break; |
| 336 | case 'u': |
| 337 | flags |= DP_F_UNSIGNED; |
| 338 | if (cflags == DP_C_SHORT) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 339 | value = va_arg (args, unsigned int); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 340 | else if (cflags == DP_C_LONG) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 341 | value = (long)va_arg (args, unsigned long int); |
| 342 | else if (cflags == DP_C_LLONG) |
| 343 | value = (LLONG)va_arg (args, unsigned LLONG); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 344 | else |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 345 | value = (long)va_arg (args, unsigned int); |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame^] | 346 | if (fmtint(buffer, &currlen, maxlen, value, |
| 347 | 10, min, max, flags) == -1) |
| 348 | return -1; |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 349 | break; |
| 350 | case 'X': |
| 351 | flags |= DP_F_UP; |
| 352 | case 'x': |
| 353 | flags |= DP_F_UNSIGNED; |
| 354 | if (cflags == DP_C_SHORT) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 355 | value = va_arg (args, unsigned int); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 356 | else if (cflags == DP_C_LONG) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 357 | value = (long)va_arg (args, unsigned long int); |
| 358 | else if (cflags == DP_C_LLONG) |
| 359 | value = (LLONG)va_arg (args, unsigned LLONG); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 360 | else |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 361 | value = (long)va_arg (args, unsigned int); |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame^] | 362 | if (fmtint(buffer, &currlen, maxlen, value, |
| 363 | 16, min, max, flags) == -1) |
| 364 | return -1; |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 365 | break; |
| 366 | case 'f': |
| 367 | if (cflags == DP_C_LDOUBLE) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 368 | fvalue = va_arg (args, LDOUBLE); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 369 | else |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 370 | fvalue = va_arg (args, double); |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame^] | 371 | if (fmtfp(buffer, &currlen, maxlen, fvalue, |
| 372 | min, max, flags) == -1) |
| 373 | return -1; |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 374 | break; |
| 375 | case 'E': |
| 376 | flags |= DP_F_UP; |
| 377 | case 'e': |
| 378 | if (cflags == DP_C_LDOUBLE) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 379 | fvalue = va_arg (args, LDOUBLE); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 380 | else |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 381 | fvalue = va_arg (args, double); |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame^] | 382 | if (fmtfp(buffer, &currlen, maxlen, fvalue, |
| 383 | min, max, flags) == -1) |
| 384 | return -1; |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 385 | break; |
| 386 | case 'G': |
| 387 | flags |= DP_F_UP; |
| 388 | case 'g': |
| 389 | if (cflags == DP_C_LDOUBLE) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 390 | fvalue = va_arg (args, LDOUBLE); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 391 | else |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 392 | fvalue = va_arg (args, double); |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame^] | 393 | if (fmtfp(buffer, &currlen, maxlen, fvalue, |
| 394 | min, max, flags) == -1) |
| 395 | return -1; |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 396 | break; |
| 397 | case 'c': |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame^] | 398 | DOPR_OUTCH(buffer, currlen, maxlen, |
| 399 | va_arg (args, int)); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 400 | break; |
| 401 | case 's': |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 402 | 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 Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame^] | 408 | if (fmtstr(buffer, &currlen, maxlen, |
| 409 | strvalue, flags, min, max) == -1) |
| 410 | return -1; |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 411 | break; |
| 412 | case 'p': |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 413 | strvalue = va_arg (args, void *); |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame^] | 414 | if (fmtint(buffer, &currlen, maxlen, |
| 415 | (long) strvalue, 16, min, max, flags) == -1) |
| 416 | return -1; |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 417 | break; |
| 418 | case 'n': |
| 419 | if (cflags == DP_C_SHORT) { |
| 420 | short int *num; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 421 | num = va_arg (args, short int *); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 422 | *num = currlen; |
| 423 | } else if (cflags == DP_C_LONG) { |
| 424 | long int *num; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 425 | 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 Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 431 | } else { |
| 432 | int *num; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 433 | num = va_arg (args, int *); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 434 | *num = currlen; |
| 435 | } |
| 436 | break; |
| 437 | case '%': |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame^] | 438 | DOPR_OUTCH(buffer, currlen, maxlen, ch); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 439 | break; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 440 | case 'w': |
| 441 | /* not supported yet, treat as next char */ |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 442 | ch = *format++; |
| 443 | break; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 444 | default: |
| 445 | /* Unknown, skip */ |
| 446 | break; |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 447 | } |
| 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 Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 455 | default: |
| 456 | /* hmm? */ |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 457 | break; /* some picky compilers need this */ |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 458 | } |
| 459 | } |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 460 | 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 Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame^] | 467 | return currlen < INT_MAX ? (int)currlen : -1; |
Damien Miller | 42b81ff | 1999-11-26 12:21:24 +1100 | [diff] [blame] | 468 | } |
| 469 | |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame^] | 470 | static int |
| 471 | fmtstr(char *buffer, size_t *currlen, size_t maxlen, |
| 472 | char *value, int flags, int min, int max) |
Damien Miller | 42b81ff | 1999-11-26 12:21:24 +1100 | [diff] [blame] | 473 | { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 474 | 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 Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 481 | value = "<NULL>"; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 482 | } |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 483 | |
Darren Tucker | 4127f55 | 2004-09-23 21:35:09 +1000 | [diff] [blame] | 484 | for (strln = 0; strln < max && value[strln]; ++strln); /* strlen */ |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 485 | padlen = min - strln; |
| 486 | if (padlen < 0) |
| 487 | padlen = 0; |
| 488 | if (flags & DP_F_MINUS) |
| 489 | padlen = -padlen; /* Left Justify */ |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 490 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 491 | while ((padlen > 0) && (cnt < max)) { |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame^] | 492 | DOPR_OUTCH(buffer, *currlen, maxlen, ' '); |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 493 | --padlen; |
| 494 | ++cnt; |
| 495 | } |
| 496 | while (*value && (cnt < max)) { |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame^] | 497 | DOPR_OUTCH(buffer, *currlen, maxlen, *value++); |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 498 | ++cnt; |
| 499 | } |
| 500 | while ((padlen < 0) && (cnt < max)) { |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame^] | 501 | DOPR_OUTCH(buffer, *currlen, maxlen, ' '); |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 502 | ++padlen; |
| 503 | ++cnt; |
| 504 | } |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame^] | 505 | return 0; |
Damien Miller | 42b81ff | 1999-11-26 12:21:24 +1100 | [diff] [blame] | 506 | } |
| 507 | |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 508 | /* Have to handle DP_F_NUM (ie 0x and 0 alternates) */ |
| 509 | |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame^] | 510 | static int |
| 511 | fmtint(char *buffer, size_t *currlen, size_t maxlen, |
Darren Tucker | 9834cab | 2006-03-19 00:07:07 +1100 | [diff] [blame] | 512 | LLONG value, int base, int min, int max, int flags) |
Damien Miller | 42b81ff | 1999-11-26 12:21:24 +1100 | [diff] [blame] | 513 | { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 514 | int signvalue = 0; |
Darren Tucker | 9834cab | 2006-03-19 00:07:07 +1100 | [diff] [blame] | 515 | unsigned LLONG uvalue; |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 516 | char convert[20]; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 517 | int place = 0; |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 518 | int spadlen = 0; /* amount to space pad */ |
| 519 | int zpadlen = 0; /* amount to zero pad */ |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 520 | int caps = 0; |
| 521 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 522 | if (max < 0) |
| 523 | max = 0; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 524 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 525 | uvalue = value; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 526 | |
| 527 | if(!(flags & DP_F_UNSIGNED)) { |
| 528 | if( value < 0 ) { |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 529 | signvalue = '-'; |
| 530 | uvalue = -value; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 531 | } else { |
| 532 | if (flags & DP_F_PLUS) /* Do a sign (+/i) */ |
| 533 | signvalue = '+'; |
| 534 | else if (flags & DP_F_SPACE) |
| 535 | signvalue = ' '; |
| 536 | } |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 537 | } |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 538 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 539 | if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */ |
| 540 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 541 | do { |
| 542 | convert[place++] = |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 543 | (caps? "0123456789ABCDEF":"0123456789abcdef") |
| 544 | [uvalue % (unsigned)base ]; |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 545 | uvalue = (uvalue / (unsigned)base ); |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 546 | } while(uvalue && (place < 20)); |
| 547 | if (place == 20) place--; |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 548 | convert[place] = 0; |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 549 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 550 | zpadlen = max - place; |
| 551 | spadlen = min - MAX (max, place) - (signvalue ? 1 : 0); |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 552 | if (zpadlen < 0) zpadlen = 0; |
| 553 | if (spadlen < 0) spadlen = 0; |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 554 | 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 Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 560 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 561 | #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 Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 566 | /* Spaces */ |
| 567 | while (spadlen > 0) { |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame^] | 568 | DOPR_OUTCH(buffer, *currlen, maxlen, ' '); |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 569 | --spadlen; |
| 570 | } |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 571 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 572 | /* Sign */ |
| 573 | if (signvalue) |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame^] | 574 | DOPR_OUTCH(buffer, *currlen, maxlen, signvalue); |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 575 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 576 | /* Zeros */ |
| 577 | if (zpadlen > 0) { |
| 578 | while (zpadlen > 0) { |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame^] | 579 | DOPR_OUTCH(buffer, *currlen, maxlen, '0'); |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 580 | --zpadlen; |
| 581 | } |
| 582 | } |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 583 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 584 | /* Digits */ |
| 585 | while (place > 0) |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame^] | 586 | DOPR_OUTCH(buffer, *currlen, maxlen, convert[--place]); |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 587 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 588 | /* Left Justified spaces */ |
| 589 | while (spadlen < 0) { |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame^] | 590 | DOPR_OUTCH(buffer, *currlen, maxlen, ' '); |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 591 | ++spadlen; |
| 592 | } |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame^] | 593 | return 0; |
Damien Miller | 42b81ff | 1999-11-26 12:21:24 +1100 | [diff] [blame] | 594 | } |
| 595 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 596 | static LDOUBLE abs_val(LDOUBLE value) |
Damien Miller | 42b81ff | 1999-11-26 12:21:24 +1100 | [diff] [blame] | 597 | { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 598 | LDOUBLE result = value; |
Damien Miller | 42b81ff | 1999-11-26 12:21:24 +1100 | [diff] [blame] | 599 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 600 | if (value < 0) |
| 601 | result = -value; |
| 602 | |
| 603 | return result; |
| 604 | } |
| 605 | |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame^] | 606 | static LDOUBLE POW10(int val) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 607 | { |
| 608 | LDOUBLE result = 1; |
| 609 | |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame^] | 610 | while (val) { |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 611 | result *= 10; |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame^] | 612 | val--; |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 613 | } |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 614 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 615 | return result; |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 616 | } |
| 617 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 618 | static LLONG ROUND(LDOUBLE value) |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 619 | { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 620 | LLONG intpart; |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 621 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 622 | intpart = (LLONG)value; |
| 623 | value = value - intpart; |
| 624 | if (value >= 0.5) intpart++; |
| 625 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 626 | return intpart; |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 627 | } |
| 628 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 629 | /* a replacement for modf that doesn't need the math library. Should |
| 630 | be portable, but slow */ |
| 631 | static double my_modf(double x0, double *iptr) |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 632 | { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 633 | 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 Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame^] | 646 | /* |
| 647 | * yikes! the number is beyond what we can handle. |
| 648 | * What do we do? |
| 649 | */ |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 650 | (*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 Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame^] | 668 | static int |
| 669 | fmtfp (char *buffer, size_t *currlen, size_t maxlen, |
| 670 | LDOUBLE fvalue, int min, int max, int flags) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 671 | { |
| 672 | int signvalue = 0; |
| 673 | double ufvalue; |
| 674 | char iconvert[311]; |
| 675 | char fconvert[311]; |
| 676 | int iplace = 0; |
| 677 | int fplace = 0; |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 678 | int padlen = 0; /* amount to pad */ |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 679 | int zpadlen = 0; |
| 680 | int caps = 0; |
| 681 | int idx; |
| 682 | double intpart; |
| 683 | double fracpart; |
| 684 | double temp; |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 685 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 686 | /* |
| 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 Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 692 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 693 | ufvalue = abs_val (fvalue); |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 694 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 695 | if (fvalue < 0) { |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 696 | signvalue = '-'; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 697 | } 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 Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 705 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 706 | #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 Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 713 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 714 | /* |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 715 | * Sorry, we only support 16 digits past the decimal because of our |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 716 | * conversion method |
| 717 | */ |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 718 | if (max > 16) |
| 719 | max = 16; |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 720 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 721 | /* We "cheat" by converting the fractional part to integer by |
| 722 | * multiplying by a factor of 10 |
| 723 | */ |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 724 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 725 | temp = ufvalue; |
| 726 | my_modf(temp, &intpart); |
| 727 | |
| 728 | fracpart = ROUND((POW10(max)) * (ufvalue - intpart)); |
| 729 | |
| 730 | if (fracpart >= POW10(max)) { |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 731 | intpart++; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 732 | fracpart -= POW10(max); |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 733 | } |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 734 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 735 | /* Convert integer part */ |
| 736 | do { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 737 | 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 Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 742 | iconvert[iplace++] = |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 743 | (caps? "0123456789ABCDEF":"0123456789abcdef")[idx]; |
| 744 | } while (intpart && (iplace < 311)); |
| 745 | if (iplace == 311) iplace--; |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 746 | iconvert[iplace] = 0; |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 747 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 748 | /* Convert fractional part */ |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 749 | 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 Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 762 | fconvert[fplace] = 0; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 763 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 764 | /* -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 Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 767 | if (zpadlen < 0) zpadlen = 0; |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 768 | if (padlen < 0) |
| 769 | padlen = 0; |
| 770 | if (flags & DP_F_MINUS) |
| 771 | padlen = -padlen; /* Left Justifty */ |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 772 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 773 | if ((flags & DP_F_ZERO) && (padlen > 0)) { |
| 774 | if (signvalue) { |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame^] | 775 | DOPR_OUTCH(buffer, *currlen, maxlen, signvalue); |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 776 | --padlen; |
| 777 | signvalue = 0; |
| 778 | } |
| 779 | while (padlen > 0) { |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame^] | 780 | DOPR_OUTCH(buffer, *currlen, maxlen, '0'); |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 781 | --padlen; |
| 782 | } |
| 783 | } |
| 784 | while (padlen > 0) { |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame^] | 785 | DOPR_OUTCH(buffer, *currlen, maxlen, ' '); |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 786 | --padlen; |
| 787 | } |
| 788 | if (signvalue) |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame^] | 789 | DOPR_OUTCH(buffer, *currlen, maxlen, signvalue); |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 790 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 791 | while (iplace > 0) |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame^] | 792 | DOPR_OUTCH(buffer, *currlen, maxlen, iconvert[--iplace]); |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 793 | |
| 794 | #ifdef DEBUG_SNPRINTF |
| 795 | printf("fmtfp: fplace=%d zpadlen=%d\n", fplace, zpadlen); |
| 796 | #endif |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 797 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 798 | /* |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 799 | * Decimal point. This should probably use locale to find the correct |
| 800 | * char to print out. |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 801 | */ |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 802 | if (max > 0) { |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame^] | 803 | DOPR_OUTCH(buffer, *currlen, maxlen, '.'); |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 804 | |
| 805 | while (zpadlen > 0) { |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame^] | 806 | DOPR_OUTCH(buffer, *currlen, maxlen, '0'); |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 807 | --zpadlen; |
| 808 | } |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 809 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 810 | while (fplace > 0) |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame^] | 811 | DOPR_OUTCH(buffer, *currlen, maxlen, |
| 812 | fconvert[--fplace]); |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 813 | } |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 814 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 815 | while (padlen < 0) { |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame^] | 816 | DOPR_OUTCH(buffer, *currlen, maxlen, ' '); |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 817 | ++padlen; |
| 818 | } |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame^] | 819 | return 0; |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 820 | } |
| 821 | #endif /* !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF) */ |
| 822 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 823 | #if !defined(HAVE_VSNPRINTF) |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame^] | 824 | static int |
| 825 | vsnprintf (char *str, size_t count, const char *fmt, va_list args) |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 826 | { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 827 | return dopr(str, count, fmt, args); |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 828 | } |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 829 | #endif |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 830 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 831 | #if !defined(HAVE_SNPRINTF) |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame^] | 832 | static int |
| 833 | snprintf(char *str, size_t count, const char *fmt, ...) |
Damien Miller | c6b3bbe | 1999-12-13 08:27:33 +1100 | [diff] [blame] | 834 | { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 835 | size_t ret; |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 836 | va_list ap; |
| 837 | |
| 838 | va_start(ap, fmt); |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 839 | ret = vsnprintf(str, count, fmt, ap); |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 840 | va_end(ap); |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 841 | return ret; |
Damien Miller | c6b3bbe | 1999-12-13 08:27:33 +1100 | [diff] [blame] | 842 | } |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 843 | #endif |