printf.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154
  1. /* Copyright (C) 2002 Manuel Novoa III
  2. * My stdio library for linux and (soon) elks.
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Library General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Library General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Library General Public
  15. * License along with this library; if not, write to the Free
  16. * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. /* This code needs a lot of clean up. Some of that is on hold until uClibc
  19. * gets a better configuration system (on Erik's todo list).
  20. * The other cleanup will take place during the implementation/integration of
  21. * the wide char (un)formatted i/o functions which I'm currently working on.
  22. */
  23. /* ATTENTION! ATTENTION! ATTENTION! ATTENTION! ATTENTION!
  24. *
  25. * This code is currently under development. Also, I plan to port
  26. * it to elks which is a 16-bit environment with a fairly limited
  27. * compiler. Therefore, please refrain from modifying this code
  28. * and, instead, pass any bug-fixes, etc. to me. Thanks. Manuel
  29. *
  30. * ATTENTION! ATTENTION! ATTENTION! ATTENTION! ATTENTION! */
  31. /* 4-01-2002
  32. * Initialize thread locks for fake files in vsnprintf and vdprintf.
  33. * reported by Erik Andersen (andersen@codepoet.com)
  34. * Fix an arg promotion handling bug in _do_one_spec for %c.
  35. * reported by Ilguiz Latypov <ilatypov@superbt.com>
  36. *
  37. * 5-10-2002
  38. * Remove __isdigit and use new ctype.h version.
  39. * Add conditional setting of QUAL_CHARS for size_t and ptrdiff_t.
  40. */
  41. #define _ISOC99_SOURCE /* for ULLONG primarily... */
  42. #define _GNU_SOURCE
  43. #define _STDIO_UTILITY /* We're using _uintmaxtostr. */
  44. #include <stdio.h>
  45. #include <stdlib.h>
  46. #include <string.h>
  47. #include <stddef.h>
  48. #include <ctype.h>
  49. #include <limits.h>
  50. #include <stdarg.h>
  51. #include <assert.h>
  52. #include <stdint.h>
  53. #include <errno.h>
  54. #define __PRINTF_INFO_NO_BITFIELD
  55. #include <printf.h>
  56. #ifdef __STDIO_THREADSAFE
  57. #include <pthread.h>
  58. #endif /* __STDIO_THREADSAFE */
  59. /**********************************************************************/
  60. /* These provide some control over printf's feature set */
  61. #define __STDIO_PRINTF_FLOAT
  62. #define __STDIO_PRINTF_M_SUPPORT
  63. /**********************************************************************/
  64. #if defined(__UCLIBC__) && !defined(__UCLIBC_HAS_FLOATS__)
  65. #undef __STDIO_PRINTF_FLOAT
  66. #endif
  67. #ifdef __BCC__
  68. #undef __STDIO_PRINTF_FLOAT
  69. #endif
  70. #ifndef __STDIO_PRINTF_FLOAT
  71. #undef L__dtostr
  72. #endif
  73. /**********************************************************************/
  74. #define __STDIO_GLIBC_CUSTOM_PRINTF
  75. /* TODO -- move these to a configuration section? */
  76. #define MAX_FIELD_WIDTH 4095
  77. #define MAX_USER_SPEC 10
  78. #define MAX_POS_ARGS 10
  79. /* TODO - fix the defs below */
  80. #define MAX_ARGS_PER_SPEC (MAX_POS_ARGS-2)
  81. #if MAX_ARGS_PER_SPEC + 2 > MAX_POS_ARGS
  82. #define MAX_ARGS MAX_ARGS_PER_SPEC + 2
  83. #else
  84. #define MAX_ARGS MAX_POS_ARGS
  85. #endif
  86. /**********************************************************************/
  87. #define __PA_FLAG_INTMASK \
  88. (__PA_FLAG_CHAR|PA_FLAG_SHORT|__PA_FLAG_INT|PA_FLAG_LONG|PA_FLAG_LONG_LONG)
  89. extern printf_function _custom_printf_handler[MAX_USER_SPEC];
  90. extern printf_arginfo_function *_custom_printf_arginfo[MAX_USER_SPEC];
  91. extern char *_custom_printf_spec;
  92. /**********************************************************************/
  93. #define SPEC_FLAGS " +0-#'I"
  94. enum {
  95. FLAG_SPACE = 0x01,
  96. FLAG_PLUS = 0x02, /* must be 2 * FLAG_SPACE */
  97. FLAG_ZERO = 0x04,
  98. FLAG_MINUS = 0x08, /* must be 2 * FLAG_ZERO */
  99. FLAG_HASH = 0x10,
  100. FLAG_THOUSANDS = 0x20,
  101. FLAG_I18N = 0x40 /* only works for d, i, u */
  102. };
  103. /**********************************************************************/
  104. /* float layout 01234567890123456789 TODO: B?*/
  105. #define SPEC_CHARS "npxXoudifFeEgGaACScs"
  106. enum {
  107. CONV_n = 0,
  108. CONV_p,
  109. CONV_x, CONV_X, CONV_o, CONV_u, CONV_d, CONV_i,
  110. CONV_f, CONV_F, CONV_e, CONV_E, CONV_g, CONV_G, CONV_a, CONV_A,
  111. CONV_C, CONV_S, CONV_c, CONV_s,
  112. #ifdef __STDIO_PRINTF_M_SUPPORT
  113. CONV_m,
  114. #endif
  115. CONV_custom0 /* must be last */
  116. };
  117. /* p x X o u d i */
  118. #define SPEC_BASE { 16, 16, 16, 8, 10, 10, 10 }
  119. #define SPEC_RANGES { CONV_n, CONV_p, CONV_i, CONV_A, \
  120. CONV_C, CONV_S, CONV_c, CONV_s, CONV_custom0 }
  121. #define SPEC_OR_MASK { \
  122. /* n */ (PA_FLAG_PTR|PA_INT), \
  123. /* p */ PA_POINTER, \
  124. /* oxXudi */ PA_INT, \
  125. /* fFeEgGaA */ PA_DOUBLE, \
  126. /* C */ PA_WCHAR, \
  127. /* S */ PA_WSTRING, \
  128. /* c */ PA_CHAR, \
  129. /* s */ PA_STRING, \
  130. }
  131. #define SPEC_AND_MASK { \
  132. /* n */ (PA_FLAG_PTR|__PA_INTMASK), \
  133. /* p */ PA_POINTER, \
  134. /* oxXudi */ (__PA_INTMASK), \
  135. /* fFeEgGaA */ (PA_FLAG_LONG_DOUBLE|PA_DOUBLE), \
  136. /* C */ (PA_WCHAR), \
  137. /* S */ (PA_WSTRING), \
  138. /* c */ (PA_CHAR), \
  139. /* s */ (PA_STRING), \
  140. }
  141. /**********************************************************************/
  142. /*
  143. * In order to ease translation to what arginfo and _print_info._flags expect,
  144. * we map: 0:int 1:char 2:longlong 4:long 8:short
  145. * and then _flags |= (((q << 7) + q) & 0x701) and argtype |= (_flags & 0x701)
  146. */
  147. /* TODO -- Fix the table below to take into account stdint.h. */
  148. /* #ifndef LLONG_MAX */
  149. /* #error fix QUAL_CHARS for no long long! Affects 'L', 'j', 'q', 'll'. */
  150. /* #else */
  151. /* #if LLONG_MAX != INTMAX_MAX */
  152. /* #error fix QUAL_CHARS intmax_t entry 'j'! */
  153. /* #endif */
  154. /* #endif */
  155. #ifdef PDS
  156. #error PDS already defined!
  157. #endif
  158. #ifdef SS
  159. #error SS already defined!
  160. #endif
  161. #ifdef IMS
  162. #error IMS already defined!
  163. #endif
  164. #if PTRDIFF_MAX == INT_MAX
  165. #define PDS 0
  166. #elif PTRDIFF_MAX == LONG_MAX
  167. #define PDS 4
  168. #elif defined(LLONG_MAX) && (PTRDIFF_MAX == LLONG_MAX)
  169. #define PDS 8
  170. #else
  171. #error fix QUAL_CHARS ptrdiff_t entry 't'!
  172. #endif
  173. #if SIZE_MAX == UINT_MAX
  174. #define SS 0
  175. #elif SIZE_MAX == ULONG_MAX
  176. #define SS 4
  177. #elif defined(LLONG_MAX) && (SIZE_MAX == ULLONG_MAX)
  178. #define SS 8
  179. #else
  180. #error fix QUAL_CHARS size_t entries 'z', 'Z'!
  181. #endif
  182. #if INTMAX_MAX == INT_MAX
  183. #define IMS 0
  184. #elif INTMAX_MAX == LONG_MAX
  185. #define IMS 4
  186. #elif defined(LLONG_MAX) && (INTMAX_MAX == LLONG_MAX)
  187. #define IMS 8
  188. #else
  189. #error fix QUAL_CHARS ptrdiff_t entry 't'!
  190. #endif
  191. #define QUAL_CHARS { \
  192. /* j:(u)intmax_t z:(s)size_t t:ptrdiff_t \0:int */ \
  193. /* q:long_long Z:(s)size_t */ \
  194. 'h', 'l', 'L', 'j', 'z', 't', 'q', 'Z', 0, \
  195. 2, 4, 8, IMS, SS, PDS, 8, SS, 0, /* TODO -- fix!!! */\
  196. 1, 8 \
  197. }
  198. /**********************************************************************/
  199. #ifdef __STDIO_VA_ARG_PTR
  200. #ifdef __BCC__
  201. #define __va_arg_ptr(ap,type) (((type *)(ap += sizeof(type))) - 1)
  202. #endif
  203. #if 1
  204. #ifdef __GNUC__
  205. /* TODO -- need other than for 386 as well! */
  206. #ifndef __va_rounded_size
  207. #define __va_rounded_size(TYPE) \
  208. (((sizeof (TYPE) + sizeof (int) - 1) / sizeof (int)) * sizeof (int))
  209. #endif
  210. #define __va_arg_ptr(AP, TYPE) \
  211. (AP = (va_list) ((char *) (AP) + __va_rounded_size (TYPE)), \
  212. ((void *) ((char *) (AP) - __va_rounded_size (TYPE))))
  213. #endif
  214. #endif
  215. #endif /* __STDIO_VA_ARG_PTR */
  216. #ifdef __va_arg_ptr
  217. #define GET_VA_ARG(AP,F,TYPE,ARGS) (*(AP) = __va_arg_ptr(ARGS,TYPE))
  218. #define GET_ARG_VALUE(AP,F,TYPE) (*((TYPE *)(*(AP))))
  219. #else
  220. typedef union {
  221. wchar_t wc;
  222. unsigned int u;
  223. unsigned long ul;
  224. #ifdef ULLONG_MAX
  225. unsigned long long ull;
  226. #endif
  227. #ifdef __STDIO_PRINTF_FLOAT
  228. double d;
  229. long double ld;
  230. #endif /* __STDIO_PRINTF_FLOAT */
  231. void *p;
  232. } argvalue_t;
  233. #define GET_VA_ARG(AU,F,TYPE,ARGS) (AU->F = va_arg(ARGS,TYPE))
  234. #define GET_ARG_VALUE(AU,F,TYPE) ((TYPE)((AU)->F))
  235. #endif
  236. typedef struct {
  237. const char *fmtpos; /* TODO: move below struct?? */
  238. struct printf_info info;
  239. int maxposarg; /* > 0 if args are positional, 0 if not, -1 if unknown */
  240. int num_data_args; /* TODO: use sentinal??? */
  241. unsigned int conv_num;
  242. unsigned char argnumber[4]; /* width | prec | 1st data | unused */
  243. int argtype[MAX_ARGS];
  244. va_list arg;
  245. #ifdef __va_arg_ptr
  246. void *argptr[MAX_ARGS];
  247. #else
  248. /* While this is wasteful of space in the case where pos args aren't
  249. * enabled, it is also needed to support custom printf handlers. */
  250. argvalue_t argvalue[MAX_ARGS];
  251. #endif
  252. } ppfs_t; /* parse printf format state */
  253. /**********************************************************************/
  254. /* TODO: fix printf to return 0 and set errno if format error. Standard says
  255. only returns -1 if sets error indicator for the stream. */
  256. #ifdef __STDIO_PRINTF_FLOAT
  257. extern size_t _dtostr(FILE * fp, long double x, struct printf_info *info);
  258. #endif
  259. #define _outnstr(stream, string, len) _stdio_fwrite(s, len, stream) /* TODO */
  260. extern int _do_one_spec(FILE * __restrict stream, ppfs_t *ppfs, int *count);
  261. extern int _ppfs_init(ppfs_t *ppfs, const char *fmt0); /* validates */
  262. extern void _ppfs_prepargs(ppfs_t *ppfs, va_list arg); /* sets posargptrs */
  263. extern void _ppfs_setargs(ppfs_t *ppfs); /* sets argptrs for current spec */
  264. extern int _ppfs_parsespec(ppfs_t *ppfs); /* parses specifier */
  265. /**********************************************************************/
  266. #ifdef L_parse_printf_format
  267. /* NOTE: This function differs from the glibc version in that parsing stops
  268. * upon encountering an invalid conversion specifier. Since this is the way
  269. * my printf functions work, I think it makes sense to do it that way here.
  270. * Unfortunately, since glibc sets the return type as size_t, we have no way
  271. * of returning that the template is illegal, other than returning 0.
  272. */
  273. size_t parse_printf_format(register const char *template,
  274. size_t n, register int *argtypes)
  275. {
  276. ppfs_t ppfs;
  277. size_t i;
  278. size_t count = 0;
  279. if (_ppfs_init(&ppfs, template) >= 0) {
  280. if (ppfs.maxposarg > 0) { /* Using positional args. */
  281. count = ppfs.maxposarg;
  282. if (n > count) {
  283. n = count;
  284. }
  285. for (i = 0 ; i < n ; i++) {
  286. *argtypes++ = ppfs.argtype[i];
  287. }
  288. } else { /* Not using positional args. */
  289. while (*template) {
  290. if ((*template == '%') && (*++template != '%')) {
  291. ppfs.fmtpos = template;
  292. _ppfs_parsespec(&ppfs); /* Can't fail. */
  293. template = ppfs.fmtpos; /* Update to one past spec end. */
  294. if (ppfs.info.width == INT_MIN) {
  295. ++count;
  296. if (n > 0) {
  297. *argtypes++ = PA_INT;
  298. --n;
  299. }
  300. }
  301. if (ppfs.info.prec == INT_MIN) {
  302. ++count;
  303. if (n > 0) {
  304. *argtypes++ = PA_INT;
  305. --n;
  306. }
  307. }
  308. for (i = 0 ; i < ppfs.num_data_args ; i++) {
  309. if ((ppfs.argtype[i]) != __PA_NOARG) {
  310. ++count;
  311. if (n > 0) {
  312. *argtypes++ = ppfs.argtype[i];
  313. --n;
  314. }
  315. }
  316. }
  317. } else {
  318. ++template;
  319. }
  320. }
  321. }
  322. }
  323. return count;
  324. }
  325. #endif
  326. /**********************************************************************/
  327. #ifdef L_vfprintf
  328. /* We only support ascii digits (or their USC equivalent codes) in
  329. * precision and width settings in *printf (wide) format strings.
  330. * In other words, we don't currently support glibc's 'I' flag.
  331. * We do accept it, but it is currently ignored. */
  332. /* TODO -- Implement grouping flag... on hold (locale support). */
  333. int vfprintf(FILE * __restrict stream, register const char * __restrict format,
  334. va_list arg)
  335. {
  336. ppfs_t ppfs;
  337. int count, r;
  338. register const char *s;
  339. __STDIO_THREADLOCK(stream);
  340. count = 0;
  341. s = format;
  342. if (_ppfs_init(&ppfs, format) < 0) { /* Bad format string. */
  343. _outnstr(stream, format, strlen(format));
  344. count = -1;
  345. } else {
  346. _ppfs_prepargs(&ppfs, arg);
  347. do {
  348. while (*format && (*format != '%')) {
  349. ++format;
  350. }
  351. if (format-s) { /* output any literal text in format string */
  352. if ( (r = _outnstr(stream, s, format-s)) < 0) {
  353. count = -1;
  354. break;
  355. }
  356. count += r;
  357. }
  358. if (!*format) { /* we're done */
  359. break;
  360. }
  361. if (format[1] != '%') { /* if we get here, *format == '%' */
  362. /* TODO: _do_one_spec needs to know what the output funcs are!!! */
  363. ppfs.fmtpos = ++format;
  364. /* TODO: check -- should only fail on stream error */
  365. if ( (r = _do_one_spec(stream, &ppfs, &count)) < 0) {
  366. count = -1;
  367. break;
  368. }
  369. s = format = ppfs.fmtpos;
  370. } else { /* %% means literal %, so start new string */
  371. s = ++format;
  372. ++format;
  373. }
  374. } while (1);
  375. }
  376. __STDIO_THREADUNLOCK(stream);
  377. return count;
  378. }
  379. #endif
  380. /**********************************************************************/
  381. #ifdef L__ppfs_init
  382. int _ppfs_init(register ppfs_t *ppfs, const char *fmt0)
  383. {
  384. int r;
  385. /* First, zero out everything... argnumber[], argtype[], argptr[] */
  386. memset(ppfs, 0, sizeof(ppfs_t)); /* TODO: nonportable???? */
  387. --ppfs->maxposarg; /* set to -1 */
  388. /* now set all argtypes to no-arg */
  389. {
  390. #if 1
  391. /* TODO - use memset here since already "paid for"? */
  392. register int *p = ppfs->argtype;
  393. r = MAX_ARGS;
  394. do {
  395. *p++ = __PA_NOARG;
  396. } while (--r);
  397. #else
  398. /* TODO -- get rid of this?? */
  399. register char *p = (char *) ((MAX_ARGS-1) * sizeof(int));
  400. do {
  401. *((int *)(((char *)ppfs) + ((int)p) + offsetof(ppfs_t,argtype))) = __PA_NOARG;
  402. p -= sizeof(int);
  403. } while (p);
  404. #endif
  405. }
  406. /*
  407. * Run through the entire format string to validate it and initialize
  408. * the positional arg numbers (if any).
  409. */
  410. {
  411. register const char *fmt = fmt0;
  412. while (*fmt) {
  413. if ((*fmt == '%') && (*++fmt != '%')) {
  414. ppfs->fmtpos = fmt; /* back up to the '%' */
  415. if ((r = _ppfs_parsespec(ppfs)) < 0) {
  416. return -1;
  417. }
  418. fmt = ppfs->fmtpos; /* update to one past end of spec */
  419. } else {
  420. ++fmt;
  421. }
  422. }
  423. ppfs->fmtpos = fmt0; /* rewind */
  424. }
  425. /* If we have positional args, make sure we know all the types. */
  426. {
  427. register int *p = ppfs->argtype;
  428. r = ppfs->maxposarg;
  429. while (--r >= 0) {
  430. if ( *p == __PA_NOARG ) { /* missing arg type!!! */
  431. return -1;
  432. }
  433. ++p;
  434. }
  435. }
  436. return 0;
  437. }
  438. #endif
  439. /**********************************************************************/
  440. #ifdef L__ppfs_prepargs
  441. void _ppfs_prepargs(register ppfs_t *ppfs, va_list arg)
  442. {
  443. int i;
  444. #ifdef __va_copy
  445. __va_copy(ppfs->arg, arg);
  446. #else
  447. /* TODO -- maybe create a bits/vacopy.h for arch specific versions
  448. * to ensure we get the right behavior? Either that or fall back
  449. * on the portable (but costly in size) method of using a va_list *.
  450. * That means a pointer derefs in the va_arg() invocations... */
  451. #warning __va_copy is not defined, using a simple copy instead...
  452. /* the glibc manual suggests that this will usually suffice when
  453. __va_copy doesn't exist. */
  454. ppfs->arg = arg;
  455. #endif
  456. if ((i = ppfs->maxposarg) > 0) { /* init for positional args */
  457. ppfs->num_data_args = i;
  458. ppfs->info.width = ppfs->info.prec = ppfs->maxposarg = 0;
  459. _ppfs_setargs(ppfs);
  460. ppfs->maxposarg = i;
  461. }
  462. }
  463. #endif
  464. /**********************************************************************/
  465. #ifdef L__ppfs_setargs
  466. void _ppfs_setargs(register ppfs_t *ppfs)
  467. {
  468. #ifdef __va_arg_ptr
  469. register void **p = ppfs->argptr;
  470. #else
  471. register argvalue_t *p = ppfs->argvalue;
  472. #endif
  473. int i;
  474. if (ppfs->maxposarg == 0) { /* initing for or no pos args */
  475. if (ppfs->info.width == INT_MIN) {
  476. ppfs->info.width =
  477. #ifdef __va_arg_ptr
  478. *(int *)
  479. #endif
  480. GET_VA_ARG(p,u,unsigned int,ppfs->arg);
  481. }
  482. if (ppfs->info.prec == INT_MIN) {
  483. ppfs->info.prec =
  484. #ifdef __va_arg_ptr
  485. *(int *)
  486. #endif
  487. GET_VA_ARG(p,u,unsigned int,ppfs->arg);
  488. }
  489. i = 0;
  490. while (i < ppfs->num_data_args) {
  491. switch(ppfs->argtype[i++]) {
  492. case (PA_INT|PA_FLAG_LONG_LONG):
  493. #ifdef ULLONG_MAX
  494. GET_VA_ARG(p,ull,unsigned long long,ppfs->arg);
  495. break;
  496. #endif
  497. case (PA_INT|PA_FLAG_LONG):
  498. #if ULONG_MAX != UINT_MAX
  499. GET_VA_ARG(p,ul,unsigned long,ppfs->arg);
  500. break;
  501. #endif
  502. case PA_CHAR: /* TODO - be careful */
  503. /* ... users could use above and really want below!! */
  504. case (PA_INT|__PA_FLAG_CHAR):/* TODO -- translate this!!! */
  505. case (PA_INT|PA_FLAG_SHORT):
  506. case PA_INT:
  507. GET_VA_ARG(p,u,unsigned int,ppfs->arg);
  508. break;
  509. case PA_WCHAR: /* TODO -- assume int? */
  510. /* we're assuming wchar_t is at least an int */
  511. GET_VA_ARG(p,wc,wchar_t,ppfs->arg);
  512. break;
  513. #ifdef __STDIO_PRINTF_FLOAT
  514. /* PA_FLOAT */
  515. case PA_DOUBLE:
  516. GET_VA_ARG(p,d,double,ppfs->arg);
  517. break;
  518. case (PA_DOUBLE|PA_FLAG_LONG_DOUBLE):
  519. GET_VA_ARG(p,ld,long double,ppfs->arg);
  520. break;
  521. #else /* __STDIO_PRINTF_FLOAT */
  522. case PA_DOUBLE:
  523. case (PA_DOUBLE|PA_FLAG_LONG_DOUBLE):
  524. assert(0);
  525. continue;
  526. #endif /* __STDIO_PRINTF_FLOAT */
  527. default:
  528. /* TODO -- really need to ensure this can't happen */
  529. assert(ppfs->argtype[i-1] & PA_FLAG_PTR);
  530. case PA_POINTER:
  531. case PA_STRING:
  532. case PA_WSTRING:
  533. GET_VA_ARG(p,p,void *,ppfs->arg);
  534. break;
  535. case __PA_NOARG:
  536. continue;
  537. }
  538. ++p;
  539. }
  540. } else {
  541. if (ppfs->info.width == INT_MIN) {
  542. ppfs->info.width
  543. = (int) GET_ARG_VALUE(p + ppfs->argnumber[0] - 1,u,unsigned int);
  544. }
  545. if (ppfs->info.prec == INT_MIN) {
  546. ppfs->info.prec
  547. = (int) GET_ARG_VALUE(p + ppfs->argnumber[1] - 1,u,unsigned int);
  548. }
  549. }
  550. /* Now we know the width and precision. */
  551. if (ppfs->info.width < 0) {
  552. ppfs->info.width = -ppfs->info.width;
  553. PRINT_INFO_SET_FLAG(&(ppfs->info),left);
  554. PRINT_INFO_CLR_FLAG(&(ppfs->info),space);
  555. ppfs->info.pad = ' ';
  556. }
  557. #if 0
  558. /* NOTE -- keep neg for now so float knows! */
  559. if (ppfs->info.prec < 0) { /* spec says treat as omitted. */
  560. /* so use default prec... 1 for everything but floats and strings. */
  561. ppfs->info.prec = 1;
  562. }
  563. #endif
  564. }
  565. #endif
  566. /**********************************************************************/
  567. #ifdef L__ppfs_parsespec
  568. /* Notes: argtype differs from glibc for the following:
  569. * mine glibc
  570. * lc PA_WCHAR PA_CHAR the standard says %lc means %C
  571. * ls PA_WSTRING PA_STRING the standard says %ls means %S
  572. * {*}n {*}|PA_FLAG_PTR PA_FLAG_PTR size of n can be qualified
  573. */
  574. /* TODO: store positions of positional args */
  575. /* TODO -- WARNING -- assumes aligned on integer boundaries!!! */
  576. /* TODO -- disable if not using positional args!!! */
  577. #define _OVERLAPPING_DIFFERENT_ARGS
  578. /* TODO -- rethink this -- perhaps we should set to largest type??? */
  579. #ifdef _OVERLAPPING_DIFFERENT_ARGS
  580. #define PROMOTED_SIZE_OF(X) ((sizeof(X) + sizeof(int) - 1) / sizeof(X))
  581. static const short int type_codes[] = {
  582. __PA_NOARG, /* must be first entry */
  583. PA_POINTER,
  584. PA_STRING,
  585. PA_WSTRING,
  586. PA_CHAR,
  587. PA_INT|PA_FLAG_SHORT,
  588. PA_INT,
  589. PA_INT|PA_FLAG_LONG,
  590. PA_INT|PA_FLAG_LONG_LONG,
  591. PA_WCHAR,
  592. #ifdef __STDIO_PRINTF_FLOAT
  593. /* PA_FLOAT, */
  594. PA_DOUBLE,
  595. PA_DOUBLE|PA_FLAG_LONG_DOUBLE,
  596. #endif /* __STDIO_PRINTF_FLOAT */
  597. };
  598. static const unsigned char type_sizes[] = {
  599. /* unknown type consumes no arg */
  600. 0, /* must be first entry */
  601. PROMOTED_SIZE_OF(void *),
  602. PROMOTED_SIZE_OF(char *),
  603. PROMOTED_SIZE_OF(wchar_t *),
  604. PROMOTED_SIZE_OF(char),
  605. PROMOTED_SIZE_OF(short),
  606. PROMOTED_SIZE_OF(int),
  607. PROMOTED_SIZE_OF(long),
  608. #ifdef ULLONG_MAX
  609. PROMOTED_SIZE_OF(long long),
  610. #else
  611. PROMOTED_SIZE_OF(long), /* TODO -- is this correct? (above too) */
  612. #endif
  613. PROMOTED_SIZE_OF(wchar_t),
  614. #ifdef __STDIO_PRINTF_FLOAT
  615. /* PROMOTED_SIZE_OF(float), */
  616. PROMOTED_SIZE_OF(double),
  617. PROMOTED_SIZE_OF(long double),
  618. #endif /* __STDIO_PRINTF_FLOAT */
  619. };
  620. static int _promoted_size(int argtype)
  621. {
  622. register const short int *p;
  623. /* note -- since any unrecognized type is treated as a pointer */
  624. p = type_codes + sizeof(type_codes)/sizeof(type_codes[0]);
  625. do {
  626. if (*--p == argtype) {
  627. break;
  628. }
  629. } while (p > type_codes);
  630. return type_sizes[(int)(p - type_codes)];
  631. }
  632. static int _is_equal_or_bigger_arg(int curtype, int newtype)
  633. {
  634. /* Quick test */
  635. if (newtype == __PA_NOARG) {
  636. return 0;
  637. }
  638. if ((curtype == __PA_NOARG) || (curtype == newtype)) {
  639. return 1;
  640. }
  641. /* Ok... slot is already filled and types are different in name. */
  642. /* So, compare promoted sizes of curtype and newtype args. */
  643. return _promoted_size(curtype) <= _promoted_size(newtype);
  644. }
  645. #else
  646. #define _is_equal_or_bigger_arg(C,N) (((C) == __PA_NOARG) || ((C) == (N)))
  647. #endif
  648. /* TODO - do this differently? */
  649. static char _bss_custom_printf_spec[MAX_USER_SPEC]; /* 0-init'd for us. */
  650. char *_custom_printf_spec = _bss_custom_printf_spec;
  651. printf_arginfo_function *_custom_printf_arginfo[MAX_USER_SPEC];
  652. extern int _ppfs_parsespec(ppfs_t *ppfs)
  653. {
  654. register const char *fmt;
  655. register const char *p;
  656. int preci;
  657. int width;
  658. int flags;
  659. int dataargtype;
  660. int i;
  661. int dpoint;
  662. int maxposarg;
  663. int p_m_spec_chars;
  664. int n;
  665. int argtype[MAX_ARGS_PER_SPEC+2];
  666. int argnumber[3]; /* width, precision, 1st data arg */
  667. unsigned int conv_num;
  668. static const char spec_flags[] = SPEC_FLAGS;
  669. static const char spec_chars[] = SPEC_CHARS;/* TODO: b? */
  670. static const char spec_ranges[] = SPEC_RANGES;
  671. static const short spec_or_mask[] = SPEC_OR_MASK;
  672. static const short spec_and_mask[] = SPEC_AND_MASK;
  673. static const char qual_chars[] = QUAL_CHARS;
  674. /* WIDE note: we can test against '%' here since we don't allow */
  675. /* WIDE note: other mappings of '%' in the wide char set. */
  676. preci = -1;
  677. width = flags = dpoint = 0;
  678. argnumber[0] = 0;
  679. argnumber[1] = 0;
  680. argtype[0] = __PA_NOARG;
  681. argtype[1] = __PA_NOARG;
  682. maxposarg = ppfs->maxposarg;
  683. fmt = ppfs->fmtpos;
  684. assert(fmt[-1] == '%');
  685. assert(fmt[0] != '%');
  686. /* Process arg pos and/or flags and/or width and/or precision. */
  687. width_precision:
  688. p = fmt;
  689. if (*fmt == '*') {
  690. argtype[-dpoint] = PA_INT;
  691. ++fmt;
  692. }
  693. i = 0;
  694. while (__isdigit(*fmt)) {
  695. if (i < MAX_FIELD_WIDTH) { /* Avoid overflow. */
  696. i = (i * 10) + (*fmt - '0');
  697. }
  698. ++fmt;
  699. }
  700. if (p[-1] == '%') { /* Check for a position. */
  701. /* TODO: if val not in range, then error */
  702. if ((*fmt == '$') && (i > 0)) {/* Positional spec. */
  703. ++fmt;
  704. if (maxposarg == 0) {
  705. return -1;
  706. }
  707. if ((argnumber[2] = i) > maxposarg) {
  708. maxposarg = i;
  709. }
  710. /* Now fall through to check flags. */
  711. } else {
  712. if (maxposarg > 0) {
  713. return -1;
  714. }
  715. maxposarg = 0; /* Possible redundant store, but cuts size. */
  716. if ((fmt > p) && (*p != '0')) {
  717. goto PREC_WIDTH;
  718. }
  719. fmt = p; /* Back up for possible '0's flag. */
  720. /* Now fall through to check flags. */
  721. }
  722. restart_flags: /* Process flags. */
  723. i = 1;
  724. p = spec_flags;
  725. do {
  726. if (*fmt == *p++) {
  727. ++fmt;
  728. flags |= i;
  729. goto restart_flags;
  730. }
  731. i += i; /* Better than i <<= 1 for bcc */
  732. } while (*p);
  733. i = 0;
  734. /* If '+' then ignore ' ', and if '-' then ignore '0'. */
  735. /* Note: Need to ignore '0' when prec is an arg with val < 0, */
  736. /* but that test needs to wait until the arg is retrieved. */
  737. flags &= ~((flags & (FLAG_PLUS|FLAG_MINUS)) >> 1);
  738. /* Note: Ignore '0' when prec is specified < 0 too (in printf). */
  739. if (fmt[-1] != '%') { /* If we've done anything, loop for width. */
  740. goto width_precision;
  741. }
  742. }
  743. PREC_WIDTH:
  744. if (*p == '*') { /* Prec or width takes an arg. */
  745. if (maxposarg) {
  746. if ((*fmt++ != '$') || (i <= 0)) {
  747. /* Using pos args and no $ or invalid arg number. */
  748. return -1;
  749. }
  750. argnumber[-dpoint] = i;
  751. } else if (++p != fmt) {
  752. /* Not using pos args but digits followed *. */
  753. return -1;
  754. }
  755. i = INT_MIN;
  756. }
  757. if (!dpoint) {
  758. width = i;
  759. if (*fmt == '.') {
  760. ++fmt;
  761. dpoint = -1; /* To use as default precison. */
  762. goto width_precision;
  763. }
  764. } else {
  765. preci = i;
  766. }
  767. /* Process qualifier. */
  768. p = qual_chars;
  769. do {
  770. if (*fmt == *p) {
  771. ++fmt;
  772. break;
  773. }
  774. } while (*++p);
  775. if ((p - qual_chars < 2) && (*fmt == *p)) {
  776. p += ((sizeof(qual_chars)-2) / 2);
  777. ++fmt;
  778. }
  779. dataargtype = ((int)(p[(sizeof(qual_chars)-2) / 2])) << 8;
  780. /* Process conversion specifier. */
  781. if (!*fmt) {
  782. return -1;
  783. }
  784. p = spec_chars;
  785. do {
  786. if (*fmt == *p) {
  787. p_m_spec_chars = p - spec_chars;
  788. if ((p_m_spec_chars >= CONV_c)
  789. && (dataargtype & PA_FLAG_LONG)) {
  790. p_m_spec_chars -= 2; /* lc -> C and ls -> S */
  791. }
  792. conv_num = p_m_spec_chars;
  793. p = spec_ranges-1;
  794. while (p_m_spec_chars > *++p) {}
  795. i = p - spec_ranges;
  796. argtype[2] = (dataargtype | spec_or_mask[i]) & spec_and_mask[i];
  797. p = spec_chars;
  798. break;
  799. }
  800. } while(*++p);
  801. ppfs->info.spec = *fmt;
  802. ppfs->info.prec = preci;
  803. ppfs->info.width = width;
  804. ppfs->info.pad = ((flags & FLAG_ZERO) ? '0' : ' ');
  805. ppfs->info._flags = (flags & ~FLAG_ZERO) | (dataargtype & __PA_INTMASK);
  806. ppfs->num_data_args = 1;
  807. if (!*p) {
  808. #ifdef __STDIO_GLIBC_CUSTOM_PRINTF
  809. /* TODO -- gnu %m support build option. */
  810. #ifdef __STDIO_PRINTF_M_SUPPORT
  811. if (*fmt == 'm') {
  812. conv_num = CONV_m;
  813. ppfs->num_data_args = 0;
  814. goto DONE;
  815. }
  816. #endif
  817. /* Handle custom arg -- WARNING -- overwrites p!!! */
  818. conv_num = CONV_custom0;
  819. p = _custom_printf_spec;
  820. do {
  821. if (*p == *fmt) {
  822. if ((ppfs->num_data_args
  823. = ((*_custom_printf_arginfo[(int)(p-_custom_printf_spec)])
  824. (&(ppfs->info), MAX_ARGS_PER_SPEC, argtype+2)))
  825. > MAX_ARGS_PER_SPEC) {
  826. break; /* Error -- too many args! */
  827. }
  828. goto DONE;
  829. }
  830. } while (++p < (_custom_printf_spec + MAX_USER_SPEC));
  831. #endif /* __STDIO_GLIBC_CUSTOM_PRINTF */
  832. /* Otherwise error. */
  833. return -1;
  834. }
  835. #ifdef __STDIO_GLIBC_CUSTOM_PRINTF
  836. DONE:
  837. #endif
  838. if (maxposarg > 0) {
  839. i = 0;
  840. do {
  841. /* Update maxposarg and check that NL_ARGMAX is not exceeded. */
  842. n = ((i <= 2)
  843. ? (ppfs->argnumber[i] = argnumber[i])
  844. : argnumber[2] + (i-2));
  845. if (n > maxposarg) {
  846. if ((maxposarg = n) > NL_ARGMAX) {
  847. return -1;
  848. }
  849. }
  850. --n;
  851. /* Record argtype with largest size (current, new). */
  852. if (_is_equal_or_bigger_arg(ppfs->argtype[n], argtype[i])) {
  853. ppfs->argtype[n] = argtype[i];
  854. }
  855. } while (++i < ppfs->num_data_args + 2);
  856. } else {
  857. ppfs->argnumber[2] = 1;
  858. memcpy(ppfs->argtype, argtype + 2, ppfs->num_data_args * sizeof(int));
  859. }
  860. ppfs->maxposarg = maxposarg;
  861. ppfs->fmtpos = ++fmt;
  862. ppfs->conv_num = conv_num;
  863. return ppfs->num_data_args + 2;
  864. }
  865. #endif
  866. /**********************************************************************/
  867. #ifdef L_register_printf_function
  868. int register_printf_function(int spec, printf_function handler,
  869. printf_arginfo_function arginfo)
  870. {
  871. register char *r;
  872. register char *p;
  873. if (spec && (arginfo != NULL)) { /* TODO -- check if spec is valid char */
  874. r = NULL;
  875. p = _custom_printf_spec + MAX_USER_SPEC;
  876. do {
  877. --p;
  878. if (!*p) {
  879. r = p;
  880. }
  881. #ifdef __BCC__
  882. else /* bcc generates less code with fall-through */
  883. #endif
  884. if (*p == spec) {
  885. r = p;
  886. p = _custom_printf_spec;
  887. }
  888. } while (p > _custom_printf_spec);
  889. if (r) {
  890. if (handler) {
  891. *r = spec;
  892. _custom_printf_handler[(int)(r - p)] = handler;
  893. _custom_printf_arginfo[(int)(r - p)] = arginfo;
  894. } else {
  895. *r = 0;
  896. }
  897. return 0;
  898. }
  899. /* TODO -- if asked to unregister a non-existent spec, return what? */
  900. }
  901. return -1;
  902. }
  903. #endif
  904. /**********************************************************************/
  905. #ifdef L__do_one_spec
  906. printf_function _custom_printf_handler[MAX_USER_SPEC];
  907. extern void _store_inttype(void *dest, int desttype, uintmax_t val);
  908. extern uintmax_t _load_inttype(int desttype, const void *src, int uflag);
  909. extern void _charpad(FILE * __restrict stream, char padchar, size_t numpad);
  910. extern void output(FILE * __restrict stream, const char *s);
  911. #define output(F,S) fputs(S,F)
  912. #undef putc
  913. void _charpad(FILE * __restrict stream, char padchar, size_t numpad)
  914. {
  915. /* TODO -- Use a buffer to cut down on function calls... */
  916. char pad[1];
  917. *pad = padchar;
  918. while (numpad) {
  919. _stdio_fwrite(pad, 1, stream);
  920. --numpad;
  921. }
  922. }
  923. /* TODO -- Dynamically allocate work space to accomodate stack-poor archs? */
  924. int _do_one_spec(FILE * __restrict stream, register ppfs_t *ppfs, int *count)
  925. {
  926. static const char spec_base[] = SPEC_BASE;
  927. static const char prefix[] = "+\0-\0 \0000x\0000X";
  928. /* 0 2 4 6 9 11*/
  929. enum {
  930. PREFIX_PLUS = 0,
  931. PREFIX_MINUS = 2,
  932. PREFIX_SPACE = 4,
  933. PREFIX_LWR_X = 6,
  934. PREFIX_UPR_X = 9,
  935. PREFIX_NONE = 11
  936. };
  937. #ifdef __va_arg_ptr
  938. const void * const *argptr;
  939. #else
  940. const void * argptr[MAX_ARGS_PER_SPEC];
  941. #endif
  942. int *argtype;
  943. size_t slen;
  944. int base;
  945. int numpad;
  946. int numfill = 0; /* TODO: fix */
  947. int prefix_num = PREFIX_NONE;
  948. char padchar = ' ';
  949. char buf[64];
  950. #ifdef NDEBUG
  951. _ppfs_parsespec(ppfs);
  952. #else
  953. if (_ppfs_parsespec(ppfs) < 0) { /* TODO: just for debugging */
  954. abort();
  955. }
  956. #endif
  957. _ppfs_setargs(ppfs);
  958. argtype = ppfs->argtype + ppfs->argnumber[2] - 1;
  959. /* Deal with the argptr vs argvalue issue. */
  960. #ifdef __va_arg_ptr
  961. argptr = (const void * const *) ppfs->argptr;
  962. if (ppfs->maxposarg > 0) { /* Using positional args... */
  963. argptr += ppfs->argnumber[2] - 1;
  964. }
  965. #else
  966. /* Need to build a local copy... */
  967. {
  968. register argvalue_t *p = ppfs->argvalue;
  969. int i;
  970. if (ppfs->maxposarg > 0) { /* Using positional args... */
  971. p += ppfs->argnumber[2] - 1;
  972. }
  973. for (i = 0 ; i < ppfs->num_data_args ; i++ ) {
  974. argptr[i] = (void *) p++;
  975. }
  976. }
  977. #endif
  978. {
  979. register char *s; /* TODO: Should s be unsigned char * ? */
  980. if (ppfs->conv_num == CONV_n) {
  981. _store_inttype(*(void **)*argptr,
  982. ppfs->info._flags & __PA_INTMASK,
  983. (intmax_t) (*count));
  984. return 0;
  985. }
  986. if (ppfs->conv_num <= CONV_i) { /* pointer or (un)signed int */
  987. base = spec_base[(int)(ppfs->conv_num - CONV_p)];
  988. if (ppfs->conv_num <= CONV_u) { /* pointer or unsigned int */
  989. if (ppfs->conv_num == CONV_p) { /* pointer */
  990. prefix_num = PREFIX_LWR_X;
  991. } else { /* unsigned int */
  992. }
  993. } else { /* signed int */
  994. base = -base;
  995. }
  996. if (ppfs->info.prec < 0) { /* Ignore '0' flag if prec specified. */
  997. padchar = ppfs->info.pad;
  998. }
  999. s = _uintmaxtostr(buf + sizeof(buf) - 1,
  1000. (uintmax_t)
  1001. _load_inttype(*argtype & __PA_INTMASK,
  1002. *argptr, base), base,
  1003. ((ppfs->conv_num == CONV_X)
  1004. ? __UIM_UPPER : __UIM_LOWER));
  1005. if (ppfs->conv_num > CONV_u) { /* signed int */
  1006. if (*s == '-') {
  1007. PRINT_INFO_SET_FLAG(&(ppfs->info),showsign);
  1008. ++s; /* handle '-' in the prefix string */
  1009. prefix_num = PREFIX_MINUS;
  1010. } else if (PRINT_INFO_FLAG_VAL(&(ppfs->info),showsign)) {
  1011. prefix_num = PREFIX_PLUS;
  1012. } else if (PRINT_INFO_FLAG_VAL(&(ppfs->info),space)) {
  1013. prefix_num = PREFIX_SPACE;
  1014. }
  1015. }
  1016. slen = (char *)(buf + sizeof(buf) - 1) - s;
  1017. numfill = ((ppfs->info.prec < 0) ? 1 : ppfs->info.prec);
  1018. if (PRINT_INFO_FLAG_VAL(&(ppfs->info),alt)) {
  1019. if (ppfs->conv_num <= CONV_x) { /* x or p */
  1020. prefix_num = PREFIX_LWR_X;
  1021. }
  1022. if (ppfs->conv_num == CONV_X) {
  1023. prefix_num = PREFIX_UPR_X;
  1024. }
  1025. if ((ppfs->conv_num == CONV_o) && (numfill < slen)) {
  1026. numfill = ((*s == '0') ? 1 : slen + 1);
  1027. }
  1028. }
  1029. if (*s == '0') {
  1030. if (prefix_num >= PREFIX_LWR_X) {
  1031. prefix_num = PREFIX_NONE;
  1032. }
  1033. if (ppfs->conv_num == CONV_p) {/* null pointer */
  1034. s = "(nil)";
  1035. slen = 5;
  1036. numfill = 0;
  1037. } else if (numfill == 0) { /* if precision 0, no output */
  1038. slen = 0;
  1039. }
  1040. }
  1041. numfill = ((numfill > slen) ? numfill - slen : 0);
  1042. } else if (ppfs->conv_num <= CONV_A) { /* floating point */
  1043. #ifdef __STDIO_PRINTF_FLOAT
  1044. *count += _dtostr(stream,
  1045. (PRINT_INFO_FLAG_VAL(&(ppfs->info),is_long_double)
  1046. ? *(long double *) *argptr
  1047. : (long double) (* (double *) *argptr)),
  1048. &ppfs->info);
  1049. return 0;
  1050. #else /* __STDIO_PRINTF_FLOAT */
  1051. return -1; /* TODO -- try ton continue? */
  1052. #endif /* __STDIO_PRINTF_FLOAT */
  1053. } else if (ppfs->conv_num <= CONV_S) { /* wide char or string */
  1054. #if 1
  1055. return -1; /* TODO -- wide */
  1056. #else
  1057. if (ppfs->conv_num == CONV_S) { /* wide string */
  1058. } else { /* wide char */
  1059. }
  1060. #endif
  1061. } else if (ppfs->conv_num <= CONV_s) { /* char or string */
  1062. if (ppfs->conv_num == CONV_s) { /* string */
  1063. s = *((char **) (*argptr));
  1064. if (s) {
  1065. SET_STRING_LEN:
  1066. slen = strnlen(s, ((ppfs->info.prec >= 0)
  1067. ? ppfs->info.prec : SIZE_MAX));
  1068. } else {
  1069. s = "(null)";
  1070. slen = 6;
  1071. }
  1072. } else { /* char */
  1073. s = (char *) buf;
  1074. *s = (unsigned char)(*((const int *) *argptr));
  1075. s[1] = 0;
  1076. slen = 1;
  1077. }
  1078. #ifdef __STDIO_PRINTF_M_SUPPORT
  1079. } else if (ppfs->conv_num == CONV_m) {
  1080. s = _stdio_strerror_r(errno, buf, sizeof(buf));
  1081. goto SET_STRING_LEN;
  1082. #endif
  1083. } else {
  1084. assert(ppfs->conv_num == CONV_custom0);
  1085. s = _custom_printf_spec;
  1086. do {
  1087. if (*s == ppfs->info.spec) {
  1088. int rv;
  1089. /* TODO -- check return value for sanity? */
  1090. rv = (*_custom_printf_handler
  1091. [(int)(s-_custom_printf_spec)])
  1092. (stream, &ppfs->info, argptr);
  1093. if (rv < 0) {
  1094. return -1;
  1095. }
  1096. *count += rv;
  1097. return 0;
  1098. }
  1099. } while (++s < (_custom_printf_spec + MAX_USER_SPEC));
  1100. assert(0);
  1101. return -1;
  1102. }
  1103. {
  1104. size_t t;
  1105. t = slen + numfill;
  1106. if (prefix_num != PREFIX_NONE) {
  1107. t += ((prefix_num < PREFIX_LWR_X) ? 1 : 2);
  1108. }
  1109. numpad = ((ppfs->info.width > t) ? (ppfs->info.width - t) : 0);
  1110. *count += t + numpad;
  1111. }
  1112. if (padchar == '0') { /* TODO: check this */
  1113. numfill += numpad;
  1114. numpad = 0;
  1115. }
  1116. /* Now handle the output itself. */
  1117. if (!PRINT_INFO_FLAG_VAL(&(ppfs->info),left)) {
  1118. _charpad(stream, ' ', numpad);
  1119. numpad = 0;
  1120. }
  1121. output(stream, prefix + prefix_num);
  1122. _charpad(stream, '0', numfill);
  1123. _outnstr(stream, s, slen);
  1124. _charpad(stream, ' ', numpad);
  1125. }
  1126. return 0;
  1127. }
  1128. #endif
  1129. /**********************************************************************/
  1130. #ifdef L_vsnprintf
  1131. #ifdef __STDIO_BUFFERS
  1132. int vsnprintf(char *__restrict buf, size_t size,
  1133. const char * __restrict format, va_list arg)
  1134. {
  1135. FILE f;
  1136. int rv;
  1137. #ifdef __STDIO_GETC_MACRO
  1138. f.bufgetc =
  1139. #endif
  1140. f.bufpos = f.bufread = f.bufstart = buf;
  1141. if (size > SIZE_MAX - (size_t) buf) {
  1142. size = SIZE_MAX - (size_t) buf;
  1143. }
  1144. #ifdef __STDIO_PUTC_MACRO
  1145. f.bufputc =
  1146. #endif
  1147. f.bufend = buf + size;
  1148. #if 0 /* shouldn't be necessary */
  1149. /* #ifdef __STDIO_GLIBC_CUSTOM_STREAMS */
  1150. f.cookie = &(f.filedes);
  1151. f.gcs.read = 0;
  1152. f.gcs.write = 0;
  1153. f.gcs.seek = 0;
  1154. f.gcs.close = 0;
  1155. #endif
  1156. f.filedes = -2; /* for debugging */
  1157. f.modeflags = (__FLAG_NARROW|__FLAG_WRITEONLY|__FLAG_WRITING);
  1158. #ifdef __STDIO_MBSTATE
  1159. __INIT_MBSTATE(&(f.state));
  1160. #endif /* __STDIO_MBSTATE */
  1161. #ifdef __STDIO_THREADSAFE
  1162. __stdio_init_mutex(&f.lock);
  1163. #endif
  1164. rv = vfprintf(&f, format, arg);
  1165. if (size) {
  1166. if (f.bufpos == f.bufend) {
  1167. --f.bufpos;
  1168. }
  1169. *f.bufpos = 0;
  1170. }
  1171. return rv;
  1172. }
  1173. #else /* __STDIO_BUFFERS */
  1174. #ifdef __STDIO_GLIBC_CUSTOM_STREAMS
  1175. typedef struct {
  1176. size_t pos;
  1177. size_t len;
  1178. unsigned char *buf;
  1179. FILE *fp;
  1180. } __snpf_cookie;
  1181. #define COOKIE ((__snpf_cookie *) cookie)
  1182. static ssize_t snpf_write(register void *cookie, const char *buf,
  1183. size_t bufsize)
  1184. {
  1185. size_t count;
  1186. register char *p;
  1187. /* Note: bufsize < SSIZE_MAX because of _stdio_WRITE. */
  1188. if (COOKIE->len > COOKIE->pos) {
  1189. count = COOKIE->len - COOKIE->pos - 1; /* Leave space for nul. */
  1190. if (count > bufsize) {
  1191. count = bufsize;
  1192. }
  1193. p = COOKIE->buf + COOKIE->pos;
  1194. while (count) {
  1195. *p++ = *buf++;
  1196. --count;
  1197. }
  1198. *p = 0;
  1199. }
  1200. COOKIE->pos += bufsize;
  1201. return bufsize;
  1202. }
  1203. #undef COOKIE
  1204. int vsnprintf(char *__restrict buf, size_t size,
  1205. const char * __restrict format, va_list arg)
  1206. {
  1207. FILE f;
  1208. __snpf_cookie cookie;
  1209. int rv;
  1210. cookie.buf = buf;
  1211. cookie.len = size;
  1212. cookie.pos = 0;
  1213. cookie.fp = &f;
  1214. f.cookie = &cookie;
  1215. f.gcs.write = snpf_write;
  1216. f.gcs.read = NULL;
  1217. f.gcs.seek = NULL;
  1218. f.gcs.close = NULL;
  1219. f.filedes = -1; /* For debugging. */
  1220. f.modeflags = (__FLAG_NARROW|__FLAG_WRITEONLY|__FLAG_WRITING);
  1221. #ifdef __STDIO_MBSTATE
  1222. __INIT_MBSTATE(&(f.state));
  1223. #endif /* __STDIO_MBSTATE */
  1224. #ifdef __STDIO_THREADSAFE
  1225. __stdio_init_mutex(&f.lock);
  1226. #endif
  1227. rv = vfprintf(&f, format, arg);
  1228. return rv;
  1229. }
  1230. #else /* __STDIO_GLIBC_CUSTOM_STREAMS */
  1231. #warning skipping vsnprintf since no buffering and no custom streams!
  1232. #endif /* __STDIO_GLIBC_CUSTOM_STREAMS */
  1233. #endif /* __STDIO_BUFFERS */
  1234. #endif
  1235. /**********************************************************************/
  1236. #ifdef L_vdprintf
  1237. int vdprintf(int filedes, const char * __restrict format, va_list arg)
  1238. {
  1239. FILE f;
  1240. int rv;
  1241. #ifdef __STDIO_BUFFERS
  1242. char buf[64]; /* TODO: provide _optional_ buffering? */
  1243. #ifdef __STDIO_GETC_MACRO
  1244. f.bufgetc =
  1245. #endif
  1246. f.bufpos = f.bufread = f.bufstart = buf;
  1247. #ifdef __STDIO_PUTC_MACRO
  1248. f.bufputc =
  1249. #endif
  1250. f.bufend = buf + sizeof(buf);
  1251. #endif /* __STDIO_BUFFERS */
  1252. #ifdef __STDIO_GLIBC_CUSTOM_STREAMS
  1253. f.cookie = &(f.filedes);
  1254. f.gcs.read = _cs_read;
  1255. f.gcs.write = _cs_write;
  1256. f.gcs.seek = 0; /* The internal seek func handles normals. */
  1257. f.gcs.close = _cs_close;
  1258. #endif
  1259. f.filedes = filedes;
  1260. f.modeflags = (__FLAG_NARROW|__FLAG_WRITEONLY|__FLAG_WRITING);
  1261. #ifdef __STDIO_MBSTATE
  1262. __INIT_MBSTATE(&(f.state));
  1263. #endif /* __STDIO_MBSTATE */
  1264. #ifdef __STDIO_THREADSAFE
  1265. __stdio_init_mutex(&f.lock);
  1266. #endif
  1267. rv = vfprintf(&f, format, arg);
  1268. return fflush(&f) ? -1 : rv;
  1269. }
  1270. #endif
  1271. /**********************************************************************/
  1272. #ifdef L_vasprintf
  1273. #if !defined(__STDIO_BUFFERS) && !defined(__STDIO_GLIBC_CUSTOM_STREAMS)
  1274. #warning skipping vasprintf since no buffering and no custom streams!
  1275. #else
  1276. int vasprintf(char **__restrict buf, const char * __restrict format,
  1277. va_list arg)
  1278. {
  1279. /* TODO -- change the default implementation? */
  1280. #ifndef __STDIO_GLIBC_CUSTOM_STREAMS
  1281. /* This implementation actually calls the printf machinery twice, but only
  1282. * only does one malloc. This can be a problem though when custom printf
  1283. * specs or the %m specifier are involved because the results of the
  1284. * second call might be different from the first. */
  1285. int rv = vsnprintf(NULL, 0, format, arg);
  1286. return (((rv >= 0) && ((*buf = malloc(++rv)) != NULL))
  1287. ? vsnprintf(*buf, rv, format, arg)
  1288. : -1);
  1289. #else
  1290. FILE *f;
  1291. size_t size;
  1292. int rv;
  1293. /* TODO - do the memstream stuff inline here to avoid fclose and the openlist? */
  1294. if ((f = open_memstream(buf, &size)) == NULL) {
  1295. return -1;
  1296. }
  1297. rv = vfprintf(f, format, arg);
  1298. fclose(f);
  1299. if (rv < 0) {
  1300. free(*buf);
  1301. *buf = NULL;
  1302. return -1;
  1303. }
  1304. return rv;
  1305. #endif
  1306. }
  1307. #endif
  1308. #endif
  1309. /**********************************************************************/
  1310. #ifdef L_vprintf
  1311. int vprintf(const char * __restrict format, va_list arg)
  1312. {
  1313. return vfprintf(stdout, format, arg);
  1314. }
  1315. #endif
  1316. /**********************************************************************/
  1317. #ifdef L_vsprintf
  1318. #if !defined(__STDIO_BUFFERS) && !defined(__STDIO_GLIBC_CUSTOM_STREAMS)
  1319. #warning skipping vsprintf since no buffering and no custom streams!
  1320. #else
  1321. int vsprintf(char *__restrict buf, const char * __restrict format,
  1322. va_list arg)
  1323. {
  1324. return vsnprintf(buf, SIZE_MAX, format, arg);
  1325. }
  1326. #endif
  1327. #endif
  1328. /**********************************************************************/
  1329. #ifdef L_fprintf
  1330. int fprintf(FILE * __restrict stream, const char * __restrict format, ...)
  1331. {
  1332. va_list arg;
  1333. int rv;
  1334. va_start(arg, format);
  1335. rv = vfprintf(stream, format, arg);
  1336. va_end(arg);
  1337. return rv;
  1338. }
  1339. #endif
  1340. /**********************************************************************/
  1341. #ifdef L_snprintf
  1342. #if !defined(__STDIO_BUFFERS) && !defined(__STDIO_GLIBC_CUSTOM_STREAMS)
  1343. #warning skipping snprintf since no buffering and no custom streams!
  1344. #else
  1345. int snprintf(char *__restrict buf, size_t size,
  1346. const char * __restrict format, ...)
  1347. {
  1348. va_list arg;
  1349. int rv;
  1350. va_start(arg, format);
  1351. rv = vsnprintf(buf, size, format, arg);
  1352. va_end(arg);
  1353. return rv;
  1354. }
  1355. #endif
  1356. #endif
  1357. /**********************************************************************/
  1358. #ifdef L_dprintf
  1359. int dprintf(int filedes, const char * __restrict format, ...)
  1360. {
  1361. va_list arg;
  1362. int rv;
  1363. va_start(arg, format);
  1364. rv = vdprintf(filedes, format, arg);
  1365. va_end(arg);
  1366. return rv;
  1367. }
  1368. #endif
  1369. /**********************************************************************/
  1370. #ifdef L_asprintf
  1371. #if !defined(__STDIO_BUFFERS) && !defined(__STDIO_GLIBC_CUSTOM_STREAMS)
  1372. #warning skipping asprintf and __asprintf since no buffering and no custom streams!
  1373. #else
  1374. weak_alias(__asprintf,asprintf)
  1375. int __asprintf(char **__restrict buf, const char * __restrict format, ...)
  1376. {
  1377. va_list arg;
  1378. int rv;
  1379. va_start(arg, format);
  1380. rv = vasprintf(buf, format, arg);
  1381. va_end(arg);
  1382. return rv;
  1383. }
  1384. #endif
  1385. #endif
  1386. /**********************************************************************/
  1387. #ifdef L_printf
  1388. int printf(const char * __restrict format, ...)
  1389. {
  1390. va_list arg;
  1391. int rv;
  1392. va_start(arg, format);
  1393. rv = vfprintf(stdout, format, arg);
  1394. va_end(arg);
  1395. return rv;
  1396. }
  1397. #endif
  1398. /**********************************************************************/
  1399. #ifdef L_sprintf
  1400. #if !defined(__STDIO_BUFFERS) && !defined(__STDIO_GLIBC_CUSTOM_STREAMS)
  1401. #warning skipping sprintf since no buffering and no custom streams!
  1402. #else
  1403. int sprintf(char *__restrict buf, const char * __restrict format, ...)
  1404. {
  1405. va_list arg;
  1406. int rv;
  1407. va_start(arg, format);
  1408. rv = vsnprintf(buf, SIZE_MAX, format, arg);
  1409. va_end(arg);
  1410. return rv;
  1411. }
  1412. #endif
  1413. #endif
  1414. /**********************************************************************/
  1415. #ifdef L__dtostr
  1416. /*
  1417. * Copyright (C) 2000, 2001 Manuel Novoa III
  1418. *
  1419. * Function: size_t _dtostr(FILE *fp, long double x, struct printf_info *info)
  1420. *
  1421. * This was written for uClibc to provide floating point support for
  1422. * the printf functions. It handles +/- infinity and nan on i386.
  1423. *
  1424. * Notes:
  1425. *
  1426. * At most MAX_DIGITS significant digits are kept. Any trailing digits
  1427. * are treated as 0 as they are really just the results of rounding noise
  1428. * anyway. If you want to do better, use an arbitary precision arithmetic
  1429. * package. ;-)
  1430. *
  1431. * It should also be fairly portable, as not assumptions are made about the
  1432. * bit-layout of doubles.
  1433. *
  1434. * It should be too difficult to convert this to handle long doubles on i386.
  1435. * For information, see the comments below.
  1436. *
  1437. * TODO:
  1438. * long double and/or float version? (note: for float can trim code some).
  1439. *
  1440. * Decrease the size. This is really much bigger than I'd like.
  1441. */
  1442. /*****************************************************************************/
  1443. /* Don't change anything that follows unless you know what you're doing. */
  1444. /*****************************************************************************/
  1445. /*
  1446. * Configuration for the scaling power table. Ignoring denormals, you
  1447. * should have 2**EXP_TABLE_SIZE >= LDBL_MAX_EXP >= 2**(EXP_TABLE_SIZE-1).
  1448. * The minimum for standard C is 6. For IEEE 8bit doubles, 9 suffices.
  1449. * For long doubles on i386, use 13.
  1450. */
  1451. #define EXP_TABLE_SIZE 13
  1452. /*
  1453. * Set this to the maximum number of digits you want converted.
  1454. * Conversion is done in blocks of DIGITS_PER_BLOCK (9 by default) digits.
  1455. * (20) 17 digits suffices to uniquely determine a (long) double on i386.
  1456. */
  1457. #define MAX_DIGITS 20
  1458. /*
  1459. * Set this to the smallest integer type capable of storing a pointer.
  1460. */
  1461. #define INT_OR_PTR int
  1462. /*
  1463. * This is really only used to check for infinities. The macro produces
  1464. * smaller code for i386 and, since this is tested before any floating point
  1465. * calculations, it doesn't appear to suffer from the excess precision problem
  1466. * caused by the FPU that strtod had. If it causes problems, call the function
  1467. * and compile zoicheck.c with -ffloat-store.
  1468. */
  1469. #define _zero_or_inf_check(x) ( x == (x/4) )
  1470. /*
  1471. * Fairly portable nan check. Bitwise for i386 generated larger code.
  1472. * If you have a better version, comment this out.
  1473. */
  1474. #define isnan(x) (x != x)
  1475. /*****************************************************************************/
  1476. /* Don't change anything that follows peroid!!! ;-) */
  1477. /*****************************************************************************/
  1478. #include <float.h>
  1479. /*****************************************************************************/
  1480. /*
  1481. * Set things up for the scaling power table.
  1482. */
  1483. #if EXP_TABLE_SIZE < 6
  1484. #error EXP_TABLE_SIZE should be at least 6 to comply with standards
  1485. #endif
  1486. #define EXP_TABLE_MAX (1U<<(EXP_TABLE_SIZE-1))
  1487. /*
  1488. * Only bother checking if this is too small.
  1489. */
  1490. #if LDBL_MAX_10_EXP/2 > EXP_TABLE_MAX
  1491. #error larger EXP_TABLE_SIZE needed
  1492. #endif
  1493. /*
  1494. * With 32 bit ints, we can get 9 digits per block.
  1495. */
  1496. #define DIGITS_PER_BLOCK 9
  1497. #if UINT_MAX >= 4294967295UL
  1498. #define DIGIT_BLOCK_TYPE int
  1499. #define DB_FMT "%.*d"
  1500. #elif LONG_MAX >= 4294967295UL
  1501. #define DIGIT_BLOCK_TYPE long
  1502. #define DB_FMT "%.*ld"
  1503. #else
  1504. #error need at least 32 bit longs
  1505. #endif
  1506. /* Maximum number of calls to fnprintf to output double. */
  1507. #define MAX_CALLS 8
  1508. /*****************************************************************************/
  1509. #define NUM_DIGIT_BLOCKS ((MAX_DIGITS+DIGITS_PER_BLOCK-1)/DIGITS_PER_BLOCK)
  1510. /* extra space for '-', '.', 'e+###', and nul */
  1511. #define BUF_SIZE ( 3 + NUM_DIGIT_BLOCKS * DIGITS_PER_BLOCK )
  1512. /*****************************************************************************/
  1513. static const char *fmts[] = {
  1514. "%0*d", "%.*s", ".", "inf", "INF", "nan", "NAN", "%*s"
  1515. };
  1516. /*****************************************************************************/
  1517. size_t _dtostr(FILE * fp, long double x, struct printf_info *info)
  1518. {
  1519. long double exp_table[EXP_TABLE_SIZE];
  1520. long double p10;
  1521. DIGIT_BLOCK_TYPE digit_block; /* int of at least 32 bits */
  1522. int i, j;
  1523. int round, o_exp;
  1524. int exp, exp_neg;
  1525. int width, preci;
  1526. char *s;
  1527. char *e;
  1528. char buf[BUF_SIZE];
  1529. INT_OR_PTR pc_fwi[2*MAX_CALLS];
  1530. INT_OR_PTR *ppc;
  1531. char exp_buf[8];
  1532. char drvr[8];
  1533. char *pdrvr;
  1534. int npc;
  1535. int cnt;
  1536. char sign_str[2];
  1537. char o_mode;
  1538. char mode;
  1539. /* check that INT_OR_PTR is sufficiently large */
  1540. assert( sizeof(INT_OR_PTR) == sizeof(char *) );
  1541. width = info->width;
  1542. preci = info->prec;
  1543. mode = info->spec;
  1544. if (mode == 'a') {
  1545. mode = 'g'; /* TODO -- fix */
  1546. }
  1547. if (mode == 'A') {
  1548. mode = 'G'; /* TODO -- fix */
  1549. }
  1550. if (preci < 0) {
  1551. preci = 6;
  1552. }
  1553. *sign_str = '\0';
  1554. if (PRINT_INFO_FLAG_VAL(info,showsign)) {
  1555. *sign_str = '+';
  1556. } else if (PRINT_INFO_FLAG_VAL(info,space)) {
  1557. *sign_str = ' ';
  1558. }
  1559. /* *sign_str = flag[FLAG_PLUS]; */
  1560. *(sign_str+1) = 0;
  1561. if (isnan(x)) { /* nan check */
  1562. pdrvr = drvr + 1;
  1563. *pdrvr++ = 5 + (mode < 'a');
  1564. pc_fwi[2] = 3;
  1565. info->pad = ' ';
  1566. /* flag[FLAG_0_PAD] = 0; */
  1567. goto EXIT_SPECIAL;
  1568. }
  1569. if (x == 0) { /* handle 0 now to avoid false positive */
  1570. exp = -1;
  1571. goto GENERATE_DIGITS;
  1572. }
  1573. if (x < 0) { /* convert negatives to positives */
  1574. *sign_str = '-';
  1575. x = -x;
  1576. }
  1577. if (_zero_or_inf_check(x)) { /* must be inf since zero handled above */
  1578. pdrvr = drvr + 1;
  1579. *pdrvr++ = 3 + + (mode < 'a');
  1580. pc_fwi[2] = 3;
  1581. info->pad = ' ';
  1582. /* flag[FLAG_0_PAD] = 0; */
  1583. goto EXIT_SPECIAL;
  1584. }
  1585. /* need to build the scaling table */
  1586. for (i = 0, p10 = 10 ; i < EXP_TABLE_SIZE ; i++) {
  1587. exp_table[i] = p10;
  1588. p10 *= p10;
  1589. }
  1590. exp_neg = 0;
  1591. if (x < 1e8) { /* do we need to scale up or down? */
  1592. exp_neg = 1;
  1593. }
  1594. exp = DIGITS_PER_BLOCK - 1;
  1595. i = EXP_TABLE_SIZE;
  1596. j = EXP_TABLE_MAX;
  1597. while ( i-- ) { /* scale x such that 1e8 <= x < 1e9 */
  1598. if (exp_neg) {
  1599. if (x * exp_table[i] < 1e9) {
  1600. x *= exp_table[i];
  1601. exp -= j;
  1602. }
  1603. } else {
  1604. if (x / exp_table[i] >= 1e8) {
  1605. x /= exp_table[i];
  1606. exp += j;
  1607. }
  1608. }
  1609. j >>= 1;
  1610. }
  1611. if (x >= 1e9) { /* handle bad rounding case */
  1612. x /= 10;
  1613. ++exp;
  1614. }
  1615. assert(x < 1e9);
  1616. GENERATE_DIGITS:
  1617. s = buf + 2; /* leave space for '\0' and '0' */
  1618. #if 1
  1619. #define ONE_E_NINE 1000000000L
  1620. #else
  1621. #define ONE_E_NINE 1e9
  1622. #endif
  1623. for (i = 0 ; i < NUM_DIGIT_BLOCKS ; ++i ) {
  1624. digit_block = (DIGIT_BLOCK_TYPE) x;
  1625. x = (x - digit_block) * ONE_E_NINE;
  1626. s += sprintf(s, DB_FMT, DIGITS_PER_BLOCK, digit_block);
  1627. }
  1628. /*************************************************************************/
  1629. *exp_buf = 'e';
  1630. if (mode < 'a') {
  1631. *exp_buf = 'E';
  1632. mode += ('a' - 'A');
  1633. }
  1634. o_mode = mode;
  1635. round = preci;
  1636. if ((mode == 'g') && (round > 0)){
  1637. --round;
  1638. }
  1639. if (mode == 'f') {
  1640. round += exp;
  1641. }
  1642. s = buf;
  1643. *s++ = 0; /* terminator for rounding and 0-triming */
  1644. *s = '0'; /* space to round */
  1645. i = 0;
  1646. e = s + MAX_DIGITS + 1;
  1647. if (round < MAX_DIGITS) {
  1648. e = s + round + 2;
  1649. if (*e >= '5') {
  1650. i = 1;
  1651. }
  1652. }
  1653. do { /* handle rounding and trim trailing 0s */
  1654. *--e += i; /* add the carry */
  1655. } while ((*e == '0') || (*e > '9'));
  1656. o_exp = exp;
  1657. if (e <= s) { /* we carried into extra digit */
  1658. ++o_exp;
  1659. e = s; /* needed if all 0s */
  1660. } else {
  1661. ++s;
  1662. }
  1663. *++e = 0; /* ending nul char */
  1664. if ((mode == 'g') && ((o_exp >= -4) && (o_exp <= round))) {
  1665. mode = 'f';
  1666. }
  1667. exp = o_exp;
  1668. if (mode != 'f') {
  1669. o_exp = 0;
  1670. }
  1671. if (o_exp < 0) {
  1672. *--s = '0'; /* fake the first digit */
  1673. }
  1674. pdrvr = drvr+1;
  1675. ppc = pc_fwi+2;
  1676. *pdrvr++ = 0;
  1677. *ppc++ = 1;
  1678. *ppc++ = (INT_OR_PTR)(*s++ - '0');
  1679. i = e - s; /* total digits */
  1680. if (o_exp >= 0) {
  1681. if (o_exp >= i) { /* all digit(s) left of decimal */
  1682. *pdrvr++ = 1;
  1683. *ppc++ = i;
  1684. *ppc++ = (INT_OR_PTR)(s);
  1685. o_exp -= i;
  1686. i = 0;
  1687. if (o_exp>0) { /* have 0s left of decimal */
  1688. *pdrvr++ = 0;
  1689. *ppc++ = o_exp;
  1690. *ppc++ = 0;
  1691. }
  1692. } else if (o_exp > 0) { /* decimal between digits */
  1693. *pdrvr++ = 1;
  1694. *ppc++ = o_exp;
  1695. *ppc++ = (INT_OR_PTR)(s);
  1696. s += o_exp;
  1697. i -= o_exp;
  1698. }
  1699. o_exp = -1;
  1700. }
  1701. if (PRINT_INFO_FLAG_VAL(info,alt)
  1702. /* flag[FLAG_HASH] */
  1703. || (i) || ((o_mode != 'g') && (preci > 0))) {
  1704. *pdrvr++ = 2; /* need decimal */
  1705. *ppc++ = 1; /* needed for width calc */
  1706. ppc++;
  1707. }
  1708. if (++o_exp < 0) { /* have 0s right of decimal */
  1709. *pdrvr++ = 0;
  1710. *ppc++ = -o_exp;
  1711. *ppc++ = 0;
  1712. }
  1713. if (i) { /* have digit(s) right of decimal */
  1714. *pdrvr++ = 1;
  1715. *ppc++ = i;
  1716. *ppc++ = (INT_OR_PTR)(s);
  1717. }
  1718. if (o_mode != 'g') {
  1719. i -= o_exp;
  1720. if (i < preci) { /* have 0s right of digits */
  1721. i = preci - i;
  1722. *pdrvr++ = 0;
  1723. *ppc++ = i;
  1724. *ppc++ = 0;
  1725. }
  1726. }
  1727. /* build exponent string */
  1728. if (mode != 'f') {
  1729. *pdrvr++ = 1;
  1730. *ppc++ = sprintf(exp_buf,"%c%+.2d", *exp_buf, exp);
  1731. *ppc++ = (INT_OR_PTR) exp_buf;
  1732. }
  1733. EXIT_SPECIAL:
  1734. npc = pdrvr - drvr;
  1735. ppc = pc_fwi + 2;
  1736. for (i=1 ; i< npc ; i++) {
  1737. width -= *(ppc++);
  1738. ppc++;
  1739. }
  1740. i = 0;
  1741. if (*sign_str) {
  1742. i = 1;
  1743. }
  1744. width -= i;
  1745. if (width <= 0) {
  1746. width = 0;
  1747. } else {
  1748. if (PRINT_INFO_FLAG_VAL(info,left)) { /* padding on right */
  1749. /* flag[FLAG_MINUS_LJUSTIFY] */
  1750. ++npc;
  1751. *pdrvr++ = 7;
  1752. *ppc = width;
  1753. *++ppc = (INT_OR_PTR)("");
  1754. width = 0;
  1755. } else if (info->pad == '0') { /* 0 padding */
  1756. /* (flag[FLAG_0_PAD] == '0') */
  1757. pc_fwi[2] += width;
  1758. width = 0;
  1759. }
  1760. }
  1761. *drvr = 7;
  1762. ppc = pc_fwi;
  1763. *ppc++ = width + i;
  1764. *ppc = (INT_OR_PTR) sign_str;
  1765. pdrvr = drvr;
  1766. ppc = pc_fwi;
  1767. cnt = 0;
  1768. for (i=0 ; i<npc ; i++) {
  1769. #if 1
  1770. fprintf(fp, fmts[(int)(*pdrvr++)], (INT_OR_PTR)(*(ppc)),
  1771. (INT_OR_PTR)(*(ppc+1)));
  1772. #else
  1773. j = fprintf(fp, fmts[(int)(*pdrvr++)], (INT_OR_PTR)(*(ppc)),
  1774. (INT_OR_PTR)(*(ppc+1)));
  1775. assert(j == *ppc);
  1776. #endif
  1777. /* if (size > *ppc) { */
  1778. /* size -= *ppc; */
  1779. /* } */
  1780. cnt += *ppc; /* to avoid problems if j == -1 */
  1781. ppc += 2;
  1782. }
  1783. return cnt;
  1784. }
  1785. #endif
  1786. /**********************************************************************/
  1787. #ifdef L__store_inttype
  1788. /* TODO -- right now, assumes intmax_t is either long or long long */
  1789. /* We assume int may be short or long, but short and long are different. */
  1790. void _store_inttype(register void *dest, int desttype, uintmax_t val)
  1791. {
  1792. if (desttype == __PA_FLAG_CHAR) { /* assume char not int */
  1793. *((unsigned char *) dest) = val;
  1794. return;
  1795. }
  1796. #if defined(LLONG_MAX) && (LONG_MAX != LLONG_MAX)
  1797. if (desttype == PA_FLAG_LONG_LONG) {
  1798. *((unsigned long long int *) dest) = val;
  1799. return;
  1800. }
  1801. #endif /* LLONG_MAX */
  1802. #if SHRT_MAX != INT_MAX
  1803. if (desttype == PA_FLAG_SHORT) {
  1804. *((unsigned short int *) dest) = val;
  1805. return;
  1806. }
  1807. #endif /* SHRT_MAX */
  1808. #if LONG_MAX != INT_MAX
  1809. if (desttype == PA_FLAG_LONG) {
  1810. *((unsigned long int *) dest) = val;
  1811. return;
  1812. }
  1813. #endif /* LONG_MAX */
  1814. *((unsigned int *) dest) = val;
  1815. }
  1816. #endif
  1817. /**********************************************************************/
  1818. #ifdef L__load_inttype
  1819. extern uintmax_t _load_inttype(int desttype, register const void *src,
  1820. int uflag)
  1821. {
  1822. if (uflag >= 0) { /* unsigned */
  1823. #if LONG_MAX != INT_MAX
  1824. if (desttype & (PA_FLAG_LONG|PA_FLAG_LONG_LONG)) {
  1825. #ifdef LLONG_MAX
  1826. if (desttype == PA_FLAG_LONG_LONG) {
  1827. return *((unsigned long long int *) src);
  1828. }
  1829. #endif
  1830. return *((unsigned long int *) src);
  1831. }
  1832. #else /* LONG_MAX != INT_MAX */
  1833. #ifdef LLONG_MAX
  1834. if (desttype & PA_FLAG_LONG_LONG) {
  1835. return *((unsigned long long int *) src);
  1836. }
  1837. #endif
  1838. #endif /* LONG_MAX != INT_MAX */
  1839. {
  1840. unsigned int x;
  1841. x = *((unsigned int *) src);
  1842. if (desttype == __PA_FLAG_CHAR) x = (unsigned char) x;
  1843. #if SHRT_MAX != INT_MAX
  1844. if (desttype == PA_FLAG_SHORT) x = (unsigned short int) x;
  1845. #endif
  1846. return x;
  1847. }
  1848. } else { /* signed */
  1849. #if LONG_MAX != INT_MAX
  1850. if (desttype & (PA_FLAG_LONG|PA_FLAG_LONG_LONG)) {
  1851. #ifdef LLONG_MAX
  1852. if (desttype == PA_FLAG_LONG_LONG) {
  1853. return *((long long int *) src);
  1854. }
  1855. #endif
  1856. return *((long int *) src);
  1857. }
  1858. #else /* LONG_MAX != INT_MAX */
  1859. #ifdef LLONG_MAX
  1860. if (desttype & PA_FLAG_LONG_LONG) {
  1861. return *((long long int *) src);
  1862. }
  1863. #endif
  1864. #endif /* LONG_MAX != INT_MAX */
  1865. {
  1866. int x;
  1867. x = *((int *) src);
  1868. if (desttype == __PA_FLAG_CHAR) x = (char) x;
  1869. #if SHRT_MAX != INT_MAX
  1870. if (desttype == PA_FLAG_SHORT) x = (short int) x;
  1871. #endif
  1872. return x;
  1873. }
  1874. }
  1875. }
  1876. #endif
  1877. /**********************************************************************/