printf.c 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188
  1. /* Copyright (C) 2002, 2003 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. * Nov 21, 2002
  60. * Add *wprintf functions. Currently they don't support floating point
  61. * conversions. That will wait until the rewrite of _dtostr.
  62. *
  63. * Aug 1, 2003
  64. * Optional hexadecimal float notation support for %a/%A.
  65. * Floating point output now works for *wprintf.
  66. * Support for glibc locale-specific digit grouping for floats.
  67. * Misc bug fixes.
  68. */
  69. /* TODO:
  70. *
  71. * Should we validate that *printf format strings are valid multibyte
  72. * strings in the current locale? ANSI/ISO C99 seems to imply this
  73. * and Plauger's printf implementation in his Standard C Library book
  74. * treats this as an error.
  75. *
  76. * Implement %a, %A, and locale-specific grouping for the printf floating
  77. * point conversions. To be done in the rewrite of _dtostr().
  78. */
  79. #define _ISOC99_SOURCE /* for ULLONG primarily... */
  80. #define _GNU_SOURCE
  81. #define _STDIO_UTILITY /* We're using _uintmaxtostr. */
  82. #include <stdio.h>
  83. #include <stdlib.h>
  84. #include <string.h>
  85. #include <stddef.h>
  86. #include <ctype.h>
  87. #include <limits.h>
  88. #include <stdarg.h>
  89. #include <assert.h>
  90. #include <stdint.h>
  91. #include <errno.h>
  92. #include <locale.h>
  93. #define __PRINTF_INFO_NO_BITFIELD
  94. #include <printf.h>
  95. #ifdef __STDIO_THREADSAFE
  96. #include <stdio_ext.h>
  97. #include <pthread.h>
  98. #endif /* __STDIO_THREADSAFE */
  99. #ifdef __UCLIBC_HAS_WCHAR__
  100. #include <wchar.h>
  101. #endif /* __UCLIBC_HAS_WCHAR__ */
  102. /* Some older or broken gcc toolchains define LONG_LONG_MAX but not
  103. * LLONG_MAX. Since LLONG_MAX is part of the standard, that's what
  104. * we use. So complain if we do not have it but should.
  105. */
  106. #if !defined(LLONG_MAX) && defined(LONG_LONG_MAX)
  107. #error Apparently, LONG_LONG_MAX is defined but LLONG_MAX is not. You need to fix your toolchain headers to support the standard macros for (unsigned) long long.
  108. #endif
  109. /**********************************************************************/
  110. /* These provide some control over printf's feature set */
  111. /* This is undefined below depeding on uClibc's configuration. */
  112. #define __STDIO_PRINTF_FLOAT 1
  113. /* Now controlled by uClibc_stdio.h. */
  114. /* #define __STDIO_PRINTF_M_SUPPORT */
  115. /**********************************************************************/
  116. #if defined(__UCLIBC__) && !defined(__UCLIBC_HAS_FLOATS__)
  117. #undef __STDIO_PRINTF_FLOAT
  118. #endif
  119. #ifdef __BCC__
  120. #undef __STDIO_PRINTF_FLOAT
  121. #endif
  122. #ifdef __STDIO_PRINTF_FLOAT
  123. #include <float.h>
  124. #include <bits/uClibc_fpmax.h>
  125. #else /* __STDIO_PRINTF_FLOAT */
  126. #undef L__fpmaxtostr
  127. #endif /* __STDIO_PRINTF_FLOAT */
  128. /**********************************************************************/
  129. /* Now controlled by uClibc_stdio.h. */
  130. /* #define __STDIO_GLIBC_CUSTOM_PRINTF */
  131. /* TODO -- move these to a configuration section? */
  132. #define MAX_FIELD_WIDTH 4095
  133. #ifdef __UCLIBC_MJN3_ONLY__
  134. #ifdef L_register_printf_function
  135. /* emit only once */
  136. #warning WISHLIST: Make MAX_USER_SPEC configurable?
  137. #warning WISHLIST: Make MAX_ARGS_PER_SPEC configurable?
  138. #endif
  139. #endif /* __UCLIBC_MJN3_ONLY__ */
  140. #ifdef __STDIO_GLIBC_CUSTOM_PRINTF
  141. #define MAX_USER_SPEC 10
  142. #define MAX_ARGS_PER_SPEC 5
  143. #else /* __STDIO_GLIBC_CUSTOM_PRINTF */
  144. #undef MAX_USER_SPEC
  145. #define MAX_ARGS_PER_SPEC 1
  146. #endif /* __STDIO_GLIBC_CUSTOM_PRINTF */
  147. #if MAX_ARGS_PER_SPEC < 1
  148. #error MAX_ARGS_PER_SPEC < 1!
  149. #undef MAX_ARGS_PER_SPEC
  150. #define MAX_ARGS_PER_SPEC 1
  151. #endif
  152. #if defined(NL_ARGMAX) && (NL_ARGMAX < 9)
  153. #error NL_ARGMAX < 9!
  154. #endif
  155. #if defined(NL_ARGMAX) && (NL_ARGMAX >= (MAX_ARGS_PER_SPEC + 2))
  156. #define MAX_ARGS NL_ARGMAX
  157. #else
  158. /* N for spec itself, plus 1 each for width and precision */
  159. #define MAX_ARGS (MAX_ARGS_PER_SPEC + 2)
  160. #endif
  161. /**********************************************************************/
  162. /* Deal with pre-C99 compilers. */
  163. #ifndef va_copy
  164. #ifdef __va_copy
  165. #define va_copy(A,B) __va_copy(A,B)
  166. #else
  167. /* TODO -- maybe create a bits/vacopy.h for arch specific versions
  168. * to ensure we get the right behavior? Either that or fall back
  169. * on the portable (but costly in size) method of using a va_list *.
  170. * That means a pointer derefs in the va_arg() invocations... */
  171. #warning Neither va_copy (C99/SUSv3) or __va_copy is defined. Using a simple copy instead. But you should really check that this is appropriate...
  172. /* the glibc manual suggests that this will usually suffice when
  173. __va_copy doesn't exist. */
  174. #define va_copy(A,B) A = B
  175. #endif
  176. #endif /* va_copy */
  177. /**********************************************************************/
  178. #define __PA_FLAG_INTMASK \
  179. (__PA_FLAG_CHAR|PA_FLAG_SHORT|__PA_FLAG_INT|PA_FLAG_LONG|PA_FLAG_LONG_LONG)
  180. #ifdef __STDIO_GLIBC_CUSTOM_PRINTF
  181. extern printf_function _custom_printf_handler[MAX_USER_SPEC];
  182. extern printf_arginfo_function *_custom_printf_arginfo[MAX_USER_SPEC];
  183. extern char *_custom_printf_spec;
  184. #endif /* __STDIO_GLIBC_CUSTOM_PRINTF */
  185. /**********************************************************************/
  186. #define SPEC_FLAGS " +0-#'I"
  187. enum {
  188. FLAG_SPACE = 0x01,
  189. FLAG_PLUS = 0x02, /* must be 2 * FLAG_SPACE */
  190. FLAG_ZERO = 0x04,
  191. FLAG_MINUS = 0x08, /* must be 2 * FLAG_ZERO */
  192. FLAG_HASH = 0x10,
  193. FLAG_THOUSANDS = 0x20,
  194. FLAG_I18N = 0x40, /* only works for d, i, u */
  195. FLAG_WIDESTREAM = 0x80
  196. };
  197. /**********************************************************************/
  198. /* float layout 01234567890123456789 TODO: B?*/
  199. #define SPEC_CHARS "npxXoudifFeEgGaACScs"
  200. enum {
  201. CONV_n = 0,
  202. CONV_p,
  203. CONV_x, CONV_X, CONV_o, CONV_u, CONV_d, CONV_i,
  204. CONV_f, CONV_F, CONV_e, CONV_E, CONV_g, CONV_G, CONV_a, CONV_A,
  205. CONV_C, CONV_S, CONV_c, CONV_s,
  206. #ifdef __STDIO_PRINTF_M_SUPPORT
  207. CONV_m,
  208. #endif
  209. CONV_custom0 /* must be last */
  210. };
  211. /* p x X o u d i */
  212. #define SPEC_BASE { 16, 16, 16, 8, 10, 10, 10 }
  213. #define SPEC_RANGES { CONV_n, CONV_p, CONV_i, CONV_A, \
  214. CONV_C, CONV_S, CONV_c, CONV_s, CONV_custom0 }
  215. #define SPEC_OR_MASK { \
  216. /* n */ (PA_FLAG_PTR|PA_INT), \
  217. /* p */ PA_POINTER, \
  218. /* oxXudi */ PA_INT, \
  219. /* fFeEgGaA */ PA_DOUBLE, \
  220. /* C */ PA_WCHAR, \
  221. /* S */ PA_WSTRING, \
  222. /* c */ PA_CHAR, \
  223. /* s */ PA_STRING, \
  224. }
  225. #define SPEC_AND_MASK { \
  226. /* n */ (PA_FLAG_PTR|__PA_INTMASK), \
  227. /* p */ PA_POINTER, \
  228. /* oxXudi */ (__PA_INTMASK), \
  229. /* fFeEgGaA */ (PA_FLAG_LONG_DOUBLE|PA_DOUBLE), \
  230. /* C */ (PA_WCHAR), \
  231. /* S */ (PA_WSTRING), \
  232. /* c */ (PA_CHAR), \
  233. /* s */ (PA_STRING), \
  234. }
  235. /**********************************************************************/
  236. /*
  237. * In order to ease translation to what arginfo and _print_info._flags expect,
  238. * we map: 0:int 1:char 2:longlong 4:long 8:short
  239. * and then _flags |= (((q << 7) + q) & 0x701) and argtype |= (_flags & 0x701)
  240. */
  241. /* TODO -- Fix the table below to take into account stdint.h. */
  242. /* #ifndef LLONG_MAX */
  243. /* #error fix QUAL_CHARS for no long long! Affects 'L', 'j', 'q', 'll'. */
  244. /* #else */
  245. /* #if LLONG_MAX != INTMAX_MAX */
  246. /* #error fix QUAL_CHARS intmax_t entry 'j'! */
  247. /* #endif */
  248. /* #endif */
  249. #ifdef PDS
  250. #error PDS already defined!
  251. #endif
  252. #ifdef SS
  253. #error SS already defined!
  254. #endif
  255. #ifdef IMS
  256. #error IMS already defined!
  257. #endif
  258. #if PTRDIFF_MAX == INT_MAX
  259. #define PDS 0
  260. #elif PTRDIFF_MAX == LONG_MAX
  261. #define PDS 4
  262. #elif defined(LLONG_MAX) && (PTRDIFF_MAX == LLONG_MAX)
  263. #define PDS 8
  264. #else
  265. #error fix QUAL_CHARS ptrdiff_t entry 't'!
  266. #endif
  267. #if SIZE_MAX == UINT_MAX
  268. #define SS 0
  269. #elif SIZE_MAX == ULONG_MAX
  270. #define SS 4
  271. #elif defined(LLONG_MAX) && (SIZE_MAX == ULLONG_MAX)
  272. #define SS 8
  273. #else
  274. #error fix QUAL_CHARS size_t entries 'z', 'Z'!
  275. #endif
  276. #if INTMAX_MAX == INT_MAX
  277. #define IMS 0
  278. #elif INTMAX_MAX == LONG_MAX
  279. #define IMS 4
  280. #elif defined(LLONG_MAX) && (INTMAX_MAX == LLONG_MAX)
  281. #define IMS 8
  282. #else
  283. #error fix QUAL_CHARS intmax_t entry 'j'!
  284. #endif
  285. #define QUAL_CHARS { \
  286. /* j:(u)intmax_t z:(s)size_t t:ptrdiff_t \0:int */ \
  287. /* q:long_long Z:(s)size_t */ \
  288. 'h', 'l', 'L', 'j', 'z', 't', 'q', 'Z', 0, \
  289. 2, 4, 8, IMS, SS, PDS, 8, SS, 0, /* TODO -- fix!!! */\
  290. 1, 8 \
  291. }
  292. /**********************************************************************/
  293. #ifdef __STDIO_VA_ARG_PTR
  294. #ifdef __BCC__
  295. #define __va_arg_ptr(ap,type) (((type *)(ap += sizeof(type))) - 1)
  296. #endif
  297. #if 1
  298. #ifdef __GNUC__
  299. /* TODO -- need other than for 386 as well! */
  300. #ifndef __va_rounded_size
  301. #define __va_rounded_size(TYPE) \
  302. (((sizeof (TYPE) + sizeof (int) - 1) / sizeof (int)) * sizeof (int))
  303. #endif
  304. #define __va_arg_ptr(AP, TYPE) \
  305. (AP = (va_list) ((char *) (AP) + __va_rounded_size (TYPE)), \
  306. ((void *) ((char *) (AP) - __va_rounded_size (TYPE))))
  307. #endif
  308. #endif
  309. #endif /* __STDIO_VA_ARG_PTR */
  310. #ifdef __va_arg_ptr
  311. #define GET_VA_ARG(AP,F,TYPE,ARGS) (*(AP) = __va_arg_ptr(ARGS,TYPE))
  312. #define GET_ARG_VALUE(AP,F,TYPE) (*((TYPE *)(*(AP))))
  313. #else
  314. typedef union {
  315. wchar_t wc;
  316. unsigned int u;
  317. unsigned long ul;
  318. #ifdef ULLONG_MAX
  319. unsigned long long ull;
  320. #endif
  321. #ifdef __STDIO_PRINTF_FLOAT
  322. double d;
  323. long double ld;
  324. #endif /* __STDIO_PRINTF_FLOAT */
  325. void *p;
  326. } argvalue_t;
  327. #define GET_VA_ARG(AU,F,TYPE,ARGS) (AU->F = va_arg(ARGS,TYPE))
  328. #define GET_ARG_VALUE(AU,F,TYPE) ((TYPE)((AU)->F))
  329. #endif
  330. typedef struct {
  331. const char *fmtpos; /* TODO: move below struct?? */
  332. struct printf_info info;
  333. #ifdef NL_ARGMAX
  334. int maxposarg; /* > 0 if args are positional, 0 if not, -1 if unknown */
  335. #endif /* NL_ARGMAX */
  336. int num_data_args; /* TODO: use sentinal??? */
  337. unsigned int conv_num;
  338. unsigned char argnumber[4]; /* width | prec | 1st data | unused */
  339. int argtype[MAX_ARGS];
  340. va_list arg;
  341. #ifdef __va_arg_ptr
  342. void *argptr[MAX_ARGS];
  343. #else
  344. /* if defined(NL_ARGMAX) || defined(__STDIO_GLIBC_CUSTOM_PRINTF) */
  345. /* While this is wasteful of space in the case where pos args aren't
  346. * enabled, it is also needed to support custom printf handlers. */
  347. argvalue_t argvalue[MAX_ARGS];
  348. #endif
  349. } ppfs_t; /* parse printf format state */
  350. /**********************************************************************/
  351. /* TODO: fix printf to return 0 and set errno if format error. Standard says
  352. only returns -1 if sets error indicator for the stream. */
  353. #ifdef __STDIO_PRINTF_FLOAT
  354. typedef void (__fp_outfunc_t)(FILE *fp, intptr_t type, intptr_t len,
  355. intptr_t buf);
  356. extern size_t _fpmaxtostr(FILE * fp, __fpmax_t x, struct printf_info *info,
  357. __fp_outfunc_t fp_outfunc);
  358. #endif
  359. extern int _ppfs_init(ppfs_t *ppfs, const char *fmt0); /* validates */
  360. extern void _ppfs_prepargs(ppfs_t *ppfs, va_list arg); /* sets posargptrs */
  361. extern void _ppfs_setargs(ppfs_t *ppfs); /* sets argptrs for current spec */
  362. extern int _ppfs_parsespec(ppfs_t *ppfs); /* parses specifier */
  363. extern void _store_inttype(void *dest, int desttype, uintmax_t val);
  364. extern uintmax_t _load_inttype(int desttype, const void *src, int uflag);
  365. /**********************************************************************/
  366. #ifdef L_parse_printf_format
  367. /* NOTE: This function differs from the glibc version in that parsing stops
  368. * upon encountering an invalid conversion specifier. Since this is the way
  369. * my printf functions work, I think it makes sense to do it that way here.
  370. * Unfortunately, since glibc sets the return type as size_t, we have no way
  371. * of returning that the template is illegal, other than returning 0.
  372. */
  373. size_t parse_printf_format(register const char *template,
  374. size_t n, register int *argtypes)
  375. {
  376. ppfs_t ppfs;
  377. size_t i;
  378. size_t count = 0;
  379. if (_ppfs_init(&ppfs, template) >= 0) {
  380. #ifdef NL_ARGMAX
  381. if (ppfs.maxposarg > 0) { /* Using positional args. */
  382. count = ppfs.maxposarg;
  383. if (n > count) {
  384. n = count;
  385. }
  386. for (i = 0 ; i < n ; i++) {
  387. *argtypes++ = ppfs.argtype[i];
  388. }
  389. } else { /* Not using positional args. */
  390. #endif /* NL_ARGMAX */
  391. while (*template) {
  392. if ((*template == '%') && (*++template != '%')) {
  393. ppfs.fmtpos = template;
  394. _ppfs_parsespec(&ppfs); /* Can't fail. */
  395. template = ppfs.fmtpos; /* Update to one past spec end. */
  396. if (ppfs.info.width == INT_MIN) {
  397. ++count;
  398. if (n > 0) {
  399. *argtypes++ = PA_INT;
  400. --n;
  401. }
  402. }
  403. if (ppfs.info.prec == INT_MIN) {
  404. ++count;
  405. if (n > 0) {
  406. *argtypes++ = PA_INT;
  407. --n;
  408. }
  409. }
  410. for (i = 0 ; i < ppfs.num_data_args ; i++) {
  411. if ((ppfs.argtype[i]) != __PA_NOARG) {
  412. ++count;
  413. if (n > 0) {
  414. *argtypes++ = ppfs.argtype[i];
  415. --n;
  416. }
  417. }
  418. }
  419. } else {
  420. ++template;
  421. }
  422. }
  423. #ifdef NL_ARGMAX
  424. }
  425. #endif /* NL_ARGMAX */
  426. }
  427. return count;
  428. }
  429. #endif
  430. /**********************************************************************/
  431. #ifdef L__ppfs_init
  432. int _ppfs_init(register ppfs_t *ppfs, const char *fmt0)
  433. {
  434. int r;
  435. /* First, zero out everything... argnumber[], argtype[], argptr[] */
  436. memset(ppfs, 0, sizeof(ppfs_t)); /* TODO: nonportable???? */
  437. #ifdef NL_ARGMAX
  438. --ppfs->maxposarg; /* set to -1 */
  439. #endif /* NL_ARGMAX */
  440. ppfs->fmtpos = fmt0;
  441. #ifdef __UCLIBC_MJN3_ONLY__
  442. #warning TODO: Make checking of the format string in C locale an option.
  443. #endif
  444. #ifdef __UCLIBC_HAS_LOCALE__
  445. /* To support old programs, don't check mb validity if in C locale. */
  446. if (((__UCLIBC_CURLOCALE_DATA).encoding) != __ctype_encoding_7_bit) {
  447. /* ANSI/ISO C99 requires format string to be a valid multibyte string
  448. * beginning and ending in its initial shift state. */
  449. static const char invalid_mbs[] = "Invalid multibyte format string.";
  450. mbstate_t mbstate;
  451. const char *p;
  452. mbstate.mask = 0; /* Initialize the mbstate. */
  453. p = fmt0;
  454. if (mbsrtowcs(NULL, &p, SIZE_MAX, &mbstate) == ((size_t)(-1))) {
  455. ppfs->fmtpos = invalid_mbs;
  456. return -1;
  457. }
  458. }
  459. #endif /* __UCLIBC_HAS_LOCALE__ */
  460. /* now set all argtypes to no-arg */
  461. {
  462. #if 1
  463. /* TODO - use memset here since already "paid for"? */
  464. register int *p = ppfs->argtype;
  465. r = MAX_ARGS;
  466. do {
  467. *p++ = __PA_NOARG;
  468. } while (--r);
  469. #else
  470. /* TODO -- get rid of this?? */
  471. register char *p = (char *) ((MAX_ARGS-1) * sizeof(int));
  472. do {
  473. *((int *)(((char *)ppfs) + ((int)p) + offsetof(ppfs_t,argtype))) = __PA_NOARG;
  474. p -= sizeof(int);
  475. } while (p);
  476. #endif
  477. }
  478. /*
  479. * Run through the entire format string to validate it and initialize
  480. * the positional arg numbers (if any).
  481. */
  482. {
  483. register const char *fmt = fmt0;
  484. while (*fmt) {
  485. if ((*fmt == '%') && (*++fmt != '%')) {
  486. ppfs->fmtpos = fmt; /* back up to the '%' */
  487. if ((r = _ppfs_parsespec(ppfs)) < 0) {
  488. return -1;
  489. }
  490. fmt = ppfs->fmtpos; /* update to one past end of spec */
  491. } else {
  492. ++fmt;
  493. }
  494. }
  495. ppfs->fmtpos = fmt0; /* rewind */
  496. }
  497. #ifdef NL_MAX_ARG
  498. /* If we have positional args, make sure we know all the types. */
  499. {
  500. register int *p = ppfs->argtype;
  501. r = ppfs->maxposarg;
  502. while (--r >= 0) {
  503. if ( *p == __PA_NOARG ) { /* missing arg type!!! */
  504. return -1;
  505. }
  506. ++p;
  507. }
  508. }
  509. #endif /* NL_MAX_ARG */
  510. return 0;
  511. }
  512. #endif
  513. /**********************************************************************/
  514. #ifdef L__ppfs_prepargs
  515. void _ppfs_prepargs(register ppfs_t *ppfs, va_list arg)
  516. {
  517. int i;
  518. va_copy(ppfs->arg, arg);
  519. #ifdef NL_ARGMAX
  520. if ((i = ppfs->maxposarg) > 0) { /* init for positional args */
  521. ppfs->num_data_args = i;
  522. ppfs->info.width = ppfs->info.prec = ppfs->maxposarg = 0;
  523. _ppfs_setargs(ppfs);
  524. ppfs->maxposarg = i;
  525. }
  526. #endif /* NL_ARGMAX */
  527. }
  528. #endif
  529. /**********************************************************************/
  530. #ifdef L__ppfs_setargs
  531. void _ppfs_setargs(register ppfs_t *ppfs)
  532. {
  533. #ifdef __va_arg_ptr
  534. register void **p = ppfs->argptr;
  535. #else
  536. register argvalue_t *p = ppfs->argvalue;
  537. #endif
  538. int i;
  539. #ifdef NL_ARGMAX
  540. if (ppfs->maxposarg == 0) { /* initing for or no pos args */
  541. #endif /* NL_ARGMAX */
  542. if (ppfs->info.width == INT_MIN) {
  543. ppfs->info.width =
  544. #ifdef __va_arg_ptr
  545. *(int *)
  546. #endif
  547. GET_VA_ARG(p,u,unsigned int,ppfs->arg);
  548. }
  549. if (ppfs->info.prec == INT_MIN) {
  550. ppfs->info.prec =
  551. #ifdef __va_arg_ptr
  552. *(int *)
  553. #endif
  554. GET_VA_ARG(p,u,unsigned int,ppfs->arg);
  555. }
  556. i = 0;
  557. while (i < ppfs->num_data_args) {
  558. switch(ppfs->argtype[i++]) {
  559. case (PA_INT|PA_FLAG_LONG_LONG):
  560. #ifdef ULLONG_MAX
  561. GET_VA_ARG(p,ull,unsigned long long,ppfs->arg);
  562. break;
  563. #endif
  564. case (PA_INT|PA_FLAG_LONG):
  565. #if ULONG_MAX != UINT_MAX
  566. GET_VA_ARG(p,ul,unsigned long,ppfs->arg);
  567. break;
  568. #endif
  569. case PA_CHAR: /* TODO - be careful */
  570. /* ... users could use above and really want below!! */
  571. case (PA_INT|__PA_FLAG_CHAR):/* TODO -- translate this!!! */
  572. case (PA_INT|PA_FLAG_SHORT):
  573. case PA_INT:
  574. GET_VA_ARG(p,u,unsigned int,ppfs->arg);
  575. break;
  576. case PA_WCHAR: /* TODO -- assume int? */
  577. /* we're assuming wchar_t is at least an int */
  578. GET_VA_ARG(p,wc,wchar_t,ppfs->arg);
  579. break;
  580. #ifdef __STDIO_PRINTF_FLOAT
  581. /* PA_FLOAT */
  582. case PA_DOUBLE:
  583. GET_VA_ARG(p,d,double,ppfs->arg);
  584. break;
  585. case (PA_DOUBLE|PA_FLAG_LONG_DOUBLE):
  586. GET_VA_ARG(p,ld,long double,ppfs->arg);
  587. break;
  588. #else /* __STDIO_PRINTF_FLOAT */
  589. case PA_DOUBLE:
  590. case (PA_DOUBLE|PA_FLAG_LONG_DOUBLE):
  591. assert(0);
  592. continue;
  593. #endif /* __STDIO_PRINTF_FLOAT */
  594. default:
  595. /* TODO -- really need to ensure this can't happen */
  596. assert(ppfs->argtype[i-1] & PA_FLAG_PTR);
  597. case PA_POINTER:
  598. case PA_STRING:
  599. case PA_WSTRING:
  600. GET_VA_ARG(p,p,void *,ppfs->arg);
  601. break;
  602. case __PA_NOARG:
  603. continue;
  604. }
  605. ++p;
  606. }
  607. #ifdef NL_ARGMAX
  608. } else {
  609. if (ppfs->info.width == INT_MIN) {
  610. ppfs->info.width
  611. = (int) GET_ARG_VALUE(p + ppfs->argnumber[0] - 1,u,unsigned int);
  612. }
  613. if (ppfs->info.prec == INT_MIN) {
  614. ppfs->info.prec
  615. = (int) GET_ARG_VALUE(p + ppfs->argnumber[1] - 1,u,unsigned int);
  616. }
  617. }
  618. #endif /* NL_ARGMAX */
  619. /* Now we know the width and precision. */
  620. if (ppfs->info.width < 0) {
  621. ppfs->info.width = -ppfs->info.width;
  622. PRINT_INFO_SET_FLAG(&(ppfs->info),left);
  623. PRINT_INFO_CLR_FLAG(&(ppfs->info),space);
  624. ppfs->info.pad = ' ';
  625. }
  626. #if 0
  627. /* NOTE -- keep neg for now so float knows! */
  628. if (ppfs->info.prec < 0) { /* spec says treat as omitted. */
  629. /* so use default prec... 1 for everything but floats and strings. */
  630. ppfs->info.prec = 1;
  631. }
  632. #endif
  633. }
  634. #endif
  635. /**********************************************************************/
  636. #ifdef L__ppfs_parsespec
  637. /* Notes: argtype differs from glibc for the following:
  638. * mine glibc
  639. * lc PA_WCHAR PA_CHAR the standard says %lc means %C
  640. * ls PA_WSTRING PA_STRING the standard says %ls means %S
  641. * {*}n {*}|PA_FLAG_PTR PA_FLAG_PTR size of n can be qualified
  642. */
  643. /* TODO: store positions of positional args */
  644. /* TODO -- WARNING -- assumes aligned on integer boundaries!!! */
  645. /* TODO -- disable if not using positional args!!! */
  646. #define _OVERLAPPING_DIFFERENT_ARGS
  647. /* TODO -- rethink this -- perhaps we should set to largest type??? */
  648. #ifdef _OVERLAPPING_DIFFERENT_ARGS
  649. #define PROMOTED_SIZE_OF(X) ((sizeof(X) + sizeof(int) - 1) / sizeof(X))
  650. static const short int type_codes[] = {
  651. __PA_NOARG, /* must be first entry */
  652. PA_POINTER,
  653. PA_STRING,
  654. PA_WSTRING,
  655. PA_CHAR,
  656. PA_INT|PA_FLAG_SHORT,
  657. PA_INT,
  658. PA_INT|PA_FLAG_LONG,
  659. PA_INT|PA_FLAG_LONG_LONG,
  660. PA_WCHAR,
  661. #ifdef __STDIO_PRINTF_FLOAT
  662. /* PA_FLOAT, */
  663. PA_DOUBLE,
  664. PA_DOUBLE|PA_FLAG_LONG_DOUBLE,
  665. #endif /* __STDIO_PRINTF_FLOAT */
  666. };
  667. static const unsigned char type_sizes[] = {
  668. /* unknown type consumes no arg */
  669. 0, /* must be first entry */
  670. PROMOTED_SIZE_OF(void *),
  671. PROMOTED_SIZE_OF(char *),
  672. PROMOTED_SIZE_OF(wchar_t *),
  673. PROMOTED_SIZE_OF(char),
  674. PROMOTED_SIZE_OF(short),
  675. PROMOTED_SIZE_OF(int),
  676. PROMOTED_SIZE_OF(long),
  677. #ifdef ULLONG_MAX
  678. PROMOTED_SIZE_OF(long long),
  679. #else
  680. PROMOTED_SIZE_OF(long), /* TODO -- is this correct? (above too) */
  681. #endif
  682. PROMOTED_SIZE_OF(wchar_t),
  683. #ifdef __STDIO_PRINTF_FLOAT
  684. /* PROMOTED_SIZE_OF(float), */
  685. PROMOTED_SIZE_OF(double),
  686. PROMOTED_SIZE_OF(long double),
  687. #endif /* __STDIO_PRINTF_FLOAT */
  688. };
  689. static int _promoted_size(int argtype)
  690. {
  691. register const short int *p;
  692. /* note -- since any unrecognized type is treated as a pointer */
  693. p = type_codes + sizeof(type_codes)/sizeof(type_codes[0]);
  694. do {
  695. if (*--p == argtype) {
  696. break;
  697. }
  698. } while (p > type_codes);
  699. return type_sizes[(int)(p - type_codes)];
  700. }
  701. static int _is_equal_or_bigger_arg(int curtype, int newtype)
  702. {
  703. /* Quick test */
  704. if (newtype == __PA_NOARG) {
  705. return 0;
  706. }
  707. if ((curtype == __PA_NOARG) || (curtype == newtype)) {
  708. return 1;
  709. }
  710. /* Ok... slot is already filled and types are different in name. */
  711. /* So, compare promoted sizes of curtype and newtype args. */
  712. return _promoted_size(curtype) <= _promoted_size(newtype);
  713. }
  714. #else
  715. #define _is_equal_or_bigger_arg(C,N) (((C) == __PA_NOARG) || ((C) == (N)))
  716. #endif
  717. #ifdef __STDIO_GLIBC_CUSTOM_PRINTF
  718. /* TODO - do this differently? */
  719. static char _bss_custom_printf_spec[MAX_USER_SPEC]; /* 0-init'd for us. */
  720. char *_custom_printf_spec = _bss_custom_printf_spec;
  721. printf_arginfo_function *_custom_printf_arginfo[MAX_USER_SPEC];
  722. printf_function _custom_printf_handler[MAX_USER_SPEC];
  723. #endif /* __STDIO_GLIBC_CUSTOM_PRINTF */
  724. extern int _ppfs_parsespec(ppfs_t *ppfs)
  725. {
  726. register const char *fmt;
  727. register const char *p;
  728. int preci;
  729. int width;
  730. int flags;
  731. int dataargtype;
  732. int i;
  733. int dpoint;
  734. #ifdef NL_ARGMAX
  735. int maxposarg;
  736. #endif /* NL_ARGMAX */
  737. int p_m_spec_chars;
  738. int n;
  739. int argtype[MAX_ARGS_PER_SPEC+2];
  740. int argnumber[3]; /* width, precision, 1st data arg */
  741. static const char spec_flags[] = SPEC_FLAGS;
  742. static const char spec_chars[] = SPEC_CHARS;/* TODO: b? */
  743. static const char spec_ranges[] = SPEC_RANGES;
  744. static const short spec_or_mask[] = SPEC_OR_MASK;
  745. static const short spec_and_mask[] = SPEC_AND_MASK;
  746. static const char qual_chars[] = QUAL_CHARS;
  747. #ifdef __UCLIBC_HAS_WCHAR__
  748. char buf[32];
  749. #endif /* __UCLIBC_HAS_WCHAR__ */
  750. /* WIDE note: we can test against '%' here since we don't allow */
  751. /* WIDE note: other mappings of '%' in the wide char set. */
  752. preci = -1;
  753. argnumber[0] = 0;
  754. argnumber[1] = 0;
  755. argtype[0] = __PA_NOARG;
  756. argtype[1] = __PA_NOARG;
  757. #ifdef NL_ARGMAX
  758. maxposarg = ppfs->maxposarg;
  759. #endif /* NL_ARGMAX */
  760. #ifdef __UCLIBC_HAS_WCHAR__
  761. /* This is somewhat lame, but saves a lot of code. If we're dealing with
  762. * a wide stream, that means the format is a wchar string. So, copy it
  763. * char-by-char into a normal char buffer for processing. Make the buffer
  764. * (buf) big enough so that any reasonable format specifier will fit.
  765. * While there a legal specifiers that won't, the all involve duplicate
  766. * flags or outrageous field widths/precisions. */
  767. width = dpoint = 0;
  768. if ((flags = ppfs->info._flags & FLAG_WIDESTREAM) == 0) {
  769. fmt = ppfs->fmtpos;
  770. } else {
  771. fmt = buf + 1;
  772. i = 0;
  773. do {
  774. if ((buf[i] = (char) (((wchar_t *) ppfs->fmtpos)[i-1]))
  775. != (((wchar_t *) ppfs->fmtpos)[i-1])
  776. ) {
  777. return -1;
  778. }
  779. } while (buf[i++]);
  780. buf[sizeof(buf)-1] = 0;
  781. }
  782. #else /* __UCLIBC_HAS_WCHAR__ */
  783. width = flags = dpoint = 0;
  784. fmt = ppfs->fmtpos;
  785. #endif /* __UCLIBC_HAS_WCHAR__ */
  786. assert(fmt[-1] == '%');
  787. assert(fmt[0] != '%');
  788. /* Process arg pos and/or flags and/or width and/or precision. */
  789. width_precision:
  790. p = fmt;
  791. if (*fmt == '*') {
  792. argtype[-dpoint] = PA_INT;
  793. ++fmt;
  794. }
  795. i = 0;
  796. while (isdigit(*fmt)) {
  797. if (i < MAX_FIELD_WIDTH) { /* Avoid overflow. */
  798. i = (i * 10) + (*fmt - '0');
  799. }
  800. ++fmt;
  801. }
  802. if (p[-1] == '%') { /* Check for a position. */
  803. /* TODO: if val not in range, then error */
  804. #ifdef NL_ARGMAX
  805. if ((*fmt == '$') && (i > 0)) {/* Positional spec. */
  806. ++fmt;
  807. if (maxposarg == 0) {
  808. return -1;
  809. }
  810. if ((argnumber[2] = i) > maxposarg) {
  811. maxposarg = i;
  812. }
  813. /* Now fall through to check flags. */
  814. } else {
  815. if (maxposarg > 0) {
  816. #ifdef __STDIO_PRINTF_M_SUPPORT
  817. #ifdef __UCLIBC_MJN3_ONLY__
  818. #warning TODO: Support prec and width for %m when positional args used
  819. /* Actually, positional arg processing will fail in general
  820. * for specifiers that don't require an arg. */
  821. #endif /* __UCLIBC_MJN3_ONLY__ */
  822. if (*fmt == 'm') {
  823. goto PREC_WIDTH;
  824. }
  825. #endif /* __STDIO_PRINTF_M_SUPPORT */
  826. return -1;
  827. }
  828. maxposarg = 0; /* Possible redundant store, but cuts size. */
  829. if ((fmt > p) && (*p != '0')) {
  830. goto PREC_WIDTH;
  831. }
  832. fmt = p; /* Back up for possible '0's flag. */
  833. /* Now fall through to check flags. */
  834. }
  835. #else /* NL_ARGMAX */
  836. if (*fmt == '$') { /* Positional spec. */
  837. return -1;
  838. }
  839. if ((fmt > p) && (*p != '0')) {
  840. goto PREC_WIDTH;
  841. }
  842. fmt = p; /* Back up for possible '0's flag. */
  843. /* Now fall through to check flags. */
  844. #endif /* NL_ARGMAX */
  845. restart_flags: /* Process flags. */
  846. i = 1;
  847. p = spec_flags;
  848. do {
  849. if (*fmt == *p++) {
  850. ++fmt;
  851. flags |= i;
  852. goto restart_flags;
  853. }
  854. i += i; /* Better than i <<= 1 for bcc */
  855. } while (*p);
  856. i = 0;
  857. /* If '+' then ignore ' ', and if '-' then ignore '0'. */
  858. /* Note: Need to ignore '0' when prec is an arg with val < 0, */
  859. /* but that test needs to wait until the arg is retrieved. */
  860. flags &= ~((flags & (FLAG_PLUS|FLAG_MINUS)) >> 1);
  861. /* Note: Ignore '0' when prec is specified < 0 too (in printf). */
  862. if (fmt[-1] != '%') { /* If we've done anything, loop for width. */
  863. goto width_precision;
  864. }
  865. }
  866. PREC_WIDTH:
  867. if (*p == '*') { /* Prec or width takes an arg. */
  868. #ifdef NL_ARGMAX
  869. if (maxposarg) {
  870. if ((*fmt++ != '$') || (i <= 0)) {
  871. /* Using pos args and no $ or invalid arg number. */
  872. return -1;
  873. }
  874. argnumber[-dpoint] = i;
  875. } else
  876. #endif /* NL_ARGMAX */
  877. if (++p != fmt) {
  878. /* Not using pos args but digits followed *. */
  879. return -1;
  880. }
  881. i = INT_MIN;
  882. }
  883. if (!dpoint) {
  884. width = i;
  885. if (*fmt == '.') {
  886. ++fmt;
  887. dpoint = -1; /* To use as default precison. */
  888. goto width_precision;
  889. }
  890. } else {
  891. preci = i;
  892. }
  893. /* Process qualifier. */
  894. p = qual_chars;
  895. do {
  896. if (*fmt == *p) {
  897. ++fmt;
  898. break;
  899. }
  900. } while (*++p);
  901. if ((p - qual_chars < 2) && (*fmt == *p)) {
  902. p += ((sizeof(qual_chars)-2) / 2);
  903. ++fmt;
  904. }
  905. dataargtype = ((int)(p[(sizeof(qual_chars)-2) / 2])) << 8;
  906. /* Process conversion specifier. */
  907. if (!*fmt) {
  908. return -1;
  909. }
  910. p = spec_chars;
  911. do {
  912. if (*fmt == *p) {
  913. p_m_spec_chars = p - spec_chars;
  914. if ((p_m_spec_chars >= CONV_c)
  915. && (dataargtype & PA_FLAG_LONG)) {
  916. p_m_spec_chars -= 2; /* lc -> C and ls -> S */
  917. }
  918. ppfs->conv_num = p_m_spec_chars;
  919. p = spec_ranges-1;
  920. while (p_m_spec_chars > *++p) {}
  921. i = p - spec_ranges;
  922. argtype[2] = (dataargtype | spec_or_mask[i]) & spec_and_mask[i];
  923. p = spec_chars;
  924. break;
  925. }
  926. } while(*++p);
  927. ppfs->info.spec = *fmt;
  928. ppfs->info.prec = preci;
  929. ppfs->info.width = width;
  930. ppfs->info.pad = ((flags & FLAG_ZERO) ? '0' : ' ');
  931. ppfs->info._flags = (flags & ~FLAG_ZERO) | (dataargtype & __PA_INTMASK);
  932. ppfs->num_data_args = 1;
  933. if (!*p) {
  934. #ifdef __STDIO_PRINTF_M_SUPPORT
  935. if (*fmt == 'm') {
  936. ppfs->conv_num = CONV_m;
  937. ppfs->num_data_args = 0;
  938. goto DONE;
  939. }
  940. #endif
  941. #ifdef __STDIO_GLIBC_CUSTOM_PRINTF
  942. /* Handle custom arg -- WARNING -- overwrites p!!! */
  943. ppfs->conv_num = CONV_custom0;
  944. p = _custom_printf_spec;
  945. do {
  946. if (*p == *fmt) {
  947. if ((ppfs->num_data_args
  948. = ((*_custom_printf_arginfo[(int)(p-_custom_printf_spec)])
  949. (&(ppfs->info), MAX_ARGS_PER_SPEC, argtype+2)))
  950. > MAX_ARGS_PER_SPEC) {
  951. break; /* Error -- too many args! */
  952. }
  953. goto DONE;
  954. }
  955. } while (++p < (_custom_printf_spec + MAX_USER_SPEC));
  956. #endif /* __STDIO_GLIBC_CUSTOM_PRINTF */
  957. /* Otherwise error. */
  958. return -1;
  959. }
  960. #if defined(__STDIO_GLIBC_CUSTOM_PRINTF) || defined(__STDIO_PRINTF_M_SUPPORT)
  961. DONE:
  962. #endif
  963. #ifdef NL_ARGMAX
  964. if (maxposarg > 0) {
  965. i = 0;
  966. do {
  967. /* Update maxposarg and check that NL_ARGMAX is not exceeded. */
  968. n = ((i <= 2)
  969. ? (ppfs->argnumber[i] = argnumber[i])
  970. : argnumber[2] + (i-2));
  971. if (n > maxposarg) {
  972. if ((maxposarg = n) > NL_ARGMAX) {
  973. return -1;
  974. }
  975. }
  976. --n;
  977. /* Record argtype with largest size (current, new). */
  978. if (_is_equal_or_bigger_arg(ppfs->argtype[n], argtype[i])) {
  979. ppfs->argtype[n] = argtype[i];
  980. }
  981. } while (++i < ppfs->num_data_args + 2);
  982. } else {
  983. #endif /* NL_ARGMAX */
  984. ppfs->argnumber[2] = 1;
  985. memcpy(ppfs->argtype, argtype + 2, ppfs->num_data_args * sizeof(int));
  986. #ifdef NL_ARGMAX
  987. }
  988. ppfs->maxposarg = maxposarg;
  989. #endif /* NL_ARGMAX */
  990. #ifdef __UCLIBC_HAS_WCHAR__
  991. if ((flags = ppfs->info._flags & FLAG_WIDESTREAM) == 0) {
  992. ppfs->fmtpos = ++fmt;
  993. } else {
  994. ppfs->fmtpos = (const char *) (((const wchar_t *)(ppfs->fmtpos))
  995. + (fmt - buf) );
  996. }
  997. #else /* __UCLIBC_HAS_WCHAR__ */
  998. ppfs->fmtpos = ++fmt;
  999. #endif /* __UCLIBC_HAS_WCHAR__ */
  1000. return ppfs->num_data_args + 2;
  1001. }
  1002. #endif
  1003. /**********************************************************************/
  1004. #ifdef L_register_printf_function
  1005. #ifdef __STDIO_GLIBC_CUSTOM_PRINTF
  1006. int register_printf_function(int spec, printf_function handler,
  1007. printf_arginfo_function arginfo)
  1008. {
  1009. register char *r;
  1010. register char *p;
  1011. if (spec && (arginfo != NULL)) { /* TODO -- check if spec is valid char */
  1012. r = NULL;
  1013. p = _custom_printf_spec + MAX_USER_SPEC;
  1014. do {
  1015. --p;
  1016. if (!*p) {
  1017. r = p;
  1018. }
  1019. #ifdef __BCC__
  1020. else /* bcc generates less code with fall-through */
  1021. #endif
  1022. if (*p == spec) {
  1023. r = p;
  1024. p = _custom_printf_spec;
  1025. }
  1026. } while (p > _custom_printf_spec);
  1027. if (r) {
  1028. if (handler) {
  1029. *r = spec;
  1030. _custom_printf_handler[(int)(r - p)] = handler;
  1031. _custom_printf_arginfo[(int)(r - p)] = arginfo;
  1032. } else {
  1033. *r = 0;
  1034. }
  1035. return 0;
  1036. }
  1037. /* TODO -- if asked to unregister a non-existent spec, return what? */
  1038. }
  1039. return -1;
  1040. }
  1041. #endif
  1042. #endif
  1043. /**********************************************************************/
  1044. #ifdef L_vsnprintf
  1045. #ifdef __UCLIBC_MJN3_ONLY__
  1046. #warning WISHLIST: Implement vsnprintf for non-buffered and no custom stream case.
  1047. #endif /* __UCLIBC_MJN3_ONLY__ */
  1048. #ifdef __STDIO_BUFFERS
  1049. int vsnprintf(char *__restrict buf, size_t size,
  1050. const char * __restrict format, va_list arg)
  1051. {
  1052. FILE f;
  1053. int rv;
  1054. #ifdef __STDIO_GETC_MACRO
  1055. f.bufgetc =
  1056. #endif
  1057. f.bufpos = f.bufread = f.bufstart = buf;
  1058. if (size > SIZE_MAX - (size_t) buf) {
  1059. size = SIZE_MAX - (size_t) buf;
  1060. }
  1061. #ifdef __STDIO_PUTC_MACRO
  1062. f.bufputc =
  1063. #endif
  1064. f.bufend = buf + size;
  1065. #if 0 /* shouldn't be necessary */
  1066. /* #ifdef __STDIO_GLIBC_CUSTOM_STREAMS */
  1067. f.cookie = &(f.filedes);
  1068. f.gcs.read = 0;
  1069. f.gcs.write = 0;
  1070. f.gcs.seek = 0;
  1071. f.gcs.close = 0;
  1072. #endif
  1073. f.filedes = -2; /* for debugging */
  1074. f.modeflags = (__FLAG_NARROW|__FLAG_WRITEONLY|__FLAG_WRITING);
  1075. #ifdef __STDIO_MBSTATE
  1076. __INIT_MBSTATE(&(f.state));
  1077. #endif /* __STDIO_MBSTATE */
  1078. #ifdef __STDIO_THREADSAFE
  1079. f.user_locking = 0;
  1080. __stdio_init_mutex(&f.lock);
  1081. #endif
  1082. rv = vfprintf(&f, format, arg);
  1083. if (size) {
  1084. if (f.bufpos == f.bufend) {
  1085. --f.bufpos;
  1086. }
  1087. *f.bufpos = 0;
  1088. }
  1089. return rv;
  1090. }
  1091. #else /* __STDIO_BUFFERS */
  1092. #ifdef __STDIO_GLIBC_CUSTOM_STREAMS
  1093. typedef struct {
  1094. size_t pos;
  1095. size_t len;
  1096. unsigned char *buf;
  1097. FILE *fp;
  1098. } __snpf_cookie;
  1099. #define COOKIE ((__snpf_cookie *) cookie)
  1100. static ssize_t snpf_write(register void *cookie, const char *buf,
  1101. size_t bufsize)
  1102. {
  1103. size_t count;
  1104. register char *p;
  1105. /* Note: bufsize < SSIZE_MAX because of _stdio_WRITE. */
  1106. if (COOKIE->len > COOKIE->pos) {
  1107. count = COOKIE->len - COOKIE->pos - 1; /* Leave space for nul. */
  1108. if (count > bufsize) {
  1109. count = bufsize;
  1110. }
  1111. p = COOKIE->buf + COOKIE->pos;
  1112. while (count) {
  1113. *p++ = *buf++;
  1114. --count;
  1115. }
  1116. *p = 0;
  1117. }
  1118. COOKIE->pos += bufsize;
  1119. return bufsize;
  1120. }
  1121. #undef COOKIE
  1122. int vsnprintf(char *__restrict buf, size_t size,
  1123. const char * __restrict format, va_list arg)
  1124. {
  1125. FILE f;
  1126. __snpf_cookie cookie;
  1127. int rv;
  1128. cookie.buf = buf;
  1129. cookie.len = size;
  1130. cookie.pos = 0;
  1131. cookie.fp = &f;
  1132. f.cookie = &cookie;
  1133. f.gcs.write = snpf_write;
  1134. f.gcs.read = NULL;
  1135. f.gcs.seek = NULL;
  1136. f.gcs.close = NULL;
  1137. f.filedes = -1; /* For debugging. */
  1138. f.modeflags = (__FLAG_NARROW|__FLAG_WRITEONLY|__FLAG_WRITING);
  1139. #ifdef __STDIO_MBSTATE
  1140. __INIT_MBSTATE(&(f.state));
  1141. #endif /* __STDIO_MBSTATE */
  1142. #ifdef __STDIO_THREADSAFE
  1143. f.user_locking = 0;
  1144. __stdio_init_mutex(&f.lock);
  1145. #endif
  1146. rv = vfprintf(&f, format, arg);
  1147. return rv;
  1148. }
  1149. #else /* __STDIO_GLIBC_CUSTOM_STREAMS */
  1150. #warning Skipping vsnprintf since no buffering and no custom streams!
  1151. #endif /* __STDIO_GLIBC_CUSTOM_STREAMS */
  1152. #endif /* __STDIO_BUFFERS */
  1153. #endif
  1154. /**********************************************************************/
  1155. #ifdef L_vdprintf
  1156. int vdprintf(int filedes, const char * __restrict format, va_list arg)
  1157. {
  1158. FILE f;
  1159. int rv;
  1160. #ifdef __STDIO_BUFFERS
  1161. char buf[64]; /* TODO: provide _optional_ buffering? */
  1162. #ifdef __STDIO_GETC_MACRO
  1163. f.bufgetc =
  1164. #endif
  1165. f.bufpos = f.bufread = f.bufstart = buf;
  1166. #ifdef __STDIO_PUTC_MACRO
  1167. f.bufputc =
  1168. #endif
  1169. f.bufend = buf + sizeof(buf);
  1170. #endif /* __STDIO_BUFFERS */
  1171. #ifdef __STDIO_GLIBC_CUSTOM_STREAMS
  1172. f.cookie = &(f.filedes);
  1173. f.gcs.read = _cs_read;
  1174. f.gcs.write = _cs_write;
  1175. f.gcs.seek = 0; /* The internal seek func handles normals. */
  1176. f.gcs.close = _cs_close;
  1177. #endif
  1178. f.filedes = filedes;
  1179. f.modeflags = (__FLAG_NARROW|__FLAG_WRITEONLY|__FLAG_WRITING);
  1180. #ifdef __STDIO_MBSTATE
  1181. __INIT_MBSTATE(&(f.state));
  1182. #endif /* __STDIO_MBSTATE */
  1183. #ifdef __STDIO_THREADSAFE
  1184. f.user_locking = 0;
  1185. __stdio_init_mutex(&f.lock);
  1186. #endif
  1187. rv = vfprintf(&f, format, arg);
  1188. return fflush(&f) ? -1 : rv;
  1189. }
  1190. #endif
  1191. /**********************************************************************/
  1192. #ifdef L_vasprintf
  1193. #if !defined(__STDIO_BUFFERS) && !defined(__STDIO_GLIBC_CUSTOM_STREAMS)
  1194. #warning Skipping vasprintf since no buffering and no custom streams!
  1195. #else
  1196. int vasprintf(char **__restrict buf, const char * __restrict format,
  1197. va_list arg)
  1198. {
  1199. /* TODO -- change the default implementation? */
  1200. #ifndef __STDIO_GLIBC_CUSTOM_STREAMS
  1201. /* This implementation actually calls the printf machinery twice, but only
  1202. * only does one malloc. This can be a problem though when custom printf
  1203. * specs or the %m specifier are involved because the results of the
  1204. * second call might be different from the first. */
  1205. va_list arg2;
  1206. int rv;
  1207. va_copy(arg2, arg);
  1208. rv = vsnprintf(NULL, 0, format, arg2);
  1209. va_end(arg2);
  1210. return (((rv >= 0) && ((*buf = malloc(++rv)) != NULL))
  1211. ? vsnprintf(*buf, rv, format, arg)
  1212. : -1);
  1213. #else
  1214. FILE *f;
  1215. size_t size;
  1216. int rv;
  1217. /* TODO - do the memstream stuff inline here to avoid fclose and the openlist? */
  1218. if ((f = open_memstream(buf, &size)) == NULL) {
  1219. return -1;
  1220. }
  1221. rv = vfprintf(f, format, arg);
  1222. fclose(f);
  1223. if (rv < 0) {
  1224. free(*buf);
  1225. *buf = NULL;
  1226. return -1;
  1227. }
  1228. return rv;
  1229. #endif
  1230. }
  1231. #endif
  1232. #endif
  1233. /**********************************************************************/
  1234. #ifdef L_vprintf
  1235. int vprintf(const char * __restrict format, va_list arg)
  1236. {
  1237. return vfprintf(stdout, format, arg);
  1238. }
  1239. #endif
  1240. /**********************************************************************/
  1241. #ifdef L_vsprintf
  1242. #if !defined(__STDIO_BUFFERS) && !defined(__STDIO_GLIBC_CUSTOM_STREAMS)
  1243. #warning Skipping vsprintf since no buffering and no custom streams!
  1244. #else
  1245. int vsprintf(char *__restrict buf, const char * __restrict format,
  1246. va_list arg)
  1247. {
  1248. return vsnprintf(buf, SIZE_MAX, format, arg);
  1249. }
  1250. #endif
  1251. #endif
  1252. /**********************************************************************/
  1253. #ifdef L_fprintf
  1254. int fprintf(FILE * __restrict stream, const char * __restrict format, ...)
  1255. {
  1256. va_list arg;
  1257. int rv;
  1258. va_start(arg, format);
  1259. rv = vfprintf(stream, format, arg);
  1260. va_end(arg);
  1261. return rv;
  1262. }
  1263. #endif
  1264. /**********************************************************************/
  1265. #ifdef L_snprintf
  1266. #if !defined(__STDIO_BUFFERS) && !defined(__STDIO_GLIBC_CUSTOM_STREAMS)
  1267. #warning Skipping snprintf since no buffering and no custom streams!
  1268. #else
  1269. int snprintf(char *__restrict buf, size_t size,
  1270. const char * __restrict format, ...)
  1271. {
  1272. va_list arg;
  1273. int rv;
  1274. va_start(arg, format);
  1275. rv = vsnprintf(buf, size, format, arg);
  1276. va_end(arg);
  1277. return rv;
  1278. }
  1279. #endif
  1280. #endif
  1281. /**********************************************************************/
  1282. #ifdef L_dprintf
  1283. int dprintf(int filedes, const char * __restrict format, ...)
  1284. {
  1285. va_list arg;
  1286. int rv;
  1287. va_start(arg, format);
  1288. rv = vdprintf(filedes, format, arg);
  1289. va_end(arg);
  1290. return rv;
  1291. }
  1292. #endif
  1293. /**********************************************************************/
  1294. #ifdef L_asprintf
  1295. #if !defined(__STDIO_BUFFERS) && !defined(__STDIO_GLIBC_CUSTOM_STREAMS)
  1296. #warning Skipping asprintf and __asprintf since no buffering and no custom streams!
  1297. #else
  1298. weak_alias(__asprintf,asprintf)
  1299. int __asprintf(char **__restrict buf, const char * __restrict format, ...)
  1300. {
  1301. va_list arg;
  1302. int rv;
  1303. va_start(arg, format);
  1304. rv = vasprintf(buf, format, arg);
  1305. va_end(arg);
  1306. return rv;
  1307. }
  1308. #endif
  1309. #endif
  1310. /**********************************************************************/
  1311. #ifdef L_printf
  1312. int printf(const char * __restrict format, ...)
  1313. {
  1314. va_list arg;
  1315. int rv;
  1316. va_start(arg, format);
  1317. rv = vfprintf(stdout, format, arg);
  1318. va_end(arg);
  1319. return rv;
  1320. }
  1321. #endif
  1322. /**********************************************************************/
  1323. #ifdef L_sprintf
  1324. #if !defined(__STDIO_BUFFERS) && !defined(__STDIO_GLIBC_CUSTOM_STREAMS)
  1325. #warning Skipping sprintf since no buffering and no custom streams!
  1326. #else
  1327. int sprintf(char *__restrict buf, const char * __restrict format, ...)
  1328. {
  1329. va_list arg;
  1330. int rv;
  1331. va_start(arg, format);
  1332. rv = vsnprintf(buf, SIZE_MAX, format, arg);
  1333. va_end(arg);
  1334. return rv;
  1335. }
  1336. #endif
  1337. #endif
  1338. /**********************************************************************/
  1339. #ifdef L_vswprintf
  1340. #ifdef __STDIO_BUFFERS
  1341. int vswprintf(wchar_t *__restrict buf, size_t size,
  1342. const wchar_t * __restrict format, va_list arg)
  1343. {
  1344. FILE f;
  1345. int rv;
  1346. #ifdef __STDIO_GETC_MACRO
  1347. f.bufgetc =
  1348. #endif
  1349. f.bufpos = f.bufread = f.bufstart = (char *) buf;
  1350. /* if (size > SIZE_MAX - (size_t) buf) { */
  1351. /* size = SIZE_MAX - (size_t) buf; */
  1352. /* } */
  1353. #ifdef __STDIO_PUTC_MACRO
  1354. f.bufputc =
  1355. #endif
  1356. f.bufend = (char *)(buf + size);
  1357. #if 0 /* shouldn't be necessary */
  1358. /* #ifdef __STDIO_GLIBC_CUSTOM_STREAMS */
  1359. f.cookie = &(f.filedes);
  1360. f.gcs.read = 0;
  1361. f.gcs.write = 0;
  1362. f.gcs.seek = 0;
  1363. f.gcs.close = 0;
  1364. #endif
  1365. f.filedes = -3; /* for debugging */
  1366. f.modeflags = (__FLAG_WIDE|__FLAG_WRITEONLY|__FLAG_WRITING);
  1367. #ifdef __STDIO_MBSTATE
  1368. __INIT_MBSTATE(&(f.state));
  1369. #endif /* __STDIO_MBSTATE */
  1370. #ifdef __STDIO_THREADSAFE
  1371. f.user_locking = 0;
  1372. __stdio_init_mutex(&f.lock);
  1373. #endif
  1374. rv = vfwprintf(&f, format, arg);
  1375. /* NOTE: Return behaviour differs from snprintf... */
  1376. if (f.bufpos == f.bufend) {
  1377. rv = -1;
  1378. if (size) {
  1379. f.bufpos = (char *)(((wchar_t *) f.bufpos) - 1);
  1380. }
  1381. }
  1382. if (size) {
  1383. *((wchar_t *) f.bufpos) = 0;
  1384. }
  1385. return rv;
  1386. }
  1387. #else /* __STDIO_BUFFERS */
  1388. #warning Skipping vswprintf since no buffering!
  1389. #endif /* __STDIO_BUFFERS */
  1390. #endif
  1391. /**********************************************************************/
  1392. #ifdef L_swprintf
  1393. #ifdef __STDIO_BUFFERS
  1394. int swprintf(wchar_t *__restrict buf, size_t size,
  1395. const wchar_t * __restrict format, ...)
  1396. {
  1397. va_list arg;
  1398. int rv;
  1399. va_start(arg, format);
  1400. rv = vswprintf(buf, size, format, arg);
  1401. va_end(arg);
  1402. return rv;
  1403. }
  1404. #else /* __STDIO_BUFFERS */
  1405. #warning Skipping vsWprintf since no buffering!
  1406. #endif /* __STDIO_BUFFERS */
  1407. #endif
  1408. /**********************************************************************/
  1409. #ifdef L_fwprintf
  1410. int fwprintf(FILE * __restrict stream, const wchar_t * __restrict format, ...)
  1411. {
  1412. va_list arg;
  1413. int rv;
  1414. va_start(arg, format);
  1415. rv = vfwprintf(stream, format, arg);
  1416. va_end(arg);
  1417. return rv;
  1418. }
  1419. #endif
  1420. /**********************************************************************/
  1421. #ifdef L_vwprintf
  1422. int vwprintf(const wchar_t * __restrict format, va_list arg)
  1423. {
  1424. return vfwprintf(stdout, format, arg);
  1425. }
  1426. #endif
  1427. /**********************************************************************/
  1428. #ifdef L_wprintf
  1429. int wprintf(const wchar_t * __restrict format, ...)
  1430. {
  1431. va_list arg;
  1432. int rv;
  1433. va_start(arg, format);
  1434. rv = vfwprintf(stdout, format, arg);
  1435. va_end(arg);
  1436. return rv;
  1437. }
  1438. #endif
  1439. /**********************************************************************/
  1440. #ifdef L__fpmaxtostr
  1441. /* Copyright (C) 2000, 2001, 2003 Manuel Novoa III
  1442. *
  1443. * Function:
  1444. *
  1445. * size_t _fpmaxtostr(FILE * fp, __fpmax_t x, struct printf_info *info,
  1446. * __fp_outfunc_t fp_outfunc);
  1447. *
  1448. * This is derived from the old _dtostr, whic I wrote for uClibc to provide
  1449. * floating point support for the printf functions. It handles +/- infinity,
  1450. * nan, and signed 0 assuming you have ieee arithmetic. It also now handles
  1451. * digit grouping (for the uClibc supported locales) and hexadecimal float
  1452. * notation. Finally, via the fp_outfunc parameter, it now supports wide
  1453. * output.
  1454. *
  1455. * Notes:
  1456. *
  1457. * At most DECIMAL_DIG significant digits are kept. Any trailing digits
  1458. * are treated as 0 as they are really just the results of rounding noise
  1459. * anyway. If you want to do better, use an arbitary precision arithmetic
  1460. * package. ;-)
  1461. *
  1462. * It should also be fairly portable, as no assumptions are made about the
  1463. * bit-layout of doubles. Of course, that does make it less efficient than
  1464. * it could be.
  1465. *
  1466. */
  1467. /*****************************************************************************/
  1468. /* Don't change anything that follows unless you know what you're doing. */
  1469. /*****************************************************************************/
  1470. /* Fairly portable nan check. Bitwise for i386 generated larger code.
  1471. * If you have a better version, comment this out.
  1472. */
  1473. #define isnan(x) ((x) != (x))
  1474. /* Without seminumerical functions to examine the sign bit, this is
  1475. * about the best we can do to test for '-0'.
  1476. */
  1477. #define zeroisnegative(x) ((1./(x)) < 0)
  1478. /*****************************************************************************/
  1479. /* Don't change anything that follows peroid!!! ;-) */
  1480. /*****************************************************************************/
  1481. #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
  1482. #if FLT_RADIX != 2
  1483. #error FLT_RADIX != 2 is not currently supported
  1484. #endif
  1485. #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
  1486. #define NUM_HEX_DIGITS ((FPMAX_MANT_DIG + 3)/ 4)
  1487. /* WARNING: Adjust _fp_out_wide() below if this changes! */
  1488. /* With 32 bit ints, we can get 9 decimal digits per block. */
  1489. #define DIGITS_PER_BLOCK 9
  1490. #define HEX_DIGITS_PER_BLOCK 8
  1491. /* Maximum number of subcases to output double is...
  1492. * 0 - sign
  1493. * 1 - padding and initial digit
  1494. * 2 - digits left of the radix
  1495. * 3 - 0s left of the radix or radix
  1496. * 4 - radix or digits right of the radix
  1497. * 5 - 0s right of the radix
  1498. * 6 - exponent
  1499. * 7 - trailing space padding
  1500. * although not all cases may occur.
  1501. */
  1502. #define MAX_CALLS 8
  1503. /*****************************************************************************/
  1504. #define NUM_DIGIT_BLOCKS ((DECIMAL_DIG+DIGITS_PER_BLOCK-1)/DIGITS_PER_BLOCK)
  1505. #define NUM_HEX_DIGIT_BLOCKS \
  1506. ((NUM_HEX_DIGITS+HEX_DIGITS_PER_BLOCK-1)/HEX_DIGITS_PER_BLOCK)
  1507. /* WARNING: Adjust _fp_out_wide() below if this changes! */
  1508. /* extra space for '-', '.', 'e+###', and nul */
  1509. #define BUF_SIZE ( 3 + NUM_DIGIT_BLOCKS * DIGITS_PER_BLOCK )
  1510. /*****************************************************************************/
  1511. static const char fmt[] = "inf\0INF\0nan\0NAN\0.\0,";
  1512. #define INF_OFFSET 0 /* must be 1st */
  1513. #define NAN_OFFSET 8 /* must be 2nd.. see hex sign handling */
  1514. #define DECPT_OFFSET 16
  1515. #define THOUSEP_OFFSET 18
  1516. #define EMPTY_STRING_OFFSET 3
  1517. /*****************************************************************************/
  1518. #if FPMAX_MAX_10_EXP < -FPMAX_MIN_10_EXP
  1519. #error scaling code can not handle FPMAX_MAX_10_EXP < -FPMAX_MIN_10_EXP
  1520. #endif
  1521. static const __fpmax_t exp10_table[] =
  1522. {
  1523. 1e1L, 1e2L, 1e4L, 1e8L, 1e16L, 1e32L, /* floats */
  1524. #if FPMAX_MAX_10_EXP < 32
  1525. #error unsupported FPMAX_MAX_10_EXP (< 32). ANSI/ISO C requires >= 37.
  1526. #endif
  1527. #if FPMAX_MAX_10_EXP >= 64
  1528. 1e64L,
  1529. #endif
  1530. #if FPMAX_MAX_10_EXP >= 128
  1531. 1e128L,
  1532. #endif
  1533. #if FPMAX_MAX_10_EXP >= 256
  1534. 1e256L,
  1535. #endif
  1536. #if FPMAX_MAX_10_EXP >= 512
  1537. 1e512L,
  1538. #endif
  1539. #if FPMAX_MAX_10_EXP >= 1024
  1540. 1e1024L,
  1541. #endif
  1542. #if FPMAX_MAX_10_EXP >= 2048
  1543. 1e2048L,
  1544. #endif
  1545. #if FPMAX_MAX_10_EXP >= 4096
  1546. 1e4096L
  1547. #endif
  1548. #if FPMAX_MAX_10_EXP >= 8192
  1549. #error unsupported FPMAX_MAX_10_EXP. please increase table
  1550. #endif
  1551. };
  1552. #define EXP10_TABLE_SIZE (sizeof(exp10_table)/sizeof(exp10_table[0]))
  1553. #define EXP10_TABLE_MAX (1U<<(EXP10_TABLE_SIZE-1))
  1554. /*****************************************************************************/
  1555. #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
  1556. #if FLT_RADIX != 2
  1557. #error FLT_RADIX != 2 is not currently supported
  1558. #endif
  1559. #if FPMAX_MAX_EXP < -FPMAX_MIN_EXP
  1560. #error scaling code can not handle FPMAX_MAX_EXP < -FPMAX_MIN_EXP
  1561. #endif
  1562. static const __fpmax_t exp16_table[] = {
  1563. 0x1.0p4L, 0x1.0p8L, 0x1.0p16L, 0x1.0p32L, 0x1.0p64L,
  1564. #if FPMAX_MAX_EXP >= 128
  1565. 0x1.0p128L,
  1566. #endif
  1567. #if FPMAX_MAX_EXP >= 256
  1568. 0x1.0p256L,
  1569. #endif
  1570. #if FPMAX_MAX_EXP >= 512
  1571. 0x1.0p512L,
  1572. #endif
  1573. #if FPMAX_MAX_EXP >= 1024
  1574. 0x1.0p1024L,
  1575. #endif
  1576. #if FPMAX_MAX_EXP >= 2048
  1577. 0x1.0p2048L,
  1578. #endif
  1579. #if FPMAX_MAX_EXP >= 4096
  1580. 0x1.0p4096L,
  1581. #endif
  1582. #if FPMAX_MAX_EXP >= 8192
  1583. 0x1.0p8192L,
  1584. #endif
  1585. #if FPMAX_MAX_EXP >= 16384
  1586. 0x1.0p16384L
  1587. #endif
  1588. #if FPMAX_MAX_EXP >= 32768
  1589. #error unsupported FPMAX_MAX_EXP. please increase table
  1590. #endif
  1591. };
  1592. #define EXP16_TABLE_SIZE (sizeof(exp16_table)/sizeof(exp16_table[0]))
  1593. #define EXP16_TABLE_MAX (1U<<(EXP16_TABLE_SIZE-1))
  1594. #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
  1595. /*****************************************************************************/
  1596. #define FPO_ZERO_PAD (0x80 | '0')
  1597. #define FPO_STR_WIDTH (0x80 | ' ');
  1598. #define FPO_STR_PREC 'p'
  1599. size_t _fpmaxtostr(FILE * fp, __fpmax_t x, struct printf_info *info,
  1600. __fp_outfunc_t fp_outfunc)
  1601. {
  1602. #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
  1603. __fpmax_t lower_bnd;
  1604. __fpmax_t upper_bnd = 1e9;
  1605. #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
  1606. uint_fast32_t digit_block;
  1607. #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
  1608. uint_fast32_t base = 10;
  1609. const __fpmax_t *power_table;
  1610. int dpb = DIGITS_PER_BLOCK;
  1611. int ndb = NUM_DIGIT_BLOCKS;
  1612. int nd = DECIMAL_DIG;
  1613. int sufficient_precision = 0;
  1614. #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
  1615. #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
  1616. int num_groups = 0;
  1617. int initial_group; /* This does not need to be initialized. */
  1618. int tslen; /* This does not need to be initialized. */
  1619. int nblk2; /* This does not need to be initialized. */
  1620. const char *ts; /* This does not need to be initialized. */
  1621. #endif /* __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__ */
  1622. int i, j;
  1623. int round, o_exp;
  1624. int exp, exp_neg;
  1625. int width, preci;
  1626. int cnt;
  1627. char *s;
  1628. char *e;
  1629. intptr_t pc_fwi[3*MAX_CALLS];
  1630. intptr_t *ppc;
  1631. intptr_t *ppc_last;
  1632. #ifdef __UCLIBC_MJN3_ONLY__
  1633. #warning TODO: The size of exp_buf[] should really be determined by the float constants.
  1634. #endif /* __UCLIBC_MJN3_ONLY__ */
  1635. char exp_buf[16];
  1636. char buf[BUF_SIZE];
  1637. char sign_str[6]; /* Last 2 are for 1st digit + nul. */
  1638. char o_mode;
  1639. char mode;
  1640. width = info->width;
  1641. preci = info->prec;
  1642. mode = info->spec;
  1643. *exp_buf = 'e';
  1644. if ((mode|0x20) == 'a') {
  1645. #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
  1646. *exp_buf = 'p';
  1647. if (preci < 0) {
  1648. preci = NUM_HEX_DIGITS;
  1649. sufficient_precision = 1;
  1650. }
  1651. #else
  1652. mode += ('g' - 'a');
  1653. #endif
  1654. }
  1655. if (preci < 0) {
  1656. preci = 6;
  1657. }
  1658. *sign_str = '\0';
  1659. if (PRINT_INFO_FLAG_VAL(info,showsign)) {
  1660. *sign_str = '+';
  1661. } else if (PRINT_INFO_FLAG_VAL(info,space)) {
  1662. *sign_str = ' ';
  1663. }
  1664. *(sign_str+1) = 0;
  1665. pc_fwi[5] = INF_OFFSET;
  1666. if (isnan(x)) { /* First, check for nan. */
  1667. pc_fwi[5] = NAN_OFFSET;
  1668. goto INF_NAN;
  1669. }
  1670. if (x == 0) { /* Handle 0 now to avoid false positive. */
  1671. #if 1
  1672. if (zeroisnegative(x)) { /* Handle 'signed' zero. */
  1673. *sign_str = '-';
  1674. }
  1675. #endif
  1676. exp = -1;
  1677. goto GENERATE_DIGITS;
  1678. }
  1679. if (x < 0) { /* Convert negatives to positives. */
  1680. *sign_str = '-';
  1681. x = -x;
  1682. }
  1683. if (__FPMAX_ZERO_OR_INF_CHECK(x)) { /* Inf since zero handled above. */
  1684. INF_NAN:
  1685. info->pad = ' ';
  1686. ppc = pc_fwi + 6;
  1687. pc_fwi[3] = FPO_STR_PREC;
  1688. pc_fwi[4] = 3;
  1689. if (mode < 'a') {
  1690. pc_fwi[5] += 4;
  1691. }
  1692. pc_fwi[5] = (intptr_t)(fmt + pc_fwi[5]);
  1693. goto EXIT_SPECIAL;
  1694. }
  1695. #ifdef __UCLIBC_MJN3_ONLY__
  1696. #warning TODO: Clean up defines when hexadecimal float notation is unsupported.
  1697. #endif /* __UCLIBC_MJN3_ONLY__ */
  1698. #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
  1699. if ((mode|0x20) == 'a') {
  1700. lower_bnd = 0x1.0p31L;
  1701. upper_bnd = 0x1.0p32L;
  1702. power_table = exp16_table;
  1703. exp = HEX_DIGITS_PER_BLOCK - 1;
  1704. i = EXP16_TABLE_SIZE;
  1705. j = EXP16_TABLE_MAX;
  1706. dpb = HEX_DIGITS_PER_BLOCK;
  1707. ndb = NUM_HEX_DIGIT_BLOCKS;
  1708. nd = NUM_HEX_DIGITS;
  1709. base = 16;
  1710. } else {
  1711. lower_bnd = 1e8;
  1712. /* upper_bnd = 1e9; */
  1713. power_table = exp10_table;
  1714. exp = DIGITS_PER_BLOCK - 1;
  1715. i = EXP10_TABLE_SIZE;
  1716. j = EXP10_TABLE_MAX;
  1717. /* dpb = DIGITS_PER_BLOCK; */
  1718. /* ndb = NUM_DIGIT_BLOCKS; */
  1719. /* base = 10; */
  1720. }
  1721. #else /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
  1722. #define lower_bnd 1e8
  1723. #define upper_bnd 1e9
  1724. #define power_table exp10_table
  1725. #define dpb DIGITS_PER_BLOCK
  1726. #define base 10
  1727. #define ndb NUM_DIGIT_BLOCKS
  1728. #define nd DECIMAL_DIG
  1729. exp = DIGITS_PER_BLOCK - 1;
  1730. i = EXP10_TABLE_SIZE;
  1731. j = EXP10_TABLE_MAX;
  1732. #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
  1733. exp_neg = 0;
  1734. if (x < lower_bnd) { /* Do we need to scale up or down? */
  1735. exp_neg = 1;
  1736. }
  1737. do {
  1738. --i;
  1739. if (exp_neg) {
  1740. if (x * power_table[i] < upper_bnd) {
  1741. x *= power_table[i];
  1742. exp -= j;
  1743. }
  1744. } else {
  1745. if (x / power_table[i] >= lower_bnd) {
  1746. x /= power_table[i];
  1747. exp += j;
  1748. }
  1749. }
  1750. j >>= 1;
  1751. } while (i);
  1752. if (x >= upper_bnd) { /* Handle bad rounding case. */
  1753. x /= power_table[0];
  1754. ++exp;
  1755. }
  1756. assert(x < upper_bnd);
  1757. GENERATE_DIGITS:
  1758. s = buf + 2; /* Leave space for '\0' and '0'. */
  1759. i = 0;
  1760. do {
  1761. digit_block = (uint_fast32_t) x;
  1762. assert(digit_block < upper_bnd);
  1763. #ifdef __UCLIBC_MJN3_ONLY__
  1764. #warning CONSIDER: Can rounding be a problem?
  1765. #endif /* __UCLIBC_MJN3_ONLY__ */
  1766. x = (x - digit_block) * upper_bnd;
  1767. s += dpb;
  1768. j = 0;
  1769. do {
  1770. s[- ++j] = '0' + (digit_block % base);
  1771. digit_block /= base;
  1772. } while (j < dpb);
  1773. } while (++i < ndb);
  1774. /*************************************************************************/
  1775. if (mode < 'a') {
  1776. *exp_buf -= ('a' - 'A'); /* e->E and p->P */
  1777. mode += ('a' - 'A');
  1778. }
  1779. o_mode = mode;
  1780. if ((mode == 'g') && (preci > 0)){
  1781. --preci;
  1782. }
  1783. round = preci;
  1784. if (mode == 'f') {
  1785. round += exp;
  1786. if (round < -1) {
  1787. memset(buf, '0', DECIMAL_DIG); /* OK, since 'f' -> decimal case. */
  1788. exp = -1;
  1789. round = -1;
  1790. }
  1791. }
  1792. s = buf;
  1793. *s++ = 0; /* Terminator for rounding and 0-triming. */
  1794. *s = '0'; /* Space to round. */
  1795. i = 0;
  1796. e = s + nd + 1;
  1797. if (round < nd) {
  1798. e = s + round + 2;
  1799. if (*e >= '0' + (base/2)) { /* NOTE: We always round away from 0! */
  1800. i = 1;
  1801. }
  1802. }
  1803. do { /* Handle rounding and trim trailing 0s. */
  1804. *--e += i; /* Add the carry. */
  1805. } while ((*e == '0') || (*e > '0' - 1 + base));
  1806. #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
  1807. if ((mode|0x20) == 'a') {
  1808. char *q;
  1809. for (q = e ; *q ; --q) {
  1810. if (*q > '9') {
  1811. *q += (*exp_buf - ('p' - 'a') - '9' - 1);
  1812. }
  1813. }
  1814. if (e > s) {
  1815. exp *= 4; /* Change from base 16 to base 2. */
  1816. }
  1817. }
  1818. #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
  1819. o_exp = exp;
  1820. if (e <= s) { /* We carried into an extra digit. */
  1821. ++o_exp;
  1822. e = s; /* Needed if all 0s. */
  1823. } else {
  1824. ++s;
  1825. }
  1826. *++e = 0; /* Terminating nul char. */
  1827. if ((mode == 'g') && ((o_exp >= -4) && (o_exp <= round))) {
  1828. mode = 'f';
  1829. }
  1830. exp = o_exp;
  1831. if (mode != 'f') {
  1832. o_exp = 0;
  1833. }
  1834. if (o_exp < 0) { /* Exponent is < 0, so */
  1835. *--s = '0'; /* fake the first 0 digit. */
  1836. }
  1837. pc_fwi[3] = FPO_ZERO_PAD;
  1838. pc_fwi[4] = 1;
  1839. pc_fwi[5] = (intptr_t)(sign_str + 4);
  1840. sign_str[4] = *s++;
  1841. sign_str[5] = 0;
  1842. ppc = pc_fwi + 6;
  1843. i = e - s; /* Total digits is 'i'. */
  1844. if (o_exp >= 0) {
  1845. #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
  1846. const char *p;
  1847. if (PRINT_INFO_FLAG_VAL(info,group)
  1848. && *(p = __UCLIBC_CURLOCALE_DATA.grouping)
  1849. ) {
  1850. int nblk1;
  1851. nblk2 = nblk1 = *p;
  1852. if (*++p) {
  1853. nblk2 = *p;
  1854. assert(!*++p);
  1855. }
  1856. if (o_exp >= nblk1) {
  1857. num_groups = (o_exp - nblk1) / nblk2 + 1;
  1858. initial_group = (o_exp - nblk1) % nblk2;
  1859. #ifdef __UCLIBC_HAS_WCHAR__
  1860. if (PRINT_INFO_FLAG_VAL(info,wide)) {
  1861. /* _fp_out_wide() will fix this up. */
  1862. ts = fmt + THOUSEP_OFFSET;
  1863. tslen = 1;
  1864. } else {
  1865. #endif /* __UCLIBC_HAS_WCHAR__ */
  1866. ts = __UCLIBC_CURLOCALE_DATA.thousands_sep;
  1867. tslen = __UCLIBC_CURLOCALE_DATA.thousands_sep_len;
  1868. #ifdef __UCLIBC_HAS_WCHAR__
  1869. }
  1870. #endif /* __UCLIBC_HAS_WCHAR__ */
  1871. width -= num_groups * tslen;
  1872. }
  1873. }
  1874. #endif /* __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__ */
  1875. ppc[0] = FPO_STR_PREC;
  1876. ppc[2] = (intptr_t)(s);
  1877. if (o_exp >= i) { /* all digit(s) left of decimal */
  1878. ppc[1] = i;
  1879. ppc += 3;
  1880. o_exp -= i;
  1881. i = 0;
  1882. if (o_exp>0) { /* have 0s left of decimal */
  1883. ppc[0] = FPO_ZERO_PAD;
  1884. ppc[1] = o_exp;
  1885. ppc[2] = (intptr_t)(fmt + EMPTY_STRING_OFFSET);
  1886. ppc += 3;
  1887. }
  1888. } else if (o_exp > 0) { /* decimal between digits */
  1889. ppc[1] = o_exp;
  1890. ppc += 3;
  1891. s += o_exp;
  1892. i -= o_exp;
  1893. }
  1894. o_exp = -1;
  1895. }
  1896. if (PRINT_INFO_FLAG_VAL(info,alt)
  1897. || (i)
  1898. || ((o_mode != 'g')
  1899. #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
  1900. && (o_mode != 'a')
  1901. #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
  1902. && (preci > 0))
  1903. ) {
  1904. ppc[0] = FPO_STR_PREC;
  1905. #ifdef __LOCALE_C_ONLY
  1906. ppc[1] = 1;
  1907. ppc[2] = (intptr_t)(fmt + DECPT_OFFSET);
  1908. #else /* __LOCALE_C_ONLY */
  1909. #ifdef __UCLIBC_HAS_WCHAR__
  1910. if (PRINT_INFO_FLAG_VAL(info,wide)) {
  1911. /* _fp_out_wide() will fix this up. */
  1912. ppc[1] = 1;
  1913. ppc[2] = (intptr_t)(fmt + DECPT_OFFSET);
  1914. } else {
  1915. #endif /* __UCLIBC_HAS_WCHAR__ */
  1916. ppc[1] = __UCLIBC_CURLOCALE_DATA.decimal_point_len;
  1917. ppc[2] = (intptr_t)(__UCLIBC_CURLOCALE_DATA.decimal_point);
  1918. #ifdef __UCLIBC_HAS_WCHAR__
  1919. }
  1920. #endif /* __UCLIBC_HAS_WCHAR__ */
  1921. #endif /* __LOCALE_C_ONLY */
  1922. ppc += 3;
  1923. }
  1924. if (++o_exp < 0) { /* Have 0s right of decimal. */
  1925. ppc[0] = FPO_ZERO_PAD;
  1926. ppc[1] = -o_exp;
  1927. ppc[2] = (intptr_t)(fmt + EMPTY_STRING_OFFSET);
  1928. ppc += 3;
  1929. }
  1930. if (i) { /* Have digit(s) right of decimal. */
  1931. ppc[0] = FPO_STR_PREC;
  1932. ppc[1] = i;
  1933. ppc[2] = (intptr_t)(s);
  1934. ppc += 3;
  1935. }
  1936. if (((o_mode != 'g') || PRINT_INFO_FLAG_VAL(info,alt))
  1937. #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
  1938. && !sufficient_precision
  1939. #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
  1940. ) {
  1941. i -= o_exp;
  1942. if (i < preci) { /* Have 0s right of digits. */
  1943. i = preci - i;
  1944. ppc[0] = FPO_ZERO_PAD;
  1945. ppc[1] = i;
  1946. ppc[2] = (intptr_t)(fmt + EMPTY_STRING_OFFSET);
  1947. ppc += 3;
  1948. }
  1949. }
  1950. /* Build exponent string. */
  1951. if (mode != 'f') {
  1952. char *p = exp_buf + sizeof(exp_buf);
  1953. char exp_char = *exp_buf;
  1954. char exp_sign = '+';
  1955. #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
  1956. int min_exp_dig_plus_2 = ((o_mode != 'a') ? (2+2) : (2+1));
  1957. #else /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
  1958. #define min_exp_dig_plus_2 (2+2)
  1959. #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
  1960. if (exp < 0) {
  1961. exp_sign = '-';
  1962. exp = -exp;
  1963. }
  1964. *--p = 0; /* nul-terminate */
  1965. j = 2; /* Count exp_char and exp_sign. */
  1966. do {
  1967. *--p = '0' + (exp % 10);
  1968. exp /= 10;
  1969. } while ((++j < min_exp_dig_plus_2) || exp); /* char+sign+mindigits */
  1970. *--p = exp_sign;
  1971. *--p = exp_char;
  1972. ppc[0] = FPO_STR_PREC;
  1973. ppc[1] = j;
  1974. ppc[2] = (intptr_t)(p);
  1975. ppc += 3;
  1976. }
  1977. EXIT_SPECIAL:
  1978. ppc_last = ppc;
  1979. ppc = pc_fwi + 4; /* Need width fields starting with second. */
  1980. do {
  1981. width -= *ppc;
  1982. ppc += 3;
  1983. } while (ppc < ppc_last);
  1984. ppc = pc_fwi;
  1985. ppc[0] = FPO_STR_WIDTH;
  1986. ppc[1] = i = ((*sign_str) != 0);
  1987. ppc[2] = (intptr_t) sign_str;
  1988. #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
  1989. if (((mode|0x20) == 'a') && (pc_fwi[3] >= 16)) { /* Hex sign handling. */
  1990. /* Hex and not inf or nan, so prefix with 0x. */
  1991. char *h = sign_str + i;
  1992. *h = '0';
  1993. *++h = 'x' - 'p' + *exp_buf;
  1994. *++h = 0;
  1995. ppc[1] = (i += 2);
  1996. }
  1997. #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
  1998. if ((width -= i) > 0) {
  1999. if (PRINT_INFO_FLAG_VAL(info,left)) { /* Left-justified. */
  2000. ppc_last[0] = FPO_STR_WIDTH;
  2001. ppc_last[1] = width;
  2002. ppc_last[2] = (intptr_t)(fmt + EMPTY_STRING_OFFSET);
  2003. ppc_last += 3;
  2004. } else if (info->pad == '0') { /* 0 padding */
  2005. ppc[4] += width; /* Pad second field. */
  2006. } else {
  2007. ppc[1] += width; /* Pad first (sign) field. */
  2008. }
  2009. }
  2010. cnt = 0;
  2011. do {
  2012. #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
  2013. if ((ppc == pc_fwi + 6) && num_groups) {
  2014. const char *gp = (const char *) ppc[2];
  2015. int len = ppc[1];
  2016. int blk = initial_group;
  2017. cnt += num_groups * tslen; /* Adjust count now for sep chars. */
  2018. /* printf("\n"); */
  2019. do {
  2020. if (!blk) { /* Initial group could be 0 digits long! */
  2021. blk = nblk2;
  2022. } else if (len >= blk) { /* Enough digits for a group. */
  2023. /* printf("norm: len=%d blk=%d \"%.*s\"\n", len, blk, blk, gp); */
  2024. fp_outfunc(fp, *ppc, blk, (intptr_t) gp);
  2025. assert(gp);
  2026. if (*gp) {
  2027. gp += blk;
  2028. }
  2029. len -= blk;
  2030. } else { /* Transition to 0s. */
  2031. /* printf("trans: len=%d blk=%d \"%.*s\"\n", len, blk, len, gp); */
  2032. if (len) {
  2033. /* printf("len\n"); */
  2034. fp_outfunc(fp, *ppc, len, (intptr_t) gp);
  2035. gp += len;
  2036. }
  2037. if (ppc[3] == FPO_ZERO_PAD) { /* Need to group 0s */
  2038. /* printf("zeropad\n"); */
  2039. cnt += ppc[1];
  2040. ppc += 3;
  2041. gp = (const char *) ppc[2];
  2042. blk -= len; /* blk > len, so blk still > 0. */
  2043. len = ppc[1];
  2044. continue; /* Don't decrement num_groups here. */
  2045. } else {
  2046. assert(num_groups == 0);
  2047. break;
  2048. }
  2049. }
  2050. if (num_groups <= 0) {
  2051. break;
  2052. }
  2053. --num_groups;
  2054. fp_outfunc(fp, FPO_STR_PREC, tslen, (intptr_t) ts);
  2055. blk = nblk2;
  2056. /* printf("num_groups=%d blk=%d\n", num_groups, blk); */
  2057. } while (1);
  2058. } else
  2059. #endif /* __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__ */
  2060. fp_outfunc(fp, *ppc, ppc[1], ppc[2]); /* NOTE: Remember 'else' above! */
  2061. cnt += ppc[1];
  2062. ppc += 3;
  2063. } while (ppc < ppc_last);
  2064. return cnt;
  2065. }
  2066. #endif
  2067. /**********************************************************************/
  2068. #ifdef L__store_inttype
  2069. /* Right now, we assume intmax_t is either long or long long */
  2070. #ifdef INTMAX_MAX
  2071. #ifdef LLONG_MAX
  2072. #if INTMAX_MAX > LLONG_MAX
  2073. #error INTMAX_MAX > LLONG_MAX! The printf code needs to be updated!
  2074. #endif
  2075. #elif INTMAX_MAX > LONG_MAX
  2076. #error No LLONG_MAX and INTMAX_MAX > LONG_MAX! The printf code needs to be updated!
  2077. #endif /* LLONG_MAX */
  2078. #endif /* INTMAX_MAX */
  2079. /* We assume int may be short or long, but short and long are different. */
  2080. void _store_inttype(register void *dest, int desttype, uintmax_t val)
  2081. {
  2082. if (desttype == __PA_FLAG_CHAR) { /* assume char not int */
  2083. *((unsigned char *) dest) = val;
  2084. return;
  2085. }
  2086. #if defined(LLONG_MAX) && (LONG_MAX != LLONG_MAX)
  2087. if (desttype == PA_FLAG_LONG_LONG) {
  2088. *((unsigned long long int *) dest) = val;
  2089. return;
  2090. }
  2091. #endif /* LLONG_MAX */
  2092. #if SHRT_MAX != INT_MAX
  2093. if (desttype == PA_FLAG_SHORT) {
  2094. *((unsigned short int *) dest) = val;
  2095. return;
  2096. }
  2097. #endif /* SHRT_MAX */
  2098. #if LONG_MAX != INT_MAX
  2099. if (desttype == PA_FLAG_LONG) {
  2100. *((unsigned long int *) dest) = val;
  2101. return;
  2102. }
  2103. #endif /* LONG_MAX */
  2104. *((unsigned int *) dest) = val;
  2105. }
  2106. #endif
  2107. /**********************************************************************/
  2108. #ifdef L__load_inttype
  2109. extern uintmax_t _load_inttype(int desttype, register const void *src,
  2110. int uflag)
  2111. {
  2112. if (uflag >= 0) { /* unsigned */
  2113. #if LONG_MAX != INT_MAX
  2114. if (desttype & (PA_FLAG_LONG|PA_FLAG_LONG_LONG)) {
  2115. #ifdef LLONG_MAX
  2116. if (desttype == PA_FLAG_LONG_LONG) {
  2117. return *((unsigned long long int *) src);
  2118. }
  2119. #endif
  2120. return *((unsigned long int *) src);
  2121. }
  2122. #else /* LONG_MAX != INT_MAX */
  2123. #ifdef LLONG_MAX
  2124. if (desttype & PA_FLAG_LONG_LONG) {
  2125. return *((unsigned long long int *) src);
  2126. }
  2127. #endif
  2128. #endif /* LONG_MAX != INT_MAX */
  2129. {
  2130. unsigned int x;
  2131. x = *((unsigned int *) src);
  2132. if (desttype == __PA_FLAG_CHAR) x = (unsigned char) x;
  2133. #if SHRT_MAX != INT_MAX
  2134. if (desttype == PA_FLAG_SHORT) x = (unsigned short int) x;
  2135. #endif
  2136. return x;
  2137. }
  2138. } else { /* signed */
  2139. #if LONG_MAX != INT_MAX
  2140. if (desttype & (PA_FLAG_LONG|PA_FLAG_LONG_LONG)) {
  2141. #ifdef LLONG_MAX
  2142. if (desttype == PA_FLAG_LONG_LONG) {
  2143. return *((long long int *) src);
  2144. }
  2145. #endif
  2146. return *((long int *) src);
  2147. }
  2148. #else /* LONG_MAX != INT_MAX */
  2149. #ifdef LLONG_MAX
  2150. if (desttype & PA_FLAG_LONG_LONG) {
  2151. return *((long long int *) src);
  2152. }
  2153. #endif
  2154. #endif /* LONG_MAX != INT_MAX */
  2155. {
  2156. int x;
  2157. x = *((int *) src);
  2158. if (desttype == __PA_FLAG_CHAR) x = (char) x;
  2159. #if SHRT_MAX != INT_MAX
  2160. if (desttype == PA_FLAG_SHORT) x = (short int) x;
  2161. #endif
  2162. return x;
  2163. }
  2164. }
  2165. }
  2166. #endif
  2167. /**********************************************************************/
  2168. #if defined(L_vfprintf) || defined(L_vfwprintf)
  2169. /* We only support ascii digits (or their USC equivalent codes) in
  2170. * precision and width settings in *printf (wide) format strings.
  2171. * In other words, we don't currently support glibc's 'I' flag.
  2172. * We do accept it, but it is currently ignored. */
  2173. static void _charpad(FILE * __restrict stream, int padchar, size_t numpad);
  2174. #ifdef L_vfprintf
  2175. #define VFPRINTF vfprintf
  2176. #define FMT_TYPE char
  2177. #define OUTNSTR _outnstr
  2178. #define STRLEN strlen
  2179. #define _PPFS_init _ppfs_init
  2180. #define OUTPUT(F,S) fputs(S,F)
  2181. #define _outnstr(stream, string, len) _stdio_fwrite(string, len, stream)
  2182. #define FP_OUT _fp_out_narrow
  2183. #ifdef __STDIO_PRINTF_FLOAT
  2184. static void _fp_out_narrow(FILE *fp, intptr_t type, intptr_t len, intptr_t buf)
  2185. {
  2186. if (type & 0x80) { /* Some type of padding needed. */
  2187. int buflen = strlen((const char *) buf);
  2188. if ((len -= buflen) > 0) {
  2189. _charpad(fp, (type & 0x7f), len);
  2190. }
  2191. len = buflen;
  2192. }
  2193. OUTNSTR(fp, (const char *) buf, len);
  2194. }
  2195. #endif /* __STDIO_PRINTF_FLOAT */
  2196. #else /* L_vfprintf */
  2197. #define VFPRINTF vfwprintf
  2198. #define FMT_TYPE wchar_t
  2199. #define OUTNSTR _outnwcs
  2200. #define STRLEN wcslen
  2201. #define _PPFS_init _ppwfs_init
  2202. #define OUTPUT(F,S) fputws(S,F)
  2203. #define _outnwcs(stream, wstring, len) _wstdio_fwrite(wstring, len, stream)
  2204. #define FP_OUT _fp_out_wide
  2205. static void _outnstr(FILE *stream, const char *s, size_t wclen)
  2206. {
  2207. /* NOTE!!! len here is the number of wchars we want to generate!!! */
  2208. wchar_t wbuf[64];
  2209. mbstate_t mbstate;
  2210. size_t todo, r;
  2211. mbstate.mask = 0;
  2212. todo = wclen;
  2213. while (todo) {
  2214. r = mbsrtowcs(wbuf, &s,
  2215. ((todo <= sizeof(wbuf)/sizeof(wbuf[0]))
  2216. ? todo
  2217. : sizeof(wbuf)/sizeof(wbuf[0])),
  2218. &mbstate);
  2219. assert(((ssize_t)r) > 0);
  2220. _outnwcs(stream, wbuf, r);
  2221. todo -= r;
  2222. }
  2223. }
  2224. #ifdef __STDIO_PRINTF_FLOAT
  2225. #ifdef __UCLIBC_MJN3_ONLY__
  2226. #warning TODO: Move defines from _fpmaxtostr. Put them in a common header.
  2227. #endif
  2228. /* The following defines are from _fpmaxtostr.*/
  2229. #define DIGITS_PER_BLOCK 9
  2230. #define NUM_DIGIT_BLOCKS ((DECIMAL_DIG+DIGITS_PER_BLOCK-1)/DIGITS_PER_BLOCK)
  2231. #define BUF_SIZE ( 3 + NUM_DIGIT_BLOCKS * DIGITS_PER_BLOCK )
  2232. static void _fp_out_wide(FILE *fp, intptr_t type, intptr_t len, intptr_t buf)
  2233. {
  2234. wchar_t wbuf[BUF_SIZE];
  2235. const char *s = (const char *) buf;
  2236. int i;
  2237. if (type & 0x80) { /* Some type of padding needed */
  2238. int buflen = strlen(s);
  2239. if ((len -= buflen) > 0) {
  2240. _charpad(fp, (type & 0x7f), len);
  2241. }
  2242. len = buflen;
  2243. }
  2244. if (len > 0) {
  2245. i = 0;
  2246. do {
  2247. #ifdef __LOCALE_C_ONLY
  2248. wbuf[i] = s[i];
  2249. #else /* __LOCALE_C_ONLY */
  2250. #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
  2251. if (s[i] == ',') {
  2252. wbuf[i] = __UCLIBC_CURLOCALE_DATA.thousands_sep_wc;
  2253. } else
  2254. #endif /* __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__ */
  2255. if (s[i] == '.') {
  2256. wbuf[i] = __UCLIBC_CURLOCALE_DATA.decimal_point_wc;
  2257. } else {
  2258. wbuf[i] = s[i];
  2259. }
  2260. #endif /* __LOCALE_C_ONLY */
  2261. } while (++i < len);
  2262. OUTNSTR(fp, wbuf, len);
  2263. }
  2264. }
  2265. #endif /* __STDIO_PRINTF_FLOAT */
  2266. static int _ppwfs_init(register ppfs_t *ppfs, const wchar_t *fmt0)
  2267. {
  2268. static const wchar_t invalid_wcs[] = L"Invalid wide format string.";
  2269. int r;
  2270. /* First, zero out everything... argnumber[], argtype[], argptr[] */
  2271. memset(ppfs, 0, sizeof(ppfs_t)); /* TODO: nonportable???? */
  2272. #ifdef NL_ARGMAX
  2273. --ppfs->maxposarg; /* set to -1 */
  2274. #endif /* NL_ARGMAX */
  2275. ppfs->fmtpos = (const char *) fmt0;
  2276. ppfs->info._flags = FLAG_WIDESTREAM;
  2277. {
  2278. mbstate_t mbstate;
  2279. const wchar_t *p;
  2280. mbstate.mask = 0; /* Initialize the mbstate. */
  2281. p = fmt0;
  2282. if (wcsrtombs(NULL, &p, SIZE_MAX, &mbstate) == ((size_t)(-1))) {
  2283. ppfs->fmtpos = (const char *) invalid_wcs;
  2284. return -1;
  2285. }
  2286. }
  2287. /* now set all argtypes to no-arg */
  2288. {
  2289. #if 1
  2290. /* TODO - use memset here since already "paid for"? */
  2291. register int *p = ppfs->argtype;
  2292. r = MAX_ARGS;
  2293. do {
  2294. *p++ = __PA_NOARG;
  2295. } while (--r);
  2296. #else
  2297. /* TODO -- get rid of this?? */
  2298. register char *p = (char *) ((MAX_ARGS-1) * sizeof(int));
  2299. do {
  2300. *((int *)(((char *)ppfs) + ((int)p) + offsetof(ppfs_t,argtype))) = __PA_NOARG;
  2301. p -= sizeof(int);
  2302. } while (p);
  2303. #endif
  2304. }
  2305. /*
  2306. * Run through the entire format string to validate it and initialize
  2307. * the positional arg numbers (if any).
  2308. */
  2309. {
  2310. register const wchar_t *fmt = fmt0;
  2311. while (*fmt) {
  2312. if ((*fmt == '%') && (*++fmt != '%')) {
  2313. ppfs->fmtpos = (const char *) fmt; /* back up to the '%' */
  2314. if ((r = _ppfs_parsespec(ppfs)) < 0) {
  2315. return -1;
  2316. }
  2317. fmt = (const wchar_t *) ppfs->fmtpos; /* update to one past end of spec */
  2318. } else {
  2319. ++fmt;
  2320. }
  2321. }
  2322. ppfs->fmtpos = (const char *) fmt0; /* rewind */
  2323. }
  2324. #ifdef NL_ARGMAX
  2325. /* If we have positional args, make sure we know all the types. */
  2326. {
  2327. register int *p = ppfs->argtype;
  2328. r = ppfs->maxposarg;
  2329. while (--r >= 0) {
  2330. if ( *p == __PA_NOARG ) { /* missing arg type!!! */
  2331. return -1;
  2332. }
  2333. ++p;
  2334. }
  2335. }
  2336. #endif /* NL_ARGMAX */
  2337. return 0;
  2338. }
  2339. #endif /* L_vfprintf */
  2340. static void _charpad(FILE * __restrict stream, int padchar, size_t numpad)
  2341. {
  2342. /* TODO -- Use a buffer to cut down on function calls... */
  2343. FMT_TYPE pad[1];
  2344. *pad = padchar;
  2345. while (numpad) {
  2346. OUTNSTR(stream, pad, 1);
  2347. --numpad;
  2348. }
  2349. }
  2350. /* TODO -- Dynamically allocate work space to accomodate stack-poor archs? */
  2351. static int _do_one_spec(FILE * __restrict stream,
  2352. register ppfs_t *ppfs, int *count)
  2353. {
  2354. static const char spec_base[] = SPEC_BASE;
  2355. #ifdef L_vfprintf
  2356. static const char prefix[] = "+\0-\0 \0000x\0000X";
  2357. /* 0 2 4 6 9 11*/
  2358. #else /* L_vfprintf */
  2359. static const wchar_t prefix[] = L"+\0-\0 \0000x\0000X";
  2360. #endif /* L_vfprintf */
  2361. enum {
  2362. PREFIX_PLUS = 0,
  2363. PREFIX_MINUS = 2,
  2364. PREFIX_SPACE = 4,
  2365. PREFIX_LWR_X = 6,
  2366. PREFIX_UPR_X = 9,
  2367. PREFIX_NONE = 11
  2368. };
  2369. #ifdef __va_arg_ptr
  2370. const void * const *argptr;
  2371. #else
  2372. const void * argptr[MAX_ARGS_PER_SPEC];
  2373. #endif
  2374. int *argtype;
  2375. #ifdef __UCLIBC_HAS_WCHAR__
  2376. const wchar_t *ws = NULL;
  2377. mbstate_t mbstate;
  2378. #endif /* __UCLIBC_HAS_WCHAR__ */
  2379. size_t slen;
  2380. #ifdef L_vfprintf
  2381. #define SLEN slen
  2382. #else
  2383. size_t SLEN;
  2384. wchar_t wbuf[2];
  2385. #endif
  2386. int base;
  2387. int numpad;
  2388. int alphacase;
  2389. int numfill = 0; /* TODO: fix */
  2390. int prefix_num = PREFIX_NONE;
  2391. char padchar = ' ';
  2392. #ifdef __UCLIBC_MJN3_ONLY__
  2393. #warning TODO: Determine appropriate buf size.
  2394. #endif /* __UCLIBC_MJN3_ONLY__ */
  2395. /* TODO: buf needs to be big enough for any possible error return strings
  2396. * and also for any locale-grouped long long integer strings generated.
  2397. * This should be large enough for any of the current archs/locales, but
  2398. * eventually this should be handled robustly. */
  2399. char buf[128];
  2400. #ifdef NDEBUG
  2401. _ppfs_parsespec(ppfs);
  2402. #else
  2403. if (_ppfs_parsespec(ppfs) < 0) { /* TODO: just for debugging */
  2404. abort();
  2405. }
  2406. #endif
  2407. _ppfs_setargs(ppfs);
  2408. argtype = ppfs->argtype + ppfs->argnumber[2] - 1;
  2409. /* Deal with the argptr vs argvalue issue. */
  2410. #ifdef __va_arg_ptr
  2411. argptr = (const void * const *) ppfs->argptr;
  2412. #ifdef NL_ARGMAX
  2413. if (ppfs->maxposarg > 0) { /* Using positional args... */
  2414. argptr += ppfs->argnumber[2] - 1;
  2415. }
  2416. #endif /* NL_ARGMAX */
  2417. #else
  2418. /* Need to build a local copy... */
  2419. {
  2420. register argvalue_t *p = ppfs->argvalue;
  2421. int i;
  2422. #ifdef NL_ARGMAX
  2423. if (ppfs->maxposarg > 0) { /* Using positional args... */
  2424. p += ppfs->argnumber[2] - 1;
  2425. }
  2426. #endif /* NL_ARGMAX */
  2427. for (i = 0 ; i < ppfs->num_data_args ; i++ ) {
  2428. argptr[i] = (void *) p++;
  2429. }
  2430. }
  2431. #endif
  2432. {
  2433. register char *s = NULL; /* TODO: Should s be unsigned char * ? */
  2434. if (ppfs->conv_num == CONV_n) {
  2435. _store_inttype(*(void **)*argptr,
  2436. ppfs->info._flags & __PA_INTMASK,
  2437. (intmax_t) (*count));
  2438. return 0;
  2439. }
  2440. if (ppfs->conv_num <= CONV_i) { /* pointer or (un)signed int */
  2441. alphacase = __UIM_LOWER;
  2442. #ifdef __UCLIBC_MJN3_ONLY__
  2443. #ifdef L_vfprintf
  2444. #warning CONSIDER: Should we ignore these flags if stub locale? What about custom specs?
  2445. #endif
  2446. #endif /* __UCLIBC_MJN3_ONLY__ */
  2447. if ((base = spec_base[(int)(ppfs->conv_num - CONV_p)]) == 10) {
  2448. if (PRINT_INFO_FLAG_VAL(&(ppfs->info),group)) {
  2449. alphacase = __UIM_GROUP;
  2450. }
  2451. if (PRINT_INFO_FLAG_VAL(&(ppfs->info),i18n)) {
  2452. alphacase |= 0x80;
  2453. }
  2454. }
  2455. if (ppfs->conv_num <= CONV_u) { /* pointer or unsigned int */
  2456. if (ppfs->conv_num == CONV_X) {
  2457. alphacase = __UIM_UPPER;
  2458. }
  2459. if (ppfs->conv_num == CONV_p) { /* pointer */
  2460. prefix_num = PREFIX_LWR_X;
  2461. } else { /* unsigned int */
  2462. }
  2463. } else { /* signed int */
  2464. base = -base;
  2465. }
  2466. if (ppfs->info.prec < 0) { /* Ignore '0' flag if prec specified. */
  2467. padchar = ppfs->info.pad;
  2468. }
  2469. #ifdef __UCLIBC_MJN3_ONLY__
  2470. #ifdef L_vfprintf
  2471. #warning CONSIDER: If using outdigits and/or grouping, how should we interpret precision?
  2472. #endif
  2473. #endif /* __UCLIBC_MJN3_ONLY__ */
  2474. s = _uintmaxtostr(buf + sizeof(buf) - 1,
  2475. (uintmax_t)
  2476. _load_inttype(*argtype & __PA_INTMASK,
  2477. *argptr, base), base, alphacase);
  2478. if (ppfs->conv_num > CONV_u) { /* signed int */
  2479. if (*s == '-') {
  2480. PRINT_INFO_SET_FLAG(&(ppfs->info),showsign);
  2481. ++s; /* handle '-' in the prefix string */
  2482. prefix_num = PREFIX_MINUS;
  2483. } else if (PRINT_INFO_FLAG_VAL(&(ppfs->info),showsign)) {
  2484. prefix_num = PREFIX_PLUS;
  2485. } else if (PRINT_INFO_FLAG_VAL(&(ppfs->info),space)) {
  2486. prefix_num = PREFIX_SPACE;
  2487. }
  2488. }
  2489. slen = (char *)(buf + sizeof(buf) - 1) - s;
  2490. #ifdef L_vfwprintf
  2491. {
  2492. const char *q = s;
  2493. mbstate.mask = 0; /* Initialize the mbstate. */
  2494. SLEN = mbsrtowcs(NULL, &q, 0, &mbstate);
  2495. }
  2496. #endif
  2497. numfill = ((ppfs->info.prec < 0) ? 1 : ppfs->info.prec);
  2498. if (PRINT_INFO_FLAG_VAL(&(ppfs->info),alt)) {
  2499. if (ppfs->conv_num <= CONV_x) { /* x or p */
  2500. prefix_num = PREFIX_LWR_X;
  2501. }
  2502. if (ppfs->conv_num == CONV_X) {
  2503. prefix_num = PREFIX_UPR_X;
  2504. }
  2505. if ((ppfs->conv_num == CONV_o) && (numfill <= SLEN)) {
  2506. numfill = ((*s == '0') ? 1 : SLEN + 1);
  2507. }
  2508. }
  2509. if (*s == '0') {
  2510. if (prefix_num >= PREFIX_LWR_X) {
  2511. prefix_num = PREFIX_NONE;
  2512. }
  2513. if (ppfs->conv_num == CONV_p) {/* null pointer */
  2514. s = "(nil)";
  2515. #ifdef L_vfwprintf
  2516. SLEN =
  2517. #endif
  2518. slen = 5;
  2519. numfill = 0;
  2520. } else if (numfill == 0) { /* if precision 0, no output */
  2521. #ifdef L_vfwprintf
  2522. SLEN =
  2523. #endif
  2524. slen = 0;
  2525. }
  2526. }
  2527. numfill = ((numfill > SLEN) ? numfill - SLEN : 0);
  2528. } else if (ppfs->conv_num <= CONV_A) { /* floating point */
  2529. #ifdef __STDIO_PRINTF_FLOAT
  2530. *count +=
  2531. _fpmaxtostr(stream,
  2532. (__fpmax_t)
  2533. (PRINT_INFO_FLAG_VAL(&(ppfs->info),is_long_double)
  2534. ? *(long double *) *argptr
  2535. : (long double) (* (double *) *argptr)),
  2536. &ppfs->info, FP_OUT );
  2537. return 0;
  2538. #else /* __STDIO_PRINTF_FLOAT */
  2539. return -1; /* TODO -- try to continue? */
  2540. #endif /* __STDIO_PRINTF_FLOAT */
  2541. } else if (ppfs->conv_num <= CONV_S) { /* wide char or string */
  2542. #ifdef L_vfprintf
  2543. #ifdef __UCLIBC_HAS_WCHAR__
  2544. mbstate.mask = 0; /* Initialize the mbstate. */
  2545. if (ppfs->conv_num == CONV_S) { /* wide string */
  2546. if (!(ws = *((const wchar_t **) *argptr))) {
  2547. goto NULL_STRING;
  2548. }
  2549. /* We use an awful uClibc-specific hack here, passing
  2550. * (char*) &ws as the conversion destination. This signals
  2551. * uClibc's wcsrtombs that we want a "restricted" length
  2552. * such that the mbs fits in a buffer of the specified
  2553. * size with no partial conversions. */
  2554. if ((slen = wcsrtombs((char *) &ws, &ws, /* Use awful hack! */
  2555. ((ppfs->info.prec >= 0)
  2556. ? ppfs->info.prec
  2557. : SIZE_MAX), &mbstate))
  2558. == ((size_t)-1)
  2559. ) {
  2560. return -1; /* EILSEQ */
  2561. }
  2562. } else { /* wide char */
  2563. s = buf;
  2564. slen = wcrtomb(s, (*((const wchar_t *) *argptr)), &mbstate);
  2565. if (slen == ((size_t)-1)) {
  2566. return -1; /* EILSEQ */
  2567. }
  2568. s[slen] = 0; /* TODO - Is this necessary? */
  2569. }
  2570. #else /* __UCLIBC_HAS_WCHAR__ */
  2571. return -1;
  2572. #endif /* __UCLIBC_HAS_WCHAR__ */
  2573. } else if (ppfs->conv_num <= CONV_s) { /* char or string */
  2574. if (ppfs->conv_num == CONV_s) { /* string */
  2575. s = *((char **) (*argptr));
  2576. if (s) {
  2577. #ifdef __STDIO_PRINTF_M_SUPPORT
  2578. SET_STRING_LEN:
  2579. #endif
  2580. slen = strnlen(s, ((ppfs->info.prec >= 0)
  2581. ? ppfs->info.prec : SIZE_MAX));
  2582. } else {
  2583. #ifdef __UCLIBC_HAS_WCHAR__
  2584. NULL_STRING:
  2585. #endif
  2586. s = "(null)";
  2587. slen = 6;
  2588. }
  2589. } else { /* char */
  2590. s = buf;
  2591. *s = (unsigned char)(*((const int *) *argptr));
  2592. s[1] = 0;
  2593. slen = 1;
  2594. }
  2595. #else /* L_vfprintf */
  2596. if (ppfs->conv_num == CONV_S) { /* wide string */
  2597. ws = *((wchar_t **) (*argptr));
  2598. if (!ws) {
  2599. goto NULL_STRING;
  2600. }
  2601. SLEN = wcsnlen(ws, ((ppfs->info.prec >= 0)
  2602. ? ppfs->info.prec : SIZE_MAX));
  2603. } else { /* wide char */
  2604. *wbuf = (wchar_t)(*((const wint_t *) *argptr));
  2605. CHAR_CASE:
  2606. ws = wbuf;
  2607. wbuf[1] = 0;
  2608. SLEN = 1;
  2609. }
  2610. } else if (ppfs->conv_num <= CONV_s) { /* char or string */
  2611. if (ppfs->conv_num == CONV_s) { /* string */
  2612. #ifdef __UCLIBC_MJN3_ONLY__
  2613. #warning TODO: Fix %s for vfwprintf... output upto illegal sequence?
  2614. #endif /* __UCLIBC_MJN3_ONLY__ */
  2615. s = *((char **) (*argptr));
  2616. if (s) {
  2617. #ifdef __STDIO_PRINTF_M_SUPPORT
  2618. SET_STRING_LEN:
  2619. #endif
  2620. /* We use an awful uClibc-specific hack here, passing
  2621. * (wchar_t*) &mbstate as the conversion destination.
  2622. * This signals uClibc's mbsrtowcs that we want a
  2623. * "restricted" length such that the mbs fits in a buffer
  2624. * of the specified size with no partial conversions. */
  2625. {
  2626. const char *q = s;
  2627. mbstate.mask = 0; /* Initialize the mbstate. */
  2628. SLEN = mbsrtowcs((wchar_t *) &mbstate, &q,
  2629. ((ppfs->info.prec >= 0)
  2630. ? ppfs->info.prec : SIZE_MAX),
  2631. &mbstate);
  2632. }
  2633. if (SLEN == ((size_t)(-1))) {
  2634. return -1; /* EILSEQ */
  2635. }
  2636. } else {
  2637. NULL_STRING:
  2638. s = "(null)";
  2639. SLEN = slen = 6;
  2640. }
  2641. } else { /* char */
  2642. *wbuf = btowc( (unsigned char)(*((const int *) *argptr)) );
  2643. goto CHAR_CASE;
  2644. }
  2645. #endif /* L_vfprintf */
  2646. #ifdef __STDIO_PRINTF_M_SUPPORT
  2647. } else if (ppfs->conv_num == CONV_m) {
  2648. s = _glibc_strerror_r(errno, buf, sizeof(buf));
  2649. goto SET_STRING_LEN;
  2650. #endif
  2651. } else {
  2652. #ifdef __STDIO_GLIBC_CUSTOM_PRINTF
  2653. assert(ppfs->conv_num == CONV_custom0);
  2654. s = _custom_printf_spec;
  2655. do {
  2656. if (*s == ppfs->info.spec) {
  2657. int rv;
  2658. /* TODO -- check return value for sanity? */
  2659. rv = (*_custom_printf_handler
  2660. [(int)(s-_custom_printf_spec)])
  2661. (stream, &ppfs->info, argptr);
  2662. if (rv < 0) {
  2663. return -1;
  2664. }
  2665. *count += rv;
  2666. return 0;
  2667. }
  2668. } while (++s < (_custom_printf_spec + MAX_USER_SPEC));
  2669. #endif /* __STDIO_GLIBC_CUSTOM_PRINTF */
  2670. assert(0);
  2671. return -1;
  2672. }
  2673. #ifdef __UCLIBC_MJN3_ONLY__
  2674. #ifdef L_vfprintf
  2675. #warning CONSIDER: If using outdigits and/or grouping, how should we pad?
  2676. #endif
  2677. #endif /* __UCLIBC_MJN3_ONLY__ */
  2678. {
  2679. size_t t;
  2680. t = SLEN + numfill;
  2681. if (prefix_num != PREFIX_NONE) {
  2682. t += ((prefix_num < PREFIX_LWR_X) ? 1 : 2);
  2683. }
  2684. numpad = ((ppfs->info.width > t) ? (ppfs->info.width - t) : 0);
  2685. *count += t + numpad;
  2686. }
  2687. if (padchar == '0') { /* TODO: check this */
  2688. numfill += numpad;
  2689. numpad = 0;
  2690. }
  2691. /* Now handle the output itself. */
  2692. if (!PRINT_INFO_FLAG_VAL(&(ppfs->info),left)) {
  2693. _charpad(stream, ' ', numpad);
  2694. numpad = 0;
  2695. }
  2696. OUTPUT(stream, prefix + prefix_num);
  2697. _charpad(stream, '0', numfill);
  2698. #ifdef L_vfprintf
  2699. #ifdef __UCLIBC_HAS_WCHAR__
  2700. if (!ws) {
  2701. assert(s);
  2702. _outnstr(stream, s, slen);
  2703. } else { /* wide string */
  2704. size_t t;
  2705. mbstate.mask = 0; /* Initialize the mbstate. */
  2706. while (slen) {
  2707. t = (slen <= sizeof(buf)) ? slen : sizeof(buf);
  2708. t = wcsrtombs(buf, &ws, t, &mbstate);
  2709. assert (t != ((size_t)(-1)));
  2710. _outnstr(stream, buf, t);
  2711. slen -= t;
  2712. }
  2713. }
  2714. #else /* __UCLIBC_HAS_WCHAR__ */
  2715. _outnstr(stream, s, slen);
  2716. #endif /* __UCLIBC_HAS_WCHAR__ */
  2717. #else /* L_vfprintf */
  2718. if (!ws) {
  2719. assert(s);
  2720. _outnstr(stream, s, SLEN);
  2721. } else {
  2722. _outnwcs(stream, ws, SLEN);
  2723. }
  2724. #endif /* L_vfprintf */
  2725. _charpad(stream, ' ', numpad);
  2726. }
  2727. return 0;
  2728. }
  2729. int VFPRINTF (FILE * __restrict stream,
  2730. register const FMT_TYPE * __restrict format,
  2731. va_list arg)
  2732. {
  2733. ppfs_t ppfs;
  2734. int count, r;
  2735. register const FMT_TYPE *s;
  2736. __STDIO_THREADLOCK(stream);
  2737. count = 0;
  2738. s = format;
  2739. if (_PPFS_init(&ppfs, format) < 0) { /* Bad format string. */
  2740. OUTNSTR(stream, (const FMT_TYPE *) ppfs.fmtpos,
  2741. STRLEN((const FMT_TYPE *)(ppfs.fmtpos)));
  2742. #if defined(L_vfprintf) && !defined(NDEBUG)
  2743. fprintf(stderr,"\nIMbS: \"%s\"\n\n", format);
  2744. #endif
  2745. count = -1;
  2746. } else {
  2747. _ppfs_prepargs(&ppfs, arg); /* This did a va_copy!!! */
  2748. do {
  2749. while (*format && (*format != '%')) {
  2750. ++format;
  2751. }
  2752. if (format-s) { /* output any literal text in format string */
  2753. if ( (r = OUTNSTR(stream, s, format-s)) < 0) {
  2754. count = -1;
  2755. break;
  2756. }
  2757. count += r;
  2758. }
  2759. if (!*format) { /* we're done */
  2760. break;
  2761. }
  2762. if (format[1] != '%') { /* if we get here, *format == '%' */
  2763. /* TODO: _do_one_spec needs to know what the output funcs are!!! */
  2764. ppfs.fmtpos = (const char *)(++format);
  2765. /* TODO: check -- should only fail on stream error */
  2766. if ( (r = _do_one_spec(stream, &ppfs, &count)) < 0) {
  2767. count = -1;
  2768. break;
  2769. }
  2770. s = format = (const FMT_TYPE *) ppfs.fmtpos;
  2771. } else { /* %% means literal %, so start new string */
  2772. s = ++format;
  2773. ++format;
  2774. }
  2775. } while (1);
  2776. va_end(ppfs.arg); /* Need to clean up after va_copy! */
  2777. }
  2778. __STDIO_THREADUNLOCK(stream);
  2779. return count;
  2780. }
  2781. #endif
  2782. /**********************************************************************/