printf.c 51 KB

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