printf.c 54 KB

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