printf.c 52 KB

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