vfprintf.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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. va_list arg;
  151. qlock(&_stdiolk);
  152. va_copy(arg, args);
  153. nprint = 0;
  154. while(*s){
  155. if(*s != '%'){
  156. putc(*s++, f);
  157. nprint++;
  158. continue;
  159. }
  160. s++;
  161. flags = 0;
  162. while(lflag[*s&_IO_CHMASK]) flags |= lflag[*s++&_IO_CHMASK];
  163. if(*s == '*'){
  164. width = va_arg(arg, int);
  165. s++;
  166. if(width<0){
  167. flags |= LEFT;
  168. width = -width;
  169. }
  170. }
  171. else{
  172. width = 0;
  173. while('0'<=*s && *s<='9') width = width*10 + *s++ - '0';
  174. }
  175. if(*s == '.'){
  176. s++;
  177. if(*s == '*'){
  178. precision = va_arg(arg, int);
  179. s++;
  180. }
  181. else{
  182. precision = 0;
  183. while('0'<=*s && *s<='9') precision = precision*10 + *s++ - '0';
  184. }
  185. }
  186. else
  187. precision = -1;
  188. while(tflag[*s&_IO_CHMASK]) flags |= tflag[*s++&_IO_CHMASK];
  189. if(ocvt[(uint8_t)(*s)])
  190. nprint += (*ocvt[(uint8_t)(*s++)])(f, &arg, flags, width, precision);
  191. else if(*s){
  192. putc(*s++, f);
  193. nprint++;
  194. }
  195. }
  196. va_end(arg);
  197. qunlock(&_stdiolk);
  198. if(ferror(f)){
  199. if((f->flags&STRING) && f->wp==f->rp && f->wp>f->buf){
  200. *(f->wp-1) = '\0';
  201. return nprint;
  202. }
  203. return -1;
  204. }
  205. return nprint;
  206. }
  207. static int
  208. ocvt_c(FILE *f, va_list *args, int flags, int width, int precision)
  209. {
  210. //#pragma ref precision
  211. int i;
  212. if(!(flags&LEFT)) for(i=1; i<width; i++) putc(' ', f);
  213. putc((unsigned char)va_arg(*args, int), f);
  214. if(flags&LEFT) for(i=1; i<width; i++) putc(' ', f);
  215. return width<1 ? 1 : width;
  216. }
  217. static int
  218. ocvt_s(FILE *f, va_list *args, int flags, int width, int precision)
  219. {
  220. int i, n = 0;
  221. char *s;
  222. s = va_arg(*args, char *);
  223. if(!(flags&LEFT)){
  224. if(precision >= 0)
  225. for(i=0; i!=precision && s[i]; i++);
  226. else
  227. for(i=0; s[i]; i++);
  228. for(; i<width; i++){
  229. putc(' ', f);
  230. n++;
  231. }
  232. }
  233. if(precision >= 0){
  234. for(i=0; i!=precision && *s; i++){
  235. putc(*s++, f);
  236. n++;
  237. }
  238. } else{
  239. for(i=0;*s;i++){
  240. putc(*s++, f);
  241. n++;
  242. }
  243. }
  244. if(flags&LEFT){
  245. for(; i<width; i++){
  246. putc(' ', f);
  247. n++;
  248. }
  249. }
  250. return n;
  251. }
  252. static int
  253. ocvt_n(FILE *f, va_list *args, int flags, int width, int precision)
  254. {
  255. //#pragma ref f
  256. //#pragma ref width
  257. //#pragma ref precision
  258. if(flags&SHORT)
  259. *va_arg(*args, int16_t *) = nprint;
  260. else if(flags&LONG)
  261. *va_arg(*args, int32_t *) = nprint;
  262. else
  263. *va_arg(*args, int *) = nprint;
  264. return 0;
  265. }
  266. /*
  267. * Generic fixed-point conversion
  268. * f is the output FILE *;
  269. * args is the va_list * from which to get the number;
  270. * flags, width and precision are the results of printf-cracking;
  271. * radix is the number base to print in;
  272. * alphabet is the set of digits to use;
  273. * prefix is the prefix to print before non-zero numbers when
  274. * using ``alternate form.''
  275. */
  276. static int
  277. ocvt_fixed(FILE *f, va_list *args, int flags, int width, int precision,
  278. int radix, int sgned, char alphabet[], char *prefix)
  279. {
  280. char digits[128]; /* no reasonable machine will ever overflow this */
  281. char *sign;
  282. char *dp;
  283. int64_t snum;
  284. unsigned long num;
  285. int nout, npad, nlzero;
  286. if(sgned){
  287. if(flags&PTR) snum = (uintptr_t)va_arg(*args, void *);
  288. else if(flags&SHORT) snum = va_arg(*args, int);
  289. else if(flags&LONG) snum = va_arg(*args, int32_t);
  290. else snum = va_arg(*args, int);
  291. if(snum < 0){
  292. sign = "-";
  293. num = -snum;
  294. } else{
  295. if(flags&SIGN) sign = "+";
  296. else if(flags&SPACE) sign = " ";
  297. else sign = "";
  298. num = snum;
  299. }
  300. } else {
  301. sign = "";
  302. if(flags&PTR) num = (uintptr_t)va_arg(*args, void *);
  303. else if(flags&SHORT) num = va_arg(*args, int);
  304. else if(flags&LONG) num = va_arg(*args, unsigned long);
  305. else num = va_arg(*args, unsigned int);
  306. }
  307. if(num == 0) prefix = "";
  308. dp = digits;
  309. do{
  310. *dp++ = alphabet[num%radix];
  311. num /= radix;
  312. }while(num);
  313. if(precision==0 && dp-digits==1 && dp[-1]=='0')
  314. dp--;
  315. nlzero = precision-(dp-digits);
  316. if(nlzero < 0) nlzero = 0;
  317. if(flags&ALT){
  318. if(radix == 8) if(dp[-1]=='0' || nlzero) prefix = "";
  319. }
  320. else prefix = "";
  321. nout = dp-digits+nlzero+strlen(prefix)+strlen(sign);
  322. npad = width-nout;
  323. if(npad < 0) npad = 0;
  324. nout += npad;
  325. if(!(flags&LEFT)){
  326. if(flags&ZPAD && precision <= 0){
  327. fputs(sign, f);
  328. fputs(prefix, f);
  329. while(npad){
  330. putc('0', f);
  331. --npad;
  332. }
  333. } else{
  334. while(npad){
  335. putc(' ', f);
  336. --npad;
  337. }
  338. fputs(sign, f);
  339. fputs(prefix, f);
  340. }
  341. while(nlzero){
  342. putc('0', f);
  343. --nlzero;
  344. }
  345. while(dp!=digits) putc(*--dp, f);
  346. }
  347. else{
  348. fputs(sign, f);
  349. fputs(prefix, f);
  350. while(nlzero){
  351. putc('0', f);
  352. --nlzero;
  353. }
  354. while(dp != digits) putc(*--dp, f);
  355. while(npad){
  356. putc(' ', f);
  357. --npad;
  358. }
  359. }
  360. return nout;
  361. }
  362. static int
  363. ocvt_X(FILE *f, va_list *args, int flags, int width, int precision)
  364. {
  365. return ocvt_fixed(f, args, flags, width, precision, 16, 0, "0123456789ABCDEF", "0X");
  366. }
  367. static int
  368. ocvt_d(FILE *f, va_list *args, int flags, int width, int precision)
  369. {
  370. return ocvt_fixed(f, args, flags, width, precision, 10, 1, "0123456789", "");
  371. }
  372. static int
  373. ocvt_o(FILE *f, va_list *args, int flags, int width, int precision)
  374. {
  375. return ocvt_fixed(f, args, flags, width, precision, 8, 0, "01234567", "0");
  376. }
  377. static int
  378. ocvt_p(FILE *f, va_list *args, int flags, int width, int precision)
  379. {
  380. return ocvt_fixed(f, args, flags|PTR|ALT, width, precision, 16, 0,
  381. "0123456789ABCDEF", "0X");
  382. }
  383. static int
  384. ocvt_u(FILE *f, va_list *args, int flags, int width, int precision)
  385. {
  386. return ocvt_fixed(f, args, flags, width, precision, 10, 0, "0123456789", "");
  387. }
  388. static int
  389. ocvt_x(FILE *f, va_list *args, int flags, int width, int precision)
  390. {
  391. return ocvt_fixed(f, args, flags, width, precision, 16, 0, "0123456789abcdef", "0x");
  392. }
  393. static int ocvt_flt(FILE *, va_list *, int, int, int, char);
  394. static int
  395. ocvt_E(FILE *f, va_list *args, int flags, int width, int precision)
  396. {
  397. return ocvt_flt(f, args, flags, width, precision, 'E');
  398. }
  399. static int
  400. ocvt_G(FILE *f, va_list *args, int flags, int width, int precision)
  401. {
  402. return ocvt_flt(f, args, flags, width, precision, 'G');
  403. }
  404. static int
  405. ocvt_e(FILE *f, va_list *args, int flags, int width, int precision)
  406. {
  407. return ocvt_flt(f, args, flags, width, precision, 'e');
  408. }
  409. static int
  410. ocvt_f(FILE *f, va_list *args, int flags, int width, int precision)
  411. {
  412. return ocvt_flt(f, args, flags, width, precision, 'f');
  413. }
  414. static int
  415. ocvt_g(FILE *f, va_list *args, int flags, int width, int precision)
  416. {
  417. return ocvt_flt(f, args, flags, width, precision, 'g');
  418. }
  419. static int
  420. ocvt_flt(FILE *f, va_list *args, int flags, int width, int precision,
  421. char afmt)
  422. {
  423. int echr;
  424. char *digits, *edigits;
  425. int exponent;
  426. char fmt;
  427. int sign;
  428. int ndig;
  429. int nout, i;
  430. char ebuf[20]; /* no sensible machine will overflow this */
  431. char *eptr;
  432. double d;
  433. echr = 'e';
  434. fmt = afmt;
  435. d = va_arg(*args, double);
  436. if(precision < 0) precision = 6;
  437. switch(fmt){
  438. case 'f':
  439. digits = dtoa(d, 3, precision, &exponent, &sign, &edigits);
  440. break;
  441. case 'E':
  442. echr = 'E';
  443. fmt = 'e';
  444. /* fall through */
  445. case 'e':
  446. digits = dtoa(d, 2, 1+precision, &exponent, &sign, &edigits);
  447. break;
  448. case 'G':
  449. echr = 'E';
  450. /* fall through */
  451. case 'g':
  452. if (precision > 0)
  453. digits = dtoa(d, 2, precision, &exponent, &sign, &edigits);
  454. else {
  455. digits = dtoa(d, 0, precision, &exponent, &sign, &edigits);
  456. precision = edigits - digits;
  457. if (exponent > precision && exponent <= precision + 4)
  458. precision = exponent;
  459. }
  460. if(exponent >= -3 && exponent <= precision){
  461. fmt = 'f';
  462. precision -= exponent;
  463. }else{
  464. fmt = 'e';
  465. --precision;
  466. }
  467. break;
  468. }
  469. if (exponent == 9999) {
  470. /* Infinity or Nan */
  471. precision = 0;
  472. exponent = edigits - digits;
  473. fmt = 'f';
  474. }
  475. ndig = edigits-digits;
  476. if((afmt=='g' || afmt=='G') && !(flags&ALT)){ /* knock off trailing zeros */
  477. if(fmt == 'f'){
  478. if(precision+exponent > ndig) {
  479. precision = ndig - exponent;
  480. if(precision < 0)
  481. precision = 0;
  482. }
  483. }
  484. else{
  485. if(precision > ndig-1) precision = ndig-1;
  486. }
  487. }
  488. nout = precision; /* digits after decimal point */
  489. if(precision!=0 || flags&ALT) nout++; /* decimal point */
  490. if(fmt=='f' && exponent>0) nout += exponent; /* digits before decimal point */
  491. else nout++; /* there's always at least one */
  492. if(sign || flags&(SPACE|SIGN)) nout++; /* sign */
  493. if(fmt != 'f'){ /* exponent */
  494. eptr = ebuf;
  495. for(i=exponent<=0?1-exponent:exponent-1; i; i/=10)
  496. *eptr++ = '0' + i%10;
  497. while(eptr<ebuf+2) *eptr++ = '0';
  498. nout += eptr-ebuf+2; /* e+99 */
  499. }
  500. if(!(flags&ZPAD) && !(flags&LEFT))
  501. while(nout < width){
  502. putc(' ', f);
  503. nout++;
  504. }
  505. if(sign) putc('-', f);
  506. else if(flags&SIGN) putc('+', f);
  507. else if(flags&SPACE) putc(' ', f);
  508. if(flags&ZPAD)
  509. while(nout < width){
  510. putc('0', f);
  511. nout++;
  512. }
  513. if(fmt == 'f'){
  514. for(i=0; i<exponent; i++) putc(i<ndig?digits[i]:'0', f);
  515. if(i == 0) putc('0', f);
  516. if(precision>0 || flags&ALT) putc('.', f);
  517. for(i=0; i!=precision; i++)
  518. putc(0<=i+exponent && i+exponent<ndig?digits[i+exponent]:'0', f);
  519. }
  520. else{
  521. putc(digits[0], f);
  522. if(precision>0 || flags&ALT) putc('.', f);
  523. for(i=0; i!=precision; i++) putc(i<ndig-1?digits[i+1]:'0', f);
  524. }
  525. if(fmt != 'f'){
  526. putc(echr, f);
  527. putc(exponent<=0?'-':'+', f);
  528. while(eptr>ebuf) putc(*--eptr, f);
  529. }
  530. while(nout < width){
  531. putc(' ', f);
  532. nout++;
  533. }
  534. freedtoa(digits);
  535. return nout;
  536. }