printf.c 51 KB

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