printf.c 51 KB

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