vsnprintf.c 21 KB

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