printf.c 49 KB

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