printf.c 53 KB

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