printf.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121
  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 = strerror(errno);
  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. if (rv >= 0) fputc('\0', f);
  1277. fclose(f);
  1278. if (rv < 0) {
  1279. free(*buf);
  1280. *buf = NULL;
  1281. return -1;
  1282. }
  1283. return rv;
  1284. #endif
  1285. }
  1286. #endif
  1287. #endif
  1288. /**********************************************************************/
  1289. #ifdef L_vprintf
  1290. int vprintf(const char * __restrict format, va_list arg)
  1291. {
  1292. return vfprintf(stdout, format, arg);
  1293. }
  1294. #endif
  1295. /**********************************************************************/
  1296. #ifdef L_vsprintf
  1297. #if !defined(__STDIO_BUFFERS) && !defined(__STDIO_GLIBC_CUSTOM_STREAMS)
  1298. #warning skipping vsprintf since no buffering and no custom streams!
  1299. #else
  1300. int vsprintf(char *__restrict buf, const char * __restrict format,
  1301. va_list arg)
  1302. {
  1303. return vsnprintf(buf, SIZE_MAX, format, arg);
  1304. }
  1305. #endif
  1306. #endif
  1307. /**********************************************************************/
  1308. #ifdef L_fprintf
  1309. int fprintf(FILE * __restrict stream, const char * __restrict format, ...)
  1310. {
  1311. va_list arg;
  1312. int rv;
  1313. va_start(arg, format);
  1314. rv = vfprintf(stream, format, arg);
  1315. va_end(arg);
  1316. return rv;
  1317. }
  1318. #endif
  1319. /**********************************************************************/
  1320. #ifdef L_snprintf
  1321. #if !defined(__STDIO_BUFFERS) && !defined(__STDIO_GLIBC_CUSTOM_STREAMS)
  1322. #warning skipping snprintf since no buffering and no custom streams!
  1323. #else
  1324. int snprintf(char *__restrict buf, size_t size,
  1325. const char * __restrict format, ...)
  1326. {
  1327. va_list arg;
  1328. int rv;
  1329. va_start(arg, format);
  1330. rv = vsnprintf(buf, size, format, arg);
  1331. va_end(arg);
  1332. return rv;
  1333. }
  1334. #endif
  1335. #endif
  1336. /**********************************************************************/
  1337. #ifdef L_dprintf
  1338. int dprintf(int filedes, const char * __restrict format, ...)
  1339. {
  1340. va_list arg;
  1341. int rv;
  1342. va_start(arg, format);
  1343. rv = vdprintf(filedes, format, arg);
  1344. va_end(arg);
  1345. return rv;
  1346. }
  1347. #endif
  1348. /**********************************************************************/
  1349. #ifdef L_asprintf
  1350. #if !defined(__STDIO_BUFFERS) && !defined(__STDIO_GLIBC_CUSTOM_STREAMS)
  1351. #warning skipping asprintf and __asprintf since no buffering and no custom streams!
  1352. #else
  1353. weak_alias(__asprintf,asprintf)
  1354. int __asprintf(char **__restrict buf, const char * __restrict format, ...)
  1355. {
  1356. va_list arg;
  1357. int rv;
  1358. va_start(arg, format);
  1359. rv = vasprintf(buf, format, arg);
  1360. va_end(arg);
  1361. return rv;
  1362. }
  1363. #endif
  1364. #endif
  1365. /**********************************************************************/
  1366. #ifdef L_printf
  1367. int printf(const char * __restrict format, ...)
  1368. {
  1369. va_list arg;
  1370. int rv;
  1371. va_start(arg, format);
  1372. rv = vfprintf(stdout, format, arg);
  1373. va_end(arg);
  1374. return rv;
  1375. }
  1376. #endif
  1377. /**********************************************************************/
  1378. #ifdef L_sprintf
  1379. #if !defined(__STDIO_BUFFERS) && !defined(__STDIO_GLIBC_CUSTOM_STREAMS)
  1380. #warning skipping sprintf since no buffering and no custom streams!
  1381. #else
  1382. int sprintf(char *__restrict buf, const char * __restrict format, ...)
  1383. {
  1384. va_list arg;
  1385. int rv;
  1386. va_start(arg, format);
  1387. rv = vsnprintf(buf, SIZE_MAX, format, arg);
  1388. va_end(arg);
  1389. return rv;
  1390. }
  1391. #endif
  1392. #endif
  1393. /**********************************************************************/
  1394. #ifdef L__dtostr
  1395. /*
  1396. * Copyright (C) 2000, 2001 Manuel Novoa III
  1397. *
  1398. * Function: size_t _dtostr(FILE *fp, long double x, struct printf_info *info)
  1399. *
  1400. * This was written for uClibc to provide floating point support for
  1401. * the printf functions. It handles +/- infinity and nan on i386.
  1402. *
  1403. * Notes:
  1404. *
  1405. * At most MAX_DIGITS significant digits are kept. Any trailing digits
  1406. * are treated as 0 as they are really just the results of rounding noise
  1407. * anyway. If you want to do better, use an arbitary precision arithmetic
  1408. * package. ;-)
  1409. *
  1410. * It should also be fairly portable, as not assumptions are made about the
  1411. * bit-layout of doubles.
  1412. *
  1413. * It should be too difficult to convert this to handle long doubles on i386.
  1414. * For information, see the comments below.
  1415. *
  1416. * TODO:
  1417. * long double and/or float version? (note: for float can trim code some).
  1418. *
  1419. * Decrease the size. This is really much bigger than I'd like.
  1420. */
  1421. /*****************************************************************************/
  1422. /* Don't change anything that follows unless you know what you're doing. */
  1423. /*****************************************************************************/
  1424. /*
  1425. * Configuration for the scaling power table. Ignoring denormals, you
  1426. * should have 2**EXP_TABLE_SIZE >= LDBL_MAX_EXP >= 2**(EXP_TABLE_SIZE-1).
  1427. * The minimum for standard C is 6. For IEEE 8bit doubles, 9 suffices.
  1428. * For long doubles on i386, use 13.
  1429. */
  1430. #define EXP_TABLE_SIZE 13
  1431. /*
  1432. * Set this to the maximum number of digits you want converted.
  1433. * Conversion is done in blocks of DIGITS_PER_BLOCK (9 by default) digits.
  1434. * (20) 17 digits suffices to uniquely determine a (long) double on i386.
  1435. */
  1436. #define MAX_DIGITS 20
  1437. /*
  1438. * Set this to the smallest integer type capable of storing a pointer.
  1439. */
  1440. #define INT_OR_PTR int
  1441. /*
  1442. * This is really only used to check for infinities. The macro produces
  1443. * smaller code for i386 and, since this is tested before any floating point
  1444. * calculations, it doesn't appear to suffer from the excess precision problem
  1445. * caused by the FPU that strtod had. If it causes problems, call the function
  1446. * and compile zoicheck.c with -ffloat-store.
  1447. */
  1448. #define _zero_or_inf_check(x) ( x == (x/4) )
  1449. /*
  1450. * Fairly portable nan check. Bitwise for i386 generated larger code.
  1451. * If you have a better version, comment this out.
  1452. */
  1453. #define isnan(x) (x != x)
  1454. /*****************************************************************************/
  1455. /* Don't change anything that follows peroid!!! ;-) */
  1456. /*****************************************************************************/
  1457. #include <float.h>
  1458. /*****************************************************************************/
  1459. /*
  1460. * Set things up for the scaling power table.
  1461. */
  1462. #if EXP_TABLE_SIZE < 6
  1463. #error EXP_TABLE_SIZE should be at least 6 to comply with standards
  1464. #endif
  1465. #define EXP_TABLE_MAX (1U<<(EXP_TABLE_SIZE-1))
  1466. /*
  1467. * Only bother checking if this is too small.
  1468. */
  1469. #if LDBL_MAX_10_EXP/2 > EXP_TABLE_MAX
  1470. #error larger EXP_TABLE_SIZE needed
  1471. #endif
  1472. /*
  1473. * With 32 bit ints, we can get 9 digits per block.
  1474. */
  1475. #define DIGITS_PER_BLOCK 9
  1476. #if UINT_MAX >= 4294967295UL
  1477. #define DIGIT_BLOCK_TYPE int
  1478. #define DB_FMT "%.*d"
  1479. #elif LONG_MAX >= 4294967295UL
  1480. #define DIGIT_BLOCK_TYPE long
  1481. #define DB_FMT "%.*ld"
  1482. #else
  1483. #error need at least 32 bit longs
  1484. #endif
  1485. /* Maximum number of calls to fnprintf to output double. */
  1486. #define MAX_CALLS 8
  1487. /*****************************************************************************/
  1488. #define NUM_DIGIT_BLOCKS ((MAX_DIGITS+DIGITS_PER_BLOCK-1)/DIGITS_PER_BLOCK)
  1489. /* extra space for '-', '.', 'e+###', and nul */
  1490. #define BUF_SIZE ( 3 + NUM_DIGIT_BLOCKS * DIGITS_PER_BLOCK )
  1491. /*****************************************************************************/
  1492. static const char *fmts[] = {
  1493. "%0*d", "%.*s", ".", "inf", "INF", "nan", "NAN", "%*s"
  1494. };
  1495. /*****************************************************************************/
  1496. size_t _dtostr(FILE * fp, long double x, struct printf_info *info)
  1497. {
  1498. long double exp_table[EXP_TABLE_SIZE];
  1499. long double p10;
  1500. DIGIT_BLOCK_TYPE digit_block; /* int of at least 32 bits */
  1501. int i, j;
  1502. int round, o_exp;
  1503. int exp, exp_neg;
  1504. int width, preci;
  1505. char *s;
  1506. char *e;
  1507. char buf[BUF_SIZE];
  1508. INT_OR_PTR pc_fwi[2*MAX_CALLS];
  1509. INT_OR_PTR *ppc;
  1510. char exp_buf[8];
  1511. char drvr[8];
  1512. char *pdrvr;
  1513. int npc;
  1514. int cnt;
  1515. char sign_str[2];
  1516. char o_mode;
  1517. char mode;
  1518. /* check that INT_OR_PTR is sufficiently large */
  1519. assert( sizeof(INT_OR_PTR) == sizeof(char *) );
  1520. width = info->width;
  1521. preci = info->prec;
  1522. mode = info->spec;
  1523. if (mode == 'a') {
  1524. mode = 'g'; /* TODO -- fix */
  1525. }
  1526. if (mode == 'A') {
  1527. mode = 'G'; /* TODO -- fix */
  1528. }
  1529. if (preci < 0) {
  1530. preci = 6;
  1531. }
  1532. *sign_str = '\0';
  1533. if (PRINT_INFO_FLAG_VAL(info,showsign)) {
  1534. *sign_str = '+';
  1535. } else if (PRINT_INFO_FLAG_VAL(info,space)) {
  1536. *sign_str = ' ';
  1537. }
  1538. /* *sign_str = flag[FLAG_PLUS]; */
  1539. *(sign_str+1) = 0;
  1540. if (isnan(x)) { /* nan check */
  1541. pdrvr = drvr + 1;
  1542. *pdrvr++ = 5 + (mode < 'a');
  1543. pc_fwi[2] = 3;
  1544. info->pad = ' ';
  1545. /* flag[FLAG_0_PAD] = 0; */
  1546. goto EXIT_SPECIAL;
  1547. }
  1548. if (x == 0) { /* handle 0 now to avoid false positive */
  1549. exp = -1;
  1550. goto GENERATE_DIGITS;
  1551. }
  1552. if (x < 0) { /* convert negatives to positives */
  1553. *sign_str = '-';
  1554. x = -x;
  1555. }
  1556. if (_zero_or_inf_check(x)) { /* must be inf since zero handled above */
  1557. pdrvr = drvr + 1;
  1558. *pdrvr++ = 3 + + (mode < 'a');
  1559. pc_fwi[2] = 3;
  1560. info->pad = ' ';
  1561. /* flag[FLAG_0_PAD] = 0; */
  1562. goto EXIT_SPECIAL;
  1563. }
  1564. /* need to build the scaling table */
  1565. for (i = 0, p10 = 10 ; i < EXP_TABLE_SIZE ; i++) {
  1566. exp_table[i] = p10;
  1567. p10 *= p10;
  1568. }
  1569. exp_neg = 0;
  1570. if (x < 1e8) { /* do we need to scale up or down? */
  1571. exp_neg = 1;
  1572. }
  1573. exp = DIGITS_PER_BLOCK - 1;
  1574. i = EXP_TABLE_SIZE;
  1575. j = EXP_TABLE_MAX;
  1576. while ( i-- ) { /* scale x such that 1e8 <= x < 1e9 */
  1577. if (exp_neg) {
  1578. if (x * exp_table[i] < 1e9) {
  1579. x *= exp_table[i];
  1580. exp -= j;
  1581. }
  1582. } else {
  1583. if (x / exp_table[i] >= 1e8) {
  1584. x /= exp_table[i];
  1585. exp += j;
  1586. }
  1587. }
  1588. j >>= 1;
  1589. }
  1590. if (x >= 1e9) { /* handle bad rounding case */
  1591. x /= 10;
  1592. ++exp;
  1593. }
  1594. assert(x < 1e9);
  1595. GENERATE_DIGITS:
  1596. s = buf + 2; /* leave space for '\0' and '0' */
  1597. #if 1
  1598. #define ONE_E_NINE 1000000000L
  1599. #else
  1600. #define ONE_E_NINE 1e9
  1601. #endif
  1602. for (i = 0 ; i < NUM_DIGIT_BLOCKS ; ++i ) {
  1603. digit_block = (DIGIT_BLOCK_TYPE) x;
  1604. x = (x - digit_block) * ONE_E_NINE;
  1605. s += sprintf(s, DB_FMT, DIGITS_PER_BLOCK, digit_block);
  1606. }
  1607. /*************************************************************************/
  1608. *exp_buf = 'e';
  1609. if (mode < 'a') {
  1610. *exp_buf = 'E';
  1611. mode += ('a' - 'A');
  1612. }
  1613. o_mode = mode;
  1614. round = preci;
  1615. if ((mode == 'g') && (round > 0)){
  1616. --round;
  1617. }
  1618. if (mode == 'f') {
  1619. round += exp;
  1620. }
  1621. s = buf;
  1622. *s++ = 0; /* terminator for rounding and 0-triming */
  1623. *s = '0'; /* space to round */
  1624. i = 0;
  1625. e = s + MAX_DIGITS + 1;
  1626. if (round < MAX_DIGITS) {
  1627. e = s + round + 2;
  1628. if (*e >= '5') {
  1629. i = 1;
  1630. }
  1631. }
  1632. do { /* handle rounding and trim trailing 0s */
  1633. *--e += i; /* add the carry */
  1634. } while ((*e == '0') || (*e > '9'));
  1635. o_exp = exp;
  1636. if (e <= s) { /* we carried into extra digit */
  1637. ++o_exp;
  1638. e = s; /* needed if all 0s */
  1639. } else {
  1640. ++s;
  1641. }
  1642. *++e = 0; /* ending nul char */
  1643. if ((mode == 'g') && ((o_exp >= -4) && (o_exp <= round))) {
  1644. mode = 'f';
  1645. }
  1646. exp = o_exp;
  1647. if (mode != 'f') {
  1648. o_exp = 0;
  1649. }
  1650. if (o_exp < 0) {
  1651. *--s = '0'; /* fake the first digit */
  1652. }
  1653. pdrvr = drvr+1;
  1654. ppc = pc_fwi+2;
  1655. *pdrvr++ = 0;
  1656. *ppc++ = 1;
  1657. *ppc++ = (INT_OR_PTR)(*s++ - '0');
  1658. i = e - s; /* total digits */
  1659. if (o_exp >= 0) {
  1660. if (o_exp >= i) { /* all digit(s) left of decimal */
  1661. *pdrvr++ = 1;
  1662. *ppc++ = i;
  1663. *ppc++ = (INT_OR_PTR)(s);
  1664. o_exp -= i;
  1665. i = 0;
  1666. if (o_exp>0) { /* have 0s left of decimal */
  1667. *pdrvr++ = 0;
  1668. *ppc++ = o_exp;
  1669. *ppc++ = 0;
  1670. }
  1671. } else if (o_exp > 0) { /* decimal between digits */
  1672. *pdrvr++ = 1;
  1673. *ppc++ = o_exp;
  1674. *ppc++ = (INT_OR_PTR)(s);
  1675. s += o_exp;
  1676. i -= o_exp;
  1677. }
  1678. o_exp = -1;
  1679. }
  1680. if (PRINT_INFO_FLAG_VAL(info,alt)
  1681. /* flag[FLAG_HASH] */
  1682. || (i) || ((o_mode != 'g') && (preci > 0))) {
  1683. *pdrvr++ = 2; /* need decimal */
  1684. *ppc++ = 1; /* needed for width calc */
  1685. ppc++;
  1686. }
  1687. if (++o_exp < 0) { /* have 0s right of decimal */
  1688. *pdrvr++ = 0;
  1689. *ppc++ = -o_exp;
  1690. *ppc++ = 0;
  1691. }
  1692. if (i) { /* have digit(s) right of decimal */
  1693. *pdrvr++ = 1;
  1694. *ppc++ = i;
  1695. *ppc++ = (INT_OR_PTR)(s);
  1696. }
  1697. if (o_mode != 'g') {
  1698. i -= o_exp;
  1699. if (i < preci) { /* have 0s right of digits */
  1700. i = preci - i;
  1701. *pdrvr++ = 0;
  1702. *ppc++ = i;
  1703. *ppc++ = 0;
  1704. }
  1705. }
  1706. /* build exponent string */
  1707. if (mode != 'f') {
  1708. *pdrvr++ = 1;
  1709. *ppc++ = sprintf(exp_buf,"%c%+.2d", *exp_buf, exp);
  1710. *ppc++ = (INT_OR_PTR) exp_buf;
  1711. }
  1712. EXIT_SPECIAL:
  1713. npc = pdrvr - drvr;
  1714. ppc = pc_fwi + 2;
  1715. for (i=1 ; i< npc ; i++) {
  1716. width -= *(ppc++);
  1717. ppc++;
  1718. }
  1719. i = 0;
  1720. if (*sign_str) {
  1721. i = 1;
  1722. }
  1723. width -= i;
  1724. if (width <= 0) {
  1725. width = 0;
  1726. } else {
  1727. if (PRINT_INFO_FLAG_VAL(info,left)) { /* padding on right */
  1728. /* flag[FLAG_MINUS_LJUSTIFY] */
  1729. ++npc;
  1730. *pdrvr++ = 7;
  1731. *ppc = width;
  1732. *++ppc = (INT_OR_PTR)("");
  1733. width = 0;
  1734. } else if (info->pad == '0') { /* 0 padding */
  1735. /* (flag[FLAG_0_PAD] == '0') */
  1736. pc_fwi[2] += width;
  1737. width = 0;
  1738. }
  1739. }
  1740. *drvr = 7;
  1741. ppc = pc_fwi;
  1742. *ppc++ = width + i;
  1743. *ppc = (INT_OR_PTR) sign_str;
  1744. pdrvr = drvr;
  1745. ppc = pc_fwi;
  1746. cnt = 0;
  1747. for (i=0 ; i<npc ; i++) {
  1748. #if 1
  1749. fprintf(fp, fmts[(int)(*pdrvr++)], (INT_OR_PTR)(*(ppc)),
  1750. (INT_OR_PTR)(*(ppc+1)));
  1751. #else
  1752. j = fprintf(fp, fmts[(int)(*pdrvr++)], (INT_OR_PTR)(*(ppc)),
  1753. (INT_OR_PTR)(*(ppc+1)));
  1754. assert(j == *ppc);
  1755. #endif
  1756. /* if (size > *ppc) { */
  1757. /* size -= *ppc; */
  1758. /* } */
  1759. cnt += *ppc; /* to avoid problems if j == -1 */
  1760. ppc += 2;
  1761. }
  1762. return cnt;
  1763. }
  1764. #endif
  1765. /**********************************************************************/
  1766. #ifdef L__store_inttype
  1767. /* TODO -- right now, assumes intmax_t is either long or long long */
  1768. /* We assume int may be short or long, but short and long are different. */
  1769. void _store_inttype(register void *dest, int desttype, uintmax_t val)
  1770. {
  1771. if (desttype == __PA_FLAG_CHAR) { /* assume char not int */
  1772. *((unsigned char *) dest) = val;
  1773. return;
  1774. }
  1775. #if defined(LLONG_MAX) && (LONG_MAX != LLONG_MAX)
  1776. if (desttype == PA_FLAG_LONG_LONG) {
  1777. *((unsigned long long int *) dest) = val;
  1778. return;
  1779. }
  1780. #endif /* LLONG_MAX */
  1781. #if SHRT_MAX != INT_MAX
  1782. if (desttype == PA_FLAG_SHORT) {
  1783. *((unsigned short int *) dest) = val;
  1784. return;
  1785. }
  1786. #endif /* SHRT_MAX */
  1787. #if LONG_MAX != INT_MAX
  1788. if (desttype == PA_FLAG_LONG) {
  1789. *((unsigned long int *) dest) = val;
  1790. return;
  1791. }
  1792. #endif /* LONG_MAX */
  1793. *((unsigned int *) dest) = val;
  1794. }
  1795. #endif
  1796. /**********************************************************************/
  1797. #ifdef L__load_inttype
  1798. extern uintmax_t _load_inttype(int desttype, const void *src, int uflag)
  1799. {
  1800. if (uflag >= 0) { /* unsigned */
  1801. #if LONG_MAX != INT_MAX
  1802. if (desttype & (PA_FLAG_LONG|PA_FLAG_LONG_LONG)) {
  1803. #ifdef LLONG_MAX
  1804. if (desttype == PA_FLAG_LONG_LONG) {
  1805. return *((unsigned long long int *) src);
  1806. }
  1807. #endif
  1808. return *((unsigned long int *) src);
  1809. }
  1810. #else /* LONG_MAX != INT_MAX */
  1811. #ifdef LLONG_MAX
  1812. if (desttype & PA_FLAG_LONG_LONG) {
  1813. return *((unsigned long long int *) src);
  1814. }
  1815. #endif
  1816. #endif /* LONG_MAX != INT_MAX */
  1817. {
  1818. unsigned int x;
  1819. x = *((unsigned int *) src);
  1820. if (desttype == __PA_FLAG_CHAR) x = (unsigned char) x;
  1821. #if SHRT_MAX != INT_MAX
  1822. if (desttype == PA_FLAG_SHORT) x = (unsigned short int) x;
  1823. #endif
  1824. return x;
  1825. }
  1826. } else { /* signed */
  1827. #if LONG_MAX != INT_MAX
  1828. if (desttype & (PA_FLAG_LONG|PA_FLAG_LONG_LONG)) {
  1829. #ifdef LLONG_MAX
  1830. if (desttype == PA_FLAG_LONG_LONG) {
  1831. return *((long long int *) src);
  1832. }
  1833. #endif
  1834. return *((long int *) src);
  1835. }
  1836. #else /* LONG_MAX != INT_MAX */
  1837. #ifdef LLONG_MAX
  1838. if (desttype & PA_FLAG_LONG_LONG) {
  1839. return *((long long int *) src);
  1840. }
  1841. #endif
  1842. #endif /* LONG_MAX != INT_MAX */
  1843. {
  1844. int x;
  1845. x = *((int *) src);
  1846. if (desttype == __PA_FLAG_CHAR) x = (char) x;
  1847. #if SHRT_MAX != INT_MAX
  1848. if (desttype == PA_FLAG_SHORT) x = (short int) x;
  1849. #endif
  1850. return x;
  1851. }
  1852. }
  1853. }
  1854. #endif
  1855. /**********************************************************************/