vfprintf.c 14 KB

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