printf.c 80 KB

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