1
0

vfprintf.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. /*
  2. * pANS stdio -- vfprintf
  3. */
  4. #include "iolib.h"
  5. /*
  6. * Leading flags
  7. */
  8. #define SPACE 1 /* ' ' prepend space if no sign printed */
  9. #define ALT 2 /* '#' use alternate conversion */
  10. #define SIGN 4 /* '+' prepend sign, even if positive */
  11. #define LEFT 8 /* '-' left-justify */
  12. #define ZPAD 16 /* '0' zero-pad */
  13. /*
  14. * Trailing flags
  15. */
  16. #define SHORT 32 /* 'h' convert a short integer */
  17. #define LONG 64 /* 'l' convert a long integer */
  18. #define LDBL 128 /* 'L' convert a long double */
  19. #define PTR 256 /* convert a void * (%p) */
  20. static int lflag[] = { /* leading flags */
  21. 0, 0, 0, 0, 0, 0, 0, 0, /* ^@ ^A ^B ^C ^D ^E ^F ^G */
  22. 0, 0, 0, 0, 0, 0, 0, 0, /* ^H ^I ^J ^K ^L ^M ^N ^O */
  23. 0, 0, 0, 0, 0, 0, 0, 0, /* ^P ^Q ^R ^S ^T ^U ^V ^W */
  24. 0, 0, 0, 0, 0, 0, 0, 0, /* ^X ^Y ^Z ^[ ^\ ^] ^^ ^_ */
  25. SPACE, 0, 0, ALT, 0, 0, 0, 0, /* sp ! " # $ % & ' */
  26. 0, 0, 0, SIGN, 0, LEFT, 0, 0, /* ( ) * + , - . / */
  27. ZPAD, 0, 0, 0, 0, 0, 0, 0, /* 0 1 2 3 4 5 6 7 */
  28. 0, 0, 0, 0, 0, 0, 0, 0, /* 8 9 : ; < = > ? */
  29. 0, 0, 0, 0, 0, 0, 0, 0, /* @ A B C D E F G */
  30. 0, 0, 0, 0, 0, 0, 0, 0, /* H I J K L M N O */
  31. 0, 0, 0, 0, 0, 0, 0, 0, /* P Q R S T U V W */
  32. 0, 0, 0, 0, 0, 0, 0, 0, /* X Y Z [ \ ] ^ _ */
  33. 0, 0, 0, 0, 0, 0, 0, 0, /* ` a b c d e f g */
  34. 0, 0, 0, 0, 0, 0, 0, 0, /* h i j k l m n o */
  35. 0, 0, 0, 0, 0, 0, 0, 0, /* p q r s t u v w */
  36. 0, 0, 0, 0, 0, 0, 0, 0, /* x y z { | } ~ ^? */
  37. 0, 0, 0, 0, 0, 0, 0, 0,
  38. 0, 0, 0, 0, 0, 0, 0, 0,
  39. 0, 0, 0, 0, 0, 0, 0, 0,
  40. 0, 0, 0, 0, 0, 0, 0, 0,
  41. 0, 0, 0, 0, 0, 0, 0, 0,
  42. 0, 0, 0, 0, 0, 0, 0, 0,
  43. 0, 0, 0, 0, 0, 0, 0, 0,
  44. 0, 0, 0, 0, 0, 0, 0, 0,
  45. 0, 0, 0, 0, 0, 0, 0, 0,
  46. 0, 0, 0, 0, 0, 0, 0, 0,
  47. 0, 0, 0, 0, 0, 0, 0, 0,
  48. 0, 0, 0, 0, 0, 0, 0, 0,
  49. 0, 0, 0, 0, 0, 0, 0, 0,
  50. 0, 0, 0, 0, 0, 0, 0, 0,
  51. 0, 0, 0, 0, 0, 0, 0, 0,
  52. 0, 0, 0, 0, 0, 0, 0, 0,
  53. };
  54. static int tflag[] = { /* trailing flags */
  55. 0, 0, 0, 0, 0, 0, 0, 0, /* ^@ ^A ^B ^C ^D ^E ^F ^G */
  56. 0, 0, 0, 0, 0, 0, 0, 0, /* ^H ^I ^J ^K ^L ^M ^N ^O */
  57. 0, 0, 0, 0, 0, 0, 0, 0, /* ^P ^Q ^R ^S ^T ^U ^V ^W */
  58. 0, 0, 0, 0, 0, 0, 0, 0, /* ^X ^Y ^Z ^[ ^\ ^] ^^ ^_ */
  59. 0, 0, 0, 0, 0, 0, 0, 0, /* sp ! " # $ % & ' */
  60. 0, 0, 0, 0, 0, 0, 0, 0, /* ( ) * + , - . / */
  61. 0, 0, 0, 0, 0, 0, 0, 0, /* 0 1 2 3 4 5 6 7 */
  62. 0, 0, 0, 0, 0, 0, 0, 0, /* 8 9 : ; < = > ? */
  63. 0, 0, 0, 0, 0, 0, 0, 0, /* @ A B C D E F G */
  64. 0, 0, 0, 0, LDBL, 0, 0, 0, /* H I J K L M N O */
  65. 0, 0, 0, 0, 0, 0, 0, 0, /* P Q R S T U V W */
  66. 0, 0, 0, 0, 0, 0, 0, 0, /* X Y Z [ \ ] ^ _ */
  67. 0, 0, 0, 0, 0, 0, 0, 0, /* ` a b c d e f g */
  68. SHORT, 0, 0, 0, LONG, 0, 0, 0, /* h i j k l m n o */
  69. 0, 0, 0, 0, 0, 0, 0, 0, /* p q r s t u v w */
  70. 0, 0, 0, 0, 0, 0, 0, 0, /* x y z { | } ~ ^? */
  71. 0, 0, 0, 0, 0, 0, 0, 0,
  72. 0, 0, 0, 0, 0, 0, 0, 0,
  73. 0, 0, 0, 0, 0, 0, 0, 0,
  74. 0, 0, 0, 0, 0, 0, 0, 0,
  75. 0, 0, 0, 0, 0, 0, 0, 0,
  76. 0, 0, 0, 0, 0, 0, 0, 0,
  77. 0, 0, 0, 0, 0, 0, 0, 0,
  78. 0, 0, 0, 0, 0, 0, 0, 0,
  79. 0, 0, 0, 0, 0, 0, 0, 0,
  80. 0, 0, 0, 0, 0, 0, 0, 0,
  81. 0, 0, 0, 0, 0, 0, 0, 0,
  82. 0, 0, 0, 0, 0, 0, 0, 0,
  83. 0, 0, 0, 0, 0, 0, 0, 0,
  84. 0, 0, 0, 0, 0, 0, 0, 0,
  85. 0, 0, 0, 0, 0, 0, 0, 0,
  86. 0, 0, 0, 0, 0, 0, 0, 0,
  87. };
  88. static int ocvt_E(FILE *, va_list *, int, int, int);
  89. static int ocvt_G(FILE *, va_list *, int, int, int);
  90. static int ocvt_X(FILE *, va_list *, int, int, int);
  91. static int ocvt_c(FILE *, va_list *, int, int, int);
  92. static int ocvt_d(FILE *, va_list *, int, int, int);
  93. static int ocvt_e(FILE *, va_list *, int, int, int);
  94. static int ocvt_f(FILE *, va_list *, int, int, int);
  95. static int ocvt_g(FILE *, va_list *, int, int, int);
  96. static int ocvt_n(FILE *, va_list *, int, int, int);
  97. static int ocvt_o(FILE *, va_list *, int, int, int);
  98. static int ocvt_p(FILE *, va_list *, int, int, int);
  99. static int ocvt_s(FILE *, va_list *, int, int, int);
  100. static int ocvt_u(FILE *, va_list *, int, int, int);
  101. static int ocvt_x(FILE *, va_list *, int, int, int);
  102. static int(*ocvt[])(FILE *, va_list *, int, int, int) = {
  103. 0, 0, 0, 0, 0, 0, 0, 0, /* ^@ ^A ^B ^C ^D ^E ^F ^G */
  104. 0, 0, 0, 0, 0, 0, 0, 0, /* ^H ^I ^J ^K ^L ^M ^N ^O */
  105. 0, 0, 0, 0, 0, 0, 0, 0, /* ^P ^Q ^R ^S ^T ^U ^V ^W */
  106. 0, 0, 0, 0, 0, 0, 0, 0, /* ^X ^Y ^Z ^[ ^\ ^] ^^ ^_ */
  107. 0, 0, 0, 0, 0, 0, 0, 0, /* sp ! " # $ % & ' */
  108. 0, 0, 0, 0, 0, 0, 0, 0, /* ( ) * + , - . / */
  109. 0, 0, 0, 0, 0, 0, 0, 0, /* 0 1 2 3 4 5 6 7 */
  110. 0, 0, 0, 0, 0, 0, 0, 0, /* 8 9 : ; < = > ? */
  111. 0, 0, 0, 0, 0, ocvt_E, 0, ocvt_G, /* @ A B C D E F G */
  112. 0, 0, 0, 0, 0, 0, 0, 0, /* H I J K L M N O */
  113. 0, 0, 0, 0, 0, 0, 0, 0, /* P Q R S T U V W */
  114. ocvt_X, 0, 0, 0, 0, 0, 0, 0, /* X Y Z [ \ ] ^ _ */
  115. 0, 0, 0, ocvt_c, ocvt_d, ocvt_e, ocvt_f, ocvt_g, /* ` a b c d e f g */
  116. 0, ocvt_d, 0, 0, 0, 0, ocvt_n, ocvt_o, /* h i j k l m n o */
  117. ocvt_p, 0, 0, ocvt_s, 0, ocvt_u, 0, 0, /* p q r s t u v w */
  118. ocvt_x, 0, 0, 0, 0, 0, 0, 0, /* x y z { | } ~ ^? */
  119. 0, 0, 0, 0, 0, 0, 0, 0,
  120. 0, 0, 0, 0, 0, 0, 0, 0,
  121. 0, 0, 0, 0, 0, 0, 0, 0,
  122. 0, 0, 0, 0, 0, 0, 0, 0,
  123. 0, 0, 0, 0, 0, 0, 0, 0,
  124. 0, 0, 0, 0, 0, 0, 0, 0,
  125. 0, 0, 0, 0, 0, 0, 0, 0,
  126. 0, 0, 0, 0, 0, 0, 0, 0,
  127. 0, 0, 0, 0, 0, 0, 0, 0,
  128. 0, 0, 0, 0, 0, 0, 0, 0,
  129. 0, 0, 0, 0, 0, 0, 0, 0,
  130. 0, 0, 0, 0, 0, 0, 0, 0,
  131. 0, 0, 0, 0, 0, 0, 0, 0,
  132. 0, 0, 0, 0, 0, 0, 0, 0,
  133. 0, 0, 0, 0, 0, 0, 0, 0,
  134. 0, 0, 0, 0, 0, 0, 0, 0,
  135. };
  136. static int nprint;
  137. QLock _stdiolk;
  138. int
  139. vfprintf(FILE *f, const char *s, va_list args)
  140. {
  141. int flags, width, precision;
  142. qlock(&_stdiolk);
  143. nprint = 0;
  144. while(*s){
  145. if(*s != '%'){
  146. putc(*s++, f);
  147. nprint++;
  148. continue;
  149. }
  150. s++;
  151. flags = 0;
  152. while(lflag[*s&_IO_CHMASK]) flags |= lflag[*s++&_IO_CHMASK];
  153. if(*s == '*'){
  154. width = va_arg(args, int);
  155. s++;
  156. if(width<0){
  157. flags |= LEFT;
  158. width = -width;
  159. }
  160. }
  161. else{
  162. width = 0;
  163. while('0'<=*s && *s<='9') width = width*10 + *s++ - '0';
  164. }
  165. if(*s == '.'){
  166. s++;
  167. if(*s == '*'){
  168. precision = va_arg(args, int);
  169. s++;
  170. }
  171. else{
  172. precision = 0;
  173. while('0'<=*s && *s<='9') precision = precision*10 + *s++ - '0';
  174. }
  175. }
  176. else
  177. precision = -1;
  178. while(tflag[*s&_IO_CHMASK]) flags |= tflag[*s++&_IO_CHMASK];
  179. if(ocvt[*s]) nprint += (*ocvt[*s++])(f, &args, flags, width, precision);
  180. else if(*s){
  181. putc(*s++, f);
  182. nprint++;
  183. }
  184. }
  185. qunlock(&_stdiolk);
  186. if(ferror(f)){
  187. if((f->flags&STRING) && f->wp==f->rp && f->wp>f->buf){
  188. *(f->wp-1) = '\0';
  189. return nprint;
  190. }
  191. return -1;
  192. }
  193. return nprint;
  194. }
  195. static int
  196. ocvt_c(FILE *f, va_list *args, int flags, int width, int precision)
  197. {
  198. #pragma ref precision
  199. int i;
  200. if(!(flags&LEFT)) for(i=1; i<width; i++) putc(' ', f);
  201. putc((unsigned char)va_arg(*args, int), f);
  202. if(flags&LEFT) for(i=1; i<width; i++) putc(' ', f);
  203. return width<1 ? 1 : width;
  204. }
  205. static int
  206. ocvt_s(FILE *f, va_list *args, int flags, int width, int precision)
  207. {
  208. int i, n = 0;
  209. char *s;
  210. s = va_arg(*args, char *);
  211. if(!(flags&LEFT)){
  212. if(precision >= 0)
  213. for(i=0; i!=precision && s[i]; i++);
  214. else
  215. for(i=0; s[i]; i++);
  216. for(; i<width; i++){
  217. putc(' ', f);
  218. n++;
  219. }
  220. }
  221. if(precision >= 0){
  222. for(i=0; i!=precision && *s; i++){
  223. putc(*s++, f);
  224. n++;
  225. }
  226. } else{
  227. for(i=0;*s;i++){
  228. putc(*s++, f);
  229. n++;
  230. }
  231. }
  232. if(flags&LEFT){
  233. for(; i<width; i++){
  234. putc(' ', f);
  235. n++;
  236. }
  237. }
  238. return n;
  239. }
  240. static int
  241. ocvt_n(FILE *f, va_list *args, int flags, int width, int precision)
  242. {
  243. #pragma ref f
  244. #pragma ref width
  245. #pragma ref precision
  246. if(flags&SHORT)
  247. *va_arg(*args, short *) = nprint;
  248. else if(flags&LONG)
  249. *va_arg(*args, long *) = nprint;
  250. else
  251. *va_arg(*args, int *) = nprint;
  252. return 0;
  253. }
  254. /*
  255. * Generic fixed-point conversion
  256. * f is the output FILE *;
  257. * args is the va_list * from which to get the number;
  258. * flags, width and precision are the results of printf-cracking;
  259. * radix is the number base to print in;
  260. * alphabet is the set of digits to use;
  261. * prefix is the prefix to print before non-zero numbers when
  262. * using ``alternate form.''
  263. */
  264. static int
  265. ocvt_fixed(FILE *f, va_list *args, int flags, int width, int precision,
  266. int radix, int sgned, char alphabet[], char *prefix)
  267. {
  268. char digits[128]; /* no reasonable machine will ever overflow this */
  269. char *sign;
  270. char *dp;
  271. long snum;
  272. unsigned long num;
  273. int nout, npad, nlzero;
  274. if(sgned){
  275. if(flags&PTR) snum = (long)va_arg(*args, void *);
  276. else if(flags&SHORT) snum = va_arg(*args, short);
  277. else if(flags&LONG) snum = va_arg(*args, long);
  278. else snum = va_arg(*args, int);
  279. if(snum < 0){
  280. sign = "-";
  281. num = -snum;
  282. } else{
  283. if(flags&SIGN) sign = "+";
  284. else if(flags&SPACE) sign = " ";
  285. else sign = "";
  286. num = snum;
  287. }
  288. } else {
  289. sign = "";
  290. if(flags&PTR) num = (long)va_arg(*args, void *);
  291. else if(flags&SHORT) num = va_arg(*args, unsigned short);
  292. else if(flags&LONG) num = va_arg(*args, unsigned long);
  293. else num = va_arg(*args, unsigned int);
  294. }
  295. if(num == 0) prefix = "";
  296. dp = digits;
  297. do{
  298. *dp++ = alphabet[num%radix];
  299. num /= radix;
  300. }while(num);
  301. if(precision==0 && dp-digits==1 && dp[-1]=='0')
  302. dp--;
  303. nlzero = precision-(dp-digits);
  304. if(nlzero < 0) nlzero = 0;
  305. if(flags&ALT){
  306. if(radix == 8) if(dp[-1]=='0' || nlzero) prefix = "";
  307. }
  308. else prefix = "";
  309. nout = dp-digits+nlzero+strlen(prefix)+strlen(sign);
  310. npad = width-nout;
  311. if(npad < 0) npad = 0;
  312. nout += npad;
  313. if(!(flags&LEFT)){
  314. if(flags&ZPAD && precision <= 0){
  315. fputs(sign, f);
  316. fputs(prefix, f);
  317. while(npad){
  318. putc('0', f);
  319. --npad;
  320. }
  321. } else{
  322. while(npad){
  323. putc(' ', f);
  324. --npad;
  325. }
  326. fputs(sign, f);
  327. fputs(prefix, f);
  328. }
  329. while(nlzero){
  330. putc('0', f);
  331. --nlzero;
  332. }
  333. while(dp!=digits) putc(*--dp, f);
  334. }
  335. else{
  336. fputs(sign, f);
  337. fputs(prefix, f);
  338. while(nlzero){
  339. putc('0', f);
  340. --nlzero;
  341. }
  342. while(dp != digits) putc(*--dp, f);
  343. while(npad){
  344. putc(' ', f);
  345. --npad;
  346. }
  347. }
  348. return nout;
  349. }
  350. static int
  351. ocvt_X(FILE *f, va_list *args, int flags, int width, int precision)
  352. {
  353. return ocvt_fixed(f, args, flags, width, precision, 16, 0, "0123456789ABCDEF", "0X");
  354. }
  355. static int
  356. ocvt_d(FILE *f, va_list *args, int flags, int width, int precision)
  357. {
  358. return ocvt_fixed(f, args, flags, width, precision, 10, 1, "0123456789", "");
  359. }
  360. static int
  361. ocvt_o(FILE *f, va_list *args, int flags, int width, int precision)
  362. {
  363. return ocvt_fixed(f, args, flags, width, precision, 8, 0, "01234567", "0");
  364. }
  365. static int
  366. ocvt_p(FILE *f, va_list *args, int flags, int width, int precision)
  367. {
  368. return ocvt_fixed(f, args, flags|PTR|ALT, width, precision, 16, 0,
  369. "0123456789ABCDEF", "0X");
  370. }
  371. static int
  372. ocvt_u(FILE *f, va_list *args, int flags, int width, int precision)
  373. {
  374. return ocvt_fixed(f, args, flags, width, precision, 10, 0, "0123456789", "");
  375. }
  376. static int
  377. ocvt_x(FILE *f, va_list *args, int flags, int width, int precision)
  378. {
  379. return ocvt_fixed(f, args, flags, width, precision, 16, 0, "0123456789abcdef", "0x");
  380. }
  381. static int ocvt_flt(FILE *, va_list *, int, int, int, char);
  382. static int
  383. ocvt_E(FILE *f, va_list *args, int flags, int width, int precision)
  384. {
  385. return ocvt_flt(f, args, flags, width, precision, 'E');
  386. }
  387. static int
  388. ocvt_G(FILE *f, va_list *args, int flags, int width, int precision)
  389. {
  390. return ocvt_flt(f, args, flags, width, precision, 'G');
  391. }
  392. static int
  393. ocvt_e(FILE *f, va_list *args, int flags, int width, int precision)
  394. {
  395. return ocvt_flt(f, args, flags, width, precision, 'e');
  396. }
  397. static int
  398. ocvt_f(FILE *f, va_list *args, int flags, int width, int precision)
  399. {
  400. return ocvt_flt(f, args, flags, width, precision, 'f');
  401. }
  402. static int
  403. ocvt_g(FILE *f, va_list *args, int flags, int width, int precision)
  404. {
  405. return ocvt_flt(f, args, flags, width, precision, 'g');
  406. }
  407. static int
  408. ocvt_flt(FILE *f, va_list *args, int flags, int width, int precision, char afmt)
  409. {
  410. int echr;
  411. char *digits, *edigits;
  412. int exponent;
  413. char fmt;
  414. int sign;
  415. int ndig;
  416. int nout, i;
  417. char ebuf[20]; /* no sensible machine will overflow this */
  418. char *eptr;
  419. double d;
  420. echr = 'e';
  421. fmt = afmt;
  422. d = va_arg(*args, double);
  423. if(precision < 0) precision = 6;
  424. switch(fmt){
  425. case 'f':
  426. digits = dtoa(d, 3, precision, &exponent, &sign, &edigits);
  427. break;
  428. case 'E':
  429. echr = 'E';
  430. fmt = 'e';
  431. /* fall through */
  432. case 'e':
  433. digits = dtoa(d, 2, 1+precision, &exponent, &sign, &edigits);
  434. break;
  435. case 'G':
  436. echr = 'E';
  437. /* fall through */
  438. case 'g':
  439. if (precision > 0)
  440. digits = dtoa(d, 2, precision, &exponent, &sign, &edigits);
  441. else {
  442. digits = dtoa(d, 0, precision, &exponent, &sign, &edigits);
  443. precision = edigits - digits;
  444. if (exponent > precision && exponent <= precision + 4)
  445. precision = exponent;
  446. }
  447. if(exponent >= -3 && exponent <= precision){
  448. fmt = 'f';
  449. precision -= exponent;
  450. }else{
  451. fmt = 'e';
  452. --precision;
  453. }
  454. break;
  455. }
  456. if (exponent == 9999) {
  457. /* Infinity or Nan */
  458. precision = 0;
  459. exponent = edigits - digits;
  460. fmt = 'f';
  461. }
  462. ndig = edigits-digits;
  463. if((afmt=='g' || afmt=='G') && !(flags&ALT)){ /* knock off trailing zeros */
  464. if(fmt == 'f'){
  465. if(precision+exponent > ndig) {
  466. precision = ndig - exponent;
  467. if(precision < 0)
  468. precision = 0;
  469. }
  470. }
  471. else{
  472. if(precision > ndig-1) precision = ndig-1;
  473. }
  474. }
  475. nout = precision; /* digits after decimal point */
  476. if(precision!=0 || flags&ALT) nout++; /* decimal point */
  477. if(fmt=='f' && exponent>0) nout += exponent; /* digits before decimal point */
  478. else nout++; /* there's always at least one */
  479. if(sign || flags&(SPACE|SIGN)) nout++; /* sign */
  480. if(fmt != 'f'){ /* exponent */
  481. eptr = ebuf;
  482. for(i=exponent<=0?1-exponent:exponent-1; i; i/=10)
  483. *eptr++ = '0' + i%10;
  484. while(eptr<ebuf+2) *eptr++ = '0';
  485. nout += eptr-ebuf+2; /* e+99 */
  486. }
  487. if(!(flags&ZPAD) && !(flags&LEFT))
  488. while(nout < width){
  489. putc(' ', f);
  490. nout++;
  491. }
  492. if(sign) putc('-', f);
  493. else if(flags&SIGN) putc('+', f);
  494. else if(flags&SPACE) putc(' ', f);
  495. if(flags&ZPAD)
  496. while(nout < width){
  497. putc('0', f);
  498. nout++;
  499. }
  500. if(fmt == 'f'){
  501. for(i=0; i<exponent; i++) putc(i<ndig?digits[i]:'0', f);
  502. if(i == 0) putc('0', f);
  503. if(precision>0 || flags&ALT) putc('.', f);
  504. for(i=0; i!=precision; i++)
  505. putc(0<=i+exponent && i+exponent<ndig?digits[i+exponent]:'0', f);
  506. }
  507. else{
  508. putc(digits[0], f);
  509. if(precision>0 || flags&ALT) putc('.', f);
  510. for(i=0; i!=precision; i++) putc(i<ndig-1?digits[i+1]:'0', f);
  511. }
  512. if(fmt != 'f'){
  513. putc(echr, f);
  514. putc(exponent<=0?'-':'+', f);
  515. while(eptr>ebuf) putc(*--eptr, f);
  516. }
  517. while(nout < width){
  518. putc(' ', f);
  519. nout++;
  520. }
  521. freedtoa(digits);
  522. return nout;
  523. }