Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 1 | /************************************************************** |
| 2 | * Original: |
| 3 | * Patrick Powell Tue Apr 11 09:48:21 PDT 1995 |
| 4 | * A bombproof version of doprnt (dopr) included. |
| 5 | * Sigh. This sort of thing is always nasty do deal with. Note that |
| 6 | * the version here does not include floating point... |
Damien Miller | 42b81ff | 1999-11-26 12:21:24 +1100 | [diff] [blame] | 7 | * |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 8 | * snprintf() is used instead of sprintf() as it does limit checks |
| 9 | * for string length. This covers a nasty loophole. |
Damien Miller | 42b81ff | 1999-11-26 12:21:24 +1100 | [diff] [blame] | 10 | * |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 11 | * The other functions are there to prevent NULL pointers from |
| 12 | * causing nast effects. |
Damien Miller | 42b81ff | 1999-11-26 12:21:24 +1100 | [diff] [blame] | 13 | * |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 14 | * More Recently: |
| 15 | * Brandon Long <blong@fiction.net> 9/15/96 for mutt 0.43 |
| 16 | * This was ugly. It is still ugly. I opted out of floating point |
| 17 | * numbers, but the formatter understands just about everything |
| 18 | * from the normal C string format, at least as far as I can tell from |
| 19 | * the Solaris 2.5 printf(3S) man page. |
| 20 | * |
| 21 | * Brandon Long <blong@fiction.net> 10/22/97 for mutt 0.87.1 |
| 22 | * Ok, added some minimal floating point support, which means this |
| 23 | * probably requires libm on most operating systems. Don't yet |
| 24 | * support the exponent (e,E) and sigfig (g,G). Also, fmtint() |
| 25 | * was pretty badly broken, it just wasn't being exercised in ways |
| 26 | * which showed it, so that's been fixed. Also, formated the code |
| 27 | * to mutt conventions, and removed dead code left over from the |
| 28 | * original. Also, there is now a builtin-test, just compile with: |
| 29 | * gcc -DTEST_SNPRINTF -o snprintf snprintf.c -lm |
| 30 | * and run snprintf for results. |
| 31 | * |
| 32 | * Thomas Roessler <roessler@guug.de> 01/27/98 for mutt 0.89i |
| 33 | * The PGP code was using unsigned hexadecimal formats. |
| 34 | * Unfortunately, unsigned formats simply didn't work. |
| 35 | * |
| 36 | * Michael Elkins <me@cs.hmc.edu> 03/05/98 for mutt 0.90.8 |
| 37 | * The original code assumed that both snprintf() and vsnprintf() were |
| 38 | * missing. Some systems only have snprintf() but not vsnprintf(), so |
| 39 | * the code is now broken down under HAVE_SNPRINTF and HAVE_VSNPRINTF. |
| 40 | * |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 41 | * Ben Lindstrom <mouring@eviladmin.org> 09/27/00 for OpenSSH |
Damien Miller | 3dfb0dd | 2000-09-30 09:49:08 +1100 | [diff] [blame] | 42 | * Welcome to the world of %lld and %qd support. With other |
| 43 | * long long support. This is needed for sftp-server to work |
| 44 | * right. |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 45 | * |
| 46 | * Ben Lindstrom <mouring@eviladmin.org> 02/12/01 for OpenSSH |
| 47 | * Removed all hint of VARARGS stuff and banished it to the void, |
| 48 | * and did a bit of KNF style work to make things a bit more |
| 49 | * acceptable. Consider stealing from mutt or enlightenment. |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 50 | **************************************************************/ |
Damien Miller | 42b81ff | 1999-11-26 12:21:24 +1100 | [diff] [blame] | 51 | |
Damien Miller | e9cf357 | 2001-02-09 12:55:35 +1100 | [diff] [blame] | 52 | #include "includes.h" |
| 53 | |
Ben Lindstrom | 116b6bd | 2001-02-13 14:05:59 +0000 | [diff] [blame^] | 54 | RCSID("$Id: bsd-snprintf.c,v 1.4 2001/02/13 14:05:59 mouring Exp $"); |
Damien Miller | 42b81ff | 1999-11-26 12:21:24 +1100 | [diff] [blame] | 55 | |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 56 | #if !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF) |
Damien Miller | 42b81ff | 1999-11-26 12:21:24 +1100 | [diff] [blame] | 57 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 58 | static void |
| 59 | dopr(char *buffer, size_t maxlen, const char *format, va_list args); |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 60 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 61 | static void |
| 62 | fmtstr(char *buffer, size_t *currlen, size_t maxlen, char *value, int flags, |
| 63 | int min, int max); |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 64 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 65 | static void |
| 66 | fmtint(char *buffer, size_t *currlen, size_t maxlen, long value, int base, |
| 67 | int min, int max, int flags); |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 68 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 69 | static void |
| 70 | fmtfp(char *buffer, size_t *currlen, size_t maxlen, long double fvalue, |
| 71 | int min, int max, int flags); |
Damien Miller | 42b81ff | 1999-11-26 12:21:24 +1100 | [diff] [blame] | 72 | |
Ben Lindstrom | 116b6bd | 2001-02-13 14:05:59 +0000 | [diff] [blame^] | 73 | static void |
| 74 | dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c); |
| 75 | |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 76 | /* |
| 77 | * dopr(): poor man's version of doprintf |
| 78 | */ |
| 79 | |
| 80 | /* format read states */ |
| 81 | #define DP_S_DEFAULT 0 |
| 82 | #define DP_S_FLAGS 1 |
| 83 | #define DP_S_MIN 2 |
| 84 | #define DP_S_DOT 3 |
| 85 | #define DP_S_MAX 4 |
| 86 | #define DP_S_MOD 5 |
| 87 | #define DP_S_CONV 6 |
| 88 | #define DP_S_DONE 7 |
| 89 | |
| 90 | /* format flags - Bits */ |
| 91 | #define DP_F_MINUS (1 << 0) |
| 92 | #define DP_F_PLUS (1 << 1) |
| 93 | #define DP_F_SPACE (1 << 2) |
| 94 | #define DP_F_NUM (1 << 3) |
| 95 | #define DP_F_ZERO (1 << 4) |
| 96 | #define DP_F_UP (1 << 5) |
| 97 | #define DP_F_UNSIGNED (1 << 6) |
| 98 | |
| 99 | /* Conversion Flags */ |
Damien Miller | 3dfb0dd | 2000-09-30 09:49:08 +1100 | [diff] [blame] | 100 | #define DP_C_SHORT 1 |
| 101 | #define DP_C_LONG 2 |
| 102 | #define DP_C_LDOUBLE 3 |
| 103 | #define DP_C_LONG_LONG 4 |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 104 | |
| 105 | #define char_to_int(p) (p - '0') |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 106 | #define abs_val(p) (p < 0 ? -p : p) |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 107 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 108 | |
| 109 | static void |
| 110 | dopr(char *buffer, size_t maxlen, const char *format, va_list args) |
Damien Miller | 13bc0be | 1999-12-28 10:19:16 +1100 | [diff] [blame] | 111 | { |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 112 | char *strvalue; |
| 113 | char ch; |
| 114 | long value; |
| 115 | long double fvalue; |
| 116 | int min = 0; |
| 117 | int max = -1; |
| 118 | int state = DP_S_DEFAULT; |
| 119 | int flags = 0; |
| 120 | int cflags = 0; |
| 121 | size_t currlen = 0; |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 122 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 123 | ch = *format++; |
Damien Miller | 13bc0be | 1999-12-28 10:19:16 +1100 | [diff] [blame] | 124 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 125 | while (state != DP_S_DONE) { |
| 126 | if ((ch == '\0') || (currlen >= maxlen)) |
| 127 | state = DP_S_DONE; |
Damien Miller | 42b81ff | 1999-11-26 12:21:24 +1100 | [diff] [blame] | 128 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 129 | switch(state) { |
| 130 | case DP_S_DEFAULT: |
| 131 | if (ch == '%') |
| 132 | state = DP_S_FLAGS; |
| 133 | else |
| 134 | dopr_outch(buffer, &currlen, maxlen, ch); |
| 135 | ch = *format++; |
| 136 | break; |
| 137 | case DP_S_FLAGS: |
| 138 | switch (ch) { |
| 139 | case '-': |
| 140 | flags |= DP_F_MINUS; |
| 141 | ch = *format++; |
| 142 | break; |
| 143 | case '+': |
| 144 | flags |= DP_F_PLUS; |
| 145 | ch = *format++; |
| 146 | break; |
| 147 | case ' ': |
| 148 | flags |= DP_F_SPACE; |
| 149 | ch = *format++; |
| 150 | break; |
| 151 | case '#': |
| 152 | flags |= DP_F_NUM; |
| 153 | ch = *format++; |
| 154 | break; |
| 155 | case '0': |
| 156 | flags |= DP_F_ZERO; |
| 157 | ch = *format++; |
| 158 | break; |
| 159 | default: |
| 160 | state = DP_S_MIN; |
| 161 | break; |
| 162 | } |
| 163 | break; |
| 164 | case DP_S_MIN: |
| 165 | if (isdigit((unsigned char)ch)) { |
| 166 | min = 10*min + char_to_int (ch); |
| 167 | ch = *format++; |
| 168 | } else if (ch == '*') { |
| 169 | min = va_arg (args, int); |
| 170 | ch = *format++; |
| 171 | state = DP_S_DOT; |
| 172 | } else |
| 173 | state = DP_S_DOT; |
| 174 | break; |
| 175 | case DP_S_DOT: |
| 176 | if (ch == '.') { |
| 177 | state = DP_S_MAX; |
| 178 | ch = *format++; |
| 179 | } else |
| 180 | state = DP_S_MOD; |
| 181 | break; |
| 182 | case DP_S_MAX: |
| 183 | if (isdigit((unsigned char)ch)) { |
| 184 | if (max < 0) |
| 185 | max = 0; |
| 186 | max = 10*max + char_to_int(ch); |
| 187 | ch = *format++; |
| 188 | } else if (ch == '*') { |
| 189 | max = va_arg (args, int); |
| 190 | ch = *format++; |
| 191 | state = DP_S_MOD; |
| 192 | } else |
| 193 | state = DP_S_MOD; |
| 194 | break; |
| 195 | case DP_S_MOD: |
| 196 | switch (ch) { |
| 197 | case 'h': |
| 198 | cflags = DP_C_SHORT; |
| 199 | ch = *format++; |
| 200 | break; |
| 201 | case 'l': |
| 202 | cflags = DP_C_LONG; |
| 203 | ch = *format++; |
| 204 | if (ch == 'l') { |
| 205 | cflags = DP_C_LONG_LONG; |
| 206 | ch = *format++; |
| 207 | } |
| 208 | break; |
| 209 | case 'q': |
| 210 | cflags = DP_C_LONG_LONG; |
| 211 | ch = *format++; |
| 212 | break; |
| 213 | case 'L': |
| 214 | cflags = DP_C_LDOUBLE; |
| 215 | ch = *format++; |
| 216 | break; |
| 217 | default: |
| 218 | break; |
| 219 | } |
| 220 | state = DP_S_CONV; |
| 221 | break; |
| 222 | case DP_S_CONV: |
| 223 | switch (ch) { |
| 224 | case 'd': |
| 225 | case 'i': |
| 226 | if (cflags == DP_C_SHORT) |
| 227 | value = va_arg(args, int); |
| 228 | else if (cflags == DP_C_LONG) |
| 229 | value = va_arg(args, long int); |
| 230 | else if (cflags == DP_C_LONG_LONG) |
| 231 | value = va_arg (args, long long); |
| 232 | else |
| 233 | value = va_arg (args, int); |
| 234 | fmtint(buffer, &currlen, maxlen, value, 10, min, max, flags); |
| 235 | break; |
| 236 | case 'o': |
| 237 | flags |= DP_F_UNSIGNED; |
| 238 | if (cflags == DP_C_SHORT) |
| 239 | value = va_arg(args, unsigned int); |
| 240 | else if (cflags == DP_C_LONG) |
| 241 | value = va_arg(args, unsigned long int); |
| 242 | else if (cflags == DP_C_LONG_LONG) |
| 243 | value = va_arg(args, unsigned long long); |
| 244 | else |
| 245 | value = va_arg(args, unsigned int); |
| 246 | fmtint(buffer, &currlen, maxlen, value, 8, min, max, flags); |
| 247 | break; |
| 248 | case 'u': |
| 249 | flags |= DP_F_UNSIGNED; |
| 250 | if (cflags == DP_C_SHORT) |
| 251 | value = va_arg(args, unsigned int); |
| 252 | else if (cflags == DP_C_LONG) |
| 253 | value = va_arg(args, unsigned long int); |
| 254 | else if (cflags == DP_C_LONG_LONG) |
| 255 | value = va_arg(args, unsigned long long); |
| 256 | else |
| 257 | value = va_arg(args, unsigned int); |
| 258 | fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags); |
| 259 | break; |
| 260 | case 'X': |
| 261 | flags |= DP_F_UP; |
| 262 | case 'x': |
| 263 | flags |= DP_F_UNSIGNED; |
| 264 | if (cflags == DP_C_SHORT) |
| 265 | value = va_arg(args, unsigned int); |
| 266 | else if (cflags == DP_C_LONG) |
| 267 | value = va_arg(args, unsigned long int); |
| 268 | else if (cflags == DP_C_LONG_LONG) |
| 269 | value = va_arg(args, unsigned long long); |
| 270 | else |
| 271 | value = va_arg(args, unsigned int); |
| 272 | fmtint(buffer, &currlen, maxlen, value, 16, min, max, flags); |
| 273 | break; |
| 274 | case 'f': |
| 275 | if (cflags == DP_C_LDOUBLE) |
| 276 | fvalue = va_arg(args, long double); |
| 277 | else |
| 278 | fvalue = va_arg(args, double); |
| 279 | /* um, floating point? */ |
| 280 | fmtfp(buffer, &currlen, maxlen, fvalue, min, max, flags); |
| 281 | break; |
| 282 | case 'E': |
| 283 | flags |= DP_F_UP; |
| 284 | case 'e': |
| 285 | if (cflags == DP_C_LDOUBLE) |
| 286 | fvalue = va_arg(args, long double); |
| 287 | else |
| 288 | fvalue = va_arg(args, double); |
| 289 | break; |
| 290 | case 'G': |
| 291 | flags |= DP_F_UP; |
| 292 | case 'g': |
| 293 | if (cflags == DP_C_LDOUBLE) |
| 294 | fvalue = va_arg(args, long double); |
| 295 | else |
| 296 | fvalue = va_arg(args, double); |
| 297 | break; |
| 298 | case 'c': |
| 299 | dopr_outch(buffer, &currlen, maxlen, va_arg(args, int)); |
| 300 | break; |
| 301 | case 's': |
| 302 | strvalue = va_arg(args, char *); |
| 303 | if (max < 0) |
| 304 | max = maxlen; /* ie, no max */ |
| 305 | fmtstr(buffer, &currlen, maxlen, strvalue, flags, min, max); |
| 306 | break; |
| 307 | case 'p': |
| 308 | strvalue = va_arg(args, void *); |
| 309 | fmtint(buffer, &currlen, maxlen, (long) strvalue, 16, min, max, flags); |
| 310 | break; |
| 311 | case 'n': |
| 312 | if (cflags == DP_C_SHORT) { |
| 313 | short int *num; |
| 314 | num = va_arg(args, short int *); |
| 315 | *num = currlen; |
| 316 | } else if (cflags == DP_C_LONG) { |
| 317 | long int *num; |
| 318 | num = va_arg(args, long int *); |
| 319 | *num = currlen; |
| 320 | } else if (cflags == DP_C_LONG_LONG) { |
| 321 | long long *num; |
| 322 | num = va_arg(args, long long *); |
| 323 | *num = currlen; |
| 324 | } else { |
| 325 | int *num; |
| 326 | num = va_arg(args, int *); |
| 327 | *num = currlen; |
| 328 | } |
| 329 | break; |
| 330 | case '%': |
| 331 | dopr_outch(buffer, &currlen, maxlen, ch); |
| 332 | break; |
| 333 | case 'w': /* not supported yet, treat as next char */ |
| 334 | ch = *format++; |
| 335 | break; |
| 336 | default: /* Unknown, skip */ |
| 337 | break; |
| 338 | } |
| 339 | ch = *format++; |
| 340 | state = DP_S_DEFAULT; |
| 341 | flags = cflags = min = 0; |
| 342 | max = -1; |
| 343 | break; |
| 344 | case DP_S_DONE: |
| 345 | break; |
| 346 | default: /* hmm? */ |
| 347 | break; /* some picky compilers need this */ |
| 348 | } |
| 349 | } |
| 350 | if (currlen < maxlen - 1) |
| 351 | buffer[currlen] = '\0'; |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 352 | else |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 353 | buffer[maxlen - 1] = '\0'; |
Damien Miller | 42b81ff | 1999-11-26 12:21:24 +1100 | [diff] [blame] | 354 | } |
| 355 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 356 | static void |
| 357 | fmtstr(char *buffer, size_t *currlen, size_t maxlen, |
| 358 | char *value, int flags, int min, int max) |
Damien Miller | 42b81ff | 1999-11-26 12:21:24 +1100 | [diff] [blame] | 359 | { |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 360 | int padlen, strln; /* amount to pad */ |
| 361 | int cnt = 0; |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 362 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 363 | if (value == 0) |
| 364 | value = "<NULL>"; |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 365 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 366 | for (strln = 0; value[strln]; ++strln); /* strlen */ |
| 367 | padlen = min - strln; |
| 368 | if (padlen < 0) |
| 369 | padlen = 0; |
| 370 | if (flags & DP_F_MINUS) |
| 371 | padlen = -padlen; /* Left Justify */ |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 372 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 373 | while ((padlen > 0) && (cnt < max)) { |
| 374 | dopr_outch(buffer, currlen, maxlen, ' '); |
| 375 | --padlen; |
| 376 | ++cnt; |
| 377 | } |
| 378 | while (*value && (cnt < max)) { |
| 379 | dopr_outch(buffer, currlen, maxlen, *value++); |
| 380 | ++cnt; |
| 381 | } |
| 382 | while ((padlen < 0) && (cnt < max)) { |
| 383 | dopr_outch(buffer, currlen, maxlen, ' '); |
| 384 | ++padlen; |
| 385 | ++cnt; |
| 386 | } |
Damien Miller | 42b81ff | 1999-11-26 12:21:24 +1100 | [diff] [blame] | 387 | } |
| 388 | |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 389 | /* Have to handle DP_F_NUM (ie 0x and 0 alternates) */ |
| 390 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 391 | static void |
| 392 | fmtint(char *buffer, size_t *currlen, size_t maxlen, |
| 393 | long value, int base, int min, int max, int flags) |
Damien Miller | 42b81ff | 1999-11-26 12:21:24 +1100 | [diff] [blame] | 394 | { |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 395 | unsigned long uvalue; |
| 396 | char convert[20]; |
| 397 | int signvalue = 0; |
| 398 | int place = 0; |
| 399 | int spadlen = 0; /* amount to space pad */ |
| 400 | int zpadlen = 0; /* amount to zero pad */ |
| 401 | int caps = 0; |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 402 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 403 | if (max < 0) |
| 404 | max = 0; |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 405 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 406 | uvalue = value; |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 407 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 408 | if (!(flags & DP_F_UNSIGNED)) { |
| 409 | if (value < 0) { |
| 410 | signvalue = '-'; |
| 411 | uvalue = -value; |
| 412 | } else if (flags & DP_F_PLUS) /* Do a sign (+/i) */ |
| 413 | signvalue = '+'; |
| 414 | else if (flags & DP_F_SPACE) |
| 415 | signvalue = ' '; |
| 416 | } |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 417 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 418 | if (flags & DP_F_UP) |
| 419 | caps = 1; /* Should characters be upper case? */ |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 420 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 421 | do { |
| 422 | convert[place++] = |
| 423 | (caps? "0123456789ABCDEF":"0123456789abcdef") |
| 424 | [uvalue % (unsigned)base]; |
| 425 | uvalue = (uvalue / (unsigned)base ); |
| 426 | } while (uvalue && (place < 20)); |
| 427 | if (place == 20) |
| 428 | place--; |
| 429 | convert[place] = 0; |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 430 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 431 | zpadlen = max - place; |
| 432 | spadlen = min - MAX (max, place) - (signvalue ? 1 : 0); |
| 433 | if (zpadlen < 0) |
| 434 | zpadlen = 0; |
| 435 | if (spadlen < 0) |
| 436 | spadlen = 0; |
| 437 | if (flags & DP_F_ZERO) { |
| 438 | zpadlen = MAX(zpadlen, spadlen); |
| 439 | spadlen = 0; |
| 440 | } |
| 441 | if (flags & DP_F_MINUS) |
| 442 | spadlen = -spadlen; /* Left Justifty */ |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 443 | |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 444 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 445 | /* Spaces */ |
| 446 | while (spadlen > 0) { |
| 447 | dopr_outch(buffer, currlen, maxlen, ' '); |
| 448 | --spadlen; |
| 449 | } |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 450 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 451 | /* Sign */ |
| 452 | if (signvalue) |
| 453 | dopr_outch(buffer, currlen, maxlen, signvalue); |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 454 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 455 | /* Zeros */ |
| 456 | if (zpadlen > 0) { |
| 457 | while (zpadlen > 0) { |
| 458 | dopr_outch(buffer, currlen, maxlen, '0'); |
| 459 | --zpadlen; |
| 460 | } |
| 461 | } |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 462 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 463 | /* Digits */ |
| 464 | while (place > 0) |
| 465 | dopr_outch(buffer, currlen, maxlen, convert[--place]); |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 466 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 467 | /* Left Justified spaces */ |
| 468 | while (spadlen < 0) { |
| 469 | dopr_outch (buffer, currlen, maxlen, ' '); |
| 470 | ++spadlen; |
| 471 | } |
Damien Miller | 42b81ff | 1999-11-26 12:21:24 +1100 | [diff] [blame] | 472 | } |
| 473 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 474 | static long double |
| 475 | pow10(int exp) |
Damien Miller | 42b81ff | 1999-11-26 12:21:24 +1100 | [diff] [blame] | 476 | { |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 477 | long double result = 1; |
Damien Miller | 42b81ff | 1999-11-26 12:21:24 +1100 | [diff] [blame] | 478 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 479 | while (exp) { |
| 480 | result *= 10; |
| 481 | exp--; |
| 482 | } |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 483 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 484 | return result; |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 485 | } |
| 486 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 487 | static long |
| 488 | round(long double value) |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 489 | { |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 490 | long intpart = value; |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 491 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 492 | value -= intpart; |
| 493 | if (value >= 0.5) |
| 494 | intpart++; |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 495 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 496 | return intpart; |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 497 | } |
| 498 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 499 | static void |
| 500 | fmtfp(char *buffer, size_t *currlen, size_t maxlen, long double fvalue, |
| 501 | int min, int max, int flags) |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 502 | { |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 503 | char iconvert[20]; |
| 504 | char fconvert[20]; |
| 505 | int signvalue = 0; |
| 506 | int iplace = 0; |
| 507 | int fplace = 0; |
| 508 | int padlen = 0; /* amount to pad */ |
| 509 | int zpadlen = 0; |
| 510 | int caps = 0; |
| 511 | long intpart; |
| 512 | long fracpart; |
| 513 | long double ufvalue; |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 514 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 515 | /* |
| 516 | * AIX manpage says the default is 0, but Solaris says the default |
| 517 | * is 6, and sprintf on AIX defaults to 6 |
| 518 | */ |
| 519 | if (max < 0) |
| 520 | max = 6; |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 521 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 522 | ufvalue = abs_val(fvalue); |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 523 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 524 | if (fvalue < 0) |
| 525 | signvalue = '-'; |
| 526 | else if (flags & DP_F_PLUS) /* Do a sign (+/i) */ |
| 527 | signvalue = '+'; |
| 528 | else if (flags & DP_F_SPACE) |
| 529 | signvalue = ' '; |
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 | intpart = ufvalue; |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 532 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 533 | /* |
| 534 | * Sorry, we only support 9 digits past the decimal because of our |
| 535 | * conversion method |
| 536 | */ |
| 537 | if (max > 9) |
| 538 | max = 9; |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 539 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 540 | /* We "cheat" by converting the fractional part to integer by |
| 541 | * multiplying by a factor of 10 |
| 542 | */ |
| 543 | fracpart = round((pow10 (max)) * (ufvalue - intpart)); |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 544 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 545 | if (fracpart >= pow10 (max)) { |
| 546 | intpart++; |
| 547 | fracpart -= pow10 (max); |
| 548 | } |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 549 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 550 | /* Convert integer part */ |
| 551 | do { |
| 552 | iconvert[iplace++] = |
| 553 | (caps? "0123456789ABCDEF":"0123456789abcdef")[intpart % 10]; |
| 554 | intpart = (intpart / 10); |
| 555 | } while(intpart && (iplace < 20)); |
| 556 | if (iplace == 20) |
| 557 | iplace--; |
| 558 | iconvert[iplace] = 0; |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 559 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 560 | /* Convert fractional part */ |
| 561 | do { |
| 562 | fconvert[fplace++] = |
| 563 | (caps? "0123456789ABCDEF":"0123456789abcdef")[fracpart % 10]; |
| 564 | fracpart = (fracpart / 10); |
| 565 | } while(fracpart && (fplace < 20)); |
| 566 | if (fplace == 20) |
| 567 | fplace--; |
| 568 | fconvert[fplace] = 0; |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 569 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 570 | /* -1 for decimal point, another -1 if we are printing a sign */ |
| 571 | padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0); |
| 572 | zpadlen = max - fplace; |
| 573 | if (zpadlen < 0) |
| 574 | zpadlen = 0; |
| 575 | if (padlen < 0) |
| 576 | padlen = 0; |
| 577 | if (flags & DP_F_MINUS) |
| 578 | padlen = -padlen; /* Left Justifty */ |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 579 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 580 | if ((flags & DP_F_ZERO) && (padlen > 0)) { |
| 581 | if (signvalue) { |
| 582 | dopr_outch(buffer, currlen, maxlen, signvalue); |
| 583 | --padlen; |
| 584 | signvalue = 0; |
| 585 | } |
| 586 | while (padlen > 0) { |
| 587 | dopr_outch(buffer, currlen, maxlen, '0'); |
| 588 | --padlen; |
| 589 | } |
| 590 | } |
| 591 | while (padlen > 0) { |
| 592 | dopr_outch(buffer, currlen, maxlen, ' '); |
| 593 | --padlen; |
| 594 | } |
| 595 | if (signvalue) |
| 596 | dopr_outch(buffer, currlen, maxlen, signvalue); |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 597 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 598 | while (iplace > 0) |
| 599 | dopr_outch(buffer, currlen, maxlen, iconvert[--iplace]); |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 600 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 601 | /* |
| 602 | * Decimal point. This should probably use locale to find the correct |
| 603 | * char to print out. |
| 604 | */ |
| 605 | dopr_outch(buffer, currlen, maxlen, '.'); |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 606 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 607 | while (fplace > 0) |
| 608 | dopr_outch(buffer, currlen, maxlen, fconvert[--fplace]); |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 609 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 610 | while (zpadlen > 0) { |
| 611 | dopr_outch(buffer, currlen, maxlen, '0'); |
| 612 | --zpadlen; |
| 613 | } |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 614 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 615 | while (padlen < 0) { |
| 616 | dopr_outch(buffer, currlen, maxlen, ' '); |
| 617 | ++padlen; |
| 618 | } |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 619 | } |
| 620 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 621 | static void |
| 622 | dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c) |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 623 | { |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 624 | if (*currlen < maxlen) |
| 625 | buffer[(*currlen)++] = c; |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 626 | } |
| 627 | #endif /* !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF) */ |
| 628 | |
| 629 | #ifndef HAVE_VSNPRINTF |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 630 | int |
| 631 | vsnprintf(char *str, size_t count, const char *fmt, va_list args) |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 632 | { |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 633 | str[0] = 0; |
| 634 | dopr(str, count, fmt, args); |
| 635 | |
| 636 | return(strlen(str)); |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 637 | } |
| 638 | #endif /* !HAVE_VSNPRINTF */ |
| 639 | |
| 640 | #ifndef HAVE_SNPRINTF |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 641 | int |
| 642 | snprintf(char *str,size_t count,const char *fmt,...) |
Damien Miller | c6b3bbe | 1999-12-13 08:27:33 +1100 | [diff] [blame] | 643 | { |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 644 | va_list ap; |
| 645 | |
| 646 | va_start(ap, fmt); |
| 647 | (void) vsnprintf(str, count, fmt, ap); |
| 648 | va_end(ap); |
| 649 | |
| 650 | return(strlen(str)); |
Damien Miller | c6b3bbe | 1999-12-13 08:27:33 +1100 | [diff] [blame] | 651 | } |
Damien Miller | c6b3bbe | 1999-12-13 08:27:33 +1100 | [diff] [blame] | 652 | |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 653 | #ifdef TEST_SNPRINTF |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 654 | int |
| 655 | main(void) |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 656 | { |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 657 | #define LONG_STRING 1024 |
| 658 | char buf1[LONG_STRING]; |
| 659 | char buf2[LONG_STRING]; |
| 660 | char *fp_fmt[] = { |
| 661 | "%-1.5f", |
| 662 | "%1.5f", |
| 663 | "%123.9f", |
| 664 | "%10.5f", |
| 665 | "% 10.5f", |
| 666 | "%+22.9f", |
| 667 | "%+4.9f", |
| 668 | "%01.3f", |
| 669 | "%4f", |
| 670 | "%3.1f", |
| 671 | "%3.2f", |
| 672 | NULL |
| 673 | }; |
| 674 | double fp_nums[] = { |
| 675 | -1.5, |
| 676 | 134.21, |
| 677 | 91340.2, |
| 678 | 341.1234, |
| 679 | 0203.9, |
| 680 | 0.96, |
| 681 | 0.996, |
| 682 | 0.9996, |
| 683 | 1.996, |
| 684 | 4.136, |
| 685 | 0 |
| 686 | }; |
| 687 | char *int_fmt[] = { |
| 688 | "%-1.5d", |
| 689 | "%1.5d", |
| 690 | "%123.9d", |
| 691 | "%5.5d", |
| 692 | "%10.5d", |
| 693 | "% 10.5d", |
| 694 | "%+22.33d", |
| 695 | "%01.3d", |
| 696 | "%4d", |
| 697 | "%lld", |
| 698 | "%qd", |
| 699 | NULL |
| 700 | }; |
| 701 | long long int_nums[] = { -1, 134, 91340, 341, 0203, 0, 9999999 }; |
| 702 | int x, y; |
| 703 | int fail = 0; |
| 704 | int num = 0; |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 705 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 706 | printf("Testing snprintf format codes against system sprintf...\n"); |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 707 | |
Ben Lindstrom | 6c92dab | 2001-02-13 02:18:50 +0000 | [diff] [blame] | 708 | for (x = 0; fp_fmt[x] != NULL ; x++) { |
| 709 | for (y = 0; fp_nums[y] != 0 ; y++) { |
| 710 | snprintf(buf1, sizeof (buf1), fp_fmt[x], fp_nums[y]); |
| 711 | sprintf (buf2, fp_fmt[x], fp_nums[y]); |
| 712 | if (strcmp (buf1, buf2)) { |
| 713 | printf("snprintf doesn't match Format: %s\n\t" |
| 714 | "snprintf = %s\n\tsprintf = %s\n", |
| 715 | fp_fmt[x], buf1, buf2); |
| 716 | fail++; |
| 717 | } |
| 718 | num++; |
| 719 | } |
| 720 | } |
| 721 | for (x = 0; int_fmt[x] != NULL ; x++) { |
| 722 | for (y = 0; int_nums[y] != 0 ; y++) { |
| 723 | snprintf(buf1, sizeof (buf1), int_fmt[x], int_nums[y]); |
| 724 | sprintf(buf2, int_fmt[x], int_nums[y]); |
| 725 | if (strcmp (buf1, buf2)) { |
| 726 | printf("snprintf doesn't match Format: %s\n\t" |
| 727 | "snprintf = %s\n\tsprintf = %s\n", |
| 728 | int_fmt[x], buf1, buf2); |
| 729 | fail++; |
| 730 | } |
| 731 | num++; |
| 732 | } |
| 733 | } |
| 734 | printf("%d tests failed out of %d.\n", fail, num); |
| 735 | return(0); |
Damien Miller | 168e6ac | 2000-07-11 12:23:01 +1000 | [diff] [blame] | 736 | } |
| 737 | #endif /* SNPRINTF_TEST */ |
| 738 | |
| 739 | #endif /* !HAVE_SNPRINTF */ |