printf.c 57 KB

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