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 | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 88 | **************************************************************/ |
Damien Miller | 42b81ff | 1999-11-26 12:21:24 +1100 | [diff] [blame] | 89 | |
Damien Miller | e9cf357 | 2001-02-09 12:55:35 +1100 | [diff] [blame] | 90 | #include "includes.h" |
| 91 | |
Darren Tucker | 9834cab | 2006-03-19 00:07:07 +1100 | [diff] [blame^] | 92 | RCSID("$Id: bsd-snprintf.c,v 1.12 2006/03/18 13:07:07 dtucker Exp $"); |
Ben Lindstrom | 63941f9 | 2001-02-25 23:20:40 +0000 | [diff] [blame] | 93 | |
| 94 | #if defined(BROKEN_SNPRINTF) /* For those with broken snprintf() */ |
| 95 | # undef HAVE_SNPRINTF |
| 96 | # undef HAVE_VSNPRINTF |
| 97 | #endif |
Damien Miller | 42b81ff | 1999-11-26 12:21:24 +1100 | [diff] [blame] | 98 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 99 | #ifndef VA_COPY |
| 100 | # ifdef HAVE_VA_COPY |
| 101 | # define VA_COPY(dest, src) va_copy(dest, src) |
| 102 | # else |
| 103 | # ifdef HAVE___VA_COPY |
| 104 | # define VA_COPY(dest, src) __va_copy(dest, src) |
| 105 | # else |
| 106 | # define VA_COPY(dest, src) (dest) = (src) |
| 107 | # endif |
| 108 | # endif |
| 109 | #endif |
| 110 | |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 111 | #if !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF) |
Damien Miller | 42b81ff | 1999-11-26 12:21:24 +1100 | [diff] [blame] | 112 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 113 | #ifdef HAVE_LONG_DOUBLE |
| 114 | # define LDOUBLE long double |
| 115 | #else |
| 116 | # define LDOUBLE double |
| 117 | #endif |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 118 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 119 | #ifdef HAVE_LONG_LONG |
| 120 | # define LLONG long long |
| 121 | #else |
| 122 | # define LLONG long |
| 123 | #endif |
Ben Lindstrom | 116b6bd | 2001-02-13 14:05:59 +0000 | [diff] [blame] | 124 | |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 125 | /* |
| 126 | * dopr(): poor man's version of doprintf |
| 127 | */ |
| 128 | |
| 129 | /* format read states */ |
| 130 | #define DP_S_DEFAULT 0 |
| 131 | #define DP_S_FLAGS 1 |
| 132 | #define DP_S_MIN 2 |
| 133 | #define DP_S_DOT 3 |
| 134 | #define DP_S_MAX 4 |
| 135 | #define DP_S_MOD 5 |
| 136 | #define DP_S_CONV 6 |
| 137 | #define DP_S_DONE 7 |
| 138 | |
| 139 | /* format flags - Bits */ |
| 140 | #define DP_F_MINUS (1 << 0) |
| 141 | #define DP_F_PLUS (1 << 1) |
| 142 | #define DP_F_SPACE (1 << 2) |
| 143 | #define DP_F_NUM (1 << 3) |
| 144 | #define DP_F_ZERO (1 << 4) |
| 145 | #define DP_F_UP (1 << 5) |
| 146 | #define DP_F_UNSIGNED (1 << 6) |
| 147 | |
| 148 | /* Conversion Flags */ |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 149 | #define DP_C_SHORT 1 |
| 150 | #define DP_C_LONG 2 |
| 151 | #define DP_C_LDOUBLE 3 |
| 152 | #define DP_C_LLONG 4 |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 153 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 154 | #define char_to_int(p) ((p)- '0') |
| 155 | #ifndef MAX |
| 156 | # define MAX(p,q) (((p) >= (q)) ? (p) : (q)) |
| 157 | #endif |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 158 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 159 | static size_t dopr(char *buffer, size_t maxlen, const char *format, |
| 160 | va_list args_in); |
| 161 | static void fmtstr(char *buffer, size_t *currlen, size_t maxlen, |
| 162 | char *value, int flags, int min, int max); |
| 163 | static void fmtint(char *buffer, size_t *currlen, size_t maxlen, |
Darren Tucker | 9834cab | 2006-03-19 00:07:07 +1100 | [diff] [blame^] | 164 | LLONG value, int base, int min, int max, int flags); |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 165 | static void fmtfp(char *buffer, size_t *currlen, size_t maxlen, |
| 166 | LDOUBLE fvalue, int min, int max, int flags); |
| 167 | static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c); |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 168 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 169 | static size_t 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] | 170 | { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 171 | char ch; |
| 172 | LLONG value; |
| 173 | LDOUBLE fvalue; |
| 174 | char *strvalue; |
| 175 | int min; |
| 176 | int max; |
| 177 | int state; |
| 178 | int flags; |
| 179 | int cflags; |
| 180 | size_t currlen; |
| 181 | va_list args; |
Damien Miller | 13bc0be | 1999-12-28 10:19:16 +1100 | [diff] [blame] | 182 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 183 | VA_COPY(args, args_in); |
| 184 | |
| 185 | state = DP_S_DEFAULT; |
| 186 | currlen = flags = cflags = min = 0; |
| 187 | max = -1; |
| 188 | ch = *format++; |
| 189 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 190 | while (state != DP_S_DONE) { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 191 | if (ch == '\0') |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 192 | state = DP_S_DONE; |
Damien Miller | 42b81ff | 1999-11-26 12:21:24 +1100 | [diff] [blame] | 193 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 194 | switch(state) { |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 195 | case DP_S_DEFAULT: |
| 196 | if (ch == '%') |
| 197 | state = DP_S_FLAGS; |
| 198 | else |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 199 | dopr_outch (buffer, &currlen, maxlen, ch); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 200 | ch = *format++; |
| 201 | break; |
| 202 | case DP_S_FLAGS: |
| 203 | switch (ch) { |
| 204 | case '-': |
| 205 | flags |= DP_F_MINUS; |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 206 | ch = *format++; |
| 207 | break; |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 208 | case '+': |
| 209 | flags |= DP_F_PLUS; |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 210 | ch = *format++; |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 211 | break; |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 212 | case ' ': |
| 213 | flags |= DP_F_SPACE; |
| 214 | ch = *format++; |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 215 | break; |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 216 | case '#': |
| 217 | flags |= DP_F_NUM; |
| 218 | ch = *format++; |
| 219 | break; |
| 220 | case '0': |
| 221 | flags |= DP_F_ZERO; |
| 222 | ch = *format++; |
| 223 | break; |
| 224 | default: |
| 225 | state = DP_S_MIN; |
| 226 | break; |
| 227 | } |
| 228 | break; |
| 229 | case DP_S_MIN: |
| 230 | if (isdigit((unsigned char)ch)) { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 231 | min = 10*min + char_to_int (ch); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 232 | ch = *format++; |
| 233 | } else if (ch == '*') { |
| 234 | min = va_arg (args, int); |
| 235 | ch = *format++; |
| 236 | state = DP_S_DOT; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 237 | } else { |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 238 | state = DP_S_DOT; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 239 | } |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 240 | break; |
| 241 | case DP_S_DOT: |
| 242 | if (ch == '.') { |
| 243 | state = DP_S_MAX; |
| 244 | ch = *format++; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 245 | } else { |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 246 | state = DP_S_MOD; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 247 | } |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 248 | break; |
| 249 | case DP_S_MAX: |
| 250 | if (isdigit((unsigned char)ch)) { |
| 251 | if (max < 0) |
| 252 | max = 0; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 253 | max = 10*max + char_to_int (ch); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 254 | ch = *format++; |
| 255 | } else if (ch == '*') { |
| 256 | max = va_arg (args, int); |
| 257 | ch = *format++; |
| 258 | state = DP_S_MOD; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 259 | } else { |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 260 | state = DP_S_MOD; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 261 | } |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 262 | break; |
| 263 | case DP_S_MOD: |
| 264 | switch (ch) { |
| 265 | case 'h': |
| 266 | cflags = DP_C_SHORT; |
| 267 | ch = *format++; |
| 268 | break; |
| 269 | case 'l': |
| 270 | cflags = DP_C_LONG; |
| 271 | ch = *format++; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 272 | if (ch == 'l') { /* It's a long long */ |
| 273 | cflags = DP_C_LLONG; |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 274 | ch = *format++; |
| 275 | } |
| 276 | break; |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 277 | case 'L': |
| 278 | cflags = DP_C_LDOUBLE; |
| 279 | ch = *format++; |
| 280 | break; |
| 281 | default: |
| 282 | break; |
| 283 | } |
| 284 | state = DP_S_CONV; |
| 285 | break; |
| 286 | case DP_S_CONV: |
| 287 | switch (ch) { |
| 288 | case 'd': |
| 289 | case 'i': |
| 290 | if (cflags == DP_C_SHORT) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 291 | value = va_arg (args, int); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 292 | else if (cflags == DP_C_LONG) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 293 | value = va_arg (args, long int); |
| 294 | else if (cflags == DP_C_LLONG) |
| 295 | value = va_arg (args, LLONG); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 296 | else |
| 297 | value = va_arg (args, int); |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 298 | fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 299 | break; |
| 300 | case 'o': |
| 301 | flags |= DP_F_UNSIGNED; |
| 302 | if (cflags == DP_C_SHORT) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 303 | value = va_arg (args, unsigned int); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 304 | else if (cflags == DP_C_LONG) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 305 | value = (long)va_arg (args, unsigned long int); |
| 306 | else if (cflags == DP_C_LLONG) |
| 307 | value = (long)va_arg (args, unsigned LLONG); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 308 | else |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 309 | value = (long)va_arg (args, unsigned int); |
| 310 | fmtint (buffer, &currlen, maxlen, value, 8, min, max, flags); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 311 | break; |
| 312 | case 'u': |
| 313 | flags |= DP_F_UNSIGNED; |
| 314 | if (cflags == DP_C_SHORT) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 315 | value = va_arg (args, unsigned int); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 316 | else if (cflags == DP_C_LONG) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 317 | value = (long)va_arg (args, unsigned long int); |
| 318 | else if (cflags == DP_C_LLONG) |
| 319 | value = (LLONG)va_arg (args, unsigned LLONG); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 320 | else |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 321 | value = (long)va_arg (args, unsigned int); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 322 | fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags); |
| 323 | break; |
| 324 | case 'X': |
| 325 | flags |= DP_F_UP; |
| 326 | case 'x': |
| 327 | flags |= DP_F_UNSIGNED; |
| 328 | if (cflags == DP_C_SHORT) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 329 | value = va_arg (args, unsigned int); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 330 | else if (cflags == DP_C_LONG) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 331 | value = (long)va_arg (args, unsigned long int); |
| 332 | else if (cflags == DP_C_LLONG) |
| 333 | value = (LLONG)va_arg (args, unsigned LLONG); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 334 | else |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 335 | value = (long)va_arg (args, unsigned int); |
| 336 | fmtint (buffer, &currlen, maxlen, value, 16, min, max, flags); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 337 | break; |
| 338 | case 'f': |
| 339 | if (cflags == DP_C_LDOUBLE) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 340 | fvalue = va_arg (args, LDOUBLE); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 341 | else |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 342 | fvalue = va_arg (args, double); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 343 | /* um, floating point? */ |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 344 | fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 345 | break; |
| 346 | case 'E': |
| 347 | flags |= DP_F_UP; |
| 348 | case 'e': |
| 349 | if (cflags == DP_C_LDOUBLE) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 350 | fvalue = va_arg (args, LDOUBLE); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 351 | else |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 352 | fvalue = va_arg (args, double); |
| 353 | fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 354 | break; |
| 355 | case 'G': |
| 356 | flags |= DP_F_UP; |
| 357 | case 'g': |
| 358 | if (cflags == DP_C_LDOUBLE) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 359 | fvalue = va_arg (args, LDOUBLE); |
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 | fvalue = va_arg (args, double); |
| 362 | fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 363 | break; |
| 364 | case 'c': |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 365 | dopr_outch (buffer, &currlen, maxlen, va_arg (args, int)); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 366 | break; |
| 367 | case 's': |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 368 | strvalue = va_arg (args, char *); |
| 369 | if (!strvalue) strvalue = "(NULL)"; |
| 370 | if (max == -1) { |
| 371 | max = strlen(strvalue); |
| 372 | } |
| 373 | if (min > 0 && max >= 0 && min > max) max = min; |
| 374 | fmtstr (buffer, &currlen, maxlen, strvalue, flags, min, max); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 375 | break; |
| 376 | case 'p': |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 377 | strvalue = va_arg (args, void *); |
| 378 | fmtint (buffer, &currlen, maxlen, (long) strvalue, 16, min, max, flags); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 379 | break; |
| 380 | case 'n': |
| 381 | if (cflags == DP_C_SHORT) { |
| 382 | short int *num; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 383 | num = va_arg (args, short int *); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 384 | *num = currlen; |
| 385 | } else if (cflags == DP_C_LONG) { |
| 386 | long int *num; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 387 | num = va_arg (args, long int *); |
| 388 | *num = (long int)currlen; |
| 389 | } else if (cflags == DP_C_LLONG) { |
| 390 | LLONG *num; |
| 391 | num = va_arg (args, LLONG *); |
| 392 | *num = (LLONG)currlen; |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 393 | } else { |
| 394 | int *num; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 395 | num = va_arg (args, int *); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 396 | *num = currlen; |
| 397 | } |
| 398 | break; |
| 399 | case '%': |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 400 | dopr_outch (buffer, &currlen, maxlen, ch); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 401 | break; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 402 | case 'w': |
| 403 | /* not supported yet, treat as next char */ |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 404 | ch = *format++; |
| 405 | break; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 406 | default: |
| 407 | /* Unknown, skip */ |
| 408 | break; |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 409 | } |
| 410 | ch = *format++; |
| 411 | state = DP_S_DEFAULT; |
| 412 | flags = cflags = min = 0; |
| 413 | max = -1; |
| 414 | break; |
| 415 | case DP_S_DONE: |
| 416 | break; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 417 | default: |
| 418 | /* hmm? */ |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 419 | break; /* some picky compilers need this */ |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 420 | } |
| 421 | } |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 422 | if (maxlen != 0) { |
| 423 | if (currlen < maxlen - 1) |
| 424 | buffer[currlen] = '\0'; |
| 425 | else if (maxlen > 0) |
| 426 | buffer[maxlen - 1] = '\0'; |
| 427 | } |
| 428 | |
| 429 | return currlen; |
Damien Miller | 42b81ff | 1999-11-26 12:21:24 +1100 | [diff] [blame] | 430 | } |
| 431 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 432 | static void fmtstr(char *buffer, size_t *currlen, size_t maxlen, |
| 433 | char *value, int flags, int min, int max) |
Damien Miller | 42b81ff | 1999-11-26 12:21:24 +1100 | [diff] [blame] | 434 | { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 435 | int padlen, strln; /* amount to pad */ |
| 436 | int cnt = 0; |
| 437 | |
| 438 | #ifdef DEBUG_SNPRINTF |
| 439 | printf("fmtstr min=%d max=%d s=[%s]\n", min, max, value); |
| 440 | #endif |
| 441 | if (value == 0) { |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 442 | value = "<NULL>"; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 443 | } |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 444 | |
Darren Tucker | 4127f55 | 2004-09-23 21:35:09 +1000 | [diff] [blame] | 445 | for (strln = 0; strln < max && value[strln]; ++strln); /* strlen */ |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 446 | padlen = min - strln; |
| 447 | if (padlen < 0) |
| 448 | padlen = 0; |
| 449 | if (flags & DP_F_MINUS) |
| 450 | padlen = -padlen; /* Left Justify */ |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 451 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 452 | while ((padlen > 0) && (cnt < max)) { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 453 | dopr_outch (buffer, currlen, maxlen, ' '); |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 454 | --padlen; |
| 455 | ++cnt; |
| 456 | } |
| 457 | while (*value && (cnt < max)) { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 458 | dopr_outch (buffer, currlen, maxlen, *value++); |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 459 | ++cnt; |
| 460 | } |
| 461 | while ((padlen < 0) && (cnt < max)) { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 462 | dopr_outch (buffer, currlen, maxlen, ' '); |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 463 | ++padlen; |
| 464 | ++cnt; |
| 465 | } |
Damien Miller | 42b81ff | 1999-11-26 12:21:24 +1100 | [diff] [blame] | 466 | } |
| 467 | |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 468 | /* Have to handle DP_F_NUM (ie 0x and 0 alternates) */ |
| 469 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 470 | static void fmtint(char *buffer, size_t *currlen, size_t maxlen, |
Darren Tucker | 9834cab | 2006-03-19 00:07:07 +1100 | [diff] [blame^] | 471 | LLONG value, int base, int min, int max, int flags) |
Damien Miller | 42b81ff | 1999-11-26 12:21:24 +1100 | [diff] [blame] | 472 | { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 473 | int signvalue = 0; |
Darren Tucker | 9834cab | 2006-03-19 00:07:07 +1100 | [diff] [blame^] | 474 | unsigned LLONG uvalue; |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 475 | char convert[20]; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 476 | int place = 0; |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 477 | int spadlen = 0; /* amount to space pad */ |
| 478 | int zpadlen = 0; /* amount to zero pad */ |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 479 | int caps = 0; |
| 480 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 481 | if (max < 0) |
| 482 | max = 0; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 483 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 484 | uvalue = value; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 485 | |
| 486 | if(!(flags & DP_F_UNSIGNED)) { |
| 487 | if( value < 0 ) { |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 488 | signvalue = '-'; |
| 489 | uvalue = -value; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 490 | } else { |
| 491 | if (flags & DP_F_PLUS) /* Do a sign (+/i) */ |
| 492 | signvalue = '+'; |
| 493 | else if (flags & DP_F_SPACE) |
| 494 | signvalue = ' '; |
| 495 | } |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 496 | } |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 497 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 498 | if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */ |
| 499 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 500 | do { |
| 501 | convert[place++] = |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 502 | (caps? "0123456789ABCDEF":"0123456789abcdef") |
| 503 | [uvalue % (unsigned)base ]; |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 504 | uvalue = (uvalue / (unsigned)base ); |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 505 | } while(uvalue && (place < 20)); |
| 506 | if (place == 20) place--; |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 507 | convert[place] = 0; |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 508 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 509 | zpadlen = max - place; |
| 510 | spadlen = min - MAX (max, place) - (signvalue ? 1 : 0); |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 511 | if (zpadlen < 0) zpadlen = 0; |
| 512 | if (spadlen < 0) spadlen = 0; |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 513 | if (flags & DP_F_ZERO) { |
| 514 | zpadlen = MAX(zpadlen, spadlen); |
| 515 | spadlen = 0; |
| 516 | } |
| 517 | if (flags & DP_F_MINUS) |
| 518 | spadlen = -spadlen; /* Left Justifty */ |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 519 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 520 | #ifdef DEBUG_SNPRINTF |
| 521 | printf("zpad: %d, spad: %d, min: %d, max: %d, place: %d\n", |
| 522 | zpadlen, spadlen, min, max, place); |
| 523 | #endif |
| 524 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 525 | /* Spaces */ |
| 526 | while (spadlen > 0) { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 527 | dopr_outch (buffer, currlen, maxlen, ' '); |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 528 | --spadlen; |
| 529 | } |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 530 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 531 | /* Sign */ |
| 532 | if (signvalue) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 533 | dopr_outch (buffer, currlen, maxlen, signvalue); |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 534 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 535 | /* Zeros */ |
| 536 | if (zpadlen > 0) { |
| 537 | while (zpadlen > 0) { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 538 | dopr_outch (buffer, currlen, maxlen, '0'); |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 539 | --zpadlen; |
| 540 | } |
| 541 | } |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 542 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 543 | /* Digits */ |
| 544 | while (place > 0) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 545 | dopr_outch (buffer, currlen, maxlen, convert[--place]); |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 546 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 547 | /* Left Justified spaces */ |
| 548 | while (spadlen < 0) { |
| 549 | dopr_outch (buffer, currlen, maxlen, ' '); |
| 550 | ++spadlen; |
| 551 | } |
Damien Miller | 42b81ff | 1999-11-26 12:21:24 +1100 | [diff] [blame] | 552 | } |
| 553 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 554 | static LDOUBLE abs_val(LDOUBLE value) |
Damien Miller | 42b81ff | 1999-11-26 12:21:24 +1100 | [diff] [blame] | 555 | { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 556 | LDOUBLE result = value; |
Damien Miller | 42b81ff | 1999-11-26 12:21:24 +1100 | [diff] [blame] | 557 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 558 | if (value < 0) |
| 559 | result = -value; |
| 560 | |
| 561 | return result; |
| 562 | } |
| 563 | |
| 564 | static LDOUBLE POW10(int exp) |
| 565 | { |
| 566 | LDOUBLE result = 1; |
| 567 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 568 | while (exp) { |
| 569 | result *= 10; |
| 570 | exp--; |
| 571 | } |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 572 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 573 | return result; |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 574 | } |
| 575 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 576 | static LLONG ROUND(LDOUBLE value) |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 577 | { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 578 | LLONG intpart; |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 579 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 580 | intpart = (LLONG)value; |
| 581 | value = value - intpart; |
| 582 | if (value >= 0.5) intpart++; |
| 583 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 584 | return intpart; |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 585 | } |
| 586 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 587 | /* a replacement for modf that doesn't need the math library. Should |
| 588 | be portable, but slow */ |
| 589 | static double my_modf(double x0, double *iptr) |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 590 | { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 591 | int i; |
| 592 | long l; |
| 593 | double x = x0; |
| 594 | double f = 1.0; |
| 595 | |
| 596 | for (i=0;i<100;i++) { |
| 597 | l = (long)x; |
| 598 | if (l <= (x+1) && l >= (x-1)) break; |
| 599 | x *= 0.1; |
| 600 | f *= 10.0; |
| 601 | } |
| 602 | |
| 603 | if (i == 100) { |
| 604 | /* yikes! the number is beyond what we can handle. What do we do? */ |
| 605 | (*iptr) = 0; |
| 606 | return 0; |
| 607 | } |
| 608 | |
| 609 | if (i != 0) { |
| 610 | double i2; |
| 611 | double ret; |
| 612 | |
| 613 | ret = my_modf(x0-l*f, &i2); |
| 614 | (*iptr) = l*f + i2; |
| 615 | return ret; |
| 616 | } |
| 617 | |
| 618 | (*iptr) = l; |
| 619 | return x - (*iptr); |
| 620 | } |
| 621 | |
| 622 | |
| 623 | static void fmtfp (char *buffer, size_t *currlen, size_t maxlen, |
| 624 | LDOUBLE fvalue, int min, int max, int flags) |
| 625 | { |
| 626 | int signvalue = 0; |
| 627 | double ufvalue; |
| 628 | char iconvert[311]; |
| 629 | char fconvert[311]; |
| 630 | int iplace = 0; |
| 631 | int fplace = 0; |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 632 | int padlen = 0; /* amount to pad */ |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 633 | int zpadlen = 0; |
| 634 | int caps = 0; |
| 635 | int idx; |
| 636 | double intpart; |
| 637 | double fracpart; |
| 638 | double temp; |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 639 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 640 | /* |
| 641 | * AIX manpage says the default is 0, but Solaris says the default |
| 642 | * is 6, and sprintf on AIX defaults to 6 |
| 643 | */ |
| 644 | if (max < 0) |
| 645 | max = 6; |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 646 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 647 | ufvalue = abs_val (fvalue); |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 648 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 649 | if (fvalue < 0) { |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 650 | signvalue = '-'; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 651 | } else { |
| 652 | if (flags & DP_F_PLUS) { /* Do a sign (+/i) */ |
| 653 | signvalue = '+'; |
| 654 | } else { |
| 655 | if (flags & DP_F_SPACE) |
| 656 | signvalue = ' '; |
| 657 | } |
| 658 | } |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 659 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 660 | #if 0 |
| 661 | if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */ |
| 662 | #endif |
| 663 | |
| 664 | #if 0 |
| 665 | if (max == 0) ufvalue += 0.5; /* if max = 0 we must round */ |
| 666 | #endif |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 667 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 668 | /* |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 669 | * Sorry, we only support 16 digits past the decimal because of our |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 670 | * conversion method |
| 671 | */ |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 672 | if (max > 16) |
| 673 | max = 16; |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 674 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 675 | /* We "cheat" by converting the fractional part to integer by |
| 676 | * multiplying by a factor of 10 |
| 677 | */ |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 678 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 679 | temp = ufvalue; |
| 680 | my_modf(temp, &intpart); |
| 681 | |
| 682 | fracpart = ROUND((POW10(max)) * (ufvalue - intpart)); |
| 683 | |
| 684 | if (fracpart >= POW10(max)) { |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 685 | intpart++; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 686 | fracpart -= POW10(max); |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 687 | } |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 688 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 689 | /* Convert integer part */ |
| 690 | do { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 691 | temp = intpart*0.1; |
| 692 | my_modf(temp, &intpart); |
| 693 | idx = (int) ((temp -intpart +0.05)* 10.0); |
| 694 | /* idx = (int) (((double)(temp*0.1) -intpart +0.05) *10.0); */ |
| 695 | /* printf ("%llf, %f, %x\n", temp, intpart, idx); */ |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 696 | iconvert[iplace++] = |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 697 | (caps? "0123456789ABCDEF":"0123456789abcdef")[idx]; |
| 698 | } while (intpart && (iplace < 311)); |
| 699 | if (iplace == 311) iplace--; |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 700 | iconvert[iplace] = 0; |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 701 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 702 | /* Convert fractional part */ |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 703 | if (fracpart) |
| 704 | { |
| 705 | do { |
| 706 | temp = fracpart*0.1; |
| 707 | my_modf(temp, &fracpart); |
| 708 | idx = (int) ((temp -fracpart +0.05)* 10.0); |
| 709 | /* idx = (int) ((((temp/10) -fracpart) +0.05) *10); */ |
| 710 | /* printf ("%lf, %lf, %ld\n", temp, fracpart, idx ); */ |
| 711 | fconvert[fplace++] = |
| 712 | (caps? "0123456789ABCDEF":"0123456789abcdef")[idx]; |
| 713 | } while(fracpart && (fplace < 311)); |
| 714 | if (fplace == 311) fplace--; |
| 715 | } |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 716 | fconvert[fplace] = 0; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 717 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 718 | /* -1 for decimal point, another -1 if we are printing a sign */ |
| 719 | padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0); |
| 720 | zpadlen = max - fplace; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 721 | if (zpadlen < 0) zpadlen = 0; |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 722 | if (padlen < 0) |
| 723 | padlen = 0; |
| 724 | if (flags & DP_F_MINUS) |
| 725 | padlen = -padlen; /* Left Justifty */ |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 726 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 727 | if ((flags & DP_F_ZERO) && (padlen > 0)) { |
| 728 | if (signvalue) { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 729 | dopr_outch (buffer, currlen, maxlen, signvalue); |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 730 | --padlen; |
| 731 | signvalue = 0; |
| 732 | } |
| 733 | while (padlen > 0) { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 734 | dopr_outch (buffer, currlen, maxlen, '0'); |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 735 | --padlen; |
| 736 | } |
| 737 | } |
| 738 | while (padlen > 0) { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 739 | dopr_outch (buffer, currlen, maxlen, ' '); |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 740 | --padlen; |
| 741 | } |
| 742 | if (signvalue) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 743 | dopr_outch (buffer, currlen, maxlen, signvalue); |
| 744 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 745 | while (iplace > 0) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 746 | dopr_outch (buffer, currlen, maxlen, iconvert[--iplace]); |
| 747 | |
| 748 | #ifdef DEBUG_SNPRINTF |
| 749 | printf("fmtfp: fplace=%d zpadlen=%d\n", fplace, zpadlen); |
| 750 | #endif |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 751 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 752 | /* |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 753 | * Decimal point. This should probably use locale to find the correct |
| 754 | * char to print out. |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 755 | */ |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 756 | if (max > 0) { |
| 757 | dopr_outch (buffer, currlen, maxlen, '.'); |
| 758 | |
| 759 | while (zpadlen > 0) { |
| 760 | dopr_outch (buffer, currlen, maxlen, '0'); |
| 761 | --zpadlen; |
| 762 | } |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 763 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 764 | while (fplace > 0) |
| 765 | dopr_outch (buffer, currlen, maxlen, fconvert[--fplace]); |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 766 | } |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 767 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 768 | while (padlen < 0) { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 769 | dopr_outch (buffer, currlen, maxlen, ' '); |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 770 | ++padlen; |
| 771 | } |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 772 | } |
| 773 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 774 | static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c) |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 775 | { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 776 | if (*currlen < maxlen) { |
| 777 | buffer[(*currlen)] = c; |
| 778 | } |
| 779 | (*currlen)++; |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 780 | } |
| 781 | #endif /* !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF) */ |
| 782 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 783 | #if !defined(HAVE_VSNPRINTF) |
| 784 | int vsnprintf (char *str, size_t count, const char *fmt, va_list args) |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 785 | { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 786 | return dopr(str, count, fmt, args); |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 787 | } |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 788 | #endif |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 789 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 790 | #if !defined(HAVE_SNPRINTF) |
Darren Tucker | d40c66c | 2005-12-17 22:32:03 +1100 | [diff] [blame] | 791 | int snprintf(char *str, size_t count, SNPRINTF_CONST char *fmt, ...) |
Damien Miller | c6b3bbe | 1999-12-13 08:27:33 +1100 | [diff] [blame] | 792 | { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 793 | size_t ret; |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 794 | va_list ap; |
| 795 | |
| 796 | va_start(ap, fmt); |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 797 | ret = vsnprintf(str, count, fmt, ap); |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 798 | va_end(ap); |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 799 | return ret; |
Damien Miller | c6b3bbe | 1999-12-13 08:27:33 +1100 | [diff] [blame] | 800 | } |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 801 | #endif |
Damien Miller | c6b3bbe | 1999-12-13 08:27:33 +1100 | [diff] [blame] | 802 | |