bsd-snprintf.c 21 KB

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