printf.c 81 KB

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