4
0

snprintf.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  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. /**************************************************************
  8. * Original:
  9. * Patrick Powell Tue Apr 11 09:48:21 PDT 1995
  10. * A bombproof version of doprnt (dopr) included.
  11. * Sigh. This sort of thing is always nasty do deal with. Note that
  12. * the version here does not include floating point...
  13. *
  14. * snprintf() is used instead of sprintf() as it does limit checks
  15. * for string length. This covers a nasty loophole.
  16. *
  17. * The other functions are there to prevent NULL pointers from
  18. * causing nast effects.
  19. *
  20. * More Recently:
  21. * Brandon Long <blong@fiction.net> 9/15/96 for mutt 0.43
  22. * This was ugly. It is still ugly. I opted out of floating point
  23. * numbers, but the formatter understands just about everything
  24. * from the normal C string format, at least as far as I can tell from
  25. * the Solaris 2.5 printf(3S) man page.
  26. *
  27. * Brandon Long <blong@fiction.net> 10/22/97 for mutt 0.87.1
  28. * Ok, added some minimal floating point support, which means this
  29. * probably requires libm on most operating systems. Don't yet
  30. * support the exponent (e,E) and sigfig (g,G). Also, fmtint()
  31. * was pretty badly broken, it just wasn't being exercised in ways
  32. * which showed it, so that's been fixed. Also, formated the code
  33. * to mutt conventions, and removed dead code left over from the
  34. * original. Also, there is now a builtin-test, just compile with:
  35. * gcc -DTEST_SNPRINTF -o snprintf snprintf.c -lm
  36. * and run snprintf for results.
  37. *
  38. * Thomas Roessler <roessler@guug.de> 01/27/98 for mutt 0.89i
  39. * The PGP code was using unsigned hexadecimal formats.
  40. * Unfortunately, unsigned formats simply didn't work.
  41. *
  42. * Michael Elkins <me@cs.hmc.edu> 03/05/98 for mutt 0.90.8
  43. * The original code assumed that both snprintf() and vsnprintf() were
  44. * missing. Some systems only have snprintf() but not vsnprintf(), so
  45. * the code is now broken down under HAVE_SNPRINTF and HAVE_VSNPRINTF.
  46. *
  47. * Andrew Tridgell (tridge@samba.org) Oct 1998
  48. * fixed handling of %.0f
  49. * added test for HAVE_LONG_DOUBLE
  50. *
  51. * tridge@samba.org, idra@samba.org, April 2001
  52. * got rid of fcvt code (twas buggy and made testing harder)
  53. * added C99 semantics
  54. *
  55. **************************************************************/
  56. #ifndef NO_CONFIG_H /* for some tests */
  57. #include "config.h"
  58. #else
  59. #define NULL 0
  60. #endif
  61. #ifdef TEST_SNPRINTF /* need math library headers for testing */
  62. #include <math.h>
  63. #endif
  64. #ifdef HAVE_STRING_H
  65. #include <string.h>
  66. #endif
  67. #ifdef HAVE_STRINGS_H
  68. #include <strings.h>
  69. #endif
  70. #ifdef HAVE_CTYPE_H
  71. #include <ctype.h>
  72. #endif
  73. #ifdef HAVE_SYS_TYPES_H
  74. #include <sys/types.h>
  75. #endif
  76. #ifdef HAVE_STDARG_H
  77. #include <stdarg.h>
  78. #endif
  79. #ifdef HAVE_STDLIB_H
  80. #include <stdlib.h>
  81. #endif
  82. #if defined(HAVE_SNPRINTF) && defined(HAVE_VSNPRINTF) && defined(HAVE_C99_VSNPRINTF)
  83. /* only include stdio.h if we are not re-defining snprintf or vsnprintf */
  84. #include <stdio.h>
  85. /* make the compiler happy with an empty file */
  86. void dummy_snprintf(void) {}
  87. #else
  88. #ifdef HAVE_LONG_DOUBLE
  89. #define LDOUBLE long double
  90. #else
  91. #define LDOUBLE double
  92. #endif
  93. #ifdef HAVE_LONG_LONG
  94. #define LLONG long long
  95. #else
  96. #define LLONG long
  97. #endif
  98. /* free memory if the pointer is valid and zero the pointer */
  99. #ifndef SAFE_FREE
  100. #define SAFE_FREE(x) do { if ((x) != NULL) {free((x)); (x)=NULL;} } while(0)
  101. #endif
  102. #ifndef VA_COPY
  103. #ifdef HAVE_VA_COPY
  104. #define VA_COPY(dest, src) __va_copy(dest, src)
  105. #else
  106. #define VA_COPY(dest, src) (dest) = (src)
  107. #endif
  108. #endif
  109. static size_t dopr(char *buffer, size_t maxlen, const char *format,
  110. va_list args_in);
  111. static void fmtstr(char *buffer, size_t *currlen, size_t maxlen,
  112. char *value, int flags, int min, int max);
  113. static void fmtint(char *buffer, size_t *currlen, size_t maxlen,
  114. long value, int base, int min, int max, int flags);
  115. static void fmtfp(char *buffer, size_t *currlen, size_t maxlen,
  116. LDOUBLE fvalue, int min, int max, int flags);
  117. static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c);
  118. /*
  119. * dopr(): poor man's version of doprintf
  120. */
  121. /* format read states */
  122. #define DP_S_DEFAULT 0
  123. #define DP_S_FLAGS 1
  124. #define DP_S_MIN 2
  125. #define DP_S_DOT 3
  126. #define DP_S_MAX 4
  127. #define DP_S_MOD 5
  128. #define DP_S_CONV 6
  129. #define DP_S_DONE 7
  130. /* format flags - Bits */
  131. #define DP_F_MINUS (1 << 0)
  132. #define DP_F_PLUS (1 << 1)
  133. #define DP_F_SPACE (1 << 2)
  134. #define DP_F_NUM (1 << 3)
  135. #define DP_F_ZERO (1 << 4)
  136. #define DP_F_UP (1 << 5)
  137. #define DP_F_UNSIGNED (1 << 6)
  138. /* Conversion Flags */
  139. #define DP_C_SHORT 1
  140. #define DP_C_LONG 2
  141. #define DP_C_LDOUBLE 3
  142. #define DP_C_LLONG 4
  143. #define char_to_int(p) ((p)- '0')
  144. #ifndef MAX
  145. #define MAX(p,q) (((p) >= (q)) ? (p) : (q))
  146. #endif
  147. static size_t dopr(char *buffer, size_t maxlen, const char *format, va_list args_in)
  148. {
  149. char ch;
  150. LLONG value;
  151. LDOUBLE fvalue;
  152. char *strvalue;
  153. int min;
  154. int max;
  155. int state;
  156. int flags;
  157. int cflags;
  158. size_t currlen;
  159. va_list args;
  160. VA_COPY(args, args_in);
  161. state = DP_S_DEFAULT;
  162. currlen = flags = cflags = min = 0;
  163. max = -1;
  164. ch = *format++;
  165. while (state != DP_S_DONE) {
  166. if (ch == '\0')
  167. state = DP_S_DONE;
  168. switch(state) {
  169. case DP_S_DEFAULT:
  170. if (ch == '%')
  171. state = DP_S_FLAGS;
  172. else
  173. dopr_outch (buffer, &currlen, maxlen, ch);
  174. ch = *format++;
  175. break;
  176. case DP_S_FLAGS:
  177. switch (ch) {
  178. case '-':
  179. flags |= DP_F_MINUS;
  180. ch = *format++;
  181. break;
  182. case '+':
  183. flags |= DP_F_PLUS;
  184. ch = *format++;
  185. break;
  186. case ' ':
  187. flags |= DP_F_SPACE;
  188. ch = *format++;
  189. break;
  190. case '#':
  191. flags |= DP_F_NUM;
  192. ch = *format++;
  193. break;
  194. case '0':
  195. flags |= DP_F_ZERO;
  196. ch = *format++;
  197. break;
  198. default:
  199. state = DP_S_MIN;
  200. break;
  201. }
  202. break;
  203. case DP_S_MIN:
  204. if (isdigit((unsigned char)ch)) {
  205. min = 10*min + char_to_int (ch);
  206. ch = *format++;
  207. } else if (ch == '*') {
  208. min = va_arg (args, int);
  209. ch = *format++;
  210. state = DP_S_DOT;
  211. } else {
  212. state = DP_S_DOT;
  213. }
  214. break;
  215. case DP_S_DOT:
  216. if (ch == '.') {
  217. state = DP_S_MAX;
  218. ch = *format++;
  219. } else {
  220. state = DP_S_MOD;
  221. }
  222. break;
  223. case DP_S_MAX:
  224. if (isdigit((unsigned char)ch)) {
  225. if (max < 0)
  226. max = 0;
  227. max = 10*max + char_to_int (ch);
  228. ch = *format++;
  229. } else if (ch == '*') {
  230. max = va_arg (args, int);
  231. ch = *format++;
  232. state = DP_S_MOD;
  233. } else {
  234. state = DP_S_MOD;
  235. }
  236. break;
  237. case DP_S_MOD:
  238. switch (ch) {
  239. case 'h':
  240. cflags = DP_C_SHORT;
  241. ch = *format++;
  242. break;
  243. case 'l':
  244. cflags = DP_C_LONG;
  245. ch = *format++;
  246. if (ch == 'l') { /* It's a long long */
  247. cflags = DP_C_LLONG;
  248. ch = *format++;
  249. }
  250. break;
  251. case 'L':
  252. cflags = DP_C_LDOUBLE;
  253. ch = *format++;
  254. break;
  255. default:
  256. break;
  257. }
  258. state = DP_S_CONV;
  259. break;
  260. case DP_S_CONV:
  261. switch (ch) {
  262. case 'd':
  263. case 'i':
  264. if (cflags == DP_C_SHORT)
  265. value = va_arg (args, int);
  266. else if (cflags == DP_C_LONG)
  267. value = va_arg (args, long int);
  268. else if (cflags == DP_C_LLONG)
  269. value = va_arg (args, LLONG);
  270. else
  271. value = va_arg (args, int);
  272. fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
  273. break;
  274. case 'o':
  275. flags |= DP_F_UNSIGNED;
  276. if (cflags == DP_C_SHORT)
  277. value = va_arg (args, unsigned int);
  278. else if (cflags == DP_C_LONG)
  279. value = (long)va_arg (args, unsigned long int);
  280. else if (cflags == DP_C_LLONG)
  281. value = (long)va_arg (args, unsigned LLONG);
  282. else
  283. value = (long)va_arg (args, unsigned int);
  284. fmtint (buffer, &currlen, maxlen, value, 8, min, max, flags);
  285. break;
  286. case 'u':
  287. flags |= DP_F_UNSIGNED;
  288. if (cflags == DP_C_SHORT)
  289. value = va_arg (args, unsigned int);
  290. else if (cflags == DP_C_LONG)
  291. value = (long)va_arg (args, unsigned long int);
  292. else if (cflags == DP_C_LLONG)
  293. value = (LLONG)va_arg (args, unsigned LLONG);
  294. else
  295. value = (long)va_arg (args, unsigned int);
  296. fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
  297. break;
  298. case 'X':
  299. flags |= DP_F_UP;
  300. case 'x':
  301. flags |= DP_F_UNSIGNED;
  302. if (cflags == DP_C_SHORT)
  303. value = va_arg (args, unsigned int);
  304. else if (cflags == DP_C_LONG)
  305. value = (long)va_arg (args, unsigned long int);
  306. else if (cflags == DP_C_LLONG)
  307. value = (LLONG)va_arg (args, unsigned LLONG);
  308. else
  309. value = (long)va_arg (args, unsigned int);
  310. fmtint (buffer, &currlen, maxlen, value, 16, min, max, flags);
  311. break;
  312. case 'f':
  313. if (cflags == DP_C_LDOUBLE)
  314. fvalue = va_arg (args, LDOUBLE);
  315. else
  316. fvalue = va_arg (args, double);
  317. /* um, floating point? */
  318. fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags);
  319. break;
  320. case 'E':
  321. flags |= DP_F_UP;
  322. case 'e':
  323. if (cflags == DP_C_LDOUBLE)
  324. fvalue = va_arg (args, LDOUBLE);
  325. else
  326. fvalue = va_arg (args, double);
  327. break;
  328. case 'G':
  329. flags |= DP_F_UP;
  330. case 'g':
  331. if (cflags == DP_C_LDOUBLE)
  332. fvalue = va_arg (args, LDOUBLE);
  333. else
  334. fvalue = va_arg (args, double);
  335. break;
  336. case 'c':
  337. dopr_outch (buffer, &currlen, maxlen, va_arg (args, int));
  338. break;
  339. case 's':
  340. strvalue = va_arg (args, char *);
  341. if (!strvalue) strvalue = "(NULL)";
  342. if (max == -1) {
  343. max = strlen(strvalue);
  344. }
  345. if (min > 0 && max >= 0 && min > max) max = min;
  346. fmtstr (buffer, &currlen, maxlen, strvalue, flags, min, max);
  347. break;
  348. case 'p':
  349. strvalue = va_arg (args, void *);
  350. fmtint (buffer, &currlen, maxlen, (long) strvalue, 16, min, max, flags);
  351. break;
  352. case 'n':
  353. if (cflags == DP_C_SHORT) {
  354. short int *num;
  355. num = va_arg (args, short int *);
  356. *num = currlen;
  357. } else if (cflags == DP_C_LONG) {
  358. long int *num;
  359. num = va_arg (args, long int *);
  360. *num = (long int)currlen;
  361. } else if (cflags == DP_C_LLONG) {
  362. LLONG *num;
  363. num = va_arg (args, LLONG *);
  364. *num = (LLONG)currlen;
  365. } else {
  366. int *num;
  367. num = va_arg (args, int *);
  368. *num = currlen;
  369. }
  370. break;
  371. case '%':
  372. dopr_outch (buffer, &currlen, maxlen, ch);
  373. break;
  374. case 'w':
  375. /* not supported yet, treat as next char */
  376. ch = *format++;
  377. break;
  378. default:
  379. /* Unknown, skip */
  380. break;
  381. }
  382. ch = *format++;
  383. state = DP_S_DEFAULT;
  384. flags = cflags = min = 0;
  385. max = -1;
  386. break;
  387. case DP_S_DONE:
  388. break;
  389. default:
  390. /* hmm? */
  391. break; /* some picky compilers need this */
  392. }
  393. }
  394. if (maxlen != 0) {
  395. if (currlen < maxlen - 1)
  396. buffer[currlen] = '\0';
  397. else if (maxlen > 0)
  398. buffer[maxlen - 1] = '\0';
  399. }
  400. return currlen;
  401. }
  402. static void fmtstr(char *buffer, size_t *currlen, size_t maxlen,
  403. char *value, int flags, int min, int max)
  404. {
  405. int padlen, strln; /* amount to pad */
  406. int cnt = 0;
  407. #ifdef DEBUG_SNPRINTF
  408. printf("fmtstr min=%d max=%d s=[%s]\n", min, max, value);
  409. #endif
  410. if (value == 0) {
  411. value = "<NULL>";
  412. }
  413. for (strln = 0; value[strln]; ++strln); /* strlen */
  414. padlen = min - strln;
  415. if (padlen < 0)
  416. padlen = 0;
  417. if (flags & DP_F_MINUS)
  418. padlen = -padlen; /* Left Justify */
  419. while ((padlen > 0) && (cnt < max)) {
  420. dopr_outch (buffer, currlen, maxlen, ' ');
  421. --padlen;
  422. ++cnt;
  423. }
  424. while (*value && (cnt < max)) {
  425. dopr_outch (buffer, currlen, maxlen, *value++);
  426. ++cnt;
  427. }
  428. while ((padlen < 0) && (cnt < max)) {
  429. dopr_outch (buffer, currlen, maxlen, ' ');
  430. ++padlen;
  431. ++cnt;
  432. }
  433. }
  434. /* Have to handle DP_F_NUM (ie 0x and 0 alternates) */
  435. static void fmtint(char *buffer, size_t *currlen, size_t maxlen,
  436. long value, int base, int min, int max, int flags)
  437. {
  438. int signvalue = 0;
  439. unsigned long uvalue;
  440. char convert[20];
  441. int place = 0;
  442. int spadlen = 0; /* amount to space pad */
  443. int zpadlen = 0; /* amount to zero pad */
  444. int caps = 0;
  445. if (max < 0)
  446. max = 0;
  447. uvalue = value;
  448. if(!(flags & DP_F_UNSIGNED)) {
  449. if( value < 0 ) {
  450. signvalue = '-';
  451. uvalue = -value;
  452. } else {
  453. if (flags & DP_F_PLUS) /* Do a sign (+/i) */
  454. signvalue = '+';
  455. else if (flags & DP_F_SPACE)
  456. signvalue = ' ';
  457. }
  458. }
  459. if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
  460. do {
  461. convert[place++] =
  462. (caps? "0123456789ABCDEF":"0123456789abcdef")
  463. [uvalue % (unsigned)base ];
  464. uvalue = (uvalue / (unsigned)base );
  465. } while(uvalue && (place < 20));
  466. if (place == 20) place--;
  467. convert[place] = 0;
  468. zpadlen = max - place;
  469. spadlen = min - MAX (max, place) - (signvalue ? 1 : 0);
  470. if (zpadlen < 0) zpadlen = 0;
  471. if (spadlen < 0) spadlen = 0;
  472. if (flags & DP_F_ZERO) {
  473. zpadlen = MAX(zpadlen, spadlen);
  474. spadlen = 0;
  475. }
  476. if (flags & DP_F_MINUS)
  477. spadlen = -spadlen; /* Left Justifty */
  478. #ifdef DEBUG_SNPRINTF
  479. printf("zpad: %d, spad: %d, min: %d, max: %d, place: %d\n",
  480. zpadlen, spadlen, min, max, place);
  481. #endif
  482. /* Spaces */
  483. while (spadlen > 0) {
  484. dopr_outch (buffer, currlen, maxlen, ' ');
  485. --spadlen;
  486. }
  487. /* Sign */
  488. if (signvalue)
  489. dopr_outch (buffer, currlen, maxlen, signvalue);
  490. /* Zeros */
  491. if (zpadlen > 0) {
  492. while (zpadlen > 0) {
  493. dopr_outch (buffer, currlen, maxlen, '0');
  494. --zpadlen;
  495. }
  496. }
  497. /* Digits */
  498. while (place > 0)
  499. dopr_outch (buffer, currlen, maxlen, convert[--place]);
  500. /* Left Justified spaces */
  501. while (spadlen < 0) {
  502. dopr_outch (buffer, currlen, maxlen, ' ');
  503. ++spadlen;
  504. }
  505. }
  506. static LDOUBLE abs_val(LDOUBLE value)
  507. {
  508. LDOUBLE result = value;
  509. if (value < 0)
  510. result = -value;
  511. return result;
  512. }
  513. static LDOUBLE POW10(int exp)
  514. {
  515. LDOUBLE result = 1;
  516. while (exp) {
  517. result *= 10;
  518. exp--;
  519. }
  520. return result;
  521. }
  522. static LLONG ROUND(LDOUBLE value)
  523. {
  524. LLONG intpart;
  525. intpart = (LLONG)value;
  526. value = value - intpart;
  527. if (value >= 0.5) intpart++;
  528. return intpart;
  529. }
  530. /* a replacement for modf that doesn't need the math library. Should
  531. be portable, but slow */
  532. static double my_modf(double x0, double *iptr)
  533. {
  534. int i;
  535. long l;
  536. double x = x0;
  537. double f = 1.0;
  538. for (i=0;i<100;i++) {
  539. l = (long)x;
  540. if (l <= (x+1) && l >= (x-1)) break;
  541. x *= 0.1;
  542. f *= 10.0;
  543. }
  544. if (i == 100) {
  545. /* yikes! the number is beyond what we can handle. What do we do? */
  546. (*iptr) = 0;
  547. return 0;
  548. }
  549. if (i != 0) {
  550. double i2;
  551. double ret;
  552. ret = my_modf(x0-l*f, &i2);
  553. (*iptr) = l*f + i2;
  554. return ret;
  555. }
  556. (*iptr) = l;
  557. return x - (*iptr);
  558. }
  559. static void fmtfp (char *buffer, size_t *currlen, size_t maxlen,
  560. LDOUBLE fvalue, int min, int max, int flags)
  561. {
  562. int signvalue = 0;
  563. double ufvalue;
  564. char iconvert[311];
  565. char fconvert[311];
  566. int iplace = 0;
  567. int fplace = 0;
  568. int padlen = 0; /* amount to pad */
  569. int zpadlen = 0;
  570. int caps = 0;
  571. int index;
  572. double intpart;
  573. double fracpart;
  574. double temp;
  575. /*
  576. * AIX manpage says the default is 0, but Solaris says the default
  577. * is 6, and sprintf on AIX defaults to 6
  578. */
  579. if (max < 0)
  580. max = 6;
  581. ufvalue = abs_val (fvalue);
  582. if (fvalue < 0) {
  583. signvalue = '-';
  584. } else {
  585. if (flags & DP_F_PLUS) { /* Do a sign (+/i) */
  586. signvalue = '+';
  587. } else {
  588. if (flags & DP_F_SPACE)
  589. signvalue = ' ';
  590. }
  591. }
  592. #if 0
  593. if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
  594. #endif
  595. #if 0
  596. if (max == 0) ufvalue += 0.5; /* if max = 0 we must round */
  597. #endif
  598. /*
  599. * Sorry, we only support 16 digits past the decimal because of our
  600. * conversion method
  601. */
  602. if (max > 16)
  603. max = 16;
  604. /* We "cheat" by converting the fractional part to integer by
  605. * multiplying by a factor of 10
  606. */
  607. temp = ufvalue;
  608. my_modf(temp, &intpart);
  609. fracpart = ROUND((POW10(max)) * (ufvalue - intpart));
  610. if (fracpart >= POW10(max)) {
  611. intpart++;
  612. fracpart -= POW10(max);
  613. }
  614. /* Convert integer part */
  615. do {
  616. temp = intpart*0.1;
  617. my_modf(temp, &intpart);
  618. index = (int) ((temp -intpart +0.05)* 10.0);
  619. /* index = (int) (((double)(temp*0.1) -intpart +0.05) *10.0); */
  620. /* printf ("%llf, %f, %x\n", temp, intpart, index); */
  621. iconvert[iplace++] =
  622. (caps? "0123456789ABCDEF":"0123456789abcdef")[index];
  623. } while (intpart && (iplace < 311));
  624. if (iplace == 311) iplace--;
  625. iconvert[iplace] = 0;
  626. /* Convert fractional part */
  627. if (fracpart)
  628. {
  629. do {
  630. temp = fracpart*0.1;
  631. my_modf(temp, &fracpart);
  632. index = (int) ((temp -fracpart +0.05)* 10.0);
  633. /* index = (int) ((((temp/10) -fracpart) +0.05) *10); */
  634. /* printf ("%lf, %lf, %ld\n", temp, fracpart, index); */
  635. fconvert[fplace++] =
  636. (caps? "0123456789ABCDEF":"0123456789abcdef")[index];
  637. } while(fracpart && (fplace < 311));
  638. if (fplace == 311) fplace--;
  639. }
  640. fconvert[fplace] = 0;
  641. /* -1 for decimal point, another -1 if we are printing a sign */
  642. padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
  643. zpadlen = max - fplace;
  644. if (zpadlen < 0) zpadlen = 0;
  645. if (padlen < 0)
  646. padlen = 0;
  647. if (flags & DP_F_MINUS)
  648. padlen = -padlen; /* Left Justifty */
  649. if ((flags & DP_F_ZERO) && (padlen > 0)) {
  650. if (signvalue) {
  651. dopr_outch (buffer, currlen, maxlen, signvalue);
  652. --padlen;
  653. signvalue = 0;
  654. }
  655. while (padlen > 0) {
  656. dopr_outch (buffer, currlen, maxlen, '0');
  657. --padlen;
  658. }
  659. }
  660. while (padlen > 0) {
  661. dopr_outch (buffer, currlen, maxlen, ' ');
  662. --padlen;
  663. }
  664. if (signvalue)
  665. dopr_outch (buffer, currlen, maxlen, signvalue);
  666. while (iplace > 0)
  667. dopr_outch (buffer, currlen, maxlen, iconvert[--iplace]);
  668. #ifdef DEBUG_SNPRINTF
  669. printf("fmtfp: fplace=%d zpadlen=%d\n", fplace, zpadlen);
  670. #endif
  671. /*
  672. * Decimal point. This should probably use locale to find the correct
  673. * char to print out.
  674. */
  675. if (max > 0) {
  676. dopr_outch (buffer, currlen, maxlen, '.');
  677. while (zpadlen > 0) {
  678. dopr_outch (buffer, currlen, maxlen, '0');
  679. --zpadlen;
  680. }
  681. while (fplace > 0)
  682. dopr_outch (buffer, currlen, maxlen, fconvert[--fplace]);
  683. }
  684. while (padlen < 0) {
  685. dopr_outch (buffer, currlen, maxlen, ' ');
  686. ++padlen;
  687. }
  688. }
  689. static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c)
  690. {
  691. if (*currlen < maxlen) {
  692. buffer[(*currlen)] = c;
  693. }
  694. (*currlen)++;
  695. }
  696. /* yes this really must be a ||. Don't muck with this (tridge) */
  697. #if !defined(HAVE_VSNPRINTF) || !defined(HAVE_C99_VSNPRINTF)
  698. int vsnprintf (char *str, size_t count, const char *fmt, va_list args)
  699. {
  700. return dopr(str, count, fmt, args);
  701. }
  702. #endif
  703. /* yes this really must be a ||. Don't muck wiith this (tridge)
  704. *
  705. * The logic for these two is that we need our own definition if the
  706. * OS *either* has no definition of *sprintf, or if it does have one
  707. * that doesn't work properly according to the autoconf test. Perhaps
  708. * these should really be smb_snprintf to avoid conflicts with buggy
  709. * linkers? -- mbp
  710. */
  711. #if !defined(HAVE_SNPRINTF) /* || !defined(HAVE_C99_SNPRINTF) */
  712. int snprintf(char *str,size_t count,const char *fmt,...)
  713. {
  714. size_t ret;
  715. va_list ap;
  716. va_start(ap, fmt);
  717. ret = vsnprintf(str, count, fmt, ap);
  718. va_end(ap);
  719. return ret;
  720. }
  721. #endif
  722. #endif
  723. #ifndef HAVE_VASPRINTF
  724. int vasprintf(char **ptr, const char *format, va_list ap)
  725. {
  726. int ret;
  727. va_list ap2;
  728. VA_COPY(ap2, ap);
  729. ret = vsnprintf(NULL, 0, format, ap2);
  730. if (ret <= 0) return ret;
  731. (*ptr) = (char *)malloc(ret+1);
  732. if (!*ptr) return -1;
  733. VA_COPY(ap2, ap);
  734. ret = vsnprintf(*ptr, ret+1, format, ap2);
  735. return ret;
  736. }
  737. #endif
  738. #ifndef HAVE_ASPRINTF
  739. int asprintf(char **ptr, const char *format, ...)
  740. {
  741. va_list ap;
  742. int ret;
  743. *ptr = NULL;
  744. va_start(ap, format);
  745. ret = vasprintf(ptr, format, ap);
  746. va_end(ap);
  747. return ret;
  748. }
  749. #endif
  750. #ifdef TEST_SNPRINTF
  751. int sprintf(char *str,const char *fmt,...);
  752. int main (void)
  753. {
  754. char buf1[1024];
  755. char buf2[1024];
  756. char *fp_fmt[] = {
  757. "%1.1f",
  758. "%-1.5f",
  759. "%1.5f",
  760. "%123.9f",
  761. "%10.5f",
  762. "% 10.5f",
  763. "%+22.9f",
  764. "%+4.9f",
  765. "%01.3f",
  766. "%4f",
  767. "%3.1f",
  768. "%3.2f",
  769. "%.0f",
  770. "%f",
  771. "-16.16f",
  772. NULL
  773. };
  774. double fp_nums[] = { 6442452944.1234, -1.5, 134.21, 91340.2, 341.1234, 0203.9, 0.96, 0.996,
  775. 0.9996, 1.996, 4.136, 5.030201, 0};
  776. char *int_fmt[] = {
  777. "%-1.5d",
  778. "%1.5d",
  779. "%123.9d",
  780. "%5.5d",
  781. "%10.5d",
  782. "% 10.5d",
  783. "%+22.33d",
  784. "%01.3d",
  785. "%4d",
  786. "%d",
  787. NULL
  788. };
  789. long int_nums[] = { -1, 134, 91340, 341, 0203, 0};
  790. char *str_fmt[] = {
  791. "10.5s",
  792. "5.10s",
  793. "10.1s",
  794. "0.10s",
  795. "10.0s",
  796. "1.10s",
  797. "%s",
  798. "%.1s",
  799. "%.10s",
  800. "%10s",
  801. NULL
  802. };
  803. char *str_vals[] = {"hello", "a", "", "a longer string", NULL};
  804. int x, y;
  805. int fail = 0;
  806. int num = 0;
  807. printf ("Testing snprintf format codes against system sprintf...\n");
  808. for (x = 0; fp_fmt[x] ; x++) {
  809. for (y = 0; fp_nums[y] != 0 ; y++) {
  810. int l1 = snprintf(NULL, 0, fp_fmt[x], fp_nums[y]);
  811. int l2 = snprintf(buf1, sizeof(buf1), fp_fmt[x], fp_nums[y]);
  812. sprintf (buf2, fp_fmt[x], fp_nums[y]);
  813. if (strcmp (buf1, buf2)) {
  814. printf("snprintf doesn't match Format: %s\n\tsnprintf = [%s]\n\t sprintf = [%s]\n",
  815. fp_fmt[x], buf1, buf2);
  816. fail++;
  817. }
  818. if (l1 != l2) {
  819. printf("snprintf l1 != l2 (%d %d) %s\n", l1, l2, fp_fmt[x]);
  820. fail++;
  821. }
  822. num++;
  823. }
  824. }
  825. for (x = 0; int_fmt[x] ; x++) {
  826. for (y = 0; int_nums[y] != 0 ; y++) {
  827. int l1 = snprintf(NULL, 0, int_fmt[x], int_nums[y]);
  828. int l2 = snprintf(buf1, sizeof(buf1), int_fmt[x], int_nums[y]);
  829. sprintf (buf2, int_fmt[x], int_nums[y]);
  830. if (strcmp (buf1, buf2)) {
  831. printf("snprintf doesn't match Format: %s\n\tsnprintf = [%s]\n\t sprintf = [%s]\n",
  832. int_fmt[x], buf1, buf2);
  833. fail++;
  834. }
  835. if (l1 != l2) {
  836. printf("snprintf l1 != l2 (%d %d) %s\n", l1, l2, int_fmt[x]);
  837. fail++;
  838. }
  839. num++;
  840. }
  841. }
  842. for (x = 0; str_fmt[x] ; x++) {
  843. for (y = 0; str_vals[y] != 0 ; y++) {
  844. int l1 = snprintf(NULL, 0, str_fmt[x], str_vals[y]);
  845. int l2 = snprintf(buf1, sizeof(buf1), str_fmt[x], str_vals[y]);
  846. sprintf (buf2, str_fmt[x], str_vals[y]);
  847. if (strcmp (buf1, buf2)) {
  848. printf("snprintf doesn't match Format: %s\n\tsnprintf = [%s]\n\t sprintf = [%s]\n",
  849. str_fmt[x], buf1, buf2);
  850. fail++;
  851. }
  852. if (l1 != l2) {
  853. printf("snprintf l1 != l2 (%d %d) %s\n", l1, l2, str_fmt[x]);
  854. fail++;
  855. }
  856. num++;
  857. }
  858. }
  859. printf ("%d tests failed out of %d.\n", fail, num);
  860. printf("seeing how many digits we support\n");
  861. {
  862. double v0 = 0.12345678901234567890123456789012345678901;
  863. for (x=0; x<100; x++) {
  864. double p = pow(10, x);
  865. double r = v0*p;
  866. snprintf(buf1, sizeof(buf1), "%1.1f", r);
  867. sprintf(buf2, "%1.1f", r);
  868. if (strcmp(buf1, buf2)) {
  869. printf("we seem to support %d digits\n", x-1);
  870. break;
  871. }
  872. }
  873. }
  874. return 0;
  875. }
  876. #endif /* SNPRINTF_TEST */