printf.c 66 KB

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