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 | 04be8b9 | 2013-08-28 12:49:43 +1000 | [diff] [blame] | 163 | #define DP_C_SIZE 5 |
| 164 | #define DP_C_INTMAX 6 |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 165 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 166 | #define char_to_int(p) ((p)- '0') |
| 167 | #ifndef MAX |
| 168 | # define MAX(p,q) (((p) >= (q)) ? (p) : (q)) |
| 169 | #endif |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 170 | |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame] | 171 | #define DOPR_OUTCH(buf, pos, buflen, thechar) \ |
| 172 | do { \ |
Darren Tucker | 07877ca | 2007-01-24 00:07:29 +1100 | [diff] [blame] | 173 | if (pos + 1 >= INT_MAX) { \ |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame] | 174 | errno = ERANGE; \ |
| 175 | return -1; \ |
Darren Tucker | 07877ca | 2007-01-24 00:07:29 +1100 | [diff] [blame] | 176 | } \ |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame] | 177 | if (pos < buflen) \ |
| 178 | buf[pos] = thechar; \ |
Darren Tucker | 07877ca | 2007-01-24 00:07:29 +1100 | [diff] [blame] | 179 | (pos)++; \ |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame] | 180 | } while (0) |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 181 | |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame] | 182 | static int dopr(char *buffer, size_t maxlen, const char *format, |
| 183 | va_list args_in); |
| 184 | static int fmtstr(char *buffer, size_t *currlen, size_t maxlen, |
| 185 | char *value, int flags, int min, int max); |
| 186 | static int fmtint(char *buffer, size_t *currlen, size_t maxlen, |
Damien Miller | 04be8b9 | 2013-08-28 12:49:43 +1000 | [diff] [blame] | 187 | intmax_t value, int base, int min, int max, int flags); |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame] | 188 | static int fmtfp(char *buffer, size_t *currlen, size_t maxlen, |
| 189 | LDOUBLE fvalue, int min, int max, int flags); |
| 190 | |
| 191 | static int |
| 192 | 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] | 193 | { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 194 | char ch; |
Damien Miller | 04be8b9 | 2013-08-28 12:49:43 +1000 | [diff] [blame] | 195 | intmax_t value; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 196 | LDOUBLE fvalue; |
| 197 | char *strvalue; |
| 198 | int min; |
| 199 | int max; |
| 200 | int state; |
| 201 | int flags; |
| 202 | int cflags; |
| 203 | size_t currlen; |
| 204 | va_list args; |
Damien Miller | 13bc0be | 1999-12-28 10:19:16 +1100 | [diff] [blame] | 205 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 206 | VA_COPY(args, args_in); |
| 207 | |
| 208 | state = DP_S_DEFAULT; |
| 209 | currlen = flags = cflags = min = 0; |
| 210 | max = -1; |
| 211 | ch = *format++; |
| 212 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 213 | while (state != DP_S_DONE) { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 214 | if (ch == '\0') |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 215 | state = DP_S_DONE; |
Damien Miller | 42b81ff | 1999-11-26 12:21:24 +1100 | [diff] [blame] | 216 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 217 | switch(state) { |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 218 | case DP_S_DEFAULT: |
| 219 | if (ch == '%') |
| 220 | state = DP_S_FLAGS; |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame] | 221 | else |
| 222 | DOPR_OUTCH(buffer, currlen, maxlen, ch); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 223 | ch = *format++; |
| 224 | break; |
| 225 | case DP_S_FLAGS: |
| 226 | switch (ch) { |
| 227 | case '-': |
| 228 | flags |= DP_F_MINUS; |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 229 | ch = *format++; |
| 230 | break; |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 231 | case '+': |
| 232 | flags |= DP_F_PLUS; |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 233 | ch = *format++; |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 234 | break; |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 235 | case ' ': |
| 236 | flags |= DP_F_SPACE; |
| 237 | ch = *format++; |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 238 | break; |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 239 | case '#': |
| 240 | flags |= DP_F_NUM; |
| 241 | ch = *format++; |
| 242 | break; |
| 243 | case '0': |
| 244 | flags |= DP_F_ZERO; |
| 245 | ch = *format++; |
| 246 | break; |
| 247 | default: |
| 248 | state = DP_S_MIN; |
| 249 | break; |
| 250 | } |
| 251 | break; |
| 252 | case DP_S_MIN: |
| 253 | if (isdigit((unsigned char)ch)) { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 254 | min = 10*min + char_to_int (ch); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 255 | ch = *format++; |
| 256 | } else if (ch == '*') { |
| 257 | min = va_arg (args, int); |
| 258 | ch = *format++; |
| 259 | state = DP_S_DOT; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 260 | } else { |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 261 | state = DP_S_DOT; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 262 | } |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 263 | break; |
| 264 | case DP_S_DOT: |
| 265 | if (ch == '.') { |
| 266 | state = DP_S_MAX; |
| 267 | ch = *format++; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 268 | } else { |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 269 | state = DP_S_MOD; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 270 | } |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 271 | break; |
| 272 | case DP_S_MAX: |
| 273 | if (isdigit((unsigned char)ch)) { |
| 274 | if (max < 0) |
| 275 | max = 0; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 276 | max = 10*max + char_to_int (ch); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 277 | ch = *format++; |
| 278 | } else if (ch == '*') { |
| 279 | max = va_arg (args, int); |
| 280 | ch = *format++; |
| 281 | state = DP_S_MOD; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 282 | } else { |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 283 | state = DP_S_MOD; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 284 | } |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 285 | break; |
| 286 | case DP_S_MOD: |
| 287 | switch (ch) { |
| 288 | case 'h': |
| 289 | cflags = DP_C_SHORT; |
| 290 | ch = *format++; |
| 291 | break; |
Damien Miller | 04be8b9 | 2013-08-28 12:49:43 +1000 | [diff] [blame] | 292 | case 'j': |
| 293 | cflags = DP_C_INTMAX; |
| 294 | ch = *format++; |
| 295 | break; |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 296 | case 'l': |
| 297 | cflags = DP_C_LONG; |
| 298 | ch = *format++; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 299 | if (ch == 'l') { /* It's a long long */ |
| 300 | cflags = DP_C_LLONG; |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 301 | ch = *format++; |
| 302 | } |
| 303 | break; |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 304 | case 'L': |
| 305 | cflags = DP_C_LDOUBLE; |
| 306 | ch = *format++; |
| 307 | break; |
Damien Miller | 04be8b9 | 2013-08-28 12:49:43 +1000 | [diff] [blame] | 308 | case 'z': |
| 309 | cflags = DP_C_SIZE; |
| 310 | ch = *format++; |
| 311 | break; |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 312 | default: |
| 313 | break; |
| 314 | } |
| 315 | state = DP_S_CONV; |
| 316 | break; |
| 317 | case DP_S_CONV: |
| 318 | switch (ch) { |
| 319 | case 'd': |
| 320 | case 'i': |
| 321 | if (cflags == DP_C_SHORT) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 322 | value = va_arg (args, int); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 323 | else if (cflags == DP_C_LONG) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 324 | value = va_arg (args, long int); |
| 325 | else if (cflags == DP_C_LLONG) |
| 326 | value = va_arg (args, LLONG); |
Damien Miller | 04be8b9 | 2013-08-28 12:49:43 +1000 | [diff] [blame] | 327 | else if (cflags == DP_C_SIZE) |
| 328 | value = va_arg (args, ssize_t); |
| 329 | else if (cflags == DP_C_INTMAX) |
| 330 | value = va_arg (args, intmax_t); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 331 | else |
| 332 | value = va_arg (args, int); |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame] | 333 | if (fmtint(buffer, &currlen, maxlen, |
| 334 | value, 10, min, max, flags) == -1) |
| 335 | return -1; |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 336 | break; |
| 337 | case 'o': |
| 338 | flags |= DP_F_UNSIGNED; |
| 339 | if (cflags == DP_C_SHORT) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 340 | value = va_arg (args, unsigned int); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 341 | else if (cflags == DP_C_LONG) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 342 | value = (long)va_arg (args, unsigned long int); |
| 343 | else if (cflags == DP_C_LLONG) |
| 344 | value = (long)va_arg (args, unsigned LLONG); |
Damien Miller | 04be8b9 | 2013-08-28 12:49:43 +1000 | [diff] [blame] | 345 | else if (cflags == DP_C_SIZE) |
| 346 | value = va_arg (args, size_t); |
Damien Miller | 43968a8 | 2013-08-28 14:00:54 +1000 | [diff] [blame] | 347 | #ifdef notyet |
Damien Miller | 04be8b9 | 2013-08-28 12:49:43 +1000 | [diff] [blame] | 348 | else if (cflags == DP_C_INTMAX) |
| 349 | value = va_arg (args, uintmax_t); |
Damien Miller | 43968a8 | 2013-08-28 14:00:54 +1000 | [diff] [blame] | 350 | #endif |
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 | value = (long)va_arg (args, unsigned int); |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame] | 353 | if (fmtint(buffer, &currlen, maxlen, value, |
| 354 | 8, min, max, flags) == -1) |
| 355 | return -1; |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 356 | break; |
| 357 | case 'u': |
| 358 | flags |= DP_F_UNSIGNED; |
| 359 | if (cflags == DP_C_SHORT) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 360 | value = va_arg (args, unsigned int); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 361 | else if (cflags == DP_C_LONG) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 362 | value = (long)va_arg (args, unsigned long int); |
| 363 | else if (cflags == DP_C_LLONG) |
| 364 | value = (LLONG)va_arg (args, unsigned LLONG); |
Damien Miller | 04be8b9 | 2013-08-28 12:49:43 +1000 | [diff] [blame] | 365 | else if (cflags == DP_C_SIZE) |
| 366 | value = va_arg (args, size_t); |
Damien Miller | 43968a8 | 2013-08-28 14:00:54 +1000 | [diff] [blame] | 367 | #ifdef notyet |
Damien Miller | 04be8b9 | 2013-08-28 12:49:43 +1000 | [diff] [blame] | 368 | else if (cflags == DP_C_INTMAX) |
| 369 | value = va_arg (args, uintmax_t); |
Damien Miller | 43968a8 | 2013-08-28 14:00:54 +1000 | [diff] [blame] | 370 | #endif |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 371 | else |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 372 | value = (long)va_arg (args, unsigned int); |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame] | 373 | if (fmtint(buffer, &currlen, maxlen, value, |
| 374 | 10, min, max, flags) == -1) |
| 375 | return -1; |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 376 | break; |
| 377 | case 'X': |
| 378 | flags |= DP_F_UP; |
| 379 | case 'x': |
| 380 | flags |= DP_F_UNSIGNED; |
| 381 | if (cflags == DP_C_SHORT) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 382 | value = va_arg (args, unsigned int); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 383 | else if (cflags == DP_C_LONG) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 384 | value = (long)va_arg (args, unsigned long int); |
| 385 | else if (cflags == DP_C_LLONG) |
| 386 | value = (LLONG)va_arg (args, unsigned LLONG); |
Damien Miller | 04be8b9 | 2013-08-28 12:49:43 +1000 | [diff] [blame] | 387 | else if (cflags == DP_C_SIZE) |
| 388 | value = va_arg (args, size_t); |
Damien Miller | 43968a8 | 2013-08-28 14:00:54 +1000 | [diff] [blame] | 389 | #ifdef notyet |
Damien Miller | 04be8b9 | 2013-08-28 12:49:43 +1000 | [diff] [blame] | 390 | else if (cflags == DP_C_INTMAX) |
| 391 | value = va_arg (args, uintmax_t); |
Damien Miller | 43968a8 | 2013-08-28 14:00:54 +1000 | [diff] [blame] | 392 | #endif |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 393 | else |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 394 | value = (long)va_arg (args, unsigned int); |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame] | 395 | if (fmtint(buffer, &currlen, maxlen, value, |
| 396 | 16, min, max, flags) == -1) |
| 397 | return -1; |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 398 | break; |
| 399 | case 'f': |
| 400 | if (cflags == DP_C_LDOUBLE) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 401 | fvalue = va_arg (args, LDOUBLE); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 402 | else |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 403 | fvalue = va_arg (args, double); |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame] | 404 | if (fmtfp(buffer, &currlen, maxlen, fvalue, |
| 405 | min, max, flags) == -1) |
| 406 | return -1; |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 407 | break; |
| 408 | case 'E': |
| 409 | flags |= DP_F_UP; |
| 410 | case 'e': |
| 411 | if (cflags == DP_C_LDOUBLE) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 412 | fvalue = va_arg (args, LDOUBLE); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 413 | else |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 414 | fvalue = va_arg (args, double); |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame] | 415 | if (fmtfp(buffer, &currlen, maxlen, fvalue, |
| 416 | min, max, flags) == -1) |
| 417 | return -1; |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 418 | break; |
| 419 | case 'G': |
| 420 | flags |= DP_F_UP; |
| 421 | case 'g': |
| 422 | if (cflags == DP_C_LDOUBLE) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 423 | fvalue = va_arg (args, LDOUBLE); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 424 | else |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 425 | fvalue = va_arg (args, double); |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame] | 426 | if (fmtfp(buffer, &currlen, maxlen, fvalue, |
| 427 | min, max, flags) == -1) |
| 428 | return -1; |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 429 | break; |
| 430 | case 'c': |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame] | 431 | DOPR_OUTCH(buffer, currlen, maxlen, |
| 432 | va_arg (args, int)); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 433 | break; |
| 434 | case 's': |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 435 | strvalue = va_arg (args, char *); |
| 436 | if (!strvalue) strvalue = "(NULL)"; |
| 437 | if (max == -1) { |
| 438 | max = strlen(strvalue); |
| 439 | } |
| 440 | if (min > 0 && max >= 0 && min > max) max = min; |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame] | 441 | if (fmtstr(buffer, &currlen, maxlen, |
| 442 | strvalue, flags, min, max) == -1) |
| 443 | return -1; |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 444 | break; |
| 445 | case 'p': |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 446 | strvalue = va_arg (args, void *); |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame] | 447 | if (fmtint(buffer, &currlen, maxlen, |
| 448 | (long) strvalue, 16, min, max, flags) == -1) |
| 449 | return -1; |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 450 | break; |
Damien Miller | 04be8b9 | 2013-08-28 12:49:43 +1000 | [diff] [blame] | 451 | #if we_dont_want_this_in_openssh |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 452 | case 'n': |
| 453 | if (cflags == DP_C_SHORT) { |
| 454 | short int *num; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 455 | num = va_arg (args, short int *); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 456 | *num = currlen; |
| 457 | } else if (cflags == DP_C_LONG) { |
| 458 | long int *num; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 459 | num = va_arg (args, long int *); |
| 460 | *num = (long int)currlen; |
| 461 | } else if (cflags == DP_C_LLONG) { |
| 462 | LLONG *num; |
| 463 | num = va_arg (args, LLONG *); |
| 464 | *num = (LLONG)currlen; |
Damien Miller | 04be8b9 | 2013-08-28 12:49:43 +1000 | [diff] [blame] | 465 | } else if (cflags == DP_C_SIZE) { |
| 466 | ssize_t *num; |
| 467 | num = va_arg (args, ssize_t *); |
| 468 | *num = (ssize_t)currlen; |
| 469 | } else if (cflags == DP_C_INTMAX) { |
| 470 | intmax_t *num; |
| 471 | num = va_arg (args, intmax_t *); |
| 472 | *num = (intmax_t)currlen; |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 473 | } else { |
| 474 | int *num; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 475 | num = va_arg (args, int *); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 476 | *num = currlen; |
| 477 | } |
| 478 | break; |
Damien Miller | 04be8b9 | 2013-08-28 12:49:43 +1000 | [diff] [blame] | 479 | #endif |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 480 | case '%': |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame] | 481 | DOPR_OUTCH(buffer, currlen, maxlen, ch); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 482 | break; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 483 | case 'w': |
| 484 | /* not supported yet, treat as next char */ |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 485 | ch = *format++; |
| 486 | break; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 487 | default: |
| 488 | /* Unknown, skip */ |
| 489 | break; |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 490 | } |
| 491 | ch = *format++; |
| 492 | state = DP_S_DEFAULT; |
| 493 | flags = cflags = min = 0; |
| 494 | max = -1; |
| 495 | break; |
| 496 | case DP_S_DONE: |
| 497 | break; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 498 | default: |
| 499 | /* hmm? */ |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 500 | break; /* some picky compilers need this */ |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 501 | } |
| 502 | } |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 503 | if (maxlen != 0) { |
| 504 | if (currlen < maxlen - 1) |
| 505 | buffer[currlen] = '\0'; |
| 506 | else if (maxlen > 0) |
| 507 | buffer[maxlen - 1] = '\0'; |
| 508 | } |
| 509 | |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame] | 510 | return currlen < INT_MAX ? (int)currlen : -1; |
Damien Miller | 42b81ff | 1999-11-26 12:21:24 +1100 | [diff] [blame] | 511 | } |
| 512 | |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame] | 513 | static int |
| 514 | fmtstr(char *buffer, size_t *currlen, size_t maxlen, |
| 515 | char *value, int flags, int min, int max) |
Damien Miller | 42b81ff | 1999-11-26 12:21:24 +1100 | [diff] [blame] | 516 | { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 517 | int padlen, strln; /* amount to pad */ |
| 518 | int cnt = 0; |
| 519 | |
| 520 | #ifdef DEBUG_SNPRINTF |
| 521 | printf("fmtstr min=%d max=%d s=[%s]\n", min, max, value); |
| 522 | #endif |
| 523 | if (value == 0) { |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 524 | value = "<NULL>"; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 525 | } |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 526 | |
Darren Tucker | 4127f55 | 2004-09-23 21:35:09 +1000 | [diff] [blame] | 527 | for (strln = 0; strln < max && value[strln]; ++strln); /* strlen */ |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 528 | padlen = min - strln; |
| 529 | if (padlen < 0) |
| 530 | padlen = 0; |
| 531 | if (flags & DP_F_MINUS) |
| 532 | padlen = -padlen; /* Left Justify */ |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 533 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 534 | while ((padlen > 0) && (cnt < max)) { |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame] | 535 | DOPR_OUTCH(buffer, *currlen, maxlen, ' '); |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 536 | --padlen; |
| 537 | ++cnt; |
| 538 | } |
| 539 | while (*value && (cnt < max)) { |
Darren Tucker | 07877ca | 2007-01-24 00:07:29 +1100 | [diff] [blame] | 540 | DOPR_OUTCH(buffer, *currlen, maxlen, *value); |
Damien Miller | 4d69aea | 2014-08-22 17:48:27 +1000 | [diff] [blame^] | 541 | value++; |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 542 | ++cnt; |
| 543 | } |
| 544 | while ((padlen < 0) && (cnt < max)) { |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame] | 545 | DOPR_OUTCH(buffer, *currlen, maxlen, ' '); |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 546 | ++padlen; |
| 547 | ++cnt; |
| 548 | } |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame] | 549 | return 0; |
Damien Miller | 42b81ff | 1999-11-26 12:21:24 +1100 | [diff] [blame] | 550 | } |
| 551 | |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 552 | /* Have to handle DP_F_NUM (ie 0x and 0 alternates) */ |
| 553 | |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame] | 554 | static int |
| 555 | fmtint(char *buffer, size_t *currlen, size_t maxlen, |
Damien Miller | 4d69aea | 2014-08-22 17:48:27 +1000 | [diff] [blame^] | 556 | intmax_t value, int base, int min, int max, int flags) |
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 | int signvalue = 0; |
Darren Tucker | 9834cab | 2006-03-19 00:07:07 +1100 | [diff] [blame] | 559 | unsigned LLONG uvalue; |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 560 | char convert[20]; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 561 | int place = 0; |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 562 | int spadlen = 0; /* amount to space pad */ |
| 563 | int zpadlen = 0; /* amount to zero pad */ |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 564 | int caps = 0; |
| 565 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 566 | if (max < 0) |
| 567 | max = 0; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 568 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 569 | uvalue = value; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 570 | |
| 571 | if(!(flags & DP_F_UNSIGNED)) { |
| 572 | if( value < 0 ) { |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 573 | signvalue = '-'; |
| 574 | uvalue = -value; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 575 | } else { |
| 576 | if (flags & DP_F_PLUS) /* Do a sign (+/i) */ |
| 577 | signvalue = '+'; |
| 578 | else if (flags & DP_F_SPACE) |
| 579 | signvalue = ' '; |
| 580 | } |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 581 | } |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 582 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 583 | if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */ |
| 584 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 585 | do { |
| 586 | convert[place++] = |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 587 | (caps? "0123456789ABCDEF":"0123456789abcdef") |
| 588 | [uvalue % (unsigned)base ]; |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 589 | uvalue = (uvalue / (unsigned)base ); |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 590 | } while(uvalue && (place < 20)); |
| 591 | if (place == 20) place--; |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 592 | convert[place] = 0; |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 593 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 594 | zpadlen = max - place; |
| 595 | spadlen = min - MAX (max, place) - (signvalue ? 1 : 0); |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 596 | if (zpadlen < 0) zpadlen = 0; |
| 597 | if (spadlen < 0) spadlen = 0; |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 598 | if (flags & DP_F_ZERO) { |
| 599 | zpadlen = MAX(zpadlen, spadlen); |
| 600 | spadlen = 0; |
| 601 | } |
| 602 | if (flags & DP_F_MINUS) |
| 603 | spadlen = -spadlen; /* Left Justifty */ |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 604 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 605 | #ifdef DEBUG_SNPRINTF |
| 606 | printf("zpad: %d, spad: %d, min: %d, max: %d, place: %d\n", |
| 607 | zpadlen, spadlen, min, max, place); |
| 608 | #endif |
| 609 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 610 | /* Spaces */ |
| 611 | while (spadlen > 0) { |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame] | 612 | DOPR_OUTCH(buffer, *currlen, maxlen, ' '); |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 613 | --spadlen; |
| 614 | } |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 615 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 616 | /* Sign */ |
| 617 | if (signvalue) |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame] | 618 | DOPR_OUTCH(buffer, *currlen, maxlen, signvalue); |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 619 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 620 | /* Zeros */ |
| 621 | if (zpadlen > 0) { |
| 622 | while (zpadlen > 0) { |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame] | 623 | DOPR_OUTCH(buffer, *currlen, maxlen, '0'); |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 624 | --zpadlen; |
| 625 | } |
| 626 | } |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 627 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 628 | /* Digits */ |
Darren Tucker | 07877ca | 2007-01-24 00:07:29 +1100 | [diff] [blame] | 629 | while (place > 0) { |
| 630 | --place; |
| 631 | DOPR_OUTCH(buffer, *currlen, maxlen, convert[place]); |
| 632 | } |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 633 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 634 | /* Left Justified spaces */ |
| 635 | while (spadlen < 0) { |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame] | 636 | DOPR_OUTCH(buffer, *currlen, maxlen, ' '); |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 637 | ++spadlen; |
| 638 | } |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame] | 639 | return 0; |
Damien Miller | 42b81ff | 1999-11-26 12:21:24 +1100 | [diff] [blame] | 640 | } |
| 641 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 642 | static LDOUBLE abs_val(LDOUBLE value) |
Damien Miller | 42b81ff | 1999-11-26 12:21:24 +1100 | [diff] [blame] | 643 | { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 644 | LDOUBLE result = value; |
Damien Miller | 42b81ff | 1999-11-26 12:21:24 +1100 | [diff] [blame] | 645 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 646 | if (value < 0) |
| 647 | result = -value; |
| 648 | |
| 649 | return result; |
| 650 | } |
| 651 | |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame] | 652 | static LDOUBLE POW10(int val) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 653 | { |
| 654 | LDOUBLE result = 1; |
| 655 | |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame] | 656 | while (val) { |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 657 | result *= 10; |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame] | 658 | val--; |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 659 | } |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 660 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 661 | return result; |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 662 | } |
| 663 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 664 | static LLONG ROUND(LDOUBLE value) |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 665 | { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 666 | LLONG intpart; |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 667 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 668 | intpart = (LLONG)value; |
| 669 | value = value - intpart; |
| 670 | if (value >= 0.5) intpart++; |
| 671 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 672 | return intpart; |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 673 | } |
| 674 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 675 | /* a replacement for modf that doesn't need the math library. Should |
| 676 | be portable, but slow */ |
| 677 | static double my_modf(double x0, double *iptr) |
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 | int i; |
| 680 | long l; |
| 681 | double x = x0; |
| 682 | double f = 1.0; |
| 683 | |
| 684 | for (i=0;i<100;i++) { |
| 685 | l = (long)x; |
| 686 | if (l <= (x+1) && l >= (x-1)) break; |
| 687 | x *= 0.1; |
| 688 | f *= 10.0; |
| 689 | } |
| 690 | |
| 691 | if (i == 100) { |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame] | 692 | /* |
| 693 | * yikes! the number is beyond what we can handle. |
| 694 | * What do we do? |
| 695 | */ |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 696 | (*iptr) = 0; |
| 697 | return 0; |
| 698 | } |
| 699 | |
| 700 | if (i != 0) { |
| 701 | double i2; |
| 702 | double ret; |
| 703 | |
| 704 | ret = my_modf(x0-l*f, &i2); |
| 705 | (*iptr) = l*f + i2; |
| 706 | return ret; |
| 707 | } |
| 708 | |
| 709 | (*iptr) = l; |
| 710 | return x - (*iptr); |
| 711 | } |
| 712 | |
| 713 | |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame] | 714 | static int |
| 715 | fmtfp (char *buffer, size_t *currlen, size_t maxlen, |
| 716 | LDOUBLE fvalue, int min, int max, int flags) |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 717 | { |
| 718 | int signvalue = 0; |
| 719 | double ufvalue; |
| 720 | char iconvert[311]; |
| 721 | char fconvert[311]; |
| 722 | int iplace = 0; |
| 723 | int fplace = 0; |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 724 | int padlen = 0; /* amount to pad */ |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 725 | int zpadlen = 0; |
| 726 | int caps = 0; |
| 727 | int idx; |
| 728 | double intpart; |
| 729 | double fracpart; |
| 730 | double temp; |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 731 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 732 | /* |
| 733 | * AIX manpage says the default is 0, but Solaris says the default |
| 734 | * is 6, and sprintf on AIX defaults to 6 |
| 735 | */ |
| 736 | if (max < 0) |
| 737 | max = 6; |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 738 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 739 | ufvalue = abs_val (fvalue); |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 740 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 741 | if (fvalue < 0) { |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 742 | signvalue = '-'; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 743 | } else { |
| 744 | if (flags & DP_F_PLUS) { /* Do a sign (+/i) */ |
| 745 | signvalue = '+'; |
| 746 | } else { |
| 747 | if (flags & DP_F_SPACE) |
| 748 | signvalue = ' '; |
| 749 | } |
| 750 | } |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 751 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 752 | #if 0 |
| 753 | if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */ |
| 754 | #endif |
| 755 | |
| 756 | #if 0 |
| 757 | if (max == 0) ufvalue += 0.5; /* if max = 0 we must round */ |
| 758 | #endif |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 759 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 760 | /* |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 761 | * Sorry, we only support 16 digits past the decimal because of our |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 762 | * conversion method |
| 763 | */ |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 764 | if (max > 16) |
| 765 | max = 16; |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 766 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 767 | /* We "cheat" by converting the fractional part to integer by |
| 768 | * multiplying by a factor of 10 |
| 769 | */ |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 770 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 771 | temp = ufvalue; |
| 772 | my_modf(temp, &intpart); |
| 773 | |
| 774 | fracpart = ROUND((POW10(max)) * (ufvalue - intpart)); |
| 775 | |
| 776 | if (fracpart >= POW10(max)) { |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 777 | intpart++; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 778 | fracpart -= POW10(max); |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 779 | } |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 780 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 781 | /* Convert integer part */ |
| 782 | do { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 783 | temp = intpart*0.1; |
| 784 | my_modf(temp, &intpart); |
| 785 | idx = (int) ((temp -intpart +0.05)* 10.0); |
| 786 | /* idx = (int) (((double)(temp*0.1) -intpart +0.05) *10.0); */ |
| 787 | /* printf ("%llf, %f, %x\n", temp, intpart, idx); */ |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 788 | iconvert[iplace++] = |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 789 | (caps? "0123456789ABCDEF":"0123456789abcdef")[idx]; |
| 790 | } while (intpart && (iplace < 311)); |
| 791 | if (iplace == 311) iplace--; |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 792 | iconvert[iplace] = 0; |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 793 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 794 | /* Convert fractional part */ |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 795 | if (fracpart) |
| 796 | { |
| 797 | do { |
| 798 | temp = fracpart*0.1; |
| 799 | my_modf(temp, &fracpart); |
| 800 | idx = (int) ((temp -fracpart +0.05)* 10.0); |
| 801 | /* idx = (int) ((((temp/10) -fracpart) +0.05) *10); */ |
| 802 | /* printf ("%lf, %lf, %ld\n", temp, fracpart, idx ); */ |
| 803 | fconvert[fplace++] = |
| 804 | (caps? "0123456789ABCDEF":"0123456789abcdef")[idx]; |
| 805 | } while(fracpart && (fplace < 311)); |
| 806 | if (fplace == 311) fplace--; |
| 807 | } |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 808 | fconvert[fplace] = 0; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 809 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 810 | /* -1 for decimal point, another -1 if we are printing a sign */ |
| 811 | padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0); |
| 812 | zpadlen = max - fplace; |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 813 | if (zpadlen < 0) zpadlen = 0; |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 814 | if (padlen < 0) |
| 815 | padlen = 0; |
| 816 | if (flags & DP_F_MINUS) |
| 817 | padlen = -padlen; /* Left Justifty */ |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 818 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 819 | if ((flags & DP_F_ZERO) && (padlen > 0)) { |
| 820 | if (signvalue) { |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame] | 821 | DOPR_OUTCH(buffer, *currlen, maxlen, signvalue); |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 822 | --padlen; |
| 823 | signvalue = 0; |
| 824 | } |
| 825 | while (padlen > 0) { |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame] | 826 | DOPR_OUTCH(buffer, *currlen, maxlen, '0'); |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 827 | --padlen; |
| 828 | } |
| 829 | } |
| 830 | while (padlen > 0) { |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame] | 831 | DOPR_OUTCH(buffer, *currlen, maxlen, ' '); |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 832 | --padlen; |
| 833 | } |
| 834 | if (signvalue) |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame] | 835 | DOPR_OUTCH(buffer, *currlen, maxlen, signvalue); |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 836 | |
Darren Tucker | 07877ca | 2007-01-24 00:07:29 +1100 | [diff] [blame] | 837 | while (iplace > 0) { |
| 838 | --iplace; |
| 839 | DOPR_OUTCH(buffer, *currlen, maxlen, iconvert[iplace]); |
| 840 | } |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 841 | |
| 842 | #ifdef DEBUG_SNPRINTF |
| 843 | printf("fmtfp: fplace=%d zpadlen=%d\n", fplace, zpadlen); |
| 844 | #endif |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 845 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 846 | /* |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 847 | * Decimal point. This should probably use locale to find the correct |
| 848 | * char to print out. |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 849 | */ |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 850 | if (max > 0) { |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame] | 851 | DOPR_OUTCH(buffer, *currlen, maxlen, '.'); |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 852 | |
| 853 | while (zpadlen > 0) { |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame] | 854 | DOPR_OUTCH(buffer, *currlen, maxlen, '0'); |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 855 | --zpadlen; |
| 856 | } |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 857 | |
Darren Tucker | 07877ca | 2007-01-24 00:07:29 +1100 | [diff] [blame] | 858 | while (fplace > 0) { |
| 859 | --fplace; |
| 860 | DOPR_OUTCH(buffer, *currlen, maxlen, fconvert[fplace]); |
| 861 | } |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 862 | } |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 863 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 864 | while (padlen < 0) { |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame] | 865 | DOPR_OUTCH(buffer, *currlen, maxlen, ' '); |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 866 | ++padlen; |
| 867 | } |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame] | 868 | return 0; |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 869 | } |
| 870 | #endif /* !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF) */ |
| 871 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 872 | #if !defined(HAVE_VSNPRINTF) |
Darren Tucker | 07877ca | 2007-01-24 00:07:29 +1100 | [diff] [blame] | 873 | int |
Damien Miller | 742cc1c | 2007-01-14 21:20:30 +1100 | [diff] [blame] | 874 | vsnprintf (char *str, size_t count, const char *fmt, va_list args) |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 875 | { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 876 | return dopr(str, count, fmt, args); |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 877 | } |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 878 | #endif |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 879 | |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 880 | #if !defined(HAVE_SNPRINTF) |
Darren Tucker | 07877ca | 2007-01-24 00:07:29 +1100 | [diff] [blame] | 881 | int |
| 882 | snprintf(char *str, size_t count, SNPRINTF_CONST char *fmt, ...) |
Damien Miller | c6b3bbe | 1999-12-13 08:27:33 +1100 | [diff] [blame] | 883 | { |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 884 | size_t ret; |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 885 | va_list ap; |
| 886 | |
| 887 | va_start(ap, fmt); |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 888 | ret = vsnprintf(str, count, fmt, ap); |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 889 | va_end(ap); |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 890 | return ret; |
Damien Miller | c6b3bbe | 1999-12-13 08:27:33 +1100 | [diff] [blame] | 891 | } |
Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 892 | #endif |