snprintf.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  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...
  7. *
  8. * snprintf() is used instead of sprintf() as it does limit checks
  9. * for string length. This covers a nasty loophole.
  10. *
  11. * The other functions are there to prevent NULL pointers from
  12. * causing nast effects.
  13. *
  14. * More Recently:
  15. * Brandon Long <[email protected]> 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 <[email protected]> 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 <[email protected]> 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 <[email protected]> 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. *
  41. **************************************************************/
  42. #include <config.h>
  43. #if !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF)
  44. #include <string.h>
  45. # include <ctype.h>
  46. #include <sys/types.h>
  47. /* Define this as a fall through, HAVE_STDARG_H is probably already set */
  48. #define HAVE_VARARGS_H
  49. /* varargs declarations: */
  50. #if defined(HAVE_STDARG_H)
  51. # include <stdarg.h>
  52. # define HAVE_STDARGS /* let's hope that works everywhere (mj) */
  53. # define VA_LOCAL_DECL va_list ap
  54. # define VA_START(f) va_start(ap, f)
  55. # define VA_SHIFT(v,t) ; /* no-op for ANSI */
  56. # define VA_END va_end(ap)
  57. #else
  58. # if defined(HAVE_VARARGS_H)
  59. # include <varargs.h>
  60. # undef HAVE_STDARGS
  61. # define VA_LOCAL_DECL va_list ap
  62. # define VA_START(f) va_start(ap) /* f is ignored! */
  63. # define VA_SHIFT(v,t) v = va_arg(ap,t)
  64. # define VA_END va_end(ap)
  65. # else
  66. /*XX ** NO VARARGS ** XX*/
  67. # endif
  68. #endif
  69. /*int snprintf (char *str, size_t count, const char *fmt, ...);*/
  70. /*int vsnprintf (char *str, size_t count, const char *fmt, va_list arg);*/
  71. static void dopr (char *buffer, size_t maxlen, const char *format,
  72. va_list args);
  73. static void fmtstr (char *buffer, size_t *currlen, size_t maxlen,
  74. char *value, int flags, int min, int max);
  75. static void fmtint (char *buffer, size_t *currlen, size_t maxlen,
  76. long value, int base, int min, int max, int flags);
  77. static void fmtfp (char *buffer, size_t *currlen, size_t maxlen,
  78. long double fvalue, int min, int max, int flags);
  79. static void dopr_outch (char *buffer, size_t *currlen, size_t maxlen, char c );
  80. /*
  81. * dopr(): poor man's version of doprintf
  82. */
  83. /* format read states */
  84. #define DP_S_DEFAULT 0
  85. #define DP_S_FLAGS 1
  86. #define DP_S_MIN 2
  87. #define DP_S_DOT 3
  88. #define DP_S_MAX 4
  89. #define DP_S_MOD 5
  90. #define DP_S_CONV 6
  91. #define DP_S_DONE 7
  92. /* format flags - Bits */
  93. #define DP_F_MINUS (1 << 0)
  94. #define DP_F_PLUS (1 << 1)
  95. #define DP_F_SPACE (1 << 2)
  96. #define DP_F_NUM (1 << 3)
  97. #define DP_F_ZERO (1 << 4)
  98. #define DP_F_UP (1 << 5)
  99. #define DP_F_UNSIGNED (1 << 6)
  100. /* Conversion Flags */
  101. #define DP_C_SHORT 1
  102. #define DP_C_LONG 2
  103. #define DP_C_LDOUBLE 3
  104. #define char_to_int(p) (p - '0')
  105. #define MAX(p,q) ((p >= q) ? p : q)
  106. static void dopr (char *buffer, size_t maxlen, const char *format, va_list args)
  107. {
  108. char ch;
  109. long value;
  110. long double fvalue;
  111. char *strvalue;
  112. int min;
  113. int max;
  114. int state;
  115. int flags;
  116. int cflags;
  117. size_t currlen;
  118. state = DP_S_DEFAULT;
  119. currlen = flags = cflags = min = 0;
  120. max = -1;
  121. ch = *format++;
  122. while (state != DP_S_DONE)
  123. {
  124. if ((ch == '\0') || (currlen >= maxlen))
  125. state = DP_S_DONE;
  126. switch(state)
  127. {
  128. case DP_S_DEFAULT:
  129. if (ch == '%')
  130. state = DP_S_FLAGS;
  131. else
  132. dopr_outch (buffer, &currlen, maxlen, ch);
  133. ch = *format++;
  134. break;
  135. case DP_S_FLAGS:
  136. switch (ch)
  137. {
  138. case '-':
  139. flags |= DP_F_MINUS;
  140. ch = *format++;
  141. break;
  142. case '+':
  143. flags |= DP_F_PLUS;
  144. ch = *format++;
  145. break;
  146. case ' ':
  147. flags |= DP_F_SPACE;
  148. ch = *format++;
  149. break;
  150. case '#':
  151. flags |= DP_F_NUM;
  152. ch = *format++;
  153. break;
  154. case '0':
  155. flags |= DP_F_ZERO;
  156. ch = *format++;
  157. break;
  158. default:
  159. state = DP_S_MIN;
  160. break;
  161. }
  162. break;
  163. case DP_S_MIN:
  164. if (isdigit((unsigned char)ch))
  165. {
  166. min = 10*min + char_to_int (ch);
  167. ch = *format++;
  168. }
  169. else if (ch == '*')
  170. {
  171. min = va_arg (args, int);
  172. ch = *format++;
  173. state = DP_S_DOT;
  174. }
  175. else
  176. state = DP_S_DOT;
  177. break;
  178. case DP_S_DOT:
  179. if (ch == '.')
  180. {
  181. state = DP_S_MAX;
  182. ch = *format++;
  183. }
  184. else
  185. state = DP_S_MOD;
  186. break;
  187. case DP_S_MAX:
  188. if (isdigit((unsigned char)ch))
  189. {
  190. if (max < 0)
  191. max = 0;
  192. max = 10*max + char_to_int (ch);
  193. ch = *format++;
  194. }
  195. else if (ch == '*')
  196. {
  197. max = va_arg (args, int);
  198. ch = *format++;
  199. state = DP_S_MOD;
  200. }
  201. else
  202. state = DP_S_MOD;
  203. break;
  204. case DP_S_MOD:
  205. /* Currently, we don't support Long Long, bummer */
  206. switch (ch)
  207. {
  208. case 'h':
  209. cflags = DP_C_SHORT;
  210. ch = *format++;
  211. break;
  212. case 'l':
  213. cflags = DP_C_LONG;
  214. ch = *format++;
  215. break;
  216. case 'L':
  217. cflags = DP_C_LDOUBLE;
  218. ch = *format++;
  219. break;
  220. default:
  221. break;
  222. }
  223. state = DP_S_CONV;
  224. break;
  225. case DP_S_CONV:
  226. switch (ch)
  227. {
  228. case 'd':
  229. case 'i':
  230. if (cflags == DP_C_SHORT)
  231. value = va_arg (args, short int);
  232. else if (cflags == DP_C_LONG)
  233. value = va_arg (args, long int);
  234. else
  235. value = va_arg (args, int);
  236. fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
  237. break;
  238. case 'o':
  239. flags |= DP_F_UNSIGNED;
  240. if (cflags == DP_C_SHORT)
  241. value = va_arg (args, unsigned short int);
  242. else if (cflags == DP_C_LONG)
  243. value = va_arg (args, unsigned long int);
  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 short int);
  252. else if (cflags == DP_C_LONG)
  253. value = va_arg (args, unsigned long int);
  254. else
  255. value = va_arg (args, unsigned int);
  256. fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
  257. break;
  258. case 'X':
  259. flags |= DP_F_UP;
  260. case 'x':
  261. flags |= DP_F_UNSIGNED;
  262. if (cflags == DP_C_SHORT)
  263. value = va_arg (args, unsigned short int);
  264. else if (cflags == DP_C_LONG)
  265. value = va_arg (args, unsigned long int);
  266. else
  267. value = va_arg (args, unsigned int);
  268. fmtint (buffer, &currlen, maxlen, value, 16, min, max, flags);
  269. break;
  270. case 'f':
  271. if (cflags == DP_C_LDOUBLE)
  272. fvalue = va_arg (args, long double);
  273. else
  274. fvalue = va_arg (args, double);
  275. /* um, floating point? */
  276. fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags);
  277. break;
  278. case 'E':
  279. flags |= DP_F_UP;
  280. case 'e':
  281. if (cflags == DP_C_LDOUBLE)
  282. fvalue = va_arg (args, long double);
  283. else
  284. fvalue = va_arg (args, double);
  285. break;
  286. case 'G':
  287. flags |= DP_F_UP;
  288. case 'g':
  289. if (cflags == DP_C_LDOUBLE)
  290. fvalue = va_arg (args, long double);
  291. else
  292. fvalue = va_arg (args, double);
  293. break;
  294. case 'c':
  295. dopr_outch (buffer, &currlen, maxlen, va_arg (args, int));
  296. break;
  297. case 's':
  298. strvalue = va_arg (args, char *);
  299. if (max < 0)
  300. max = maxlen; /* ie, no max */
  301. fmtstr (buffer, &currlen, maxlen, strvalue, flags, min, max);
  302. break;
  303. case 'p':
  304. strvalue = va_arg (args, void *);
  305. fmtint (buffer, &currlen, maxlen, (long) strvalue, 16, min, max, flags);
  306. break;
  307. case 'n':
  308. if (cflags == DP_C_SHORT)
  309. {
  310. short int *num;
  311. num = va_arg (args, short int *);
  312. *num = currlen;
  313. }
  314. else if (cflags == DP_C_LONG)
  315. {
  316. long int *num;
  317. num = va_arg (args, long int *);
  318. *num = currlen;
  319. }
  320. else
  321. {
  322. int *num;
  323. num = va_arg (args, int *);
  324. *num = currlen;
  325. }
  326. break;
  327. case '%':
  328. dopr_outch (buffer, &currlen, maxlen, ch);
  329. break;
  330. case 'w':
  331. /* not supported yet, treat as next char */
  332. ch = *format++;
  333. break;
  334. default:
  335. /* Unknown, skip */
  336. break;
  337. }
  338. ch = *format++;
  339. state = DP_S_DEFAULT;
  340. flags = cflags = min = 0;
  341. max = -1;
  342. break;
  343. case DP_S_DONE:
  344. break;
  345. default:
  346. /* hmm? */
  347. break; /* some picky compilers need this */
  348. }
  349. }
  350. if (currlen < maxlen - 1)
  351. buffer[currlen] = '\0';
  352. else
  353. buffer[maxlen - 1] = '\0';
  354. }
  355. static void fmtstr (char *buffer, size_t *currlen, size_t maxlen,
  356. char *value, int flags, int min, int max)
  357. {
  358. int padlen, strln; /* amount to pad */
  359. int cnt = 0;
  360. if (value == 0)
  361. {
  362. value = "<NULL>";
  363. }
  364. for (strln = 0; value[strln]; ++strln); /* strlen */
  365. padlen = min - strln;
  366. if (padlen < 0)
  367. padlen = 0;
  368. if (flags & DP_F_MINUS)
  369. padlen = -padlen; /* Left Justify */
  370. while ((padlen > 0) && (cnt < max))
  371. {
  372. dopr_outch (buffer, currlen, maxlen, ' ');
  373. --padlen;
  374. ++cnt;
  375. }
  376. while (*value && (cnt < max))
  377. {
  378. dopr_outch (buffer, currlen, maxlen, *value++);
  379. ++cnt;
  380. }
  381. while ((padlen < 0) && (cnt < max))
  382. {
  383. dopr_outch (buffer, currlen, maxlen, ' ');
  384. ++padlen;
  385. ++cnt;
  386. }
  387. }
  388. /* Have to handle DP_F_NUM (ie 0x and 0 alternates) */
  389. static void fmtint (char *buffer, size_t *currlen, size_t maxlen,
  390. long value, int base, int min, int max, int flags)
  391. {
  392. int signvalue = 0;
  393. unsigned long uvalue;
  394. char convert[20];
  395. int place = 0;
  396. int spadlen = 0; /* amount to space pad */
  397. int zpadlen = 0; /* amount to zero pad */
  398. int caps = 0;
  399. if (max < 0)
  400. max = 0;
  401. uvalue = value;
  402. if(!(flags & DP_F_UNSIGNED))
  403. {
  404. if( value < 0 ) {
  405. signvalue = '-';
  406. uvalue = -value;
  407. }
  408. else
  409. if (flags & DP_F_PLUS) /* Do a sign (+/i) */
  410. signvalue = '+';
  411. else
  412. if (flags & DP_F_SPACE)
  413. signvalue = ' ';
  414. }
  415. if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
  416. do {
  417. convert[place++] =
  418. (caps? "0123456789ABCDEF":"0123456789abcdef")
  419. [uvalue % (unsigned)base ];
  420. uvalue = (uvalue / (unsigned)base );
  421. } while(uvalue && (place < 20));
  422. if (place == 20) place--;
  423. convert[place] = 0;
  424. zpadlen = max - place;
  425. spadlen = min - MAX (max, place) - (signvalue ? 1 : 0);
  426. if (zpadlen < 0) zpadlen = 0;
  427. if (spadlen < 0) spadlen = 0;
  428. if (flags & DP_F_ZERO)
  429. {
  430. zpadlen = MAX(zpadlen, spadlen);
  431. spadlen = 0;
  432. }
  433. if (flags & DP_F_MINUS)
  434. spadlen = -spadlen; /* Left Justifty */
  435. #ifdef DEBUG_SNPRINTF
  436. dprint (1, (debugfile, "zpad: %d, spad: %d, min: %d, max: %d, place: %d\n",
  437. zpadlen, spadlen, min, max, place));
  438. #endif
  439. /* Spaces */
  440. while (spadlen > 0)
  441. {
  442. dopr_outch (buffer, currlen, maxlen, ' ');
  443. --spadlen;
  444. }
  445. /* Sign */
  446. if (signvalue)
  447. dopr_outch (buffer, currlen, maxlen, signvalue);
  448. /* Zeros */
  449. if (zpadlen > 0)
  450. {
  451. while (zpadlen > 0)
  452. {
  453. dopr_outch (buffer, currlen, maxlen, '0');
  454. --zpadlen;
  455. }
  456. }
  457. /* Digits */
  458. while (place > 0)
  459. dopr_outch (buffer, currlen, maxlen, convert[--place]);
  460. /* Left Justified spaces */
  461. while (spadlen < 0) {
  462. dopr_outch (buffer, currlen, maxlen, ' ');
  463. ++spadlen;
  464. }
  465. }
  466. static long double abs_val (long double value)
  467. {
  468. long double result = value;
  469. if (value < 0)
  470. result = -value;
  471. return result;
  472. }
  473. static long double pow10 (int exp)
  474. {
  475. long double result = 1;
  476. while (exp)
  477. {
  478. result *= 10;
  479. exp--;
  480. }
  481. return result;
  482. }
  483. static long round (long double value)
  484. {
  485. long intpart;
  486. intpart = value;
  487. value = value - intpart;
  488. if (value >= 0.5)
  489. intpart++;
  490. return intpart;
  491. }
  492. static void fmtfp (char *buffer, size_t *currlen, size_t maxlen,
  493. long double fvalue, int min, int max, int flags)
  494. {
  495. int signvalue = 0;
  496. long double ufvalue;
  497. char iconvert[20];
  498. char fconvert[20];
  499. int iplace = 0;
  500. int fplace = 0;
  501. int padlen = 0; /* amount to pad */
  502. int zpadlen = 0;
  503. int caps = 0;
  504. long intpart;
  505. long fracpart;
  506. /*
  507. * AIX manpage says the default is 0, but Solaris says the default
  508. * is 6, and sprintf on AIX defaults to 6
  509. */
  510. if (max < 0)
  511. max = 6;
  512. ufvalue = abs_val (fvalue);
  513. if (fvalue < 0)
  514. signvalue = '-';
  515. else
  516. if (flags & DP_F_PLUS) /* Do a sign (+/i) */
  517. signvalue = '+';
  518. else
  519. if (flags & DP_F_SPACE)
  520. signvalue = ' ';
  521. #if 0
  522. if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
  523. #endif
  524. intpart = ufvalue;
  525. /*
  526. * Sorry, we only support 9 digits past the decimal because of our
  527. * conversion method
  528. */
  529. if (max > 9)
  530. max = 9;
  531. /* We "cheat" by converting the fractional part to integer by
  532. * multiplying by a factor of 10
  533. */
  534. fracpart = round ((pow10 (max)) * (ufvalue - intpart));
  535. if (fracpart >= pow10 (max))
  536. {
  537. intpart++;
  538. fracpart -= pow10 (max);
  539. }
  540. #ifdef DEBUG_SNPRINTF
  541. dprint (1, (debugfile, "fmtfp: %f =? %d.%d\n", fvalue, intpart, fracpart));
  542. #endif
  543. /* Convert integer part */
  544. do {
  545. iconvert[iplace++] =
  546. (caps? "0123456789ABCDEF":"0123456789abcdef")[intpart % 10];
  547. intpart = (intpart / 10);
  548. } while(intpart && (iplace < 20));
  549. if (iplace == 20) iplace--;
  550. iconvert[iplace] = 0;
  551. /* Convert fractional part */
  552. do {
  553. fconvert[fplace++] =
  554. (caps? "0123456789ABCDEF":"0123456789abcdef")[fracpart % 10];
  555. fracpart = (fracpart / 10);
  556. } while(fracpart && (fplace < 20));
  557. if (fplace == 20) fplace--;
  558. fconvert[fplace] = 0;
  559. /* -1 for decimal point, another -1 if we are printing a sign */
  560. padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
  561. zpadlen = max - fplace;
  562. if (zpadlen < 0)
  563. zpadlen = 0;
  564. if (padlen < 0)
  565. padlen = 0;
  566. if (flags & DP_F_MINUS)
  567. padlen = -padlen; /* Left Justifty */
  568. if ((flags & DP_F_ZERO) && (padlen > 0))
  569. {
  570. if (signvalue)
  571. {
  572. dopr_outch (buffer, currlen, maxlen, signvalue);
  573. --padlen;
  574. signvalue = 0;
  575. }
  576. while (padlen > 0)
  577. {
  578. dopr_outch (buffer, currlen, maxlen, '0');
  579. --padlen;
  580. }
  581. }
  582. while (padlen > 0)
  583. {
  584. dopr_outch (buffer, currlen, maxlen, ' ');
  585. --padlen;
  586. }
  587. if (signvalue)
  588. dopr_outch (buffer, currlen, maxlen, signvalue);
  589. while (iplace > 0)
  590. dopr_outch (buffer, currlen, maxlen, iconvert[--iplace]);
  591. /*
  592. * Decimal point. This should probably use locale to find the correct
  593. * char to print out.
  594. */
  595. dopr_outch (buffer, currlen, maxlen, '.');
  596. while (fplace > 0)
  597. dopr_outch (buffer, currlen, maxlen, fconvert[--fplace]);
  598. while (zpadlen > 0)
  599. {
  600. dopr_outch (buffer, currlen, maxlen, '0');
  601. --zpadlen;
  602. }
  603. while (padlen < 0)
  604. {
  605. dopr_outch (buffer, currlen, maxlen, ' ');
  606. ++padlen;
  607. }
  608. }
  609. static void dopr_outch (char *buffer, size_t *currlen, size_t maxlen, char c)
  610. {
  611. if (*currlen < maxlen)
  612. buffer[(*currlen)++] = c;
  613. }
  614. #endif /* !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF) */
  615. #ifndef HAVE_VSNPRINTF
  616. int mutt_vsnprintf (char *str, size_t count, const char *fmt, va_list args)
  617. {
  618. str[0] = 0;
  619. dopr(str, count, fmt, args);
  620. return(strlen(str));
  621. }
  622. #endif /* !HAVE_VSNPRINTF */
  623. #ifndef HAVE_SNPRINTF
  624. /* VARARGS3 */
  625. #ifdef HAVE_STDARGS
  626. int mutt_snprintf (char *str,size_t count,const char *fmt,...)
  627. #else
  628. int mutt_snprintf (va_alist) va_dcl
  629. #endif
  630. {
  631. #ifndef HAVE_STDARGS
  632. char *str;
  633. size_t count;
  634. char *fmt;
  635. #endif
  636. VA_LOCAL_DECL;
  637. VA_START (fmt);
  638. VA_SHIFT (str, char *);
  639. VA_SHIFT (count, size_t );
  640. VA_SHIFT (fmt, char *);
  641. (void) mutt_vsnprintf(str, count, fmt, ap);
  642. VA_END;
  643. return(strlen(str));
  644. }
  645. #ifdef TEST_SNPRINTF
  646. #ifndef LONG_STRING
  647. #define LONG_STRING 1024
  648. #endif
  649. int main (void)
  650. {
  651. char buf1[LONG_STRING];
  652. char buf2[LONG_STRING];
  653. char *fp_fmt[] = {
  654. "%-1.5f",
  655. "%1.5f",
  656. "%123.9f",
  657. "%10.5f",
  658. "% 10.5f",
  659. "%+22.9f",
  660. "%+4.9f",
  661. "%01.3f",
  662. "%4f",
  663. "%3.1f",
  664. "%3.2f",
  665. NULL
  666. };
  667. double fp_nums[] = { -1.5, 134.21, 91340.2, 341.1234, 0203.9, 0.96, 0.996,
  668. 0.9996, 1.996, 4.136, 0};
  669. char *int_fmt[] = {
  670. "%-1.5d",
  671. "%1.5d",
  672. "%123.9d",
  673. "%5.5d",
  674. "%10.5d",
  675. "% 10.5d",
  676. "%+22.33d",
  677. "%01.3d",
  678. "%4d",
  679. NULL
  680. };
  681. long int_nums[] = { -1, 134, 91340, 341, 0203, 0};
  682. int x, y;
  683. int fail = 0;
  684. int num = 0;
  685. printf ("Testing snprintf format codes against system sprintf...\n");
  686. for (x = 0; fp_fmt[x] != NULL ; x++)
  687. for (y = 0; fp_nums[y] != 0 ; y++)
  688. {
  689. mutt_snprintf (buf1, sizeof (buf1), fp_fmt[x], fp_nums[y]);
  690. sprintf (buf2, fp_fmt[x], fp_nums[y]);
  691. if (strcmp (buf1, buf2))
  692. {
  693. printf("snprintf doesn't match Format: %s\n\tsnprintf = %s\n\tsprintf = %s\n",
  694. fp_fmt[x], buf1, buf2);
  695. fail++;
  696. }
  697. num++;
  698. }
  699. for (x = 0; int_fmt[x] != NULL ; x++)
  700. for (y = 0; int_nums[y] != 0 ; y++)
  701. {
  702. mutt_snprintf (buf1, sizeof (buf1), int_fmt[x], int_nums[y]);
  703. sprintf (buf2, int_fmt[x], int_nums[y]);
  704. if (strcmp (buf1, buf2))
  705. {
  706. printf("snprintf doesn't match Format: %s\n\tsnprintf = %s\n\tsprintf = %s\n",
  707. int_fmt[x], buf1, buf2);
  708. fail++;
  709. }
  710. num++;
  711. }
  712. printf ("%d tests failed out of %d.\n", fail, num);
  713. }
  714. #endif /* SNPRINTF_TEST */
  715. #endif /* !HAVE_SNPRINTF */