stdio.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072
  1. #include <stdio.h>
  2. #if defined( LUA_NUMBER_INTEGRAL )
  3. #include <stdarg.h>
  4. int sprintf(char *s, const char *fmt, ...)
  5. {
  6. int n;
  7. va_list arg;
  8. va_start(arg, fmt);
  9. n = ets_vsprintf(s, fmt, arg);
  10. va_end(arg);
  11. return n;
  12. }
  13. int vsprintf (char *d, const char *s, va_list ap)
  14. {
  15. return ets_vsprintf(d, s, ap);
  16. }
  17. #else
  18. #define FLOATINGPT 1
  19. #define NEWFP 1
  20. #define ENDIAN_LITTLE 1234
  21. #define ENDIAN_BIG 4321
  22. #define ENDIAN_PDP 3412
  23. #define ENDIAN ENDIAN_LITTLE
  24. /* $Id: strichr.c,v 1.1.1.1 2006/08/23 17:03:06 pefo Exp $ */
  25. /*
  26. * Copyright (c) 2000-2002 Opsycon AB (www.opsycon.se)
  27. *
  28. * Redistribution and use in source and binary forms, with or without
  29. * modification, are permitted provided that the following conditions
  30. * are met:
  31. * 1. Redistributions of source code must retain the above copyright
  32. * notice, this list of conditions and the following disclaimer.
  33. * 2. Redistributions in binary form must reproduce the above copyright
  34. * notice, this list of conditions and the following disclaimer in the
  35. * documentation and/or other materials provided with the distribution.
  36. * 3. All advertising materials mentioning features or use of this software
  37. * must display the following acknowledgement:
  38. * This product includes software developed by Opsycon AB.
  39. * 4. The name of the author may not be used to endorse or promote products
  40. * derived from this software without specific prior written permission.
  41. *
  42. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
  43. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  44. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  45. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  46. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  47. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  48. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  49. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  50. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  51. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  52. * SUCH DAMAGE.
  53. *
  54. */
  55. #include <string.h>
  56. char *
  57. strichr(char *p, int c)
  58. {
  59. char *t;
  60. if (p != NULL) {
  61. for(t = p; *t; t++);
  62. for (; t >= p; t--) {
  63. *(t + 1) = *t;
  64. }
  65. *p = c;
  66. }
  67. return (p);
  68. }
  69. /* $Id: str_fmt.c,v 1.1.1.1 2006/08/23 17:03:06 pefo Exp $ */
  70. /*
  71. * Copyright (c) 2000-2002 Opsycon AB (www.opsycon.se)
  72. *
  73. * Redistribution and use in source and binary forms, with or without
  74. * modification, are permitted provided that the following conditions
  75. * are met:
  76. * 1. Redistributions of source code must retain the above copyright
  77. * notice, this list of conditions and the following disclaimer.
  78. * 2. Redistributions in binary form must reproduce the above copyright
  79. * notice, this list of conditions and the following disclaimer in the
  80. * documentation and/or other materials provided with the distribution.
  81. * 3. All advertising materials mentioning features or use of this software
  82. * must display the following acknowledgement:
  83. * This product includes software developed by Opsycon AB.
  84. * 4. The name of the author may not be used to endorse or promote products
  85. * derived from this software without specific prior written permission.
  86. *
  87. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
  88. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  89. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  90. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  91. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  92. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  93. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  94. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  95. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  96. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  97. * SUCH DAMAGE.
  98. *
  99. */
  100. #include <string.h>
  101. #define FMT_RJUST 0
  102. #define FMT_LJUST 1
  103. #define FMT_RJUST0 2
  104. #define FMT_CENTER 3
  105. /*
  106. * Format string by inserting blanks.
  107. */
  108. void
  109. str_fmt(char *p, int size, int fmt)
  110. {
  111. int n, m, len;
  112. len = strlen (p);
  113. switch (fmt) {
  114. case FMT_RJUST:
  115. for (n = size - len; n > 0; n--)
  116. strichr (p, ' ');
  117. break;
  118. case FMT_LJUST:
  119. for (m = size - len; m > 0; m--)
  120. strcat (p, " ");
  121. break;
  122. case FMT_RJUST0:
  123. for (n = size - len; n > 0; n--)
  124. strichr (p, '0');
  125. break;
  126. case FMT_CENTER:
  127. m = (size - len) / 2;
  128. n = size - (len + m);
  129. for (; m > 0; m--)
  130. strcat (p, " ");
  131. for (; n > 0; n--)
  132. strichr (p, ' ');
  133. break;
  134. }
  135. }
  136. /* $Id: strtoupp.c,v 1.1.1.1 2006/08/23 17:03:06 pefo Exp $ */
  137. /*
  138. * Copyright (c) 2000-2002 Opsycon AB (www.opsycon.se)
  139. *
  140. * Redistribution and use in source and binary forms, with or without
  141. * modification, are permitted provided that the following conditions
  142. * are met:
  143. * 1. Redistributions of source code must retain the above copyright
  144. * notice, this list of conditions and the following disclaimer.
  145. * 2. Redistributions in binary form must reproduce the above copyright
  146. * notice, this list of conditions and the following disclaimer in the
  147. * documentation and/or other materials provided with the distribution.
  148. * 3. All advertising materials mentioning features or use of this software
  149. * must display the following acknowledgement:
  150. * This product includes software developed by Opsycon AB.
  151. * 4. The name of the author may not be used to endorse or promote products
  152. * derived from this software without specific prior written permission.
  153. *
  154. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
  155. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  156. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  157. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  158. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  159. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  160. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  161. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  162. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  163. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  164. * SUCH DAMAGE.
  165. *
  166. */
  167. #include <string.h>
  168. #include <ctype.h>
  169. void
  170. strtoupper(char *p)
  171. {
  172. if(!p)
  173. return;
  174. for (; *p; p++)
  175. *p = toupper (*p);
  176. }
  177. /* $Id: atob.c,v 1.1.1.1 2006/08/23 17:03:06 pefo Exp $ */
  178. /*
  179. * Copyright (c) 2000-2002 Opsycon AB (www.opsycon.se)
  180. *
  181. * Redistribution and use in source and binary forms, with or without
  182. * modification, are permitted provided that the following conditions
  183. * are met:
  184. * 1. Redistributions of source code must retain the above copyright
  185. * notice, this list of conditions and the following disclaimer.
  186. * 2. Redistributions in binary form must reproduce the above copyright
  187. * notice, this list of conditions and the following disclaimer in the
  188. * documentation and/or other materials provided with the distribution.
  189. * 3. All advertising materials mentioning features or use of this software
  190. * must display the following acknowledgement:
  191. * This product includes software developed by Opsycon AB.
  192. * 4. The name of the author may not be used to endorse or promote products
  193. * derived from this software without specific prior written permission.
  194. *
  195. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
  196. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  197. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  198. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  199. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  200. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  201. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  202. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  203. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  204. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  205. * SUCH DAMAGE.
  206. *
  207. */
  208. //#include <sys/types.h>
  209. #include <string.h>
  210. #include <stdint.h>
  211. //#include <pmon.h>
  212. typedef unsigned int u_int;
  213. typedef unsigned long u_long;
  214. typedef int32_t register_t;
  215. typedef long long quad_t;
  216. typedef unsigned long long u_quad_t;
  217. typedef double rtype;
  218. #ifndef __P
  219. #define __P(args) args
  220. #endif
  221. static char * _getbase __P((char *, int *));
  222. static int _atob __P((unsigned long long *, char *p, int));
  223. static char *
  224. _getbase(char *p, int *basep)
  225. {
  226. if (p[0] == '0') {
  227. switch (p[1]) {
  228. case 'x':
  229. *basep = 16;
  230. break;
  231. case 't': case 'n':
  232. *basep = 10;
  233. break;
  234. case 'o':
  235. *basep = 8;
  236. break;
  237. default:
  238. *basep = 10;
  239. return (p);
  240. }
  241. return (p + 2);
  242. }
  243. *basep = 10;
  244. return (p);
  245. }
  246. /*
  247. * _atob(vp,p,base)
  248. */
  249. static int
  250. _atob (u_quad_t *vp, char *p, int base)
  251. {
  252. u_quad_t value, v1, v2;
  253. char *q, tmp[20];
  254. int digit;
  255. if (p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
  256. base = 16;
  257. p += 2;
  258. }
  259. if (base == 16 && (q = strchr (p, '.')) != 0) {
  260. if (q - p > sizeof(tmp) - 1)
  261. return (0);
  262. strncpy (tmp, p, q - p);
  263. tmp[q - p] = '\0';
  264. if (!_atob (&v1, tmp, 16))
  265. return (0);
  266. q++;
  267. if (strchr (q, '.'))
  268. return (0);
  269. if (!_atob (&v2, q, 16))
  270. return (0);
  271. *vp = (v1 << 16) + v2;
  272. return (1);
  273. }
  274. value = *vp = 0;
  275. for (; *p; p++) {
  276. if (*p >= '0' && *p <= '9')
  277. digit = *p - '0';
  278. else if (*p >= 'a' && *p <= 'f')
  279. digit = *p - 'a' + 10;
  280. else if (*p >= 'A' && *p <= 'F')
  281. digit = *p - 'A' + 10;
  282. else
  283. return (0);
  284. if (digit >= base)
  285. return (0);
  286. value *= base;
  287. value += digit;
  288. }
  289. *vp = value;
  290. return (1);
  291. }
  292. /*
  293. * atob(vp,p,base)
  294. * converts p to binary result in vp, rtn 1 on success
  295. */
  296. int
  297. atob(uint32_t *vp, char *p, int base)
  298. {
  299. u_quad_t v;
  300. if (base == 0)
  301. p = _getbase (p, &base);
  302. if (_atob (&v, p, base)) {
  303. *vp = v;
  304. return (1);
  305. }
  306. return (0);
  307. }
  308. /*
  309. * llatob(vp,p,base)
  310. * converts p to binary result in vp, rtn 1 on success
  311. */
  312. int
  313. llatob(u_quad_t *vp, char *p, int base)
  314. {
  315. if (base == 0)
  316. p = _getbase (p, &base);
  317. return _atob(vp, p, base);
  318. }
  319. /*
  320. * char *btoa(dst,value,base)
  321. * converts value to ascii, result in dst
  322. */
  323. char *
  324. btoa(char *dst, u_int value, int base, int plussgn)
  325. {
  326. char buf[34], digit;
  327. int i, j, rem, neg;
  328. if (value == 0) {
  329. dst[0] = '0';
  330. dst[1] = 0;
  331. return (dst);
  332. }
  333. neg = 0;
  334. if (base == -10) {
  335. base = 10;
  336. if (value & (1L << 31)) {
  337. value = (~value) + 1;
  338. neg = 1;
  339. }
  340. }
  341. for (i = 0; value != 0; i++) {
  342. rem = value % base;
  343. value /= base;
  344. if (rem >= 0 && rem <= 9)
  345. digit = rem + '0';
  346. else if (rem >= 10 && rem <= 36)
  347. digit = (rem - 10) + 'a';
  348. buf[i] = digit;
  349. }
  350. buf[i] = 0;
  351. if (neg)
  352. strcat (buf, "-");
  353. else if (plussgn)
  354. strcat (buf, "+");
  355. /* reverse the string */
  356. for (i = 0, j = strlen (buf) - 1; j >= 0; i++, j--)
  357. dst[i] = buf[j];
  358. dst[i] = 0;
  359. return (dst);
  360. }
  361. /*
  362. * char *btoa(dst,value,base)
  363. * converts value to ascii, result in dst
  364. */
  365. char *
  366. llbtoa(char *dst, u_quad_t value, int base, int plussgn)
  367. {
  368. char buf[66], digit;
  369. int i, j, rem, neg;
  370. if (value == 0) {
  371. dst[0] = '0';
  372. dst[1] = 0;
  373. return (dst);
  374. }
  375. neg = 0;
  376. if (base == -10) {
  377. base = 10;
  378. if (value & (1LL << 63)) {
  379. value = (~value) + 1;
  380. neg = 1;
  381. }
  382. }
  383. for (i = 0; value != 0; i++) {
  384. rem = value % base;
  385. value /= base;
  386. if (rem >= 0 && rem <= 9)
  387. digit = rem + '0';
  388. else if (rem >= 10 && rem <= 36)
  389. digit = (rem - 10) + 'a';
  390. buf[i] = digit;
  391. }
  392. buf[i] = 0;
  393. if (neg)
  394. strcat (buf, "-");
  395. else if (plussgn)
  396. strcat (buf, "+");
  397. /* reverse the string */
  398. for (i = 0, j = strlen (buf) - 1; j >= 0; i++, j--)
  399. dst[i] = buf[j];
  400. dst[i] = 0;
  401. return (dst);
  402. }
  403. /*
  404. * gethex(vp,p,n)
  405. * convert n hex digits from p to binary, result in vp,
  406. * rtn 1 on success
  407. */
  408. int
  409. gethex(int32_t *vp, char *p, int n)
  410. {
  411. u_long v;
  412. int digit;
  413. for (v = 0; n > 0; n--) {
  414. if (*p == 0)
  415. return (0);
  416. if (*p >= '0' && *p <= '9')
  417. digit = *p - '0';
  418. else if (*p >= 'a' && *p <= 'f')
  419. digit = *p - 'a' + 10;
  420. else if (*p >= 'A' && *p <= 'F')
  421. digit = *p - 'A' + 10;
  422. else
  423. return (0);
  424. v <<= 4;
  425. v |= digit;
  426. p++;
  427. }
  428. *vp = v;
  429. return (1);
  430. }
  431. /* $Id: vsprintf.c,v 1.1.1.1 2006/08/23 17:03:06 pefo Exp $ */
  432. /*
  433. * Copyright (c) 2000-2002 Opsycon AB (www.opsycon.se)
  434. *
  435. * Redistribution and use in source and binary forms, with or without
  436. * modification, are permitted provided that the following conditions
  437. * are met:
  438. * 1. Redistributions of source code must retain the above copyright
  439. * notice, this list of conditions and the following disclaimer.
  440. * 2. Redistributions in binary form must reproduce the above copyright
  441. * notice, this list of conditions and the following disclaimer in the
  442. * documentation and/or other materials provided with the distribution.
  443. * 3. All advertising materials mentioning features or use of this software
  444. * must display the following acknowledgement:
  445. * This product includes software developed by Opsycon AB.
  446. * 4. The name of the author may not be used to endorse or promote products
  447. * derived from this software without specific prior written permission.
  448. *
  449. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
  450. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  451. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  452. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  453. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  454. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  455. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  456. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  457. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  458. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  459. * SUCH DAMAGE.
  460. *
  461. */
  462. //#include <stdio.h>
  463. //#include <pmon.h>
  464. #include <stdarg.h>
  465. #include <string.h>
  466. #include <ctype.h>
  467. /*
  468. * int vsprintf(d,s,ap)
  469. */
  470. int
  471. vsprintf (char *d, const char *s, va_list ap)
  472. {
  473. const char *t;
  474. char *p, *dst, tmp[40];
  475. unsigned int n;
  476. int fmt, trunc, haddot, width, base, longlong, plussgn;
  477. #ifdef FLOATINGPT
  478. double dbl;
  479. #ifndef NEWFP
  480. EP ex;
  481. #endif
  482. #endif
  483. dst = d;
  484. for (; *s;) {
  485. if (*s == '%') {
  486. s++;
  487. fmt = FMT_RJUST;
  488. width = trunc = haddot = longlong = plussgn = 0;
  489. for (; *s; s++) {
  490. if (strchr("bcdefgilopPrRsuxX%", *s))
  491. break;
  492. else if (*s == '-')
  493. fmt = FMT_LJUST;
  494. else if (*s == '+')
  495. plussgn = 1;
  496. else if (*s == '0')
  497. fmt = FMT_RJUST0;
  498. else if (*s == '~')
  499. fmt = FMT_CENTER;
  500. else if (*s == '*') {
  501. if (haddot)
  502. trunc = va_arg(ap, int);
  503. else
  504. width = va_arg(ap, int);
  505. } else if (*s >= '1' && *s <= '9') {
  506. for (t = s; isdigit(*s); s++);
  507. strncpy(tmp, t, s - t);
  508. tmp[s - t] = '\0';
  509. atob(&n, tmp, 10);
  510. if (haddot)
  511. trunc = n;
  512. else
  513. width = n;
  514. s--;
  515. } else if (*s == '.')
  516. haddot = 1;
  517. }
  518. if (*s == '%') {
  519. *d++ = '%';
  520. *d = 0;
  521. } else if (*s == 's') {
  522. p = va_arg(ap, char *);
  523. if (p)
  524. strcpy(d, p);
  525. else
  526. strcpy(d, "(null)");
  527. } else if (*s == 'c') {
  528. n = va_arg (ap, int);
  529. *d = n;
  530. d[1] = 0;
  531. } else {
  532. if (*s == 'l') {
  533. if (*++s == 'l') {
  534. longlong = 1;
  535. ++s;
  536. }
  537. }
  538. if (strchr("bdiopPrRxXu", *s)) {
  539. if (*s == 'd' || *s == 'i')
  540. base = -10;
  541. else if (*s == 'u')
  542. base = 10;
  543. else if (*s == 'x' || *s == 'X')
  544. base = 16;
  545. else if(*s == 'p' || *s == 'P') {
  546. base = 16;
  547. if (*s == 'p') {
  548. *d++ = '0';
  549. *d++ = 'x';
  550. }
  551. fmt = FMT_RJUST0;
  552. if (sizeof(long) > 4) {
  553. width = 16;
  554. longlong = 1;
  555. } else {
  556. width = 8;
  557. }
  558. }
  559. else if(*s == 'r' || *s == 'R') {
  560. base = 16;
  561. if (*s == 'r') {
  562. *d++ = '0';
  563. *d++ = 'x';
  564. }
  565. fmt = FMT_RJUST0;
  566. if (sizeof(register_t) > 4) {
  567. width = 16;
  568. longlong = 1;
  569. } else {
  570. width = 8;
  571. }
  572. }
  573. else if (*s == 'o')
  574. base = 8;
  575. else if (*s == 'b')
  576. base = 2;
  577. if (longlong)
  578. llbtoa(d, va_arg (ap, quad_t),
  579. base, plussgn);
  580. else
  581. btoa(d, va_arg (ap, int), base, plussgn);
  582. if (*s == 'X')
  583. strtoupper(d);
  584. }
  585. #ifdef FLOATINGPT
  586. else if (strchr ("eEfgG", *s)) {
  587. //static void dtoa (char *, double, int, int, int, int);
  588. void dtoa (char *dbuf, rtype arg, int fmtch, int width, int prec, int plussgn);
  589. dbl = va_arg(ap, double);
  590. if (!haddot) {
  591. trunc = 6;
  592. }
  593. dtoa(d, dbl, *s, width, trunc, plussgn);
  594. trunc = 0;
  595. }
  596. #endif
  597. }
  598. if (trunc)
  599. d[trunc] = 0;
  600. if (width)
  601. str_fmt (d, width, fmt);
  602. for (; *d; d++);
  603. s++;
  604. } else
  605. *d++ = *s++;
  606. }
  607. *d = 0;
  608. return (d - dst);
  609. }
  610. #ifdef FLOATINGPT
  611. /*
  612. * Floating point output, cvt() onward lifted from BSD sources:
  613. *
  614. * Copyright (c) 1990 The Regents of the University of California.
  615. * All rights reserved.
  616. *
  617. * This code is derived from software contributed to Berkeley by
  618. * Chris Torek.
  619. *
  620. * Redistribution and use in source and binary forms, with or without
  621. * modification, are permitted provided that the following conditions
  622. * are met:
  623. * 1. Redistributions of source code must retain the above copyright
  624. * notice, this list of conditions and the following disclaimer.
  625. * 2. Redistributions in binary form must reproduce the above copyright
  626. * notice, this list of conditions and the following disclaimer in the
  627. * documentation and/or other materials provided with the distribution.
  628. * 3. All advertising materials mentioning features or use of this software
  629. * must display the following acknowledgement:
  630. * This product includes software developed by the University of
  631. * California, Berkeley and its contributors.
  632. * 4. Neither the name of the University nor the names of its contributors
  633. * may be used to endorse or promote products derived from this software
  634. * without specific prior written permission.
  635. *
  636. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  637. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  638. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  639. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  640. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  641. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  642. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  643. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  644. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  645. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  646. * SUCH DAMAGE.
  647. */
  648. #define MAX_FCONVERSION 512 /* largest possible real conversion */
  649. #define MAX_EXPT 5 /* largest possible exponent field */
  650. #define MAX_FRACT 39 /* largest possible fraction field */
  651. #define TESTFLAG(x) 0
  652. typedef double rtype;
  653. extern double modf(double, double *);
  654. #define to_char(n) ((n) + '0')
  655. #define to_digit(c) ((c) - '0')
  656. #define _isNan(arg) ((arg) != (arg))
  657. static int cvt (rtype arg, int prec, char *signp, int fmtch,
  658. char *startp, char *endp, int plussgn);
  659. static char *c_round (double fract, int *exp, char *start, char *end,
  660. char ch, char *signp);
  661. static char *exponent(char *p, int exp, int fmtch);
  662. /*
  663. * _finite arg not Infinity or Nan
  664. */
  665. static int _finite(rtype d)
  666. {
  667. #if ENDIAN == ENDIAN_LITTLE
  668. struct IEEEdp {
  669. unsigned manl:32;
  670. unsigned manh:20;
  671. unsigned exp:11;
  672. unsigned sign:1;
  673. } *ip;
  674. #else
  675. struct IEEEdp {
  676. unsigned sign:1;
  677. unsigned exp:11;
  678. unsigned manh:20;
  679. unsigned manl:32;
  680. } *ip;
  681. #endif
  682. ip = (struct IEEEdp *)&d;
  683. return (ip->exp != 0x7ff);
  684. }
  685. void dtoa (char *dbuf, rtype arg, int fmtch, int width, int prec, int plussgn)
  686. {
  687. char buf[MAX_FCONVERSION+1], *cp;
  688. char sign;
  689. int size;
  690. if( !_finite(arg) ) {
  691. if( _isNan(arg) )
  692. strcpy (dbuf, "NaN");
  693. else if( arg < 0)
  694. strcpy (dbuf, "-Infinity");
  695. else
  696. strcpy (dbuf, "Infinity");
  697. return;
  698. }
  699. if (prec > MAX_FRACT)
  700. prec = MAX_FRACT;
  701. /* leave room for sign at start of buffer */
  702. cp = buf + 1;
  703. /*
  704. * cvt may have to round up before the "start" of
  705. * its buffer, i.e. ``intf("%.2f", (double)9.999);'';
  706. * if the first character is still NUL, it did.
  707. * softsign avoids negative 0 if _double < 0 but
  708. * no significant digits will be shown.
  709. */
  710. *cp = '\0';
  711. size = cvt (arg, prec, &sign, fmtch, cp, buf + sizeof(buf), plussgn);
  712. if (*cp == '\0')
  713. cp++;
  714. if (sign)
  715. *--cp = sign, size++;
  716. cp[size] = 0;
  717. memcpy (dbuf, cp, size + 1);
  718. }
  719. static int
  720. cvt(rtype number, int prec, char *signp, int fmtch, char *startp, char *endp, int plussgn)
  721. {
  722. register char *p, *t;
  723. register double fract;
  724. double integer, tmp;
  725. int dotrim, expcnt, gformat;
  726. dotrim = expcnt = gformat = 0;
  727. if (number < 0) {
  728. number = -number;
  729. *signp = '-';
  730. } else if (plussgn) {
  731. *signp = '+';
  732. } else
  733. *signp = 0;
  734. fract = modf(number, &integer);
  735. /* get an extra slot for rounding. */
  736. t = ++startp;
  737. /*
  738. * get integer portion of number; put into the end of the buffer; the
  739. * .01 is added for modf(356.0 / 10, &integer) returning .59999999...
  740. */
  741. for (p = endp - 1; integer; ++expcnt) {
  742. tmp = modf(integer / 10, &integer);
  743. *p-- = to_char((int)((tmp + .01) * 10));
  744. }
  745. switch (fmtch) {
  746. case 'f':
  747. /* reverse integer into beginning of buffer */
  748. if (expcnt)
  749. for (; ++p < endp; *t++ = *p);
  750. else
  751. *t++ = '0';
  752. /*
  753. * if precision required or alternate flag set, add in a
  754. * decimal point.
  755. */
  756. if (prec || TESTFLAG(ALTERNATE_FORM))
  757. *t++ = '.';
  758. /* if requires more precision and some fraction left */
  759. if (fract) {
  760. if (prec)
  761. do {
  762. fract = modf(fract * 10, &tmp);
  763. *t++ = to_char((int)tmp);
  764. } while (--prec && fract);
  765. if (fract)
  766. startp = c_round(fract, (int *)NULL, startp,
  767. t - 1, (char)0, signp);
  768. }
  769. for (; prec--; *t++ = '0');
  770. break;
  771. case 'e':
  772. case 'E':
  773. eformat: if (expcnt) {
  774. *t++ = *++p;
  775. if (prec || TESTFLAG(ALTERNATE_FORM))
  776. *t++ = '.';
  777. /* if requires more precision and some integer left */
  778. for (; prec && ++p < endp; --prec)
  779. *t++ = *p;
  780. /*
  781. * if done precision and more of the integer component,
  782. * round using it; adjust fract so we don't re-round
  783. * later.
  784. */
  785. if (!prec && ++p < endp) {
  786. fract = 0;
  787. startp = c_round((double)0, &expcnt, startp,
  788. t - 1, *p, signp);
  789. }
  790. /* adjust expcnt for digit in front of decimal */
  791. --expcnt;
  792. }
  793. /* until first fractional digit, decrement exponent */
  794. else if (fract) {
  795. /* adjust expcnt for digit in front of decimal */
  796. for (expcnt = -1;; --expcnt) {
  797. fract = modf(fract * 10, &tmp);
  798. if (tmp)
  799. break;
  800. }
  801. *t++ = to_char((int)tmp);
  802. if (prec || TESTFLAG(ALTERNATE_FORM))
  803. *t++ = '.';
  804. }
  805. else {
  806. *t++ = '0';
  807. if (prec || TESTFLAG(ALTERNATE_FORM))
  808. *t++ = '.';
  809. }
  810. /* if requires more precision and some fraction left */
  811. if (fract) {
  812. if (prec)
  813. do {
  814. fract = modf(fract * 10, &tmp);
  815. *t++ = to_char((int)tmp);
  816. } while (--prec && fract);
  817. if (fract)
  818. startp = c_round(fract, &expcnt, startp,
  819. t - 1, (char)0, signp);
  820. }
  821. /* if requires more precision */
  822. for (; prec--; *t++ = '0');
  823. /* unless alternate flag, trim any g/G format trailing 0's */
  824. if (gformat && !TESTFLAG(ALTERNATE_FORM)) {
  825. while (t > startp && *--t == '0');
  826. if (*t == '.')
  827. --t;
  828. ++t;
  829. }
  830. t = exponent(t, expcnt, fmtch);
  831. break;
  832. case 'g':
  833. case 'G':
  834. /* a precision of 0 is treated as a precision of 1. */
  835. if (!prec)
  836. ++prec;
  837. /*
  838. * ``The style used depends on the value converted; style e
  839. * will be used only if the exponent resulting from the
  840. * conversion is less than -4 or greater than the precision.''
  841. * -- ANSI X3J11
  842. */
  843. if (expcnt > prec || (!expcnt && fract && fract < .0001)) {
  844. /*
  845. * g/G format counts "significant digits, not digits of
  846. * precision; for the e/E format, this just causes an
  847. * off-by-one problem, i.e. g/G considers the digit
  848. * before the decimal point significant and e/E doesn't
  849. * count it as precision.
  850. */
  851. --prec;
  852. fmtch -= 2; /* G->E, g->e */
  853. gformat = 1;
  854. goto eformat;
  855. }
  856. /*
  857. * reverse integer into beginning of buffer,
  858. * note, decrement precision
  859. */
  860. if (expcnt)
  861. for (; ++p < endp; *t++ = *p, --prec);
  862. else
  863. *t++ = '0';
  864. /*
  865. * if precision required or alternate flag set, add in a
  866. * decimal point. If no digits yet, add in leading 0.
  867. */
  868. if (prec || TESTFLAG(ALTERNATE_FORM)) {
  869. dotrim = 1;
  870. *t++ = '.';
  871. }
  872. else
  873. dotrim = 0;
  874. /* if requires more precision and some fraction left */
  875. if (fract) {
  876. if (prec) {
  877. do {
  878. fract = modf(fract * 10, &tmp);
  879. *t++ = to_char((int)tmp);
  880. } while(!tmp && !expcnt);
  881. while (--prec && fract) {
  882. fract = modf(fract * 10, &tmp);
  883. *t++ = to_char((int)tmp);
  884. }
  885. }
  886. if (fract)
  887. startp = c_round(fract, (int *)NULL, startp,
  888. t - 1, (char)0, signp);
  889. }
  890. /* alternate format, adds 0's for precision, else trim 0's */
  891. if (TESTFLAG(ALTERNATE_FORM))
  892. for (; prec--; *t++ = '0');
  893. else if (dotrim) {
  894. while (t > startp && *--t == '0');
  895. if (*t != '.')
  896. ++t;
  897. }
  898. }
  899. return (t - startp);
  900. }
  901. static char *
  902. c_round(double fract, int *exp, char *start, char *end, char ch, char *signp)
  903. {
  904. double tmp;
  905. if (fract)
  906. (void)modf(fract * 10, &tmp);
  907. else
  908. tmp = to_digit(ch);
  909. if (tmp > 4)
  910. for (;; --end) {
  911. if (*end == '.')
  912. --end;
  913. if (++*end <= '9')
  914. break;
  915. *end = '0';
  916. if (end == start) {
  917. if (exp) { /* e/E; increment exponent */
  918. *end = '1';
  919. ++*exp;
  920. }
  921. else { /* f; add extra digit */
  922. *--end = '1';
  923. --start;
  924. }
  925. break;
  926. }
  927. }
  928. /* ``"%.3f", (double)-0.0004'' gives you a negative 0. */
  929. else if (*signp == '-' || *signp == '+')
  930. for (;; --end) {
  931. if (*end == '.')
  932. --end;
  933. if (*end != '0')
  934. break;
  935. if (end == start)
  936. *signp = 0;
  937. }
  938. return (start);
  939. }
  940. static char *
  941. exponent(char *p, int exp, int fmtch)
  942. {
  943. char *t;
  944. char expbuf[MAX_FCONVERSION];
  945. *p++ = fmtch;
  946. if (exp < 0) {
  947. exp = -exp;
  948. *p++ = '-';
  949. }
  950. else
  951. *p++ = '+';
  952. t = expbuf + MAX_FCONVERSION;
  953. if (exp > 9) {
  954. do {
  955. *--t = to_char(exp % 10);
  956. } while ((exp /= 10) > 9);
  957. *--t = to_char(exp);
  958. for (; t < expbuf + MAX_FCONVERSION; *p++ = *t++);
  959. }
  960. else {
  961. *p++ = '0';
  962. *p++ = to_char(exp);
  963. }
  964. return (p);
  965. }
  966. int sprintf(char *s, const char *fmt, ...)
  967. {
  968. int n;
  969. va_list arg;
  970. va_start(arg, fmt);
  971. n = vsprintf(s, fmt, arg);
  972. va_end(arg);
  973. return n;
  974. }
  975. #endif /* FLOATINGPT */
  976. #endif