printf.c 52 KB

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