stdio.c 31 KB

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