printf.c 53 KB

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