vfprintf.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. /*
  2. * pANS stdio -- vfprintf
  3. */
  4. #include "iolib.h"
  5. #include <stdarg.h>
  6. #include <math.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. /*
  10. * Leading flags
  11. */
  12. #define SPACE 1 /* ' ' prepend space if no sign printed */
  13. #define ALT 2 /* '#' use alternate conversion */
  14. #define SIGN 4 /* '+' prepend sign, even if positive */
  15. #define LEFT 8 /* '-' left-justify */
  16. #define ZPAD 16 /* '0' zero-pad */
  17. /*
  18. * Trailing flags
  19. */
  20. #define SHORT 32 /* 'h' convert a short integer */
  21. #define LONG 64 /* 'l' convert a long integer */
  22. #define LDBL 128 /* 'L' convert a long double */
  23. #define PTR 256 /* convert a void * (%p) */
  24. #define VLONG 512 /* 'll' convert a long long integer */
  25. static int lflag[] = { /* leading flags */
  26. 0, 0, 0, 0, 0, 0, 0, 0, /* ^@ ^A ^B ^C ^D ^E ^F ^G */
  27. 0, 0, 0, 0, 0, 0, 0, 0, /* ^H ^I ^J ^K ^L ^M ^N ^O */
  28. 0, 0, 0, 0, 0, 0, 0, 0, /* ^P ^Q ^R ^S ^T ^U ^V ^W */
  29. 0, 0, 0, 0, 0, 0, 0, 0, /* ^X ^Y ^Z ^[ ^\ ^] ^^ ^_ */
  30. SPACE, 0, 0, ALT, 0, 0, 0, 0, /* sp ! " # $ % & ' */
  31. 0, 0, 0, SIGN, 0, LEFT, 0, 0, /* ( ) * + , - . / */
  32. ZPAD, 0, 0, 0, 0, 0, 0, 0, /* 0 1 2 3 4 5 6 7 */
  33. 0, 0, 0, 0, 0, 0, 0, 0, /* 8 9 : ; < = > ? */
  34. 0, 0, 0, 0, 0, 0, 0, 0, /* @ A B C D E F G */
  35. 0, 0, 0, 0, 0, 0, 0, 0, /* H I J K L M N O */
  36. 0, 0, 0, 0, 0, 0, 0, 0, /* P Q R S T U V W */
  37. 0, 0, 0, 0, 0, 0, 0, 0, /* X Y Z [ \ ] ^ _ */
  38. 0, 0, 0, 0, 0, 0, 0, 0, /* ` a b c d e f g */
  39. 0, 0, 0, 0, 0, 0, 0, 0, /* h i j k l m n o */
  40. 0, 0, 0, 0, 0, 0, 0, 0, /* p q r s t u v w */
  41. 0, 0, 0, 0, 0, 0, 0, 0, /* x y z { | } ~ ^? */
  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. 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. };
  59. static int tflag[] = { /* trailing flags */
  60. 0, 0, 0, 0, 0, 0, 0, 0, /* ^@ ^A ^B ^C ^D ^E ^F ^G */
  61. 0, 0, 0, 0, 0, 0, 0, 0, /* ^H ^I ^J ^K ^L ^M ^N ^O */
  62. 0, 0, 0, 0, 0, 0, 0, 0, /* ^P ^Q ^R ^S ^T ^U ^V ^W */
  63. 0, 0, 0, 0, 0, 0, 0, 0, /* ^X ^Y ^Z ^[ ^\ ^] ^^ ^_ */
  64. 0, 0, 0, 0, 0, 0, 0, 0, /* sp ! " # $ % & ' */
  65. 0, 0, 0, 0, 0, 0, 0, 0, /* ( ) * + , - . / */
  66. 0, 0, 0, 0, 0, 0, 0, 0, /* 0 1 2 3 4 5 6 7 */
  67. 0, 0, 0, 0, 0, 0, 0, 0, /* 8 9 : ; < = > ? */
  68. 0, 0, 0, 0, 0, 0, 0, 0, /* @ A B C D E F G */
  69. 0, 0, 0, 0, LDBL, 0, 0, 0, /* H I J K L M N O */
  70. 0, 0, 0, 0, 0, 0, 0, 0, /* P Q R S T U V W */
  71. 0, 0, 0, 0, 0, 0, 0, 0, /* X Y Z [ \ ] ^ _ */
  72. 0, 0, 0, 0, 0, 0, 0, 0, /* ` a b c d e f g */
  73. SHORT, 0, 0, 0, LONG, 0, 0, 0, /* h i j k l m n o */
  74. 0, 0, 0, 0, 0, 0, 0, 0, /* p q r s t u v w */
  75. 0, 0, 0, 0, 0, 0, 0, 0, /* x y z { | } ~ ^? */
  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. 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. };
  93. static int ocvt_E(FILE *, va_list *, int, int, int);
  94. static int ocvt_G(FILE *, va_list *, int, int, int);
  95. static int ocvt_X(FILE *, va_list *, int, int, int);
  96. static int ocvt_c(FILE *, va_list *, int, int, int);
  97. static int ocvt_d(FILE *, va_list *, int, int, int);
  98. static int ocvt_e(FILE *, va_list *, int, int, int);
  99. static int ocvt_f(FILE *, va_list *, int, int, int);
  100. static int ocvt_g(FILE *, va_list *, int, int, int);
  101. static int ocvt_n(FILE *, va_list *, int, int, int);
  102. static int ocvt_o(FILE *, va_list *, int, int, int);
  103. static int ocvt_p(FILE *, va_list *, int, int, int);
  104. static int ocvt_s(FILE *, va_list *, int, int, int);
  105. static int ocvt_u(FILE *, va_list *, int, int, int);
  106. static int ocvt_x(FILE *, va_list *, int, int, int);
  107. static int(*ocvt[])(FILE *, va_list *, int, int, int) = {
  108. 0, 0, 0, 0, 0, 0, 0, 0, /* ^@ ^A ^B ^C ^D ^E ^F ^G */
  109. 0, 0, 0, 0, 0, 0, 0, 0, /* ^H ^I ^J ^K ^L ^M ^N ^O */
  110. 0, 0, 0, 0, 0, 0, 0, 0, /* ^P ^Q ^R ^S ^T ^U ^V ^W */
  111. 0, 0, 0, 0, 0, 0, 0, 0, /* ^X ^Y ^Z ^[ ^\ ^] ^^ ^_ */
  112. 0, 0, 0, 0, 0, 0, 0, 0, /* sp ! " # $ % & ' */
  113. 0, 0, 0, 0, 0, 0, 0, 0, /* ( ) * + , - . / */
  114. 0, 0, 0, 0, 0, 0, 0, 0, /* 0 1 2 3 4 5 6 7 */
  115. 0, 0, 0, 0, 0, 0, 0, 0, /* 8 9 : ; < = > ? */
  116. 0, 0, 0, 0, 0, ocvt_E, 0, ocvt_G, /* @ A B C D E F G */
  117. 0, 0, 0, 0, 0, 0, 0, 0, /* H I J K L M N O */
  118. 0, 0, 0, 0, 0, 0, 0, 0, /* P Q R S T U V W */
  119. ocvt_X, 0, 0, 0, 0, 0, 0, 0, /* X Y Z [ \ ] ^ _ */
  120. 0, 0, 0, ocvt_c, ocvt_d, ocvt_e, ocvt_f, ocvt_g, /* ` a b c d e f g */
  121. 0, ocvt_d, 0, 0, 0, 0, ocvt_n, ocvt_o, /* h i j k l m n o */
  122. ocvt_p, 0, 0, ocvt_s, 0, ocvt_u, 0, 0, /* p q r s t u v w */
  123. ocvt_x, 0, 0, 0, 0, 0, 0, 0, /* x y z { | } ~ ^? */
  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. 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. };
  141. static int nprint;
  142. int
  143. vfprintf(FILE *f, const char *s, va_list args)
  144. {
  145. int tfl, flags, width, precision;
  146. nprint = 0;
  147. while(*s){
  148. if(*s != '%'){
  149. putc(*s++, f);
  150. nprint++;
  151. continue;
  152. }
  153. s++;
  154. flags = 0;
  155. while(lflag[*s&_IO_CHMASK]) flags |= lflag[*s++&_IO_CHMASK];
  156. if(*s == '*'){
  157. width = va_arg(args, int);
  158. s++;
  159. if(width<0){
  160. flags |= LEFT;
  161. width = -width;
  162. }
  163. }
  164. else{
  165. width = 0;
  166. while('0'<=*s && *s<='9') width = width*10 + *s++ - '0';
  167. }
  168. if(*s == '.'){
  169. s++;
  170. if(*s == '*'){
  171. precision = va_arg(args, int);
  172. s++;
  173. }
  174. else{
  175. precision = 0;
  176. while('0'<=*s && *s<='9') precision = precision*10 + *s++ - '0';
  177. }
  178. }
  179. else
  180. precision = -1;
  181. while(tfl = tflag[*s&_IO_CHMASK]){
  182. if(tfl == LONG && (flags & LONG)){
  183. flags &= ~LONG;
  184. tfl = VLONG;
  185. }
  186. flags |= tfl;
  187. s++;
  188. }
  189. if(ocvt[*s]) nprint += (*ocvt[*s++])(f, &args, flags, width, precision);
  190. else if(*s){
  191. putc(*s++, f);
  192. nprint++;
  193. }
  194. }
  195. return ferror(f)? -1: nprint;;
  196. }
  197. static int
  198. ocvt_c(FILE *f, va_list *args, int flags, int width, int precision)
  199. {
  200. #pragma ref precision
  201. int i;
  202. if(!(flags&LEFT)) for(i=1; i<width; i++) putc(' ', f);
  203. putc((unsigned char)va_arg(*args, int), f);
  204. if(flags&LEFT) for(i=1; i<width; i++) putc(' ', f);
  205. return width<1 ? 1 : width;
  206. }
  207. static int
  208. ocvt_s(FILE *f, va_list *args, int flags, int width, int precision)
  209. {
  210. int i, n = 0;
  211. char *s;
  212. s = va_arg(*args, char *);
  213. if(!s)
  214. s = "";
  215. if(!(flags&LEFT)){
  216. if(precision >= 0)
  217. for(i=0; i!=precision && s[i]; i++);
  218. else
  219. for(i=0; s[i]; i++);
  220. for(; i<width; i++){
  221. putc(' ', f);
  222. n++;
  223. }
  224. }
  225. if(precision >= 0){
  226. for(i=0; i!=precision && *s; i++){
  227. putc(*s++, f);
  228. n++;
  229. }
  230. } else{
  231. for(i=0;*s;i++){
  232. putc(*s++, f);
  233. n++;
  234. }
  235. }
  236. if(flags&LEFT){
  237. for(; i<width; i++){
  238. putc(' ', f);
  239. n++;
  240. }
  241. }
  242. return n;
  243. }
  244. static int
  245. ocvt_n(FILE *f, va_list *args, int flags, int width, int precision)
  246. {
  247. #pragma ref f
  248. #pragma ref width
  249. #pragma ref precision
  250. if(flags&SHORT)
  251. *va_arg(*args, short *) = nprint;
  252. else if(flags&LONG)
  253. *va_arg(*args, long *) = nprint;
  254. else if(flags&VLONG)
  255. *va_arg(*args, long long*) = nprint;
  256. else
  257. *va_arg(*args, int *) = nprint;
  258. return 0;
  259. }
  260. /*
  261. * Generic fixed-point conversion
  262. * f is the output FILE *;
  263. * args is the va_list * from which to get the number;
  264. * flags, width and precision are the results of printf-cracking;
  265. * radix is the number base to print in;
  266. * alphabet is the set of digits to use;
  267. * prefix is the prefix to print before non-zero numbers when
  268. * using ``alternate form.''
  269. */
  270. static int
  271. ocvt_fixed(FILE *f, va_list *args, int flags, int width, int precision,
  272. int radix, int sgned, char alphabet[], char *prefix)
  273. {
  274. char digits[128]; /* no reasonable machine will ever overflow this */
  275. char *sign;
  276. char *dp;
  277. long long snum;
  278. unsigned long long num;
  279. int nout, npad, nlzero;
  280. if(sgned){
  281. if(flags&PTR) snum = (long)va_arg(*args, void *);
  282. else if(flags&SHORT) snum = va_arg(*args, short);
  283. else if(flags&LONG) snum = va_arg(*args, long);
  284. else if(flags&VLONG) snum = va_arg(*args, long long);
  285. else snum = va_arg(*args, int);
  286. if(snum < 0){
  287. sign = "-";
  288. num = -snum;
  289. } else{
  290. if(flags&SIGN) sign = "+";
  291. else if(flags&SPACE) sign = " ";
  292. else sign = "";
  293. num = snum;
  294. }
  295. } else {
  296. sign = "";
  297. if(flags&PTR) num = (long)va_arg(*args, void *);
  298. else if(flags&SHORT) num = va_arg(*args, unsigned short);
  299. else if(flags&LONG) num = va_arg(*args, unsigned long);
  300. else if(flags&VLONG) num = va_arg(*args, unsigned long 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, char afmt)
  417. {
  418. extern char *_dtoa(double, int, int, int*, int*, char **);
  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(ndig == 0) {
  473. ndig = 1;
  474. digits = "0";
  475. }
  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) && !(flags&LEFT))
  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. return nout;
  535. }