vasnprintf.c 219 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608
  1. /* vsprintf with automatic memory allocation.
  2. Copyright (C) 1999, 2002-2011 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License along
  12. with this program; if not, write to the Free Software Foundation,
  13. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  14. /* This file can be parametrized with the following macros:
  15. VASNPRINTF The name of the function being defined.
  16. FCHAR_T The element type of the format string.
  17. DCHAR_T The element type of the destination (result) string.
  18. FCHAR_T_ONLY_ASCII Set to 1 to enable verification that all characters
  19. in the format string are ASCII. MUST be set if
  20. FCHAR_T and DCHAR_T are not the same type.
  21. DIRECTIVE Structure denoting a format directive.
  22. Depends on FCHAR_T.
  23. DIRECTIVES Structure denoting the set of format directives of a
  24. format string. Depends on FCHAR_T.
  25. PRINTF_PARSE Function that parses a format string.
  26. Depends on FCHAR_T.
  27. DCHAR_CPY memcpy like function for DCHAR_T[] arrays.
  28. DCHAR_SET memset like function for DCHAR_T[] arrays.
  29. DCHAR_MBSNLEN mbsnlen like function for DCHAR_T[] arrays.
  30. SNPRINTF The system's snprintf (or similar) function.
  31. This may be either snprintf or swprintf.
  32. TCHAR_T The element type of the argument and result string
  33. of the said SNPRINTF function. This may be either
  34. char or wchar_t. The code exploits that
  35. sizeof (TCHAR_T) | sizeof (DCHAR_T) and
  36. alignof (TCHAR_T) <= alignof (DCHAR_T).
  37. DCHAR_IS_TCHAR Set to 1 if DCHAR_T and TCHAR_T are the same type.
  38. DCHAR_CONV_FROM_ENCODING A function to convert from char[] to DCHAR[].
  39. DCHAR_IS_UINT8_T Set to 1 if DCHAR_T is uint8_t.
  40. DCHAR_IS_UINT16_T Set to 1 if DCHAR_T is uint16_t.
  41. DCHAR_IS_UINT32_T Set to 1 if DCHAR_T is uint32_t. */
  42. /* Tell glibc's <stdio.h> to provide a prototype for snprintf().
  43. This must come before <config.h> because <config.h> may include
  44. <features.h>, and once <features.h> has been included, it's too late. */
  45. #ifndef _GNU_SOURCE
  46. # define _GNU_SOURCE 1
  47. #endif
  48. #ifndef VASNPRINTF
  49. # include <config.h>
  50. #endif
  51. #ifndef IN_LIBINTL
  52. # include <alloca.h>
  53. #endif
  54. /* Specification. */
  55. #ifndef VASNPRINTF
  56. # if WIDE_CHAR_VERSION
  57. # include "vasnwprintf.h"
  58. # else
  59. # include "vasnprintf.h"
  60. # endif
  61. #endif
  62. #include <locale.h> /* localeconv() */
  63. #include <stdio.h> /* snprintf(), sprintf() */
  64. #include <stdlib.h> /* abort(), malloc(), realloc(), free() */
  65. #include <string.h> /* memcpy(), strlen() */
  66. #include <errno.h> /* errno */
  67. #include <limits.h> /* CHAR_BIT */
  68. #include <float.h> /* DBL_MAX_EXP, LDBL_MAX_EXP */
  69. #if HAVE_NL_LANGINFO
  70. # include <langinfo.h>
  71. #endif
  72. #ifndef VASNPRINTF
  73. # if WIDE_CHAR_VERSION
  74. # include "wprintf-parse.h"
  75. # else
  76. # include "printf-parse.h"
  77. # endif
  78. #endif
  79. /* Checked size_t computations. */
  80. #include "xsize.h"
  81. #include "verify.h"
  82. #if (NEED_PRINTF_DOUBLE || NEED_PRINTF_LONG_DOUBLE) && !defined IN_LIBINTL
  83. # include <math.h>
  84. # include "float+.h"
  85. #endif
  86. #if (NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE) && !defined IN_LIBINTL
  87. # include <math.h>
  88. # include "isnand-nolibm.h"
  89. #endif
  90. #if (NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE) && !defined IN_LIBINTL
  91. # include <math.h>
  92. # include "isnanl-nolibm.h"
  93. # include "fpucw.h"
  94. #endif
  95. #if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_DOUBLE) && !defined IN_LIBINTL
  96. # include <math.h>
  97. # include "isnand-nolibm.h"
  98. # include "printf-frexp.h"
  99. #endif
  100. #if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE) && !defined IN_LIBINTL
  101. # include <math.h>
  102. # include "isnanl-nolibm.h"
  103. # include "printf-frexpl.h"
  104. # include "fpucw.h"
  105. #endif
  106. /* Default parameters. */
  107. #ifndef VASNPRINTF
  108. # if WIDE_CHAR_VERSION
  109. # define VASNPRINTF vasnwprintf
  110. # define FCHAR_T wchar_t
  111. # define DCHAR_T wchar_t
  112. # define TCHAR_T wchar_t
  113. # define DCHAR_IS_TCHAR 1
  114. # define DIRECTIVE wchar_t_directive
  115. # define DIRECTIVES wchar_t_directives
  116. # define PRINTF_PARSE wprintf_parse
  117. # define DCHAR_CPY wmemcpy
  118. # define DCHAR_SET wmemset
  119. # else
  120. # define VASNPRINTF vasnprintf
  121. # define FCHAR_T char
  122. # define DCHAR_T char
  123. # define TCHAR_T char
  124. # define DCHAR_IS_TCHAR 1
  125. # define DIRECTIVE char_directive
  126. # define DIRECTIVES char_directives
  127. # define PRINTF_PARSE printf_parse
  128. # define DCHAR_CPY memcpy
  129. # define DCHAR_SET memset
  130. # endif
  131. #endif
  132. #if WIDE_CHAR_VERSION
  133. /* TCHAR_T is wchar_t. */
  134. # define USE_SNPRINTF 1
  135. # if HAVE_DECL__SNWPRINTF
  136. /* On Windows, the function swprintf() has a different signature than
  137. on Unix; we use the function _snwprintf() or - on mingw - snwprintf()
  138. instead. The mingw function snwprintf() has fewer bugs than the
  139. MSVCRT function _snwprintf(), so prefer that. */
  140. # if defined __MINGW32__
  141. # define SNPRINTF snwprintf
  142. # else
  143. # define SNPRINTF _snwprintf
  144. # endif
  145. # else
  146. /* Unix. */
  147. # define SNPRINTF swprintf
  148. # endif
  149. #else
  150. /* TCHAR_T is char. */
  151. /* Use snprintf if it exists under the name 'snprintf' or '_snprintf'.
  152. But don't use it on BeOS, since BeOS snprintf produces no output if the
  153. size argument is >= 0x3000000.
  154. Also don't use it on Linux libc5, since there snprintf with size = 1
  155. writes any output without bounds, like sprintf. */
  156. # if (HAVE_DECL__SNPRINTF || HAVE_SNPRINTF) && !defined __BEOS__ && !(__GNU_LIBRARY__ == 1)
  157. # define USE_SNPRINTF 1
  158. # else
  159. # define USE_SNPRINTF 0
  160. # endif
  161. # if HAVE_DECL__SNPRINTF
  162. /* Windows. The mingw function snprintf() has fewer bugs than the MSVCRT
  163. function _snprintf(), so prefer that. */
  164. # if defined __MINGW32__
  165. # define SNPRINTF snprintf
  166. /* Here we need to call the native snprintf, not rpl_snprintf. */
  167. # undef snprintf
  168. # else
  169. # define SNPRINTF _snprintf
  170. # endif
  171. # else
  172. /* Unix. */
  173. # define SNPRINTF snprintf
  174. /* Here we need to call the native snprintf, not rpl_snprintf. */
  175. # undef snprintf
  176. # endif
  177. #endif
  178. /* Here we need to call the native sprintf, not rpl_sprintf. */
  179. #undef sprintf
  180. /* GCC >= 4.0 with -Wall emits unjustified "... may be used uninitialized"
  181. warnings in this file. Use -Dlint to suppress them. */
  182. #ifdef lint
  183. # define IF_LINT(Code) Code
  184. #else
  185. # define IF_LINT(Code) /* empty */
  186. #endif
  187. /* Avoid some warnings from "gcc -Wshadow".
  188. This file doesn't use the exp() and remainder() functions. */
  189. #undef exp
  190. #define exp expo
  191. #undef remainder
  192. #define remainder rem
  193. #if (!USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99) && !WIDE_CHAR_VERSION
  194. # if (HAVE_STRNLEN && !defined _AIX)
  195. # define local_strnlen strnlen
  196. # else
  197. # ifndef local_strnlen_defined
  198. # define local_strnlen_defined 1
  199. static size_t
  200. local_strnlen (const char *string, size_t maxlen)
  201. {
  202. const char *end = memchr (string, '\0', maxlen);
  203. return end ? (size_t) (end - string) : maxlen;
  204. }
  205. # endif
  206. # endif
  207. #endif
  208. #if (((!USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99) && WIDE_CHAR_VERSION) || ((!USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || (NEED_PRINTF_DIRECTIVE_LS && !defined IN_LIBINTL)) && !WIDE_CHAR_VERSION && DCHAR_IS_TCHAR)) && HAVE_WCHAR_T
  209. # if HAVE_WCSLEN
  210. # define local_wcslen wcslen
  211. # else
  212. /* Solaris 2.5.1 has wcslen() in a separate library libw.so. To avoid
  213. a dependency towards this library, here is a local substitute.
  214. Define this substitute only once, even if this file is included
  215. twice in the same compilation unit. */
  216. # ifndef local_wcslen_defined
  217. # define local_wcslen_defined 1
  218. static size_t
  219. local_wcslen (const wchar_t *s)
  220. {
  221. const wchar_t *ptr;
  222. for (ptr = s; *ptr != (wchar_t) 0; ptr++)
  223. ;
  224. return ptr - s;
  225. }
  226. # endif
  227. # endif
  228. #endif
  229. #if (!USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99) && HAVE_WCHAR_T && WIDE_CHAR_VERSION
  230. # if HAVE_WCSNLEN
  231. # define local_wcsnlen wcsnlen
  232. # else
  233. # ifndef local_wcsnlen_defined
  234. # define local_wcsnlen_defined 1
  235. static size_t
  236. local_wcsnlen (const wchar_t *s, size_t maxlen)
  237. {
  238. const wchar_t *ptr;
  239. for (ptr = s; maxlen > 0 && *ptr != (wchar_t) 0; ptr++, maxlen--)
  240. ;
  241. return ptr - s;
  242. }
  243. # endif
  244. # endif
  245. #endif
  246. #if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE || NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE) && !defined IN_LIBINTL
  247. /* Determine the decimal-point character according to the current locale. */
  248. # ifndef decimal_point_char_defined
  249. # define decimal_point_char_defined 1
  250. static char
  251. decimal_point_char (void)
  252. {
  253. const char *point;
  254. /* Determine it in a multithread-safe way. We know nl_langinfo is
  255. multithread-safe on glibc systems and MacOS X systems, but is not required
  256. to be multithread-safe by POSIX. sprintf(), however, is multithread-safe.
  257. localeconv() is rarely multithread-safe. */
  258. # if HAVE_NL_LANGINFO && (__GLIBC__ || defined __UCLIBC__ || (defined __APPLE__ && defined __MACH__))
  259. point = nl_langinfo (RADIXCHAR);
  260. # elif 1
  261. char pointbuf[5];
  262. sprintf (pointbuf, "%#.0f", 1.0);
  263. point = &pointbuf[1];
  264. # else
  265. point = localeconv () -> decimal_point;
  266. # endif
  267. /* The decimal point is always a single byte: either '.' or ','. */
  268. return (point[0] != '\0' ? point[0] : '.');
  269. }
  270. # endif
  271. #endif
  272. #if NEED_PRINTF_INFINITE_DOUBLE && !NEED_PRINTF_DOUBLE && !defined IN_LIBINTL
  273. /* Equivalent to !isfinite(x) || x == 0, but does not require libm. */
  274. static int
  275. is_infinite_or_zero (double x)
  276. {
  277. return isnand (x) || x + x == x;
  278. }
  279. #endif
  280. #if NEED_PRINTF_INFINITE_LONG_DOUBLE && !NEED_PRINTF_LONG_DOUBLE && !defined IN_LIBINTL
  281. /* Equivalent to !isfinite(x) || x == 0, but does not require libm. */
  282. static int
  283. is_infinite_or_zerol (long double x)
  284. {
  285. return isnanl (x) || x + x == x;
  286. }
  287. #endif
  288. #if (NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_DOUBLE) && !defined IN_LIBINTL
  289. /* Converting 'long double' to decimal without rare rounding bugs requires
  290. real bignums. We use the naming conventions of GNU gmp, but vastly simpler
  291. (and slower) algorithms. */
  292. typedef unsigned int mp_limb_t;
  293. # define GMP_LIMB_BITS 32
  294. verify (sizeof (mp_limb_t) * CHAR_BIT == GMP_LIMB_BITS);
  295. typedef unsigned long long mp_twolimb_t;
  296. # define GMP_TWOLIMB_BITS 64
  297. verify (sizeof (mp_twolimb_t) * CHAR_BIT == GMP_TWOLIMB_BITS);
  298. /* Representation of a bignum >= 0. */
  299. typedef struct
  300. {
  301. size_t nlimbs;
  302. mp_limb_t *limbs; /* Bits in little-endian order, allocated with malloc(). */
  303. } mpn_t;
  304. /* Compute the product of two bignums >= 0.
  305. Return the allocated memory in case of success, NULL in case of memory
  306. allocation failure. */
  307. static void *
  308. multiply (mpn_t src1, mpn_t src2, mpn_t *dest)
  309. {
  310. const mp_limb_t *p1;
  311. const mp_limb_t *p2;
  312. size_t len1;
  313. size_t len2;
  314. if (src1.nlimbs <= src2.nlimbs)
  315. {
  316. len1 = src1.nlimbs;
  317. p1 = src1.limbs;
  318. len2 = src2.nlimbs;
  319. p2 = src2.limbs;
  320. }
  321. else
  322. {
  323. len1 = src2.nlimbs;
  324. p1 = src2.limbs;
  325. len2 = src1.nlimbs;
  326. p2 = src1.limbs;
  327. }
  328. /* Now 0 <= len1 <= len2. */
  329. if (len1 == 0)
  330. {
  331. /* src1 or src2 is zero. */
  332. dest->nlimbs = 0;
  333. dest->limbs = (mp_limb_t *) malloc (1);
  334. }
  335. else
  336. {
  337. /* Here 1 <= len1 <= len2. */
  338. size_t dlen;
  339. mp_limb_t *dp;
  340. size_t k, i, j;
  341. dlen = len1 + len2;
  342. dp = (mp_limb_t *) malloc (dlen * sizeof (mp_limb_t));
  343. if (dp == NULL)
  344. return NULL;
  345. for (k = len2; k > 0; )
  346. dp[--k] = 0;
  347. for (i = 0; i < len1; i++)
  348. {
  349. mp_limb_t digit1 = p1[i];
  350. mp_twolimb_t carry = 0;
  351. for (j = 0; j < len2; j++)
  352. {
  353. mp_limb_t digit2 = p2[j];
  354. carry += (mp_twolimb_t) digit1 * (mp_twolimb_t) digit2;
  355. carry += dp[i + j];
  356. dp[i + j] = (mp_limb_t) carry;
  357. carry = carry >> GMP_LIMB_BITS;
  358. }
  359. dp[i + len2] = (mp_limb_t) carry;
  360. }
  361. /* Normalise. */
  362. while (dlen > 0 && dp[dlen - 1] == 0)
  363. dlen--;
  364. dest->nlimbs = dlen;
  365. dest->limbs = dp;
  366. }
  367. return dest->limbs;
  368. }
  369. /* Compute the quotient of a bignum a >= 0 and a bignum b > 0.
  370. a is written as a = q * b + r with 0 <= r < b. q is the quotient, r
  371. the remainder.
  372. Finally, round-to-even is performed: If r > b/2 or if r = b/2 and q is odd,
  373. q is incremented.
  374. Return the allocated memory in case of success, NULL in case of memory
  375. allocation failure. */
  376. static void *
  377. divide (mpn_t a, mpn_t b, mpn_t *q)
  378. {
  379. /* Algorithm:
  380. First normalise a and b: a=[a[m-1],...,a[0]], b=[b[n-1],...,b[0]]
  381. with m>=0 and n>0 (in base beta = 2^GMP_LIMB_BITS).
  382. If m<n, then q:=0 and r:=a.
  383. If m>=n=1, perform a single-precision division:
  384. r:=0, j:=m,
  385. while j>0 do
  386. {Here (q[m-1]*beta^(m-1)+...+q[j]*beta^j) * b[0] + r*beta^j =
  387. = a[m-1]*beta^(m-1)+...+a[j]*beta^j und 0<=r<b[0]<beta}
  388. j:=j-1, r:=r*beta+a[j], q[j]:=floor(r/b[0]), r:=r-b[0]*q[j].
  389. Normalise [q[m-1],...,q[0]], yields q.
  390. If m>=n>1, perform a multiple-precision division:
  391. We have a/b < beta^(m-n+1).
  392. s:=intDsize-1-(highest bit in b[n-1]), 0<=s<intDsize.
  393. Shift a and b left by s bits, copying them. r:=a.
  394. r=[r[m],...,r[0]], b=[b[n-1],...,b[0]] with b[n-1]>=beta/2.
  395. For j=m-n,...,0: {Here 0 <= r < b*beta^(j+1).}
  396. Compute q* :
  397. q* := floor((r[j+n]*beta+r[j+n-1])/b[n-1]).
  398. In case of overflow (q* >= beta) set q* := beta-1.
  399. Compute c2 := ((r[j+n]*beta+r[j+n-1]) - q* * b[n-1])*beta + r[j+n-2]
  400. and c3 := b[n-2] * q*.
  401. {We have 0 <= c2 < 2*beta^2, even 0 <= c2 < beta^2 if no overflow
  402. occurred. Furthermore 0 <= c3 < beta^2.
  403. If there was overflow and
  404. r[j+n]*beta+r[j+n-1] - q* * b[n-1] >= beta, i.e. c2 >= beta^2,
  405. the next test can be skipped.}
  406. While c3 > c2, {Here 0 <= c2 < c3 < beta^2}
  407. Put q* := q* - 1, c2 := c2 + b[n-1]*beta, c3 := c3 - b[n-2].
  408. If q* > 0:
  409. Put r := r - b * q* * beta^j. In detail:
  410. [r[n+j],...,r[j]] := [r[n+j],...,r[j]] - q* * [b[n-1],...,b[0]].
  411. hence: u:=0, for i:=0 to n-1 do
  412. u := u + q* * b[i],
  413. r[j+i]:=r[j+i]-(u mod beta) (+ beta, if carry),
  414. u:=u div beta (+ 1, if carry in subtraction)
  415. r[n+j]:=r[n+j]-u.
  416. {Since always u = (q* * [b[i-1],...,b[0]] div beta^i) + 1
  417. < q* + 1 <= beta,
  418. the carry u does not overflow.}
  419. If a negative carry occurs, put q* := q* - 1
  420. and [r[n+j],...,r[j]] := [r[n+j],...,r[j]] + [0,b[n-1],...,b[0]].
  421. Set q[j] := q*.
  422. Normalise [q[m-n],..,q[0]]; this yields the quotient q.
  423. Shift [r[n-1],...,r[0]] right by s bits and normalise; this yields the
  424. rest r.
  425. The room for q[j] can be allocated at the memory location of r[n+j].
  426. Finally, round-to-even:
  427. Shift r left by 1 bit.
  428. If r > b or if r = b and q[0] is odd, q := q+1.
  429. */
  430. const mp_limb_t *a_ptr = a.limbs;
  431. size_t a_len = a.nlimbs;
  432. const mp_limb_t *b_ptr = b.limbs;
  433. size_t b_len = b.nlimbs;
  434. mp_limb_t *roomptr;
  435. mp_limb_t *tmp_roomptr = NULL;
  436. mp_limb_t *q_ptr;
  437. size_t q_len;
  438. mp_limb_t *r_ptr;
  439. size_t r_len;
  440. /* Allocate room for a_len+2 digits.
  441. (Need a_len+1 digits for the real division and 1 more digit for the
  442. final rounding of q.) */
  443. roomptr = (mp_limb_t *) malloc ((a_len + 2) * sizeof (mp_limb_t));
  444. if (roomptr == NULL)
  445. return NULL;
  446. /* Normalise a. */
  447. while (a_len > 0 && a_ptr[a_len - 1] == 0)
  448. a_len--;
  449. /* Normalise b. */
  450. for (;;)
  451. {
  452. if (b_len == 0)
  453. /* Division by zero. */
  454. abort ();
  455. if (b_ptr[b_len - 1] == 0)
  456. b_len--;
  457. else
  458. break;
  459. }
  460. /* Here m = a_len >= 0 and n = b_len > 0. */
  461. if (a_len < b_len)
  462. {
  463. /* m<n: trivial case. q=0, r := copy of a. */
  464. r_ptr = roomptr;
  465. r_len = a_len;
  466. memcpy (r_ptr, a_ptr, a_len * sizeof (mp_limb_t));
  467. q_ptr = roomptr + a_len;
  468. q_len = 0;
  469. }
  470. else if (b_len == 1)
  471. {
  472. /* n=1: single precision division.
  473. beta^(m-1) <= a < beta^m ==> beta^(m-2) <= a/b < beta^m */
  474. r_ptr = roomptr;
  475. q_ptr = roomptr + 1;
  476. {
  477. mp_limb_t den = b_ptr[0];
  478. mp_limb_t remainder = 0;
  479. const mp_limb_t *sourceptr = a_ptr + a_len;
  480. mp_limb_t *destptr = q_ptr + a_len;
  481. size_t count;
  482. for (count = a_len; count > 0; count--)
  483. {
  484. mp_twolimb_t num =
  485. ((mp_twolimb_t) remainder << GMP_LIMB_BITS) | *--sourceptr;
  486. *--destptr = num / den;
  487. remainder = num % den;
  488. }
  489. /* Normalise and store r. */
  490. if (remainder > 0)
  491. {
  492. r_ptr[0] = remainder;
  493. r_len = 1;
  494. }
  495. else
  496. r_len = 0;
  497. /* Normalise q. */
  498. q_len = a_len;
  499. if (q_ptr[q_len - 1] == 0)
  500. q_len--;
  501. }
  502. }
  503. else
  504. {
  505. /* n>1: multiple precision division.
  506. beta^(m-1) <= a < beta^m, beta^(n-1) <= b < beta^n ==>
  507. beta^(m-n-1) <= a/b < beta^(m-n+1). */
  508. /* Determine s. */
  509. size_t s;
  510. {
  511. mp_limb_t msd = b_ptr[b_len - 1]; /* = b[n-1], > 0 */
  512. /* Determine s = GMP_LIMB_BITS - integer_length (msd).
  513. Code copied from gnulib's integer_length.c. */
  514. # if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
  515. s = __builtin_clz (msd);
  516. # else
  517. # if defined DBL_EXPBIT0_WORD && defined DBL_EXPBIT0_BIT
  518. if (GMP_LIMB_BITS <= DBL_MANT_BIT)
  519. {
  520. /* Use 'double' operations.
  521. Assumes an IEEE 754 'double' implementation. */
  522. # define DBL_EXP_MASK ((DBL_MAX_EXP - DBL_MIN_EXP) | 7)
  523. # define DBL_EXP_BIAS (DBL_EXP_MASK / 2 - 1)
  524. # define NWORDS \
  525. ((sizeof (double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
  526. union { double value; unsigned int word[NWORDS]; } m;
  527. /* Use a single integer to floating-point conversion. */
  528. m.value = msd;
  529. s = GMP_LIMB_BITS
  530. - (((m.word[DBL_EXPBIT0_WORD] >> DBL_EXPBIT0_BIT) & DBL_EXP_MASK)
  531. - DBL_EXP_BIAS);
  532. }
  533. else
  534. # undef NWORDS
  535. # endif
  536. {
  537. s = 31;
  538. if (msd >= 0x10000)
  539. {
  540. msd = msd >> 16;
  541. s -= 16;
  542. }
  543. if (msd >= 0x100)
  544. {
  545. msd = msd >> 8;
  546. s -= 8;
  547. }
  548. if (msd >= 0x10)
  549. {
  550. msd = msd >> 4;
  551. s -= 4;
  552. }
  553. if (msd >= 0x4)
  554. {
  555. msd = msd >> 2;
  556. s -= 2;
  557. }
  558. if (msd >= 0x2)
  559. {
  560. msd = msd >> 1;
  561. s -= 1;
  562. }
  563. }
  564. # endif
  565. }
  566. /* 0 <= s < GMP_LIMB_BITS.
  567. Copy b, shifting it left by s bits. */
  568. if (s > 0)
  569. {
  570. tmp_roomptr = (mp_limb_t *) malloc (b_len * sizeof (mp_limb_t));
  571. if (tmp_roomptr == NULL)
  572. {
  573. free (roomptr);
  574. return NULL;
  575. }
  576. {
  577. const mp_limb_t *sourceptr = b_ptr;
  578. mp_limb_t *destptr = tmp_roomptr;
  579. mp_twolimb_t accu = 0;
  580. size_t count;
  581. for (count = b_len; count > 0; count--)
  582. {
  583. accu += (mp_twolimb_t) *sourceptr++ << s;
  584. *destptr++ = (mp_limb_t) accu;
  585. accu = accu >> GMP_LIMB_BITS;
  586. }
  587. /* accu must be zero, since that was how s was determined. */
  588. if (accu != 0)
  589. abort ();
  590. }
  591. b_ptr = tmp_roomptr;
  592. }
  593. /* Copy a, shifting it left by s bits, yields r.
  594. Memory layout:
  595. At the beginning: r = roomptr[0..a_len],
  596. at the end: r = roomptr[0..b_len-1], q = roomptr[b_len..a_len] */
  597. r_ptr = roomptr;
  598. if (s == 0)
  599. {
  600. memcpy (r_ptr, a_ptr, a_len * sizeof (mp_limb_t));
  601. r_ptr[a_len] = 0;
  602. }
  603. else
  604. {
  605. const mp_limb_t *sourceptr = a_ptr;
  606. mp_limb_t *destptr = r_ptr;
  607. mp_twolimb_t accu = 0;
  608. size_t count;
  609. for (count = a_len; count > 0; count--)
  610. {
  611. accu += (mp_twolimb_t) *sourceptr++ << s;
  612. *destptr++ = (mp_limb_t) accu;
  613. accu = accu >> GMP_LIMB_BITS;
  614. }
  615. *destptr++ = (mp_limb_t) accu;
  616. }
  617. q_ptr = roomptr + b_len;
  618. q_len = a_len - b_len + 1; /* q will have m-n+1 limbs */
  619. {
  620. size_t j = a_len - b_len; /* m-n */
  621. mp_limb_t b_msd = b_ptr[b_len - 1]; /* b[n-1] */
  622. mp_limb_t b_2msd = b_ptr[b_len - 2]; /* b[n-2] */
  623. mp_twolimb_t b_msdd = /* b[n-1]*beta+b[n-2] */
  624. ((mp_twolimb_t) b_msd << GMP_LIMB_BITS) | b_2msd;
  625. /* Division loop, traversed m-n+1 times.
  626. j counts down, b is unchanged, beta/2 <= b[n-1] < beta. */
  627. for (;;)
  628. {
  629. mp_limb_t q_star;
  630. mp_limb_t c1;
  631. if (r_ptr[j + b_len] < b_msd) /* r[j+n] < b[n-1] ? */
  632. {
  633. /* Divide r[j+n]*beta+r[j+n-1] by b[n-1], no overflow. */
  634. mp_twolimb_t num =
  635. ((mp_twolimb_t) r_ptr[j + b_len] << GMP_LIMB_BITS)
  636. | r_ptr[j + b_len - 1];
  637. q_star = num / b_msd;
  638. c1 = num % b_msd;
  639. }
  640. else
  641. {
  642. /* Overflow, hence r[j+n]*beta+r[j+n-1] >= beta*b[n-1]. */
  643. q_star = (mp_limb_t)~(mp_limb_t)0; /* q* = beta-1 */
  644. /* Test whether r[j+n]*beta+r[j+n-1] - (beta-1)*b[n-1] >= beta
  645. <==> r[j+n]*beta+r[j+n-1] + b[n-1] >= beta*b[n-1]+beta
  646. <==> b[n-1] < floor((r[j+n]*beta+r[j+n-1]+b[n-1])/beta)
  647. {<= beta !}.
  648. If yes, jump directly to the subtraction loop.
  649. (Otherwise, r[j+n]*beta+r[j+n-1] - (beta-1)*b[n-1] < beta
  650. <==> floor((r[j+n]*beta+r[j+n-1]+b[n-1])/beta) = b[n-1] ) */
  651. if (r_ptr[j + b_len] > b_msd
  652. || (c1 = r_ptr[j + b_len - 1] + b_msd) < b_msd)
  653. /* r[j+n] >= b[n-1]+1 or
  654. r[j+n] = b[n-1] and the addition r[j+n-1]+b[n-1] gives a
  655. carry. */
  656. goto subtract;
  657. }
  658. /* q_star = q*,
  659. c1 = (r[j+n]*beta+r[j+n-1]) - q* * b[n-1] (>=0, <beta). */
  660. {
  661. mp_twolimb_t c2 = /* c1*beta+r[j+n-2] */
  662. ((mp_twolimb_t) c1 << GMP_LIMB_BITS) | r_ptr[j + b_len - 2];
  663. mp_twolimb_t c3 = /* b[n-2] * q* */
  664. (mp_twolimb_t) b_2msd * (mp_twolimb_t) q_star;
  665. /* While c2 < c3, increase c2 and decrease c3.
  666. Consider c3-c2. While it is > 0, decrease it by
  667. b[n-1]*beta+b[n-2]. Because of b[n-1]*beta+b[n-2] >= beta^2/2
  668. this can happen only twice. */
  669. if (c3 > c2)
  670. {
  671. q_star = q_star - 1; /* q* := q* - 1 */
  672. if (c3 - c2 > b_msdd)
  673. q_star = q_star - 1; /* q* := q* - 1 */
  674. }
  675. }
  676. if (q_star > 0)
  677. subtract:
  678. {
  679. /* Subtract r := r - b * q* * beta^j. */
  680. mp_limb_t cr;
  681. {
  682. const mp_limb_t *sourceptr = b_ptr;
  683. mp_limb_t *destptr = r_ptr + j;
  684. mp_twolimb_t carry = 0;
  685. size_t count;
  686. for (count = b_len; count > 0; count--)
  687. {
  688. /* Here 0 <= carry <= q*. */
  689. carry =
  690. carry
  691. + (mp_twolimb_t) q_star * (mp_twolimb_t) *sourceptr++
  692. + (mp_limb_t) ~(*destptr);
  693. /* Here 0 <= carry <= beta*q* + beta-1. */
  694. *destptr++ = ~(mp_limb_t) carry;
  695. carry = carry >> GMP_LIMB_BITS; /* <= q* */
  696. }
  697. cr = (mp_limb_t) carry;
  698. }
  699. /* Subtract cr from r_ptr[j + b_len], then forget about
  700. r_ptr[j + b_len]. */
  701. if (cr > r_ptr[j + b_len])
  702. {
  703. /* Subtraction gave a carry. */
  704. q_star = q_star - 1; /* q* := q* - 1 */
  705. /* Add b back. */
  706. {
  707. const mp_limb_t *sourceptr = b_ptr;
  708. mp_limb_t *destptr = r_ptr + j;
  709. mp_limb_t carry = 0;
  710. size_t count;
  711. for (count = b_len; count > 0; count--)
  712. {
  713. mp_limb_t source1 = *sourceptr++;
  714. mp_limb_t source2 = *destptr;
  715. *destptr++ = source1 + source2 + carry;
  716. carry =
  717. (carry
  718. ? source1 >= (mp_limb_t) ~source2
  719. : source1 > (mp_limb_t) ~source2);
  720. }
  721. }
  722. /* Forget about the carry and about r[j+n]. */
  723. }
  724. }
  725. /* q* is determined. Store it as q[j]. */
  726. q_ptr[j] = q_star;
  727. if (j == 0)
  728. break;
  729. j--;
  730. }
  731. }
  732. r_len = b_len;
  733. /* Normalise q. */
  734. if (q_ptr[q_len - 1] == 0)
  735. q_len--;
  736. # if 0 /* Not needed here, since we need r only to compare it with b/2, and
  737. b is shifted left by s bits. */
  738. /* Shift r right by s bits. */
  739. if (s > 0)
  740. {
  741. mp_limb_t ptr = r_ptr + r_len;
  742. mp_twolimb_t accu = 0;
  743. size_t count;
  744. for (count = r_len; count > 0; count--)
  745. {
  746. accu = (mp_twolimb_t) (mp_limb_t) accu << GMP_LIMB_BITS;
  747. accu += (mp_twolimb_t) *--ptr << (GMP_LIMB_BITS - s);
  748. *ptr = (mp_limb_t) (accu >> GMP_LIMB_BITS);
  749. }
  750. }
  751. # endif
  752. /* Normalise r. */
  753. while (r_len > 0 && r_ptr[r_len - 1] == 0)
  754. r_len--;
  755. }
  756. /* Compare r << 1 with b. */
  757. if (r_len > b_len)
  758. goto increment_q;
  759. {
  760. size_t i;
  761. for (i = b_len;;)
  762. {
  763. mp_limb_t r_i =
  764. (i <= r_len && i > 0 ? r_ptr[i - 1] >> (GMP_LIMB_BITS - 1) : 0)
  765. | (i < r_len ? r_ptr[i] << 1 : 0);
  766. mp_limb_t b_i = (i < b_len ? b_ptr[i] : 0);
  767. if (r_i > b_i)
  768. goto increment_q;
  769. if (r_i < b_i)
  770. goto keep_q;
  771. if (i == 0)
  772. break;
  773. i--;
  774. }
  775. }
  776. if (q_len > 0 && ((q_ptr[0] & 1) != 0))
  777. /* q is odd. */
  778. increment_q:
  779. {
  780. size_t i;
  781. for (i = 0; i < q_len; i++)
  782. if (++(q_ptr[i]) != 0)
  783. goto keep_q;
  784. q_ptr[q_len++] = 1;
  785. }
  786. keep_q:
  787. if (tmp_roomptr != NULL)
  788. free (tmp_roomptr);
  789. q->limbs = q_ptr;
  790. q->nlimbs = q_len;
  791. return roomptr;
  792. }
  793. /* Convert a bignum a >= 0, multiplied with 10^extra_zeroes, to decimal
  794. representation.
  795. Destroys the contents of a.
  796. Return the allocated memory - containing the decimal digits in low-to-high
  797. order, terminated with a NUL character - in case of success, NULL in case
  798. of memory allocation failure. */
  799. static char *
  800. convert_to_decimal (mpn_t a, size_t extra_zeroes)
  801. {
  802. mp_limb_t *a_ptr = a.limbs;
  803. size_t a_len = a.nlimbs;
  804. /* 0.03345 is slightly larger than log(2)/(9*log(10)). */
  805. size_t c_len = 9 * ((size_t)(a_len * (GMP_LIMB_BITS * 0.03345f)) + 1);
  806. char *c_ptr = (char *) malloc (xsum (c_len, extra_zeroes));
  807. if (c_ptr != NULL)
  808. {
  809. char *d_ptr = c_ptr;
  810. for (; extra_zeroes > 0; extra_zeroes--)
  811. *d_ptr++ = '0';
  812. while (a_len > 0)
  813. {
  814. /* Divide a by 10^9, in-place. */
  815. mp_limb_t remainder = 0;
  816. mp_limb_t *ptr = a_ptr + a_len;
  817. size_t count;
  818. for (count = a_len; count > 0; count--)
  819. {
  820. mp_twolimb_t num =
  821. ((mp_twolimb_t) remainder << GMP_LIMB_BITS) | *--ptr;
  822. *ptr = num / 1000000000;
  823. remainder = num % 1000000000;
  824. }
  825. /* Store the remainder as 9 decimal digits. */
  826. for (count = 9; count > 0; count--)
  827. {
  828. *d_ptr++ = '0' + (remainder % 10);
  829. remainder = remainder / 10;
  830. }
  831. /* Normalize a. */
  832. if (a_ptr[a_len - 1] == 0)
  833. a_len--;
  834. }
  835. /* Remove leading zeroes. */
  836. while (d_ptr > c_ptr && d_ptr[-1] == '0')
  837. d_ptr--;
  838. /* But keep at least one zero. */
  839. if (d_ptr == c_ptr)
  840. *d_ptr++ = '0';
  841. /* Terminate the string. */
  842. *d_ptr = '\0';
  843. }
  844. return c_ptr;
  845. }
  846. # if NEED_PRINTF_LONG_DOUBLE
  847. /* Assuming x is finite and >= 0:
  848. write x as x = 2^e * m, where m is a bignum.
  849. Return the allocated memory in case of success, NULL in case of memory
  850. allocation failure. */
  851. static void *
  852. decode_long_double (long double x, int *ep, mpn_t *mp)
  853. {
  854. mpn_t m;
  855. int exp;
  856. long double y;
  857. size_t i;
  858. /* Allocate memory for result. */
  859. m.nlimbs = (LDBL_MANT_BIT + GMP_LIMB_BITS - 1) / GMP_LIMB_BITS;
  860. m.limbs = (mp_limb_t *) malloc (m.nlimbs * sizeof (mp_limb_t));
  861. if (m.limbs == NULL)
  862. return NULL;
  863. /* Split into exponential part and mantissa. */
  864. y = frexpl (x, &exp);
  865. if (!(y >= 0.0L && y < 1.0L))
  866. abort ();
  867. /* x = 2^exp * y = 2^(exp - LDBL_MANT_BIT) * (y * 2^LDBL_MANT_BIT), and the
  868. latter is an integer. */
  869. /* Convert the mantissa (y * 2^LDBL_MANT_BIT) to a sequence of limbs.
  870. I'm not sure whether it's safe to cast a 'long double' value between
  871. 2^31 and 2^32 to 'unsigned int', therefore play safe and cast only
  872. 'long double' values between 0 and 2^16 (to 'unsigned int' or 'int',
  873. doesn't matter). */
  874. # if (LDBL_MANT_BIT % GMP_LIMB_BITS) != 0
  875. # if (LDBL_MANT_BIT % GMP_LIMB_BITS) > GMP_LIMB_BITS / 2
  876. {
  877. mp_limb_t hi, lo;
  878. y *= (mp_limb_t) 1 << (LDBL_MANT_BIT % (GMP_LIMB_BITS / 2));
  879. hi = (int) y;
  880. y -= hi;
  881. if (!(y >= 0.0L && y < 1.0L))
  882. abort ();
  883. y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2);
  884. lo = (int) y;
  885. y -= lo;
  886. if (!(y >= 0.0L && y < 1.0L))
  887. abort ();
  888. m.limbs[LDBL_MANT_BIT / GMP_LIMB_BITS] = (hi << (GMP_LIMB_BITS / 2)) | lo;
  889. }
  890. # else
  891. {
  892. mp_limb_t d;
  893. y *= (mp_limb_t) 1 << (LDBL_MANT_BIT % GMP_LIMB_BITS);
  894. d = (int) y;
  895. y -= d;
  896. if (!(y >= 0.0L && y < 1.0L))
  897. abort ();
  898. m.limbs[LDBL_MANT_BIT / GMP_LIMB_BITS] = d;
  899. }
  900. # endif
  901. # endif
  902. for (i = LDBL_MANT_BIT / GMP_LIMB_BITS; i > 0; )
  903. {
  904. mp_limb_t hi, lo;
  905. y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2);
  906. hi = (int) y;
  907. y -= hi;
  908. if (!(y >= 0.0L && y < 1.0L))
  909. abort ();
  910. y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2);
  911. lo = (int) y;
  912. y -= lo;
  913. if (!(y >= 0.0L && y < 1.0L))
  914. abort ();
  915. m.limbs[--i] = (hi << (GMP_LIMB_BITS / 2)) | lo;
  916. }
  917. # if 0 /* On FreeBSD 6.1/x86, 'long double' numbers sometimes have excess
  918. precision. */
  919. if (!(y == 0.0L))
  920. abort ();
  921. # endif
  922. /* Normalise. */
  923. while (m.nlimbs > 0 && m.limbs[m.nlimbs - 1] == 0)
  924. m.nlimbs--;
  925. *mp = m;
  926. *ep = exp - LDBL_MANT_BIT;
  927. return m.limbs;
  928. }
  929. # endif
  930. # if NEED_PRINTF_DOUBLE
  931. /* Assuming x is finite and >= 0:
  932. write x as x = 2^e * m, where m is a bignum.
  933. Return the allocated memory in case of success, NULL in case of memory
  934. allocation failure. */
  935. static void *
  936. decode_double (double x, int *ep, mpn_t *mp)
  937. {
  938. mpn_t m;
  939. int exp;
  940. double y;
  941. size_t i;
  942. /* Allocate memory for result. */
  943. m.nlimbs = (DBL_MANT_BIT + GMP_LIMB_BITS - 1) / GMP_LIMB_BITS;
  944. m.limbs = (mp_limb_t *) malloc (m.nlimbs * sizeof (mp_limb_t));
  945. if (m.limbs == NULL)
  946. return NULL;
  947. /* Split into exponential part and mantissa. */
  948. y = frexp (x, &exp);
  949. if (!(y >= 0.0 && y < 1.0))
  950. abort ();
  951. /* x = 2^exp * y = 2^(exp - DBL_MANT_BIT) * (y * 2^DBL_MANT_BIT), and the
  952. latter is an integer. */
  953. /* Convert the mantissa (y * 2^DBL_MANT_BIT) to a sequence of limbs.
  954. I'm not sure whether it's safe to cast a 'double' value between
  955. 2^31 and 2^32 to 'unsigned int', therefore play safe and cast only
  956. 'double' values between 0 and 2^16 (to 'unsigned int' or 'int',
  957. doesn't matter). */
  958. # if (DBL_MANT_BIT % GMP_LIMB_BITS) != 0
  959. # if (DBL_MANT_BIT % GMP_LIMB_BITS) > GMP_LIMB_BITS / 2
  960. {
  961. mp_limb_t hi, lo;
  962. y *= (mp_limb_t) 1 << (DBL_MANT_BIT % (GMP_LIMB_BITS / 2));
  963. hi = (int) y;
  964. y -= hi;
  965. if (!(y >= 0.0 && y < 1.0))
  966. abort ();
  967. y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2);
  968. lo = (int) y;
  969. y -= lo;
  970. if (!(y >= 0.0 && y < 1.0))
  971. abort ();
  972. m.limbs[DBL_MANT_BIT / GMP_LIMB_BITS] = (hi << (GMP_LIMB_BITS / 2)) | lo;
  973. }
  974. # else
  975. {
  976. mp_limb_t d;
  977. y *= (mp_limb_t) 1 << (DBL_MANT_BIT % GMP_LIMB_BITS);
  978. d = (int) y;
  979. y -= d;
  980. if (!(y >= 0.0 && y < 1.0))
  981. abort ();
  982. m.limbs[DBL_MANT_BIT / GMP_LIMB_BITS] = d;
  983. }
  984. # endif
  985. # endif
  986. for (i = DBL_MANT_BIT / GMP_LIMB_BITS; i > 0; )
  987. {
  988. mp_limb_t hi, lo;
  989. y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2);
  990. hi = (int) y;
  991. y -= hi;
  992. if (!(y >= 0.0 && y < 1.0))
  993. abort ();
  994. y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2);
  995. lo = (int) y;
  996. y -= lo;
  997. if (!(y >= 0.0 && y < 1.0))
  998. abort ();
  999. m.limbs[--i] = (hi << (GMP_LIMB_BITS / 2)) | lo;
  1000. }
  1001. if (!(y == 0.0))
  1002. abort ();
  1003. /* Normalise. */
  1004. while (m.nlimbs > 0 && m.limbs[m.nlimbs - 1] == 0)
  1005. m.nlimbs--;
  1006. *mp = m;
  1007. *ep = exp - DBL_MANT_BIT;
  1008. return m.limbs;
  1009. }
  1010. # endif
  1011. /* Assuming x = 2^e * m is finite and >= 0, and n is an integer:
  1012. Returns the decimal representation of round (x * 10^n).
  1013. Return the allocated memory - containing the decimal digits in low-to-high
  1014. order, terminated with a NUL character - in case of success, NULL in case
  1015. of memory allocation failure. */
  1016. static char *
  1017. scale10_round_decimal_decoded (int e, mpn_t m, void *memory, int n)
  1018. {
  1019. int s;
  1020. size_t extra_zeroes;
  1021. unsigned int abs_n;
  1022. unsigned int abs_s;
  1023. mp_limb_t *pow5_ptr;
  1024. size_t pow5_len;
  1025. unsigned int s_limbs;
  1026. unsigned int s_bits;
  1027. mpn_t pow5;
  1028. mpn_t z;
  1029. void *z_memory;
  1030. char *digits;
  1031. if (memory == NULL)
  1032. return NULL;
  1033. /* x = 2^e * m, hence
  1034. y = round (2^e * 10^n * m) = round (2^(e+n) * 5^n * m)
  1035. = round (2^s * 5^n * m). */
  1036. s = e + n;
  1037. extra_zeroes = 0;
  1038. /* Factor out a common power of 10 if possible. */
  1039. if (s > 0 && n > 0)
  1040. {
  1041. extra_zeroes = (s < n ? s : n);
  1042. s -= extra_zeroes;
  1043. n -= extra_zeroes;
  1044. }
  1045. /* Here y = round (2^s * 5^n * m) * 10^extra_zeroes.
  1046. Before converting to decimal, we need to compute
  1047. z = round (2^s * 5^n * m). */
  1048. /* Compute 5^|n|, possibly shifted by |s| bits if n and s have the same
  1049. sign. 2.322 is slightly larger than log(5)/log(2). */
  1050. abs_n = (n >= 0 ? n : -n);
  1051. abs_s = (s >= 0 ? s : -s);
  1052. pow5_ptr = (mp_limb_t *) malloc (((int)(abs_n * (2.322f / GMP_LIMB_BITS)) + 1
  1053. + abs_s / GMP_LIMB_BITS + 1)
  1054. * sizeof (mp_limb_t));
  1055. if (pow5_ptr == NULL)
  1056. {
  1057. free (memory);
  1058. return NULL;
  1059. }
  1060. /* Initialize with 1. */
  1061. pow5_ptr[0] = 1;
  1062. pow5_len = 1;
  1063. /* Multiply with 5^|n|. */
  1064. if (abs_n > 0)
  1065. {
  1066. static mp_limb_t const small_pow5[13 + 1] =
  1067. {
  1068. 1, 5, 25, 125, 625, 3125, 15625, 78125, 390625, 1953125, 9765625,
  1069. 48828125, 244140625, 1220703125
  1070. };
  1071. unsigned int n13;
  1072. for (n13 = 0; n13 <= abs_n; n13 += 13)
  1073. {
  1074. mp_limb_t digit1 = small_pow5[n13 + 13 <= abs_n ? 13 : abs_n - n13];
  1075. size_t j;
  1076. mp_twolimb_t carry = 0;
  1077. for (j = 0; j < pow5_len; j++)
  1078. {
  1079. mp_limb_t digit2 = pow5_ptr[j];
  1080. carry += (mp_twolimb_t) digit1 * (mp_twolimb_t) digit2;
  1081. pow5_ptr[j] = (mp_limb_t) carry;
  1082. carry = carry >> GMP_LIMB_BITS;
  1083. }
  1084. if (carry > 0)
  1085. pow5_ptr[pow5_len++] = (mp_limb_t) carry;
  1086. }
  1087. }
  1088. s_limbs = abs_s / GMP_LIMB_BITS;
  1089. s_bits = abs_s % GMP_LIMB_BITS;
  1090. if (n >= 0 ? s >= 0 : s <= 0)
  1091. {
  1092. /* Multiply with 2^|s|. */
  1093. if (s_bits > 0)
  1094. {
  1095. mp_limb_t *ptr = pow5_ptr;
  1096. mp_twolimb_t accu = 0;
  1097. size_t count;
  1098. for (count = pow5_len; count > 0; count--)
  1099. {
  1100. accu += (mp_twolimb_t) *ptr << s_bits;
  1101. *ptr++ = (mp_limb_t) accu;
  1102. accu = accu >> GMP_LIMB_BITS;
  1103. }
  1104. if (accu > 0)
  1105. {
  1106. *ptr = (mp_limb_t) accu;
  1107. pow5_len++;
  1108. }
  1109. }
  1110. if (s_limbs > 0)
  1111. {
  1112. size_t count;
  1113. for (count = pow5_len; count > 0;)
  1114. {
  1115. count--;
  1116. pow5_ptr[s_limbs + count] = pow5_ptr[count];
  1117. }
  1118. for (count = s_limbs; count > 0;)
  1119. {
  1120. count--;
  1121. pow5_ptr[count] = 0;
  1122. }
  1123. pow5_len += s_limbs;
  1124. }
  1125. pow5.limbs = pow5_ptr;
  1126. pow5.nlimbs = pow5_len;
  1127. if (n >= 0)
  1128. {
  1129. /* Multiply m with pow5. No division needed. */
  1130. z_memory = multiply (m, pow5, &z);
  1131. }
  1132. else
  1133. {
  1134. /* Divide m by pow5 and round. */
  1135. z_memory = divide (m, pow5, &z);
  1136. }
  1137. }
  1138. else
  1139. {
  1140. pow5.limbs = pow5_ptr;
  1141. pow5.nlimbs = pow5_len;
  1142. if (n >= 0)
  1143. {
  1144. /* n >= 0, s < 0.
  1145. Multiply m with pow5, then divide by 2^|s|. */
  1146. mpn_t numerator;
  1147. mpn_t denominator;
  1148. void *tmp_memory;
  1149. tmp_memory = multiply (m, pow5, &numerator);
  1150. if (tmp_memory == NULL)
  1151. {
  1152. free (pow5_ptr);
  1153. free (memory);
  1154. return NULL;
  1155. }
  1156. /* Construct 2^|s|. */
  1157. {
  1158. mp_limb_t *ptr = pow5_ptr + pow5_len;
  1159. size_t i;
  1160. for (i = 0; i < s_limbs; i++)
  1161. ptr[i] = 0;
  1162. ptr[s_limbs] = (mp_limb_t) 1 << s_bits;
  1163. denominator.limbs = ptr;
  1164. denominator.nlimbs = s_limbs + 1;
  1165. }
  1166. z_memory = divide (numerator, denominator, &z);
  1167. free (tmp_memory);
  1168. }
  1169. else
  1170. {
  1171. /* n < 0, s > 0.
  1172. Multiply m with 2^s, then divide by pow5. */
  1173. mpn_t numerator;
  1174. mp_limb_t *num_ptr;
  1175. num_ptr = (mp_limb_t *) malloc ((m.nlimbs + s_limbs + 1)
  1176. * sizeof (mp_limb_t));
  1177. if (num_ptr == NULL)
  1178. {
  1179. free (pow5_ptr);
  1180. free (memory);
  1181. return NULL;
  1182. }
  1183. {
  1184. mp_limb_t *destptr = num_ptr;
  1185. {
  1186. size_t i;
  1187. for (i = 0; i < s_limbs; i++)
  1188. *destptr++ = 0;
  1189. }
  1190. if (s_bits > 0)
  1191. {
  1192. const mp_limb_t *sourceptr = m.limbs;
  1193. mp_twolimb_t accu = 0;
  1194. size_t count;
  1195. for (count = m.nlimbs; count > 0; count--)
  1196. {
  1197. accu += (mp_twolimb_t) *sourceptr++ << s_bits;
  1198. *destptr++ = (mp_limb_t) accu;
  1199. accu = accu >> GMP_LIMB_BITS;
  1200. }
  1201. if (accu > 0)
  1202. *destptr++ = (mp_limb_t) accu;
  1203. }
  1204. else
  1205. {
  1206. const mp_limb_t *sourceptr = m.limbs;
  1207. size_t count;
  1208. for (count = m.nlimbs; count > 0; count--)
  1209. *destptr++ = *sourceptr++;
  1210. }
  1211. numerator.limbs = num_ptr;
  1212. numerator.nlimbs = destptr - num_ptr;
  1213. }
  1214. z_memory = divide (numerator, pow5, &z);
  1215. free (num_ptr);
  1216. }
  1217. }
  1218. free (pow5_ptr);
  1219. free (memory);
  1220. /* Here y = round (x * 10^n) = z * 10^extra_zeroes. */
  1221. if (z_memory == NULL)
  1222. return NULL;
  1223. digits = convert_to_decimal (z, extra_zeroes);
  1224. free (z_memory);
  1225. return digits;
  1226. }
  1227. # if NEED_PRINTF_LONG_DOUBLE
  1228. /* Assuming x is finite and >= 0, and n is an integer:
  1229. Returns the decimal representation of round (x * 10^n).
  1230. Return the allocated memory - containing the decimal digits in low-to-high
  1231. order, terminated with a NUL character - in case of success, NULL in case
  1232. of memory allocation failure. */
  1233. static char *
  1234. scale10_round_decimal_long_double (long double x, int n)
  1235. {
  1236. int e IF_LINT(= 0);
  1237. mpn_t m;
  1238. void *memory = decode_long_double (x, &e, &m);
  1239. return scale10_round_decimal_decoded (e, m, memory, n);
  1240. }
  1241. # endif
  1242. # if NEED_PRINTF_DOUBLE
  1243. /* Assuming x is finite and >= 0, and n is an integer:
  1244. Returns the decimal representation of round (x * 10^n).
  1245. Return the allocated memory - containing the decimal digits in low-to-high
  1246. order, terminated with a NUL character - in case of success, NULL in case
  1247. of memory allocation failure. */
  1248. static char *
  1249. scale10_round_decimal_double (double x, int n)
  1250. {
  1251. int e IF_LINT(= 0);
  1252. mpn_t m;
  1253. void *memory = decode_double (x, &e, &m);
  1254. return scale10_round_decimal_decoded (e, m, memory, n);
  1255. }
  1256. # endif
  1257. # if NEED_PRINTF_LONG_DOUBLE
  1258. /* Assuming x is finite and > 0:
  1259. Return an approximation for n with 10^n <= x < 10^(n+1).
  1260. The approximation is usually the right n, but may be off by 1 sometimes. */
  1261. static int
  1262. floorlog10l (long double x)
  1263. {
  1264. int exp;
  1265. long double y;
  1266. double z;
  1267. double l;
  1268. /* Split into exponential part and mantissa. */
  1269. y = frexpl (x, &exp);
  1270. if (!(y >= 0.0L && y < 1.0L))
  1271. abort ();
  1272. if (y == 0.0L)
  1273. return INT_MIN;
  1274. if (y < 0.5L)
  1275. {
  1276. while (y < (1.0L / (1 << (GMP_LIMB_BITS / 2)) / (1 << (GMP_LIMB_BITS / 2))))
  1277. {
  1278. y *= 1.0L * (1 << (GMP_LIMB_BITS / 2)) * (1 << (GMP_LIMB_BITS / 2));
  1279. exp -= GMP_LIMB_BITS;
  1280. }
  1281. if (y < (1.0L / (1 << 16)))
  1282. {
  1283. y *= 1.0L * (1 << 16);
  1284. exp -= 16;
  1285. }
  1286. if (y < (1.0L / (1 << 8)))
  1287. {
  1288. y *= 1.0L * (1 << 8);
  1289. exp -= 8;
  1290. }
  1291. if (y < (1.0L / (1 << 4)))
  1292. {
  1293. y *= 1.0L * (1 << 4);
  1294. exp -= 4;
  1295. }
  1296. if (y < (1.0L / (1 << 2)))
  1297. {
  1298. y *= 1.0L * (1 << 2);
  1299. exp -= 2;
  1300. }
  1301. if (y < (1.0L / (1 << 1)))
  1302. {
  1303. y *= 1.0L * (1 << 1);
  1304. exp -= 1;
  1305. }
  1306. }
  1307. if (!(y >= 0.5L && y < 1.0L))
  1308. abort ();
  1309. /* Compute an approximation for l = log2(x) = exp + log2(y). */
  1310. l = exp;
  1311. z = y;
  1312. if (z < 0.70710678118654752444)
  1313. {
  1314. z *= 1.4142135623730950488;
  1315. l -= 0.5;
  1316. }
  1317. if (z < 0.8408964152537145431)
  1318. {
  1319. z *= 1.1892071150027210667;
  1320. l -= 0.25;
  1321. }
  1322. if (z < 0.91700404320467123175)
  1323. {
  1324. z *= 1.0905077326652576592;
  1325. l -= 0.125;
  1326. }
  1327. if (z < 0.9576032806985736469)
  1328. {
  1329. z *= 1.0442737824274138403;
  1330. l -= 0.0625;
  1331. }
  1332. /* Now 0.95 <= z <= 1.01. */
  1333. z = 1 - z;
  1334. /* log2(1-z) = 1/log(2) * (- z - z^2/2 - z^3/3 - z^4/4 - ...)
  1335. Four terms are enough to get an approximation with error < 10^-7. */
  1336. l -= 1.4426950408889634074 * z * (1.0 + z * (0.5 + z * ((1.0 / 3) + z * 0.25)));
  1337. /* Finally multiply with log(2)/log(10), yields an approximation for
  1338. log10(x). */
  1339. l *= 0.30102999566398119523;
  1340. /* Round down to the next integer. */
  1341. return (int) l + (l < 0 ? -1 : 0);
  1342. }
  1343. # endif
  1344. # if NEED_PRINTF_DOUBLE
  1345. /* Assuming x is finite and > 0:
  1346. Return an approximation for n with 10^n <= x < 10^(n+1).
  1347. The approximation is usually the right n, but may be off by 1 sometimes. */
  1348. static int
  1349. floorlog10 (double x)
  1350. {
  1351. int exp;
  1352. double y;
  1353. double z;
  1354. double l;
  1355. /* Split into exponential part and mantissa. */
  1356. y = frexp (x, &exp);
  1357. if (!(y >= 0.0 && y < 1.0))
  1358. abort ();
  1359. if (y == 0.0)
  1360. return INT_MIN;
  1361. if (y < 0.5)
  1362. {
  1363. while (y < (1.0 / (1 << (GMP_LIMB_BITS / 2)) / (1 << (GMP_LIMB_BITS / 2))))
  1364. {
  1365. y *= 1.0 * (1 << (GMP_LIMB_BITS / 2)) * (1 << (GMP_LIMB_BITS / 2));
  1366. exp -= GMP_LIMB_BITS;
  1367. }
  1368. if (y < (1.0 / (1 << 16)))
  1369. {
  1370. y *= 1.0 * (1 << 16);
  1371. exp -= 16;
  1372. }
  1373. if (y < (1.0 / (1 << 8)))
  1374. {
  1375. y *= 1.0 * (1 << 8);
  1376. exp -= 8;
  1377. }
  1378. if (y < (1.0 / (1 << 4)))
  1379. {
  1380. y *= 1.0 * (1 << 4);
  1381. exp -= 4;
  1382. }
  1383. if (y < (1.0 / (1 << 2)))
  1384. {
  1385. y *= 1.0 * (1 << 2);
  1386. exp -= 2;
  1387. }
  1388. if (y < (1.0 / (1 << 1)))
  1389. {
  1390. y *= 1.0 * (1 << 1);
  1391. exp -= 1;
  1392. }
  1393. }
  1394. if (!(y >= 0.5 && y < 1.0))
  1395. abort ();
  1396. /* Compute an approximation for l = log2(x) = exp + log2(y). */
  1397. l = exp;
  1398. z = y;
  1399. if (z < 0.70710678118654752444)
  1400. {
  1401. z *= 1.4142135623730950488;
  1402. l -= 0.5;
  1403. }
  1404. if (z < 0.8408964152537145431)
  1405. {
  1406. z *= 1.1892071150027210667;
  1407. l -= 0.25;
  1408. }
  1409. if (z < 0.91700404320467123175)
  1410. {
  1411. z *= 1.0905077326652576592;
  1412. l -= 0.125;
  1413. }
  1414. if (z < 0.9576032806985736469)
  1415. {
  1416. z *= 1.0442737824274138403;
  1417. l -= 0.0625;
  1418. }
  1419. /* Now 0.95 <= z <= 1.01. */
  1420. z = 1 - z;
  1421. /* log2(1-z) = 1/log(2) * (- z - z^2/2 - z^3/3 - z^4/4 - ...)
  1422. Four terms are enough to get an approximation with error < 10^-7. */
  1423. l -= 1.4426950408889634074 * z * (1.0 + z * (0.5 + z * ((1.0 / 3) + z * 0.25)));
  1424. /* Finally multiply with log(2)/log(10), yields an approximation for
  1425. log10(x). */
  1426. l *= 0.30102999566398119523;
  1427. /* Round down to the next integer. */
  1428. return (int) l + (l < 0 ? -1 : 0);
  1429. }
  1430. # endif
  1431. /* Tests whether a string of digits consists of exactly PRECISION zeroes and
  1432. a single '1' digit. */
  1433. static int
  1434. is_borderline (const char *digits, size_t precision)
  1435. {
  1436. for (; precision > 0; precision--, digits++)
  1437. if (*digits != '0')
  1438. return 0;
  1439. if (*digits != '1')
  1440. return 0;
  1441. digits++;
  1442. return *digits == '\0';
  1443. }
  1444. #endif
  1445. #if !USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99
  1446. /* Use a different function name, to make it possible that the 'wchar_t'
  1447. parametrization and the 'char' parametrization get compiled in the same
  1448. translation unit. */
  1449. # if WIDE_CHAR_VERSION
  1450. # define MAX_ROOM_NEEDED wmax_room_needed
  1451. # else
  1452. # define MAX_ROOM_NEEDED max_room_needed
  1453. # endif
  1454. /* Returns the number of TCHAR_T units needed as temporary space for the result
  1455. of sprintf or SNPRINTF of a single conversion directive. */
  1456. static inline size_t
  1457. MAX_ROOM_NEEDED (const arguments *ap, size_t arg_index, FCHAR_T conversion,
  1458. arg_type type, int flags, size_t width, int has_precision,
  1459. size_t precision, int pad_ourselves)
  1460. {
  1461. size_t tmp_length;
  1462. switch (conversion)
  1463. {
  1464. case 'd': case 'i': case 'u':
  1465. # if HAVE_LONG_LONG_INT
  1466. if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
  1467. tmp_length =
  1468. (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
  1469. * 0.30103 /* binary -> decimal */
  1470. )
  1471. + 1; /* turn floor into ceil */
  1472. else
  1473. # endif
  1474. if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
  1475. tmp_length =
  1476. (unsigned int) (sizeof (unsigned long) * CHAR_BIT
  1477. * 0.30103 /* binary -> decimal */
  1478. )
  1479. + 1; /* turn floor into ceil */
  1480. else
  1481. tmp_length =
  1482. (unsigned int) (sizeof (unsigned int) * CHAR_BIT
  1483. * 0.30103 /* binary -> decimal */
  1484. )
  1485. + 1; /* turn floor into ceil */
  1486. if (tmp_length < precision)
  1487. tmp_length = precision;
  1488. /* Multiply by 2, as an estimate for FLAG_GROUP. */
  1489. tmp_length = xsum (tmp_length, tmp_length);
  1490. /* Add 1, to account for a leading sign. */
  1491. tmp_length = xsum (tmp_length, 1);
  1492. break;
  1493. case 'o':
  1494. # if HAVE_LONG_LONG_INT
  1495. if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
  1496. tmp_length =
  1497. (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
  1498. * 0.333334 /* binary -> octal */
  1499. )
  1500. + 1; /* turn floor into ceil */
  1501. else
  1502. # endif
  1503. if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
  1504. tmp_length =
  1505. (unsigned int) (sizeof (unsigned long) * CHAR_BIT
  1506. * 0.333334 /* binary -> octal */
  1507. )
  1508. + 1; /* turn floor into ceil */
  1509. else
  1510. tmp_length =
  1511. (unsigned int) (sizeof (unsigned int) * CHAR_BIT
  1512. * 0.333334 /* binary -> octal */
  1513. )
  1514. + 1; /* turn floor into ceil */
  1515. if (tmp_length < precision)
  1516. tmp_length = precision;
  1517. /* Add 1, to account for a leading sign. */
  1518. tmp_length = xsum (tmp_length, 1);
  1519. break;
  1520. case 'x': case 'X':
  1521. # if HAVE_LONG_LONG_INT
  1522. if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
  1523. tmp_length =
  1524. (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
  1525. * 0.25 /* binary -> hexadecimal */
  1526. )
  1527. + 1; /* turn floor into ceil */
  1528. else
  1529. # endif
  1530. if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
  1531. tmp_length =
  1532. (unsigned int) (sizeof (unsigned long) * CHAR_BIT
  1533. * 0.25 /* binary -> hexadecimal */
  1534. )
  1535. + 1; /* turn floor into ceil */
  1536. else
  1537. tmp_length =
  1538. (unsigned int) (sizeof (unsigned int) * CHAR_BIT
  1539. * 0.25 /* binary -> hexadecimal */
  1540. )
  1541. + 1; /* turn floor into ceil */
  1542. if (tmp_length < precision)
  1543. tmp_length = precision;
  1544. /* Add 2, to account for a leading sign or alternate form. */
  1545. tmp_length = xsum (tmp_length, 2);
  1546. break;
  1547. case 'f': case 'F':
  1548. if (type == TYPE_LONGDOUBLE)
  1549. tmp_length =
  1550. (unsigned int) (LDBL_MAX_EXP
  1551. * 0.30103 /* binary -> decimal */
  1552. * 2 /* estimate for FLAG_GROUP */
  1553. )
  1554. + 1 /* turn floor into ceil */
  1555. + 10; /* sign, decimal point etc. */
  1556. else
  1557. tmp_length =
  1558. (unsigned int) (DBL_MAX_EXP
  1559. * 0.30103 /* binary -> decimal */
  1560. * 2 /* estimate for FLAG_GROUP */
  1561. )
  1562. + 1 /* turn floor into ceil */
  1563. + 10; /* sign, decimal point etc. */
  1564. tmp_length = xsum (tmp_length, precision);
  1565. break;
  1566. case 'e': case 'E': case 'g': case 'G':
  1567. tmp_length =
  1568. 12; /* sign, decimal point, exponent etc. */
  1569. tmp_length = xsum (tmp_length, precision);
  1570. break;
  1571. case 'a': case 'A':
  1572. if (type == TYPE_LONGDOUBLE)
  1573. tmp_length =
  1574. (unsigned int) (LDBL_DIG
  1575. * 0.831 /* decimal -> hexadecimal */
  1576. )
  1577. + 1; /* turn floor into ceil */
  1578. else
  1579. tmp_length =
  1580. (unsigned int) (DBL_DIG
  1581. * 0.831 /* decimal -> hexadecimal */
  1582. )
  1583. + 1; /* turn floor into ceil */
  1584. if (tmp_length < precision)
  1585. tmp_length = precision;
  1586. /* Account for sign, decimal point etc. */
  1587. tmp_length = xsum (tmp_length, 12);
  1588. break;
  1589. case 'c':
  1590. # if HAVE_WINT_T && !WIDE_CHAR_VERSION
  1591. if (type == TYPE_WIDE_CHAR)
  1592. tmp_length = MB_CUR_MAX;
  1593. else
  1594. # endif
  1595. tmp_length = 1;
  1596. break;
  1597. case 's':
  1598. # if HAVE_WCHAR_T
  1599. if (type == TYPE_WIDE_STRING)
  1600. {
  1601. # if WIDE_CHAR_VERSION
  1602. /* ISO C says about %ls in fwprintf:
  1603. "If the precision is not specified or is greater than the size
  1604. of the array, the array shall contain a null wide character."
  1605. So if there is a precision, we must not use wcslen. */
  1606. const wchar_t *arg = ap->arg[arg_index].a.a_wide_string;
  1607. if (has_precision)
  1608. tmp_length = local_wcsnlen (arg, precision);
  1609. else
  1610. tmp_length = local_wcslen (arg);
  1611. # else
  1612. /* ISO C says about %ls in fprintf:
  1613. "If a precision is specified, no more than that many bytes are
  1614. written (including shift sequences, if any), and the array
  1615. shall contain a null wide character if, to equal the multibyte
  1616. character sequence length given by the precision, the function
  1617. would need to access a wide character one past the end of the
  1618. array."
  1619. So if there is a precision, we must not use wcslen. */
  1620. /* This case has already been handled separately in VASNPRINTF. */
  1621. abort ();
  1622. # endif
  1623. }
  1624. else
  1625. # endif
  1626. {
  1627. # if WIDE_CHAR_VERSION
  1628. /* ISO C says about %s in fwprintf:
  1629. "If the precision is not specified or is greater than the size
  1630. of the converted array, the converted array shall contain a
  1631. null wide character."
  1632. So if there is a precision, we must not use strlen. */
  1633. /* This case has already been handled separately in VASNPRINTF. */
  1634. abort ();
  1635. # else
  1636. /* ISO C says about %s in fprintf:
  1637. "If the precision is not specified or greater than the size of
  1638. the array, the array shall contain a null character."
  1639. So if there is a precision, we must not use strlen. */
  1640. const char *arg = ap->arg[arg_index].a.a_string;
  1641. if (has_precision)
  1642. tmp_length = local_strnlen (arg, precision);
  1643. else
  1644. tmp_length = strlen (arg);
  1645. # endif
  1646. }
  1647. break;
  1648. case 'p':
  1649. tmp_length =
  1650. (unsigned int) (sizeof (void *) * CHAR_BIT
  1651. * 0.25 /* binary -> hexadecimal */
  1652. )
  1653. + 1 /* turn floor into ceil */
  1654. + 2; /* account for leading 0x */
  1655. break;
  1656. default:
  1657. abort ();
  1658. }
  1659. if (!pad_ourselves)
  1660. {
  1661. # if ENABLE_UNISTDIO
  1662. /* Padding considers the number of characters, therefore the number of
  1663. elements after padding may be
  1664. > max (tmp_length, width)
  1665. but is certainly
  1666. <= tmp_length + width. */
  1667. tmp_length = xsum (tmp_length, width);
  1668. # else
  1669. /* Padding considers the number of elements, says POSIX. */
  1670. if (tmp_length < width)
  1671. tmp_length = width;
  1672. # endif
  1673. }
  1674. tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
  1675. return tmp_length;
  1676. }
  1677. #endif
  1678. DCHAR_T *
  1679. VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
  1680. const FCHAR_T *format, va_list args)
  1681. {
  1682. DIRECTIVES d;
  1683. arguments a;
  1684. if (PRINTF_PARSE (format, &d, &a) < 0)
  1685. /* errno is already set. */
  1686. return NULL;
  1687. #define CLEANUP() \
  1688. if (d.dir != d.direct_alloc_dir) \
  1689. free (d.dir); \
  1690. if (a.arg != a.direct_alloc_arg) \
  1691. free (a.arg);
  1692. if (PRINTF_FETCHARGS (args, &a) < 0)
  1693. {
  1694. CLEANUP ();
  1695. errno = EINVAL;
  1696. return NULL;
  1697. }
  1698. {
  1699. size_t buf_neededlength;
  1700. TCHAR_T *buf;
  1701. TCHAR_T *buf_malloced;
  1702. const FCHAR_T *cp;
  1703. size_t i;
  1704. DIRECTIVE *dp;
  1705. /* Output string accumulator. */
  1706. DCHAR_T *result;
  1707. size_t allocated;
  1708. size_t length;
  1709. /* Allocate a small buffer that will hold a directive passed to
  1710. sprintf or snprintf. */
  1711. buf_neededlength =
  1712. xsum4 (7, d.max_width_length, d.max_precision_length, 6);
  1713. #if HAVE_ALLOCA
  1714. if (buf_neededlength < 4000 / sizeof (TCHAR_T))
  1715. {
  1716. buf = (TCHAR_T *) alloca (buf_neededlength * sizeof (TCHAR_T));
  1717. buf_malloced = NULL;
  1718. }
  1719. else
  1720. #endif
  1721. {
  1722. size_t buf_memsize = xtimes (buf_neededlength, sizeof (TCHAR_T));
  1723. if (size_overflow_p (buf_memsize))
  1724. goto out_of_memory_1;
  1725. buf = (TCHAR_T *) malloc (buf_memsize);
  1726. if (buf == NULL)
  1727. goto out_of_memory_1;
  1728. buf_malloced = buf;
  1729. }
  1730. if (resultbuf != NULL)
  1731. {
  1732. result = resultbuf;
  1733. allocated = *lengthp;
  1734. }
  1735. else
  1736. {
  1737. result = NULL;
  1738. allocated = 0;
  1739. }
  1740. length = 0;
  1741. /* Invariants:
  1742. result is either == resultbuf or == NULL or malloc-allocated.
  1743. If length > 0, then result != NULL. */
  1744. /* Ensures that allocated >= needed. Aborts through a jump to
  1745. out_of_memory if needed is SIZE_MAX or otherwise too big. */
  1746. #define ENSURE_ALLOCATION(needed) \
  1747. if ((needed) > allocated) \
  1748. { \
  1749. size_t memory_size; \
  1750. DCHAR_T *memory; \
  1751. \
  1752. allocated = (allocated > 0 ? xtimes (allocated, 2) : 12); \
  1753. if ((needed) > allocated) \
  1754. allocated = (needed); \
  1755. memory_size = xtimes (allocated, sizeof (DCHAR_T)); \
  1756. if (size_overflow_p (memory_size)) \
  1757. goto out_of_memory; \
  1758. if (result == resultbuf || result == NULL) \
  1759. memory = (DCHAR_T *) malloc (memory_size); \
  1760. else \
  1761. memory = (DCHAR_T *) realloc (result, memory_size); \
  1762. if (memory == NULL) \
  1763. goto out_of_memory; \
  1764. if (result == resultbuf && length > 0) \
  1765. DCHAR_CPY (memory, result, length); \
  1766. result = memory; \
  1767. }
  1768. for (cp = format, i = 0, dp = &d.dir[0]; ; cp = dp->dir_end, i++, dp++)
  1769. {
  1770. if (cp != dp->dir_start)
  1771. {
  1772. size_t n = dp->dir_start - cp;
  1773. size_t augmented_length = xsum (length, n);
  1774. ENSURE_ALLOCATION (augmented_length);
  1775. /* This copies a piece of FCHAR_T[] into a DCHAR_T[]. Here we
  1776. need that the format string contains only ASCII characters
  1777. if FCHAR_T and DCHAR_T are not the same type. */
  1778. if (sizeof (FCHAR_T) == sizeof (DCHAR_T))
  1779. {
  1780. DCHAR_CPY (result + length, (const DCHAR_T *) cp, n);
  1781. length = augmented_length;
  1782. }
  1783. else
  1784. {
  1785. do
  1786. result[length++] = (unsigned char) *cp++;
  1787. while (--n > 0);
  1788. }
  1789. }
  1790. if (i == d.count)
  1791. break;
  1792. /* Execute a single directive. */
  1793. if (dp->conversion == '%')
  1794. {
  1795. size_t augmented_length;
  1796. if (!(dp->arg_index == ARG_NONE))
  1797. abort ();
  1798. augmented_length = xsum (length, 1);
  1799. ENSURE_ALLOCATION (augmented_length);
  1800. result[length] = '%';
  1801. length = augmented_length;
  1802. }
  1803. else
  1804. {
  1805. if (!(dp->arg_index != ARG_NONE))
  1806. abort ();
  1807. if (dp->conversion == 'n')
  1808. {
  1809. switch (a.arg[dp->arg_index].type)
  1810. {
  1811. case TYPE_COUNT_SCHAR_POINTER:
  1812. *a.arg[dp->arg_index].a.a_count_schar_pointer = length;
  1813. break;
  1814. case TYPE_COUNT_SHORT_POINTER:
  1815. *a.arg[dp->arg_index].a.a_count_short_pointer = length;
  1816. break;
  1817. case TYPE_COUNT_INT_POINTER:
  1818. *a.arg[dp->arg_index].a.a_count_int_pointer = length;
  1819. break;
  1820. case TYPE_COUNT_LONGINT_POINTER:
  1821. *a.arg[dp->arg_index].a.a_count_longint_pointer = length;
  1822. break;
  1823. #if HAVE_LONG_LONG_INT
  1824. case TYPE_COUNT_LONGLONGINT_POINTER:
  1825. *a.arg[dp->arg_index].a.a_count_longlongint_pointer = length;
  1826. break;
  1827. #endif
  1828. default:
  1829. abort ();
  1830. }
  1831. }
  1832. #if ENABLE_UNISTDIO
  1833. /* The unistdio extensions. */
  1834. else if (dp->conversion == 'U')
  1835. {
  1836. arg_type type = a.arg[dp->arg_index].type;
  1837. int flags = dp->flags;
  1838. int has_width;
  1839. size_t width;
  1840. int has_precision;
  1841. size_t precision;
  1842. has_width = 0;
  1843. width = 0;
  1844. if (dp->width_start != dp->width_end)
  1845. {
  1846. if (dp->width_arg_index != ARG_NONE)
  1847. {
  1848. int arg;
  1849. if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
  1850. abort ();
  1851. arg = a.arg[dp->width_arg_index].a.a_int;
  1852. if (arg < 0)
  1853. {
  1854. /* "A negative field width is taken as a '-' flag
  1855. followed by a positive field width." */
  1856. flags |= FLAG_LEFT;
  1857. width = (unsigned int) (-arg);
  1858. }
  1859. else
  1860. width = arg;
  1861. }
  1862. else
  1863. {
  1864. const FCHAR_T *digitp = dp->width_start;
  1865. do
  1866. width = xsum (xtimes (width, 10), *digitp++ - '0');
  1867. while (digitp != dp->width_end);
  1868. }
  1869. has_width = 1;
  1870. }
  1871. has_precision = 0;
  1872. precision = 0;
  1873. if (dp->precision_start != dp->precision_end)
  1874. {
  1875. if (dp->precision_arg_index != ARG_NONE)
  1876. {
  1877. int arg;
  1878. if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
  1879. abort ();
  1880. arg = a.arg[dp->precision_arg_index].a.a_int;
  1881. /* "A negative precision is taken as if the precision
  1882. were omitted." */
  1883. if (arg >= 0)
  1884. {
  1885. precision = arg;
  1886. has_precision = 1;
  1887. }
  1888. }
  1889. else
  1890. {
  1891. const FCHAR_T *digitp = dp->precision_start + 1;
  1892. precision = 0;
  1893. while (digitp != dp->precision_end)
  1894. precision = xsum (xtimes (precision, 10), *digitp++ - '0');
  1895. has_precision = 1;
  1896. }
  1897. }
  1898. switch (type)
  1899. {
  1900. case TYPE_U8_STRING:
  1901. {
  1902. const uint8_t *arg = a.arg[dp->arg_index].a.a_u8_string;
  1903. const uint8_t *arg_end;
  1904. size_t characters;
  1905. if (has_precision)
  1906. {
  1907. /* Use only PRECISION characters, from the left. */
  1908. arg_end = arg;
  1909. characters = 0;
  1910. for (; precision > 0; precision--)
  1911. {
  1912. int count = u8_strmblen (arg_end);
  1913. if (count == 0)
  1914. break;
  1915. if (count < 0)
  1916. {
  1917. if (!(result == resultbuf || result == NULL))
  1918. free (result);
  1919. if (buf_malloced != NULL)
  1920. free (buf_malloced);
  1921. CLEANUP ();
  1922. errno = EILSEQ;
  1923. return NULL;
  1924. }
  1925. arg_end += count;
  1926. characters++;
  1927. }
  1928. }
  1929. else if (has_width)
  1930. {
  1931. /* Use the entire string, and count the number of
  1932. characters. */
  1933. arg_end = arg;
  1934. characters = 0;
  1935. for (;;)
  1936. {
  1937. int count = u8_strmblen (arg_end);
  1938. if (count == 0)
  1939. break;
  1940. if (count < 0)
  1941. {
  1942. if (!(result == resultbuf || result == NULL))
  1943. free (result);
  1944. if (buf_malloced != NULL)
  1945. free (buf_malloced);
  1946. CLEANUP ();
  1947. errno = EILSEQ;
  1948. return NULL;
  1949. }
  1950. arg_end += count;
  1951. characters++;
  1952. }
  1953. }
  1954. else
  1955. {
  1956. /* Use the entire string. */
  1957. arg_end = arg + u8_strlen (arg);
  1958. /* The number of characters doesn't matter. */
  1959. characters = 0;
  1960. }
  1961. if (has_width && width > characters
  1962. && !(dp->flags & FLAG_LEFT))
  1963. {
  1964. size_t n = width - characters;
  1965. ENSURE_ALLOCATION (xsum (length, n));
  1966. DCHAR_SET (result + length, ' ', n);
  1967. length += n;
  1968. }
  1969. # if DCHAR_IS_UINT8_T
  1970. {
  1971. size_t n = arg_end - arg;
  1972. ENSURE_ALLOCATION (xsum (length, n));
  1973. DCHAR_CPY (result + length, arg, n);
  1974. length += n;
  1975. }
  1976. # else
  1977. { /* Convert. */
  1978. DCHAR_T *converted = result + length;
  1979. size_t converted_len = allocated - length;
  1980. # if DCHAR_IS_TCHAR
  1981. /* Convert from UTF-8 to locale encoding. */
  1982. converted =
  1983. u8_conv_to_encoding (locale_charset (),
  1984. iconveh_question_mark,
  1985. arg, arg_end - arg, NULL,
  1986. converted, &converted_len);
  1987. # else
  1988. /* Convert from UTF-8 to UTF-16/UTF-32. */
  1989. converted =
  1990. U8_TO_DCHAR (arg, arg_end - arg,
  1991. converted, &converted_len);
  1992. # endif
  1993. if (converted == NULL)
  1994. {
  1995. int saved_errno = errno;
  1996. if (!(result == resultbuf || result == NULL))
  1997. free (result);
  1998. if (buf_malloced != NULL)
  1999. free (buf_malloced);
  2000. CLEANUP ();
  2001. errno = saved_errno;
  2002. return NULL;
  2003. }
  2004. if (converted != result + length)
  2005. {
  2006. ENSURE_ALLOCATION (xsum (length, converted_len));
  2007. DCHAR_CPY (result + length, converted, converted_len);
  2008. free (converted);
  2009. }
  2010. length += converted_len;
  2011. }
  2012. # endif
  2013. if (has_width && width > characters
  2014. && (dp->flags & FLAG_LEFT))
  2015. {
  2016. size_t n = width - characters;
  2017. ENSURE_ALLOCATION (xsum (length, n));
  2018. DCHAR_SET (result + length, ' ', n);
  2019. length += n;
  2020. }
  2021. }
  2022. break;
  2023. case TYPE_U16_STRING:
  2024. {
  2025. const uint16_t *arg = a.arg[dp->arg_index].a.a_u16_string;
  2026. const uint16_t *arg_end;
  2027. size_t characters;
  2028. if (has_precision)
  2029. {
  2030. /* Use only PRECISION characters, from the left. */
  2031. arg_end = arg;
  2032. characters = 0;
  2033. for (; precision > 0; precision--)
  2034. {
  2035. int count = u16_strmblen (arg_end);
  2036. if (count == 0)
  2037. break;
  2038. if (count < 0)
  2039. {
  2040. if (!(result == resultbuf || result == NULL))
  2041. free (result);
  2042. if (buf_malloced != NULL)
  2043. free (buf_malloced);
  2044. CLEANUP ();
  2045. errno = EILSEQ;
  2046. return NULL;
  2047. }
  2048. arg_end += count;
  2049. characters++;
  2050. }
  2051. }
  2052. else if (has_width)
  2053. {
  2054. /* Use the entire string, and count the number of
  2055. characters. */
  2056. arg_end = arg;
  2057. characters = 0;
  2058. for (;;)
  2059. {
  2060. int count = u16_strmblen (arg_end);
  2061. if (count == 0)
  2062. break;
  2063. if (count < 0)
  2064. {
  2065. if (!(result == resultbuf || result == NULL))
  2066. free (result);
  2067. if (buf_malloced != NULL)
  2068. free (buf_malloced);
  2069. CLEANUP ();
  2070. errno = EILSEQ;
  2071. return NULL;
  2072. }
  2073. arg_end += count;
  2074. characters++;
  2075. }
  2076. }
  2077. else
  2078. {
  2079. /* Use the entire string. */
  2080. arg_end = arg + u16_strlen (arg);
  2081. /* The number of characters doesn't matter. */
  2082. characters = 0;
  2083. }
  2084. if (has_width && width > characters
  2085. && !(dp->flags & FLAG_LEFT))
  2086. {
  2087. size_t n = width - characters;
  2088. ENSURE_ALLOCATION (xsum (length, n));
  2089. DCHAR_SET (result + length, ' ', n);
  2090. length += n;
  2091. }
  2092. # if DCHAR_IS_UINT16_T
  2093. {
  2094. size_t n = arg_end - arg;
  2095. ENSURE_ALLOCATION (xsum (length, n));
  2096. DCHAR_CPY (result + length, arg, n);
  2097. length += n;
  2098. }
  2099. # else
  2100. { /* Convert. */
  2101. DCHAR_T *converted = result + length;
  2102. size_t converted_len = allocated - length;
  2103. # if DCHAR_IS_TCHAR
  2104. /* Convert from UTF-16 to locale encoding. */
  2105. converted =
  2106. u16_conv_to_encoding (locale_charset (),
  2107. iconveh_question_mark,
  2108. arg, arg_end - arg, NULL,
  2109. converted, &converted_len);
  2110. # else
  2111. /* Convert from UTF-16 to UTF-8/UTF-32. */
  2112. converted =
  2113. U16_TO_DCHAR (arg, arg_end - arg,
  2114. converted, &converted_len);
  2115. # endif
  2116. if (converted == NULL)
  2117. {
  2118. int saved_errno = errno;
  2119. if (!(result == resultbuf || result == NULL))
  2120. free (result);
  2121. if (buf_malloced != NULL)
  2122. free (buf_malloced);
  2123. CLEANUP ();
  2124. errno = saved_errno;
  2125. return NULL;
  2126. }
  2127. if (converted != result + length)
  2128. {
  2129. ENSURE_ALLOCATION (xsum (length, converted_len));
  2130. DCHAR_CPY (result + length, converted, converted_len);
  2131. free (converted);
  2132. }
  2133. length += converted_len;
  2134. }
  2135. # endif
  2136. if (has_width && width > characters
  2137. && (dp->flags & FLAG_LEFT))
  2138. {
  2139. size_t n = width - characters;
  2140. ENSURE_ALLOCATION (xsum (length, n));
  2141. DCHAR_SET (result + length, ' ', n);
  2142. length += n;
  2143. }
  2144. }
  2145. break;
  2146. case TYPE_U32_STRING:
  2147. {
  2148. const uint32_t *arg = a.arg[dp->arg_index].a.a_u32_string;
  2149. const uint32_t *arg_end;
  2150. size_t characters;
  2151. if (has_precision)
  2152. {
  2153. /* Use only PRECISION characters, from the left. */
  2154. arg_end = arg;
  2155. characters = 0;
  2156. for (; precision > 0; precision--)
  2157. {
  2158. int count = u32_strmblen (arg_end);
  2159. if (count == 0)
  2160. break;
  2161. if (count < 0)
  2162. {
  2163. if (!(result == resultbuf || result == NULL))
  2164. free (result);
  2165. if (buf_malloced != NULL)
  2166. free (buf_malloced);
  2167. CLEANUP ();
  2168. errno = EILSEQ;
  2169. return NULL;
  2170. }
  2171. arg_end += count;
  2172. characters++;
  2173. }
  2174. }
  2175. else if (has_width)
  2176. {
  2177. /* Use the entire string, and count the number of
  2178. characters. */
  2179. arg_end = arg;
  2180. characters = 0;
  2181. for (;;)
  2182. {
  2183. int count = u32_strmblen (arg_end);
  2184. if (count == 0)
  2185. break;
  2186. if (count < 0)
  2187. {
  2188. if (!(result == resultbuf || result == NULL))
  2189. free (result);
  2190. if (buf_malloced != NULL)
  2191. free (buf_malloced);
  2192. CLEANUP ();
  2193. errno = EILSEQ;
  2194. return NULL;
  2195. }
  2196. arg_end += count;
  2197. characters++;
  2198. }
  2199. }
  2200. else
  2201. {
  2202. /* Use the entire string. */
  2203. arg_end = arg + u32_strlen (arg);
  2204. /* The number of characters doesn't matter. */
  2205. characters = 0;
  2206. }
  2207. if (has_width && width > characters
  2208. && !(dp->flags & FLAG_LEFT))
  2209. {
  2210. size_t n = width - characters;
  2211. ENSURE_ALLOCATION (xsum (length, n));
  2212. DCHAR_SET (result + length, ' ', n);
  2213. length += n;
  2214. }
  2215. # if DCHAR_IS_UINT32_T
  2216. {
  2217. size_t n = arg_end - arg;
  2218. ENSURE_ALLOCATION (xsum (length, n));
  2219. DCHAR_CPY (result + length, arg, n);
  2220. length += n;
  2221. }
  2222. # else
  2223. { /* Convert. */
  2224. DCHAR_T *converted = result + length;
  2225. size_t converted_len = allocated - length;
  2226. # if DCHAR_IS_TCHAR
  2227. /* Convert from UTF-32 to locale encoding. */
  2228. converted =
  2229. u32_conv_to_encoding (locale_charset (),
  2230. iconveh_question_mark,
  2231. arg, arg_end - arg, NULL,
  2232. converted, &converted_len);
  2233. # else
  2234. /* Convert from UTF-32 to UTF-8/UTF-16. */
  2235. converted =
  2236. U32_TO_DCHAR (arg, arg_end - arg,
  2237. converted, &converted_len);
  2238. # endif
  2239. if (converted == NULL)
  2240. {
  2241. int saved_errno = errno;
  2242. if (!(result == resultbuf || result == NULL))
  2243. free (result);
  2244. if (buf_malloced != NULL)
  2245. free (buf_malloced);
  2246. CLEANUP ();
  2247. errno = saved_errno;
  2248. return NULL;
  2249. }
  2250. if (converted != result + length)
  2251. {
  2252. ENSURE_ALLOCATION (xsum (length, converted_len));
  2253. DCHAR_CPY (result + length, converted, converted_len);
  2254. free (converted);
  2255. }
  2256. length += converted_len;
  2257. }
  2258. # endif
  2259. if (has_width && width > characters
  2260. && (dp->flags & FLAG_LEFT))
  2261. {
  2262. size_t n = width - characters;
  2263. ENSURE_ALLOCATION (xsum (length, n));
  2264. DCHAR_SET (result + length, ' ', n);
  2265. length += n;
  2266. }
  2267. }
  2268. break;
  2269. default:
  2270. abort ();
  2271. }
  2272. }
  2273. #endif
  2274. #if (!USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || (NEED_PRINTF_DIRECTIVE_LS && !defined IN_LIBINTL)) && HAVE_WCHAR_T
  2275. else if (dp->conversion == 's'
  2276. # if WIDE_CHAR_VERSION
  2277. && a.arg[dp->arg_index].type != TYPE_WIDE_STRING
  2278. # else
  2279. && a.arg[dp->arg_index].type == TYPE_WIDE_STRING
  2280. # endif
  2281. )
  2282. {
  2283. /* The normal handling of the 's' directive below requires
  2284. allocating a temporary buffer. The determination of its
  2285. length (tmp_length), in the case when a precision is
  2286. specified, below requires a conversion between a char[]
  2287. string and a wchar_t[] wide string. It could be done, but
  2288. we have no guarantee that the implementation of sprintf will
  2289. use the exactly same algorithm. Without this guarantee, it
  2290. is possible to have buffer overrun bugs. In order to avoid
  2291. such bugs, we implement the entire processing of the 's'
  2292. directive ourselves. */
  2293. int flags = dp->flags;
  2294. int has_width;
  2295. size_t width;
  2296. int has_precision;
  2297. size_t precision;
  2298. has_width = 0;
  2299. width = 0;
  2300. if (dp->width_start != dp->width_end)
  2301. {
  2302. if (dp->width_arg_index != ARG_NONE)
  2303. {
  2304. int arg;
  2305. if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
  2306. abort ();
  2307. arg = a.arg[dp->width_arg_index].a.a_int;
  2308. if (arg < 0)
  2309. {
  2310. /* "A negative field width is taken as a '-' flag
  2311. followed by a positive field width." */
  2312. flags |= FLAG_LEFT;
  2313. width = (unsigned int) (-arg);
  2314. }
  2315. else
  2316. width = arg;
  2317. }
  2318. else
  2319. {
  2320. const FCHAR_T *digitp = dp->width_start;
  2321. do
  2322. width = xsum (xtimes (width, 10), *digitp++ - '0');
  2323. while (digitp != dp->width_end);
  2324. }
  2325. has_width = 1;
  2326. }
  2327. has_precision = 0;
  2328. precision = 6;
  2329. if (dp->precision_start != dp->precision_end)
  2330. {
  2331. if (dp->precision_arg_index != ARG_NONE)
  2332. {
  2333. int arg;
  2334. if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
  2335. abort ();
  2336. arg = a.arg[dp->precision_arg_index].a.a_int;
  2337. /* "A negative precision is taken as if the precision
  2338. were omitted." */
  2339. if (arg >= 0)
  2340. {
  2341. precision = arg;
  2342. has_precision = 1;
  2343. }
  2344. }
  2345. else
  2346. {
  2347. const FCHAR_T *digitp = dp->precision_start + 1;
  2348. precision = 0;
  2349. while (digitp != dp->precision_end)
  2350. precision = xsum (xtimes (precision, 10), *digitp++ - '0');
  2351. has_precision = 1;
  2352. }
  2353. }
  2354. # if WIDE_CHAR_VERSION
  2355. /* %s in vasnwprintf. See the specification of fwprintf. */
  2356. {
  2357. const char *arg = a.arg[dp->arg_index].a.a_string;
  2358. const char *arg_end;
  2359. size_t characters;
  2360. if (has_precision)
  2361. {
  2362. /* Use only as many bytes as needed to produce PRECISION
  2363. wide characters, from the left. */
  2364. # if HAVE_MBRTOWC
  2365. mbstate_t state;
  2366. memset (&state, '\0', sizeof (mbstate_t));
  2367. # endif
  2368. arg_end = arg;
  2369. characters = 0;
  2370. for (; precision > 0; precision--)
  2371. {
  2372. int count;
  2373. # if HAVE_MBRTOWC
  2374. count = mbrlen (arg_end, MB_CUR_MAX, &state);
  2375. # else
  2376. count = mblen (arg_end, MB_CUR_MAX);
  2377. # endif
  2378. if (count == 0)
  2379. /* Found the terminating NUL. */
  2380. break;
  2381. if (count < 0)
  2382. {
  2383. /* Invalid or incomplete multibyte character. */
  2384. if (!(result == resultbuf || result == NULL))
  2385. free (result);
  2386. if (buf_malloced != NULL)
  2387. free (buf_malloced);
  2388. CLEANUP ();
  2389. errno = EILSEQ;
  2390. return NULL;
  2391. }
  2392. arg_end += count;
  2393. characters++;
  2394. }
  2395. }
  2396. else if (has_width)
  2397. {
  2398. /* Use the entire string, and count the number of wide
  2399. characters. */
  2400. # if HAVE_MBRTOWC
  2401. mbstate_t state;
  2402. memset (&state, '\0', sizeof (mbstate_t));
  2403. # endif
  2404. arg_end = arg;
  2405. characters = 0;
  2406. for (;;)
  2407. {
  2408. int count;
  2409. # if HAVE_MBRTOWC
  2410. count = mbrlen (arg_end, MB_CUR_MAX, &state);
  2411. # else
  2412. count = mblen (arg_end, MB_CUR_MAX);
  2413. # endif
  2414. if (count == 0)
  2415. /* Found the terminating NUL. */
  2416. break;
  2417. if (count < 0)
  2418. {
  2419. /* Invalid or incomplete multibyte character. */
  2420. if (!(result == resultbuf || result == NULL))
  2421. free (result);
  2422. if (buf_malloced != NULL)
  2423. free (buf_malloced);
  2424. CLEANUP ();
  2425. errno = EILSEQ;
  2426. return NULL;
  2427. }
  2428. arg_end += count;
  2429. characters++;
  2430. }
  2431. }
  2432. else
  2433. {
  2434. /* Use the entire string. */
  2435. arg_end = arg + strlen (arg);
  2436. /* The number of characters doesn't matter. */
  2437. characters = 0;
  2438. }
  2439. if (has_width && width > characters
  2440. && !(dp->flags & FLAG_LEFT))
  2441. {
  2442. size_t n = width - characters;
  2443. ENSURE_ALLOCATION (xsum (length, n));
  2444. DCHAR_SET (result + length, ' ', n);
  2445. length += n;
  2446. }
  2447. if (has_precision || has_width)
  2448. {
  2449. /* We know the number of wide characters in advance. */
  2450. size_t remaining;
  2451. # if HAVE_MBRTOWC
  2452. mbstate_t state;
  2453. memset (&state, '\0', sizeof (mbstate_t));
  2454. # endif
  2455. ENSURE_ALLOCATION (xsum (length, characters));
  2456. for (remaining = characters; remaining > 0; remaining--)
  2457. {
  2458. wchar_t wc;
  2459. int count;
  2460. # if HAVE_MBRTOWC
  2461. count = mbrtowc (&wc, arg, arg_end - arg, &state);
  2462. # else
  2463. count = mbtowc (&wc, arg, arg_end - arg);
  2464. # endif
  2465. if (count <= 0)
  2466. /* mbrtowc not consistent with mbrlen, or mbtowc
  2467. not consistent with mblen. */
  2468. abort ();
  2469. result[length++] = wc;
  2470. arg += count;
  2471. }
  2472. if (!(arg == arg_end))
  2473. abort ();
  2474. }
  2475. else
  2476. {
  2477. # if HAVE_MBRTOWC
  2478. mbstate_t state;
  2479. memset (&state, '\0', sizeof (mbstate_t));
  2480. # endif
  2481. while (arg < arg_end)
  2482. {
  2483. wchar_t wc;
  2484. int count;
  2485. # if HAVE_MBRTOWC
  2486. count = mbrtowc (&wc, arg, arg_end - arg, &state);
  2487. # else
  2488. count = mbtowc (&wc, arg, arg_end - arg);
  2489. # endif
  2490. if (count <= 0)
  2491. /* mbrtowc not consistent with mbrlen, or mbtowc
  2492. not consistent with mblen. */
  2493. abort ();
  2494. ENSURE_ALLOCATION (xsum (length, 1));
  2495. result[length++] = wc;
  2496. arg += count;
  2497. }
  2498. }
  2499. if (has_width && width > characters
  2500. && (dp->flags & FLAG_LEFT))
  2501. {
  2502. size_t n = width - characters;
  2503. ENSURE_ALLOCATION (xsum (length, n));
  2504. DCHAR_SET (result + length, ' ', n);
  2505. length += n;
  2506. }
  2507. }
  2508. # else
  2509. /* %ls in vasnprintf. See the specification of fprintf. */
  2510. {
  2511. const wchar_t *arg = a.arg[dp->arg_index].a.a_wide_string;
  2512. const wchar_t *arg_end;
  2513. size_t characters;
  2514. # if !DCHAR_IS_TCHAR
  2515. /* This code assumes that TCHAR_T is 'char'. */
  2516. verify (sizeof (TCHAR_T) == 1);
  2517. TCHAR_T *tmpsrc;
  2518. DCHAR_T *tmpdst;
  2519. size_t tmpdst_len;
  2520. # endif
  2521. size_t w;
  2522. if (has_precision)
  2523. {
  2524. /* Use only as many wide characters as needed to produce
  2525. at most PRECISION bytes, from the left. */
  2526. # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
  2527. mbstate_t state;
  2528. memset (&state, '\0', sizeof (mbstate_t));
  2529. # endif
  2530. arg_end = arg;
  2531. characters = 0;
  2532. while (precision > 0)
  2533. {
  2534. char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */
  2535. int count;
  2536. if (*arg_end == 0)
  2537. /* Found the terminating null wide character. */
  2538. break;
  2539. # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
  2540. count = wcrtomb (cbuf, *arg_end, &state);
  2541. # else
  2542. count = wctomb (cbuf, *arg_end);
  2543. # endif
  2544. if (count < 0)
  2545. {
  2546. /* Cannot convert. */
  2547. if (!(result == resultbuf || result == NULL))
  2548. free (result);
  2549. if (buf_malloced != NULL)
  2550. free (buf_malloced);
  2551. CLEANUP ();
  2552. errno = EILSEQ;
  2553. return NULL;
  2554. }
  2555. if (precision < count)
  2556. break;
  2557. arg_end++;
  2558. characters += count;
  2559. precision -= count;
  2560. }
  2561. }
  2562. # if DCHAR_IS_TCHAR
  2563. else if (has_width)
  2564. # else
  2565. else
  2566. # endif
  2567. {
  2568. /* Use the entire string, and count the number of
  2569. bytes. */
  2570. # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
  2571. mbstate_t state;
  2572. memset (&state, '\0', sizeof (mbstate_t));
  2573. # endif
  2574. arg_end = arg;
  2575. characters = 0;
  2576. for (;;)
  2577. {
  2578. char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */
  2579. int count;
  2580. if (*arg_end == 0)
  2581. /* Found the terminating null wide character. */
  2582. break;
  2583. # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
  2584. count = wcrtomb (cbuf, *arg_end, &state);
  2585. # else
  2586. count = wctomb (cbuf, *arg_end);
  2587. # endif
  2588. if (count < 0)
  2589. {
  2590. /* Cannot convert. */
  2591. if (!(result == resultbuf || result == NULL))
  2592. free (result);
  2593. if (buf_malloced != NULL)
  2594. free (buf_malloced);
  2595. CLEANUP ();
  2596. errno = EILSEQ;
  2597. return NULL;
  2598. }
  2599. arg_end++;
  2600. characters += count;
  2601. }
  2602. }
  2603. # if DCHAR_IS_TCHAR
  2604. else
  2605. {
  2606. /* Use the entire string. */
  2607. arg_end = arg + local_wcslen (arg);
  2608. /* The number of bytes doesn't matter. */
  2609. characters = 0;
  2610. }
  2611. # endif
  2612. # if !DCHAR_IS_TCHAR
  2613. /* Convert the string into a piece of temporary memory. */
  2614. tmpsrc = (TCHAR_T *) malloc (characters * sizeof (TCHAR_T));
  2615. if (tmpsrc == NULL)
  2616. goto out_of_memory;
  2617. {
  2618. TCHAR_T *tmpptr = tmpsrc;
  2619. size_t remaining;
  2620. # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
  2621. mbstate_t state;
  2622. memset (&state, '\0', sizeof (mbstate_t));
  2623. # endif
  2624. for (remaining = characters; remaining > 0; )
  2625. {
  2626. char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */
  2627. int count;
  2628. if (*arg == 0)
  2629. abort ();
  2630. # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
  2631. count = wcrtomb (cbuf, *arg, &state);
  2632. # else
  2633. count = wctomb (cbuf, *arg);
  2634. # endif
  2635. if (count <= 0)
  2636. /* Inconsistency. */
  2637. abort ();
  2638. memcpy (tmpptr, cbuf, count);
  2639. tmpptr += count;
  2640. arg++;
  2641. remaining -= count;
  2642. }
  2643. if (!(arg == arg_end))
  2644. abort ();
  2645. }
  2646. /* Convert from TCHAR_T[] to DCHAR_T[]. */
  2647. tmpdst =
  2648. DCHAR_CONV_FROM_ENCODING (locale_charset (),
  2649. iconveh_question_mark,
  2650. tmpsrc, characters,
  2651. NULL,
  2652. NULL, &tmpdst_len);
  2653. if (tmpdst == NULL)
  2654. {
  2655. int saved_errno = errno;
  2656. free (tmpsrc);
  2657. if (!(result == resultbuf || result == NULL))
  2658. free (result);
  2659. if (buf_malloced != NULL)
  2660. free (buf_malloced);
  2661. CLEANUP ();
  2662. errno = saved_errno;
  2663. return NULL;
  2664. }
  2665. free (tmpsrc);
  2666. # endif
  2667. if (has_width)
  2668. {
  2669. # if ENABLE_UNISTDIO
  2670. /* Outside POSIX, it's preferrable to compare the width
  2671. against the number of _characters_ of the converted
  2672. value. */
  2673. w = DCHAR_MBSNLEN (result + length, characters);
  2674. # else
  2675. /* The width is compared against the number of _bytes_
  2676. of the converted value, says POSIX. */
  2677. w = characters;
  2678. # endif
  2679. }
  2680. else
  2681. /* w doesn't matter. */
  2682. w = 0;
  2683. if (has_width && width > w
  2684. && !(dp->flags & FLAG_LEFT))
  2685. {
  2686. size_t n = width - w;
  2687. ENSURE_ALLOCATION (xsum (length, n));
  2688. DCHAR_SET (result + length, ' ', n);
  2689. length += n;
  2690. }
  2691. # if DCHAR_IS_TCHAR
  2692. if (has_precision || has_width)
  2693. {
  2694. /* We know the number of bytes in advance. */
  2695. size_t remaining;
  2696. # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
  2697. mbstate_t state;
  2698. memset (&state, '\0', sizeof (mbstate_t));
  2699. # endif
  2700. ENSURE_ALLOCATION (xsum (length, characters));
  2701. for (remaining = characters; remaining > 0; )
  2702. {
  2703. char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */
  2704. int count;
  2705. if (*arg == 0)
  2706. abort ();
  2707. # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
  2708. count = wcrtomb (cbuf, *arg, &state);
  2709. # else
  2710. count = wctomb (cbuf, *arg);
  2711. # endif
  2712. if (count <= 0)
  2713. /* Inconsistency. */
  2714. abort ();
  2715. memcpy (result + length, cbuf, count);
  2716. length += count;
  2717. arg++;
  2718. remaining -= count;
  2719. }
  2720. if (!(arg == arg_end))
  2721. abort ();
  2722. }
  2723. else
  2724. {
  2725. # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
  2726. mbstate_t state;
  2727. memset (&state, '\0', sizeof (mbstate_t));
  2728. # endif
  2729. while (arg < arg_end)
  2730. {
  2731. char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */
  2732. int count;
  2733. if (*arg == 0)
  2734. abort ();
  2735. # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
  2736. count = wcrtomb (cbuf, *arg, &state);
  2737. # else
  2738. count = wctomb (cbuf, *arg);
  2739. # endif
  2740. if (count <= 0)
  2741. {
  2742. /* Cannot convert. */
  2743. if (!(result == resultbuf || result == NULL))
  2744. free (result);
  2745. if (buf_malloced != NULL)
  2746. free (buf_malloced);
  2747. CLEANUP ();
  2748. errno = EILSEQ;
  2749. return NULL;
  2750. }
  2751. ENSURE_ALLOCATION (xsum (length, count));
  2752. memcpy (result + length, cbuf, count);
  2753. length += count;
  2754. arg++;
  2755. }
  2756. }
  2757. # else
  2758. ENSURE_ALLOCATION (xsum (length, tmpdst_len));
  2759. DCHAR_CPY (result + length, tmpdst, tmpdst_len);
  2760. free (tmpdst);
  2761. length += tmpdst_len;
  2762. # endif
  2763. if (has_width && width > w
  2764. && (dp->flags & FLAG_LEFT))
  2765. {
  2766. size_t n = width - w;
  2767. ENSURE_ALLOCATION (xsum (length, n));
  2768. DCHAR_SET (result + length, ' ', n);
  2769. length += n;
  2770. }
  2771. }
  2772. # endif
  2773. }
  2774. #endif
  2775. #if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_DOUBLE) && !defined IN_LIBINTL
  2776. else if ((dp->conversion == 'a' || dp->conversion == 'A')
  2777. # if !(NEED_PRINTF_DIRECTIVE_A || (NEED_PRINTF_LONG_DOUBLE && NEED_PRINTF_DOUBLE))
  2778. && (0
  2779. # if NEED_PRINTF_DOUBLE
  2780. || a.arg[dp->arg_index].type == TYPE_DOUBLE
  2781. # endif
  2782. # if NEED_PRINTF_LONG_DOUBLE
  2783. || a.arg[dp->arg_index].type == TYPE_LONGDOUBLE
  2784. # endif
  2785. )
  2786. # endif
  2787. )
  2788. {
  2789. arg_type type = a.arg[dp->arg_index].type;
  2790. int flags = dp->flags;
  2791. int has_width;
  2792. size_t width;
  2793. int has_precision;
  2794. size_t precision;
  2795. size_t tmp_length;
  2796. DCHAR_T tmpbuf[700];
  2797. DCHAR_T *tmp;
  2798. DCHAR_T *pad_ptr;
  2799. DCHAR_T *p;
  2800. has_width = 0;
  2801. width = 0;
  2802. if (dp->width_start != dp->width_end)
  2803. {
  2804. if (dp->width_arg_index != ARG_NONE)
  2805. {
  2806. int arg;
  2807. if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
  2808. abort ();
  2809. arg = a.arg[dp->width_arg_index].a.a_int;
  2810. if (arg < 0)
  2811. {
  2812. /* "A negative field width is taken as a '-' flag
  2813. followed by a positive field width." */
  2814. flags |= FLAG_LEFT;
  2815. width = (unsigned int) (-arg);
  2816. }
  2817. else
  2818. width = arg;
  2819. }
  2820. else
  2821. {
  2822. const FCHAR_T *digitp = dp->width_start;
  2823. do
  2824. width = xsum (xtimes (width, 10), *digitp++ - '0');
  2825. while (digitp != dp->width_end);
  2826. }
  2827. has_width = 1;
  2828. }
  2829. has_precision = 0;
  2830. precision = 0;
  2831. if (dp->precision_start != dp->precision_end)
  2832. {
  2833. if (dp->precision_arg_index != ARG_NONE)
  2834. {
  2835. int arg;
  2836. if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
  2837. abort ();
  2838. arg = a.arg[dp->precision_arg_index].a.a_int;
  2839. /* "A negative precision is taken as if the precision
  2840. were omitted." */
  2841. if (arg >= 0)
  2842. {
  2843. precision = arg;
  2844. has_precision = 1;
  2845. }
  2846. }
  2847. else
  2848. {
  2849. const FCHAR_T *digitp = dp->precision_start + 1;
  2850. precision = 0;
  2851. while (digitp != dp->precision_end)
  2852. precision = xsum (xtimes (precision, 10), *digitp++ - '0');
  2853. has_precision = 1;
  2854. }
  2855. }
  2856. /* Allocate a temporary buffer of sufficient size. */
  2857. if (type == TYPE_LONGDOUBLE)
  2858. tmp_length =
  2859. (unsigned int) ((LDBL_DIG + 1)
  2860. * 0.831 /* decimal -> hexadecimal */
  2861. )
  2862. + 1; /* turn floor into ceil */
  2863. else
  2864. tmp_length =
  2865. (unsigned int) ((DBL_DIG + 1)
  2866. * 0.831 /* decimal -> hexadecimal */
  2867. )
  2868. + 1; /* turn floor into ceil */
  2869. if (tmp_length < precision)
  2870. tmp_length = precision;
  2871. /* Account for sign, decimal point etc. */
  2872. tmp_length = xsum (tmp_length, 12);
  2873. if (tmp_length < width)
  2874. tmp_length = width;
  2875. tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
  2876. if (tmp_length <= sizeof (tmpbuf) / sizeof (DCHAR_T))
  2877. tmp = tmpbuf;
  2878. else
  2879. {
  2880. size_t tmp_memsize = xtimes (tmp_length, sizeof (DCHAR_T));
  2881. if (size_overflow_p (tmp_memsize))
  2882. /* Overflow, would lead to out of memory. */
  2883. goto out_of_memory;
  2884. tmp = (DCHAR_T *) malloc (tmp_memsize);
  2885. if (tmp == NULL)
  2886. /* Out of memory. */
  2887. goto out_of_memory;
  2888. }
  2889. pad_ptr = NULL;
  2890. p = tmp;
  2891. if (type == TYPE_LONGDOUBLE)
  2892. {
  2893. # if NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE
  2894. long double arg = a.arg[dp->arg_index].a.a_longdouble;
  2895. if (isnanl (arg))
  2896. {
  2897. if (dp->conversion == 'A')
  2898. {
  2899. *p++ = 'N'; *p++ = 'A'; *p++ = 'N';
  2900. }
  2901. else
  2902. {
  2903. *p++ = 'n'; *p++ = 'a'; *p++ = 'n';
  2904. }
  2905. }
  2906. else
  2907. {
  2908. int sign = 0;
  2909. DECL_LONG_DOUBLE_ROUNDING
  2910. BEGIN_LONG_DOUBLE_ROUNDING ();
  2911. if (signbit (arg)) /* arg < 0.0L or negative zero */
  2912. {
  2913. sign = -1;
  2914. arg = -arg;
  2915. }
  2916. if (sign < 0)
  2917. *p++ = '-';
  2918. else if (flags & FLAG_SHOWSIGN)
  2919. *p++ = '+';
  2920. else if (flags & FLAG_SPACE)
  2921. *p++ = ' ';
  2922. if (arg > 0.0L && arg + arg == arg)
  2923. {
  2924. if (dp->conversion == 'A')
  2925. {
  2926. *p++ = 'I'; *p++ = 'N'; *p++ = 'F';
  2927. }
  2928. else
  2929. {
  2930. *p++ = 'i'; *p++ = 'n'; *p++ = 'f';
  2931. }
  2932. }
  2933. else
  2934. {
  2935. int exponent;
  2936. long double mantissa;
  2937. if (arg > 0.0L)
  2938. mantissa = printf_frexpl (arg, &exponent);
  2939. else
  2940. {
  2941. exponent = 0;
  2942. mantissa = 0.0L;
  2943. }
  2944. if (has_precision
  2945. && precision < (unsigned int) ((LDBL_DIG + 1) * 0.831) + 1)
  2946. {
  2947. /* Round the mantissa. */
  2948. long double tail = mantissa;
  2949. size_t q;
  2950. for (q = precision; ; q--)
  2951. {
  2952. int digit = (int) tail;
  2953. tail -= digit;
  2954. if (q == 0)
  2955. {
  2956. if (digit & 1 ? tail >= 0.5L : tail > 0.5L)
  2957. tail = 1 - tail;
  2958. else
  2959. tail = - tail;
  2960. break;
  2961. }
  2962. tail *= 16.0L;
  2963. }
  2964. if (tail != 0.0L)
  2965. for (q = precision; q > 0; q--)
  2966. tail *= 0.0625L;
  2967. mantissa += tail;
  2968. }
  2969. *p++ = '0';
  2970. *p++ = dp->conversion - 'A' + 'X';
  2971. pad_ptr = p;
  2972. {
  2973. int digit;
  2974. digit = (int) mantissa;
  2975. mantissa -= digit;
  2976. *p++ = '0' + digit;
  2977. if ((flags & FLAG_ALT)
  2978. || mantissa > 0.0L || precision > 0)
  2979. {
  2980. *p++ = decimal_point_char ();
  2981. /* This loop terminates because we assume
  2982. that FLT_RADIX is a power of 2. */
  2983. while (mantissa > 0.0L)
  2984. {
  2985. mantissa *= 16.0L;
  2986. digit = (int) mantissa;
  2987. mantissa -= digit;
  2988. *p++ = digit
  2989. + (digit < 10
  2990. ? '0'
  2991. : dp->conversion - 10);
  2992. if (precision > 0)
  2993. precision--;
  2994. }
  2995. while (precision > 0)
  2996. {
  2997. *p++ = '0';
  2998. precision--;
  2999. }
  3000. }
  3001. }
  3002. *p++ = dp->conversion - 'A' + 'P';
  3003. # if WIDE_CHAR_VERSION
  3004. {
  3005. static const wchar_t decimal_format[] =
  3006. { '%', '+', 'd', '\0' };
  3007. SNPRINTF (p, 6 + 1, decimal_format, exponent);
  3008. }
  3009. while (*p != '\0')
  3010. p++;
  3011. # else
  3012. if (sizeof (DCHAR_T) == 1)
  3013. {
  3014. sprintf ((char *) p, "%+d", exponent);
  3015. while (*p != '\0')
  3016. p++;
  3017. }
  3018. else
  3019. {
  3020. char expbuf[6 + 1];
  3021. const char *ep;
  3022. sprintf (expbuf, "%+d", exponent);
  3023. for (ep = expbuf; (*p = *ep) != '\0'; ep++)
  3024. p++;
  3025. }
  3026. # endif
  3027. }
  3028. END_LONG_DOUBLE_ROUNDING ();
  3029. }
  3030. # else
  3031. abort ();
  3032. # endif
  3033. }
  3034. else
  3035. {
  3036. # if NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_DOUBLE
  3037. double arg = a.arg[dp->arg_index].a.a_double;
  3038. if (isnand (arg))
  3039. {
  3040. if (dp->conversion == 'A')
  3041. {
  3042. *p++ = 'N'; *p++ = 'A'; *p++ = 'N';
  3043. }
  3044. else
  3045. {
  3046. *p++ = 'n'; *p++ = 'a'; *p++ = 'n';
  3047. }
  3048. }
  3049. else
  3050. {
  3051. int sign = 0;
  3052. if (signbit (arg)) /* arg < 0.0 or negative zero */
  3053. {
  3054. sign = -1;
  3055. arg = -arg;
  3056. }
  3057. if (sign < 0)
  3058. *p++ = '-';
  3059. else if (flags & FLAG_SHOWSIGN)
  3060. *p++ = '+';
  3061. else if (flags & FLAG_SPACE)
  3062. *p++ = ' ';
  3063. if (arg > 0.0 && arg + arg == arg)
  3064. {
  3065. if (dp->conversion == 'A')
  3066. {
  3067. *p++ = 'I'; *p++ = 'N'; *p++ = 'F';
  3068. }
  3069. else
  3070. {
  3071. *p++ = 'i'; *p++ = 'n'; *p++ = 'f';
  3072. }
  3073. }
  3074. else
  3075. {
  3076. int exponent;
  3077. double mantissa;
  3078. if (arg > 0.0)
  3079. mantissa = printf_frexp (arg, &exponent);
  3080. else
  3081. {
  3082. exponent = 0;
  3083. mantissa = 0.0;
  3084. }
  3085. if (has_precision
  3086. && precision < (unsigned int) ((DBL_DIG + 1) * 0.831) + 1)
  3087. {
  3088. /* Round the mantissa. */
  3089. double tail = mantissa;
  3090. size_t q;
  3091. for (q = precision; ; q--)
  3092. {
  3093. int digit = (int) tail;
  3094. tail -= digit;
  3095. if (q == 0)
  3096. {
  3097. if (digit & 1 ? tail >= 0.5 : tail > 0.5)
  3098. tail = 1 - tail;
  3099. else
  3100. tail = - tail;
  3101. break;
  3102. }
  3103. tail *= 16.0;
  3104. }
  3105. if (tail != 0.0)
  3106. for (q = precision; q > 0; q--)
  3107. tail *= 0.0625;
  3108. mantissa += tail;
  3109. }
  3110. *p++ = '0';
  3111. *p++ = dp->conversion - 'A' + 'X';
  3112. pad_ptr = p;
  3113. {
  3114. int digit;
  3115. digit = (int) mantissa;
  3116. mantissa -= digit;
  3117. *p++ = '0' + digit;
  3118. if ((flags & FLAG_ALT)
  3119. || mantissa > 0.0 || precision > 0)
  3120. {
  3121. *p++ = decimal_point_char ();
  3122. /* This loop terminates because we assume
  3123. that FLT_RADIX is a power of 2. */
  3124. while (mantissa > 0.0)
  3125. {
  3126. mantissa *= 16.0;
  3127. digit = (int) mantissa;
  3128. mantissa -= digit;
  3129. *p++ = digit
  3130. + (digit < 10
  3131. ? '0'
  3132. : dp->conversion - 10);
  3133. if (precision > 0)
  3134. precision--;
  3135. }
  3136. while (precision > 0)
  3137. {
  3138. *p++ = '0';
  3139. precision--;
  3140. }
  3141. }
  3142. }
  3143. *p++ = dp->conversion - 'A' + 'P';
  3144. # if WIDE_CHAR_VERSION
  3145. {
  3146. static const wchar_t decimal_format[] =
  3147. { '%', '+', 'd', '\0' };
  3148. SNPRINTF (p, 6 + 1, decimal_format, exponent);
  3149. }
  3150. while (*p != '\0')
  3151. p++;
  3152. # else
  3153. if (sizeof (DCHAR_T) == 1)
  3154. {
  3155. sprintf ((char *) p, "%+d", exponent);
  3156. while (*p != '\0')
  3157. p++;
  3158. }
  3159. else
  3160. {
  3161. char expbuf[6 + 1];
  3162. const char *ep;
  3163. sprintf (expbuf, "%+d", exponent);
  3164. for (ep = expbuf; (*p = *ep) != '\0'; ep++)
  3165. p++;
  3166. }
  3167. # endif
  3168. }
  3169. }
  3170. # else
  3171. abort ();
  3172. # endif
  3173. }
  3174. /* The generated string now extends from tmp to p, with the
  3175. zero padding insertion point being at pad_ptr. */
  3176. if (has_width && p - tmp < width)
  3177. {
  3178. size_t pad = width - (p - tmp);
  3179. DCHAR_T *end = p + pad;
  3180. if (flags & FLAG_LEFT)
  3181. {
  3182. /* Pad with spaces on the right. */
  3183. for (; pad > 0; pad--)
  3184. *p++ = ' ';
  3185. }
  3186. else if ((flags & FLAG_ZERO) && pad_ptr != NULL)
  3187. {
  3188. /* Pad with zeroes. */
  3189. DCHAR_T *q = end;
  3190. while (p > pad_ptr)
  3191. *--q = *--p;
  3192. for (; pad > 0; pad--)
  3193. *p++ = '0';
  3194. }
  3195. else
  3196. {
  3197. /* Pad with spaces on the left. */
  3198. DCHAR_T *q = end;
  3199. while (p > tmp)
  3200. *--q = *--p;
  3201. for (; pad > 0; pad--)
  3202. *p++ = ' ';
  3203. }
  3204. p = end;
  3205. }
  3206. {
  3207. size_t count = p - tmp;
  3208. if (count >= tmp_length)
  3209. /* tmp_length was incorrectly calculated - fix the
  3210. code above! */
  3211. abort ();
  3212. /* Make room for the result. */
  3213. if (count >= allocated - length)
  3214. {
  3215. size_t n = xsum (length, count);
  3216. ENSURE_ALLOCATION (n);
  3217. }
  3218. /* Append the result. */
  3219. memcpy (result + length, tmp, count * sizeof (DCHAR_T));
  3220. if (tmp != tmpbuf)
  3221. free (tmp);
  3222. length += count;
  3223. }
  3224. }
  3225. #endif
  3226. #if (NEED_PRINTF_INFINITE_DOUBLE || NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE || NEED_PRINTF_LONG_DOUBLE) && !defined IN_LIBINTL
  3227. else if ((dp->conversion == 'f' || dp->conversion == 'F'
  3228. || dp->conversion == 'e' || dp->conversion == 'E'
  3229. || dp->conversion == 'g' || dp->conversion == 'G'
  3230. || dp->conversion == 'a' || dp->conversion == 'A')
  3231. && (0
  3232. # if NEED_PRINTF_DOUBLE
  3233. || a.arg[dp->arg_index].type == TYPE_DOUBLE
  3234. # elif NEED_PRINTF_INFINITE_DOUBLE
  3235. || (a.arg[dp->arg_index].type == TYPE_DOUBLE
  3236. /* The systems (mingw) which produce wrong output
  3237. for Inf, -Inf, and NaN also do so for -0.0.
  3238. Therefore we treat this case here as well. */
  3239. && is_infinite_or_zero (a.arg[dp->arg_index].a.a_double))
  3240. # endif
  3241. # if NEED_PRINTF_LONG_DOUBLE
  3242. || a.arg[dp->arg_index].type == TYPE_LONGDOUBLE
  3243. # elif NEED_PRINTF_INFINITE_LONG_DOUBLE
  3244. || (a.arg[dp->arg_index].type == TYPE_LONGDOUBLE
  3245. /* Some systems produce wrong output for Inf,
  3246. -Inf, and NaN. Some systems in this category
  3247. (IRIX 5.3) also do so for -0.0. Therefore we
  3248. treat this case here as well. */
  3249. && is_infinite_or_zerol (a.arg[dp->arg_index].a.a_longdouble))
  3250. # endif
  3251. ))
  3252. {
  3253. # if (NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE) && (NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE)
  3254. arg_type type = a.arg[dp->arg_index].type;
  3255. # endif
  3256. int flags = dp->flags;
  3257. int has_width;
  3258. size_t width;
  3259. int has_precision;
  3260. size_t precision;
  3261. size_t tmp_length;
  3262. DCHAR_T tmpbuf[700];
  3263. DCHAR_T *tmp;
  3264. DCHAR_T *pad_ptr;
  3265. DCHAR_T *p;
  3266. has_width = 0;
  3267. width = 0;
  3268. if (dp->width_start != dp->width_end)
  3269. {
  3270. if (dp->width_arg_index != ARG_NONE)
  3271. {
  3272. int arg;
  3273. if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
  3274. abort ();
  3275. arg = a.arg[dp->width_arg_index].a.a_int;
  3276. if (arg < 0)
  3277. {
  3278. /* "A negative field width is taken as a '-' flag
  3279. followed by a positive field width." */
  3280. flags |= FLAG_LEFT;
  3281. width = (unsigned int) (-arg);
  3282. }
  3283. else
  3284. width = arg;
  3285. }
  3286. else
  3287. {
  3288. const FCHAR_T *digitp = dp->width_start;
  3289. do
  3290. width = xsum (xtimes (width, 10), *digitp++ - '0');
  3291. while (digitp != dp->width_end);
  3292. }
  3293. has_width = 1;
  3294. }
  3295. has_precision = 0;
  3296. precision = 0;
  3297. if (dp->precision_start != dp->precision_end)
  3298. {
  3299. if (dp->precision_arg_index != ARG_NONE)
  3300. {
  3301. int arg;
  3302. if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
  3303. abort ();
  3304. arg = a.arg[dp->precision_arg_index].a.a_int;
  3305. /* "A negative precision is taken as if the precision
  3306. were omitted." */
  3307. if (arg >= 0)
  3308. {
  3309. precision = arg;
  3310. has_precision = 1;
  3311. }
  3312. }
  3313. else
  3314. {
  3315. const FCHAR_T *digitp = dp->precision_start + 1;
  3316. precision = 0;
  3317. while (digitp != dp->precision_end)
  3318. precision = xsum (xtimes (precision, 10), *digitp++ - '0');
  3319. has_precision = 1;
  3320. }
  3321. }
  3322. /* POSIX specifies the default precision to be 6 for %f, %F,
  3323. %e, %E, but not for %g, %G. Implementations appear to use
  3324. the same default precision also for %g, %G. But for %a, %A,
  3325. the default precision is 0. */
  3326. if (!has_precision)
  3327. if (!(dp->conversion == 'a' || dp->conversion == 'A'))
  3328. precision = 6;
  3329. /* Allocate a temporary buffer of sufficient size. */
  3330. # if NEED_PRINTF_DOUBLE && NEED_PRINTF_LONG_DOUBLE
  3331. tmp_length = (type == TYPE_LONGDOUBLE ? LDBL_DIG + 1 : DBL_DIG + 1);
  3332. # elif NEED_PRINTF_INFINITE_DOUBLE && NEED_PRINTF_LONG_DOUBLE
  3333. tmp_length = (type == TYPE_LONGDOUBLE ? LDBL_DIG + 1 : 0);
  3334. # elif NEED_PRINTF_LONG_DOUBLE
  3335. tmp_length = LDBL_DIG + 1;
  3336. # elif NEED_PRINTF_DOUBLE
  3337. tmp_length = DBL_DIG + 1;
  3338. # else
  3339. tmp_length = 0;
  3340. # endif
  3341. if (tmp_length < precision)
  3342. tmp_length = precision;
  3343. # if NEED_PRINTF_LONG_DOUBLE
  3344. # if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
  3345. if (type == TYPE_LONGDOUBLE)
  3346. # endif
  3347. if (dp->conversion == 'f' || dp->conversion == 'F')
  3348. {
  3349. long double arg = a.arg[dp->arg_index].a.a_longdouble;
  3350. if (!(isnanl (arg) || arg + arg == arg))
  3351. {
  3352. /* arg is finite and nonzero. */
  3353. int exponent = floorlog10l (arg < 0 ? -arg : arg);
  3354. if (exponent >= 0 && tmp_length < exponent + precision)
  3355. tmp_length = exponent + precision;
  3356. }
  3357. }
  3358. # endif
  3359. # if NEED_PRINTF_DOUBLE
  3360. # if NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE
  3361. if (type == TYPE_DOUBLE)
  3362. # endif
  3363. if (dp->conversion == 'f' || dp->conversion == 'F')
  3364. {
  3365. double arg = a.arg[dp->arg_index].a.a_double;
  3366. if (!(isnand (arg) || arg + arg == arg))
  3367. {
  3368. /* arg is finite and nonzero. */
  3369. int exponent = floorlog10 (arg < 0 ? -arg : arg);
  3370. if (exponent >= 0 && tmp_length < exponent + precision)
  3371. tmp_length = exponent + precision;
  3372. }
  3373. }
  3374. # endif
  3375. /* Account for sign, decimal point etc. */
  3376. tmp_length = xsum (tmp_length, 12);
  3377. if (tmp_length < width)
  3378. tmp_length = width;
  3379. tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
  3380. if (tmp_length <= sizeof (tmpbuf) / sizeof (DCHAR_T))
  3381. tmp = tmpbuf;
  3382. else
  3383. {
  3384. size_t tmp_memsize = xtimes (tmp_length, sizeof (DCHAR_T));
  3385. if (size_overflow_p (tmp_memsize))
  3386. /* Overflow, would lead to out of memory. */
  3387. goto out_of_memory;
  3388. tmp = (DCHAR_T *) malloc (tmp_memsize);
  3389. if (tmp == NULL)
  3390. /* Out of memory. */
  3391. goto out_of_memory;
  3392. }
  3393. pad_ptr = NULL;
  3394. p = tmp;
  3395. # if NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE
  3396. # if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
  3397. if (type == TYPE_LONGDOUBLE)
  3398. # endif
  3399. {
  3400. long double arg = a.arg[dp->arg_index].a.a_longdouble;
  3401. if (isnanl (arg))
  3402. {
  3403. if (dp->conversion >= 'A' && dp->conversion <= 'Z')
  3404. {
  3405. *p++ = 'N'; *p++ = 'A'; *p++ = 'N';
  3406. }
  3407. else
  3408. {
  3409. *p++ = 'n'; *p++ = 'a'; *p++ = 'n';
  3410. }
  3411. }
  3412. else
  3413. {
  3414. int sign = 0;
  3415. DECL_LONG_DOUBLE_ROUNDING
  3416. BEGIN_LONG_DOUBLE_ROUNDING ();
  3417. if (signbit (arg)) /* arg < 0.0L or negative zero */
  3418. {
  3419. sign = -1;
  3420. arg = -arg;
  3421. }
  3422. if (sign < 0)
  3423. *p++ = '-';
  3424. else if (flags & FLAG_SHOWSIGN)
  3425. *p++ = '+';
  3426. else if (flags & FLAG_SPACE)
  3427. *p++ = ' ';
  3428. if (arg > 0.0L && arg + arg == arg)
  3429. {
  3430. if (dp->conversion >= 'A' && dp->conversion <= 'Z')
  3431. {
  3432. *p++ = 'I'; *p++ = 'N'; *p++ = 'F';
  3433. }
  3434. else
  3435. {
  3436. *p++ = 'i'; *p++ = 'n'; *p++ = 'f';
  3437. }
  3438. }
  3439. else
  3440. {
  3441. # if NEED_PRINTF_LONG_DOUBLE
  3442. pad_ptr = p;
  3443. if (dp->conversion == 'f' || dp->conversion == 'F')
  3444. {
  3445. char *digits;
  3446. size_t ndigits;
  3447. digits =
  3448. scale10_round_decimal_long_double (arg, precision);
  3449. if (digits == NULL)
  3450. {
  3451. END_LONG_DOUBLE_ROUNDING ();
  3452. goto out_of_memory;
  3453. }
  3454. ndigits = strlen (digits);
  3455. if (ndigits > precision)
  3456. do
  3457. {
  3458. --ndigits;
  3459. *p++ = digits[ndigits];
  3460. }
  3461. while (ndigits > precision);
  3462. else
  3463. *p++ = '0';
  3464. /* Here ndigits <= precision. */
  3465. if ((flags & FLAG_ALT) || precision > 0)
  3466. {
  3467. *p++ = decimal_point_char ();
  3468. for (; precision > ndigits; precision--)
  3469. *p++ = '0';
  3470. while (ndigits > 0)
  3471. {
  3472. --ndigits;
  3473. *p++ = digits[ndigits];
  3474. }
  3475. }
  3476. free (digits);
  3477. }
  3478. else if (dp->conversion == 'e' || dp->conversion == 'E')
  3479. {
  3480. int exponent;
  3481. if (arg == 0.0L)
  3482. {
  3483. exponent = 0;
  3484. *p++ = '0';
  3485. if ((flags & FLAG_ALT) || precision > 0)
  3486. {
  3487. *p++ = decimal_point_char ();
  3488. for (; precision > 0; precision--)
  3489. *p++ = '0';
  3490. }
  3491. }
  3492. else
  3493. {
  3494. /* arg > 0.0L. */
  3495. int adjusted;
  3496. char *digits;
  3497. size_t ndigits;
  3498. exponent = floorlog10l (arg);
  3499. adjusted = 0;
  3500. for (;;)
  3501. {
  3502. digits =
  3503. scale10_round_decimal_long_double (arg,
  3504. (int)precision - exponent);
  3505. if (digits == NULL)
  3506. {
  3507. END_LONG_DOUBLE_ROUNDING ();
  3508. goto out_of_memory;
  3509. }
  3510. ndigits = strlen (digits);
  3511. if (ndigits == precision + 1)
  3512. break;
  3513. if (ndigits < precision
  3514. || ndigits > precision + 2)
  3515. /* The exponent was not guessed
  3516. precisely enough. */
  3517. abort ();
  3518. if (adjusted)
  3519. /* None of two values of exponent is
  3520. the right one. Prevent an endless
  3521. loop. */
  3522. abort ();
  3523. free (digits);
  3524. if (ndigits == precision)
  3525. exponent -= 1;
  3526. else
  3527. exponent += 1;
  3528. adjusted = 1;
  3529. }
  3530. /* Here ndigits = precision+1. */
  3531. if (is_borderline (digits, precision))
  3532. {
  3533. /* Maybe the exponent guess was too high
  3534. and a smaller exponent can be reached
  3535. by turning a 10...0 into 9...9x. */
  3536. char *digits2 =
  3537. scale10_round_decimal_long_double (arg,
  3538. (int)precision - exponent + 1);
  3539. if (digits2 == NULL)
  3540. {
  3541. free (digits);
  3542. END_LONG_DOUBLE_ROUNDING ();
  3543. goto out_of_memory;
  3544. }
  3545. if (strlen (digits2) == precision + 1)
  3546. {
  3547. free (digits);
  3548. digits = digits2;
  3549. exponent -= 1;
  3550. }
  3551. else
  3552. free (digits2);
  3553. }
  3554. /* Here ndigits = precision+1. */
  3555. *p++ = digits[--ndigits];
  3556. if ((flags & FLAG_ALT) || precision > 0)
  3557. {
  3558. *p++ = decimal_point_char ();
  3559. while (ndigits > 0)
  3560. {
  3561. --ndigits;
  3562. *p++ = digits[ndigits];
  3563. }
  3564. }
  3565. free (digits);
  3566. }
  3567. *p++ = dp->conversion; /* 'e' or 'E' */
  3568. # if WIDE_CHAR_VERSION
  3569. {
  3570. static const wchar_t decimal_format[] =
  3571. { '%', '+', '.', '2', 'd', '\0' };
  3572. SNPRINTF (p, 6 + 1, decimal_format, exponent);
  3573. }
  3574. while (*p != '\0')
  3575. p++;
  3576. # else
  3577. if (sizeof (DCHAR_T) == 1)
  3578. {
  3579. sprintf ((char *) p, "%+.2d", exponent);
  3580. while (*p != '\0')
  3581. p++;
  3582. }
  3583. else
  3584. {
  3585. char expbuf[6 + 1];
  3586. const char *ep;
  3587. sprintf (expbuf, "%+.2d", exponent);
  3588. for (ep = expbuf; (*p = *ep) != '\0'; ep++)
  3589. p++;
  3590. }
  3591. # endif
  3592. }
  3593. else if (dp->conversion == 'g' || dp->conversion == 'G')
  3594. {
  3595. if (precision == 0)
  3596. precision = 1;
  3597. /* precision >= 1. */
  3598. if (arg == 0.0L)
  3599. /* The exponent is 0, >= -4, < precision.
  3600. Use fixed-point notation. */
  3601. {
  3602. size_t ndigits = precision;
  3603. /* Number of trailing zeroes that have to be
  3604. dropped. */
  3605. size_t nzeroes =
  3606. (flags & FLAG_ALT ? 0 : precision - 1);
  3607. --ndigits;
  3608. *p++ = '0';
  3609. if ((flags & FLAG_ALT) || ndigits > nzeroes)
  3610. {
  3611. *p++ = decimal_point_char ();
  3612. while (ndigits > nzeroes)
  3613. {
  3614. --ndigits;
  3615. *p++ = '0';
  3616. }
  3617. }
  3618. }
  3619. else
  3620. {
  3621. /* arg > 0.0L. */
  3622. int exponent;
  3623. int adjusted;
  3624. char *digits;
  3625. size_t ndigits;
  3626. size_t nzeroes;
  3627. exponent = floorlog10l (arg);
  3628. adjusted = 0;
  3629. for (;;)
  3630. {
  3631. digits =
  3632. scale10_round_decimal_long_double (arg,
  3633. (int)(precision - 1) - exponent);
  3634. if (digits == NULL)
  3635. {
  3636. END_LONG_DOUBLE_ROUNDING ();
  3637. goto out_of_memory;
  3638. }
  3639. ndigits = strlen (digits);
  3640. if (ndigits == precision)
  3641. break;
  3642. if (ndigits < precision - 1
  3643. || ndigits > precision + 1)
  3644. /* The exponent was not guessed
  3645. precisely enough. */
  3646. abort ();
  3647. if (adjusted)
  3648. /* None of two values of exponent is
  3649. the right one. Prevent an endless
  3650. loop. */
  3651. abort ();
  3652. free (digits);
  3653. if (ndigits < precision)
  3654. exponent -= 1;
  3655. else
  3656. exponent += 1;
  3657. adjusted = 1;
  3658. }
  3659. /* Here ndigits = precision. */
  3660. if (is_borderline (digits, precision - 1))
  3661. {
  3662. /* Maybe the exponent guess was too high
  3663. and a smaller exponent can be reached
  3664. by turning a 10...0 into 9...9x. */
  3665. char *digits2 =
  3666. scale10_round_decimal_long_double (arg,
  3667. (int)(precision - 1) - exponent + 1);
  3668. if (digits2 == NULL)
  3669. {
  3670. free (digits);
  3671. END_LONG_DOUBLE_ROUNDING ();
  3672. goto out_of_memory;
  3673. }
  3674. if (strlen (digits2) == precision)
  3675. {
  3676. free (digits);
  3677. digits = digits2;
  3678. exponent -= 1;
  3679. }
  3680. else
  3681. free (digits2);
  3682. }
  3683. /* Here ndigits = precision. */
  3684. /* Determine the number of trailing zeroes
  3685. that have to be dropped. */
  3686. nzeroes = 0;
  3687. if ((flags & FLAG_ALT) == 0)
  3688. while (nzeroes < ndigits
  3689. && digits[nzeroes] == '0')
  3690. nzeroes++;
  3691. /* The exponent is now determined. */
  3692. if (exponent >= -4
  3693. && exponent < (long)precision)
  3694. {
  3695. /* Fixed-point notation:
  3696. max(exponent,0)+1 digits, then the
  3697. decimal point, then the remaining
  3698. digits without trailing zeroes. */
  3699. if (exponent >= 0)
  3700. {
  3701. size_t count = exponent + 1;
  3702. /* Note: count <= precision = ndigits. */
  3703. for (; count > 0; count--)
  3704. *p++ = digits[--ndigits];
  3705. if ((flags & FLAG_ALT) || ndigits > nzeroes)
  3706. {
  3707. *p++ = decimal_point_char ();
  3708. while (ndigits > nzeroes)
  3709. {
  3710. --ndigits;
  3711. *p++ = digits[ndigits];
  3712. }
  3713. }
  3714. }
  3715. else
  3716. {
  3717. size_t count = -exponent - 1;
  3718. *p++ = '0';
  3719. *p++ = decimal_point_char ();
  3720. for (; count > 0; count--)
  3721. *p++ = '0';
  3722. while (ndigits > nzeroes)
  3723. {
  3724. --ndigits;
  3725. *p++ = digits[ndigits];
  3726. }
  3727. }
  3728. }
  3729. else
  3730. {
  3731. /* Exponential notation. */
  3732. *p++ = digits[--ndigits];
  3733. if ((flags & FLAG_ALT) || ndigits > nzeroes)
  3734. {
  3735. *p++ = decimal_point_char ();
  3736. while (ndigits > nzeroes)
  3737. {
  3738. --ndigits;
  3739. *p++ = digits[ndigits];
  3740. }
  3741. }
  3742. *p++ = dp->conversion - 'G' + 'E'; /* 'e' or 'E' */
  3743. # if WIDE_CHAR_VERSION
  3744. {
  3745. static const wchar_t decimal_format[] =
  3746. { '%', '+', '.', '2', 'd', '\0' };
  3747. SNPRINTF (p, 6 + 1, decimal_format, exponent);
  3748. }
  3749. while (*p != '\0')
  3750. p++;
  3751. # else
  3752. if (sizeof (DCHAR_T) == 1)
  3753. {
  3754. sprintf ((char *) p, "%+.2d", exponent);
  3755. while (*p != '\0')
  3756. p++;
  3757. }
  3758. else
  3759. {
  3760. char expbuf[6 + 1];
  3761. const char *ep;
  3762. sprintf (expbuf, "%+.2d", exponent);
  3763. for (ep = expbuf; (*p = *ep) != '\0'; ep++)
  3764. p++;
  3765. }
  3766. # endif
  3767. }
  3768. free (digits);
  3769. }
  3770. }
  3771. else
  3772. abort ();
  3773. # else
  3774. /* arg is finite. */
  3775. if (!(arg == 0.0L))
  3776. abort ();
  3777. pad_ptr = p;
  3778. if (dp->conversion == 'f' || dp->conversion == 'F')
  3779. {
  3780. *p++ = '0';
  3781. if ((flags & FLAG_ALT) || precision > 0)
  3782. {
  3783. *p++ = decimal_point_char ();
  3784. for (; precision > 0; precision--)
  3785. *p++ = '0';
  3786. }
  3787. }
  3788. else if (dp->conversion == 'e' || dp->conversion == 'E')
  3789. {
  3790. *p++ = '0';
  3791. if ((flags & FLAG_ALT) || precision > 0)
  3792. {
  3793. *p++ = decimal_point_char ();
  3794. for (; precision > 0; precision--)
  3795. *p++ = '0';
  3796. }
  3797. *p++ = dp->conversion; /* 'e' or 'E' */
  3798. *p++ = '+';
  3799. *p++ = '0';
  3800. *p++ = '0';
  3801. }
  3802. else if (dp->conversion == 'g' || dp->conversion == 'G')
  3803. {
  3804. *p++ = '0';
  3805. if (flags & FLAG_ALT)
  3806. {
  3807. size_t ndigits =
  3808. (precision > 0 ? precision - 1 : 0);
  3809. *p++ = decimal_point_char ();
  3810. for (; ndigits > 0; --ndigits)
  3811. *p++ = '0';
  3812. }
  3813. }
  3814. else if (dp->conversion == 'a' || dp->conversion == 'A')
  3815. {
  3816. *p++ = '0';
  3817. *p++ = dp->conversion - 'A' + 'X';
  3818. pad_ptr = p;
  3819. *p++ = '0';
  3820. if ((flags & FLAG_ALT) || precision > 0)
  3821. {
  3822. *p++ = decimal_point_char ();
  3823. for (; precision > 0; precision--)
  3824. *p++ = '0';
  3825. }
  3826. *p++ = dp->conversion - 'A' + 'P';
  3827. *p++ = '+';
  3828. *p++ = '0';
  3829. }
  3830. else
  3831. abort ();
  3832. # endif
  3833. }
  3834. END_LONG_DOUBLE_ROUNDING ();
  3835. }
  3836. }
  3837. # if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
  3838. else
  3839. # endif
  3840. # endif
  3841. # if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
  3842. {
  3843. double arg = a.arg[dp->arg_index].a.a_double;
  3844. if (isnand (arg))
  3845. {
  3846. if (dp->conversion >= 'A' && dp->conversion <= 'Z')
  3847. {
  3848. *p++ = 'N'; *p++ = 'A'; *p++ = 'N';
  3849. }
  3850. else
  3851. {
  3852. *p++ = 'n'; *p++ = 'a'; *p++ = 'n';
  3853. }
  3854. }
  3855. else
  3856. {
  3857. int sign = 0;
  3858. if (signbit (arg)) /* arg < 0.0 or negative zero */
  3859. {
  3860. sign = -1;
  3861. arg = -arg;
  3862. }
  3863. if (sign < 0)
  3864. *p++ = '-';
  3865. else if (flags & FLAG_SHOWSIGN)
  3866. *p++ = '+';
  3867. else if (flags & FLAG_SPACE)
  3868. *p++ = ' ';
  3869. if (arg > 0.0 && arg + arg == arg)
  3870. {
  3871. if (dp->conversion >= 'A' && dp->conversion <= 'Z')
  3872. {
  3873. *p++ = 'I'; *p++ = 'N'; *p++ = 'F';
  3874. }
  3875. else
  3876. {
  3877. *p++ = 'i'; *p++ = 'n'; *p++ = 'f';
  3878. }
  3879. }
  3880. else
  3881. {
  3882. # if NEED_PRINTF_DOUBLE
  3883. pad_ptr = p;
  3884. if (dp->conversion == 'f' || dp->conversion == 'F')
  3885. {
  3886. char *digits;
  3887. size_t ndigits;
  3888. digits =
  3889. scale10_round_decimal_double (arg, precision);
  3890. if (digits == NULL)
  3891. goto out_of_memory;
  3892. ndigits = strlen (digits);
  3893. if (ndigits > precision)
  3894. do
  3895. {
  3896. --ndigits;
  3897. *p++ = digits[ndigits];
  3898. }
  3899. while (ndigits > precision);
  3900. else
  3901. *p++ = '0';
  3902. /* Here ndigits <= precision. */
  3903. if ((flags & FLAG_ALT) || precision > 0)
  3904. {
  3905. *p++ = decimal_point_char ();
  3906. for (; precision > ndigits; precision--)
  3907. *p++ = '0';
  3908. while (ndigits > 0)
  3909. {
  3910. --ndigits;
  3911. *p++ = digits[ndigits];
  3912. }
  3913. }
  3914. free (digits);
  3915. }
  3916. else if (dp->conversion == 'e' || dp->conversion == 'E')
  3917. {
  3918. int exponent;
  3919. if (arg == 0.0)
  3920. {
  3921. exponent = 0;
  3922. *p++ = '0';
  3923. if ((flags & FLAG_ALT) || precision > 0)
  3924. {
  3925. *p++ = decimal_point_char ();
  3926. for (; precision > 0; precision--)
  3927. *p++ = '0';
  3928. }
  3929. }
  3930. else
  3931. {
  3932. /* arg > 0.0. */
  3933. int adjusted;
  3934. char *digits;
  3935. size_t ndigits;
  3936. exponent = floorlog10 (arg);
  3937. adjusted = 0;
  3938. for (;;)
  3939. {
  3940. digits =
  3941. scale10_round_decimal_double (arg,
  3942. (int)precision - exponent);
  3943. if (digits == NULL)
  3944. goto out_of_memory;
  3945. ndigits = strlen (digits);
  3946. if (ndigits == precision + 1)
  3947. break;
  3948. if (ndigits < precision
  3949. || ndigits > precision + 2)
  3950. /* The exponent was not guessed
  3951. precisely enough. */
  3952. abort ();
  3953. if (adjusted)
  3954. /* None of two values of exponent is
  3955. the right one. Prevent an endless
  3956. loop. */
  3957. abort ();
  3958. free (digits);
  3959. if (ndigits == precision)
  3960. exponent -= 1;
  3961. else
  3962. exponent += 1;
  3963. adjusted = 1;
  3964. }
  3965. /* Here ndigits = precision+1. */
  3966. if (is_borderline (digits, precision))
  3967. {
  3968. /* Maybe the exponent guess was too high
  3969. and a smaller exponent can be reached
  3970. by turning a 10...0 into 9...9x. */
  3971. char *digits2 =
  3972. scale10_round_decimal_double (arg,
  3973. (int)precision - exponent + 1);
  3974. if (digits2 == NULL)
  3975. {
  3976. free (digits);
  3977. goto out_of_memory;
  3978. }
  3979. if (strlen (digits2) == precision + 1)
  3980. {
  3981. free (digits);
  3982. digits = digits2;
  3983. exponent -= 1;
  3984. }
  3985. else
  3986. free (digits2);
  3987. }
  3988. /* Here ndigits = precision+1. */
  3989. *p++ = digits[--ndigits];
  3990. if ((flags & FLAG_ALT) || precision > 0)
  3991. {
  3992. *p++ = decimal_point_char ();
  3993. while (ndigits > 0)
  3994. {
  3995. --ndigits;
  3996. *p++ = digits[ndigits];
  3997. }
  3998. }
  3999. free (digits);
  4000. }
  4001. *p++ = dp->conversion; /* 'e' or 'E' */
  4002. # if WIDE_CHAR_VERSION
  4003. {
  4004. static const wchar_t decimal_format[] =
  4005. /* Produce the same number of exponent digits
  4006. as the native printf implementation. */
  4007. # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
  4008. { '%', '+', '.', '3', 'd', '\0' };
  4009. # else
  4010. { '%', '+', '.', '2', 'd', '\0' };
  4011. # endif
  4012. SNPRINTF (p, 6 + 1, decimal_format, exponent);
  4013. }
  4014. while (*p != '\0')
  4015. p++;
  4016. # else
  4017. {
  4018. static const char decimal_format[] =
  4019. /* Produce the same number of exponent digits
  4020. as the native printf implementation. */
  4021. # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
  4022. "%+.3d";
  4023. # else
  4024. "%+.2d";
  4025. # endif
  4026. if (sizeof (DCHAR_T) == 1)
  4027. {
  4028. sprintf ((char *) p, decimal_format, exponent);
  4029. while (*p != '\0')
  4030. p++;
  4031. }
  4032. else
  4033. {
  4034. char expbuf[6 + 1];
  4035. const char *ep;
  4036. sprintf (expbuf, decimal_format, exponent);
  4037. for (ep = expbuf; (*p = *ep) != '\0'; ep++)
  4038. p++;
  4039. }
  4040. }
  4041. # endif
  4042. }
  4043. else if (dp->conversion == 'g' || dp->conversion == 'G')
  4044. {
  4045. if (precision == 0)
  4046. precision = 1;
  4047. /* precision >= 1. */
  4048. if (arg == 0.0)
  4049. /* The exponent is 0, >= -4, < precision.
  4050. Use fixed-point notation. */
  4051. {
  4052. size_t ndigits = precision;
  4053. /* Number of trailing zeroes that have to be
  4054. dropped. */
  4055. size_t nzeroes =
  4056. (flags & FLAG_ALT ? 0 : precision - 1);
  4057. --ndigits;
  4058. *p++ = '0';
  4059. if ((flags & FLAG_ALT) || ndigits > nzeroes)
  4060. {
  4061. *p++ = decimal_point_char ();
  4062. while (ndigits > nzeroes)
  4063. {
  4064. --ndigits;
  4065. *p++ = '0';
  4066. }
  4067. }
  4068. }
  4069. else
  4070. {
  4071. /* arg > 0.0. */
  4072. int exponent;
  4073. int adjusted;
  4074. char *digits;
  4075. size_t ndigits;
  4076. size_t nzeroes;
  4077. exponent = floorlog10 (arg);
  4078. adjusted = 0;
  4079. for (;;)
  4080. {
  4081. digits =
  4082. scale10_round_decimal_double (arg,
  4083. (int)(precision - 1) - exponent);
  4084. if (digits == NULL)
  4085. goto out_of_memory;
  4086. ndigits = strlen (digits);
  4087. if (ndigits == precision)
  4088. break;
  4089. if (ndigits < precision - 1
  4090. || ndigits > precision + 1)
  4091. /* The exponent was not guessed
  4092. precisely enough. */
  4093. abort ();
  4094. if (adjusted)
  4095. /* None of two values of exponent is
  4096. the right one. Prevent an endless
  4097. loop. */
  4098. abort ();
  4099. free (digits);
  4100. if (ndigits < precision)
  4101. exponent -= 1;
  4102. else
  4103. exponent += 1;
  4104. adjusted = 1;
  4105. }
  4106. /* Here ndigits = precision. */
  4107. if (is_borderline (digits, precision - 1))
  4108. {
  4109. /* Maybe the exponent guess was too high
  4110. and a smaller exponent can be reached
  4111. by turning a 10...0 into 9...9x. */
  4112. char *digits2 =
  4113. scale10_round_decimal_double (arg,
  4114. (int)(precision - 1) - exponent + 1);
  4115. if (digits2 == NULL)
  4116. {
  4117. free (digits);
  4118. goto out_of_memory;
  4119. }
  4120. if (strlen (digits2) == precision)
  4121. {
  4122. free (digits);
  4123. digits = digits2;
  4124. exponent -= 1;
  4125. }
  4126. else
  4127. free (digits2);
  4128. }
  4129. /* Here ndigits = precision. */
  4130. /* Determine the number of trailing zeroes
  4131. that have to be dropped. */
  4132. nzeroes = 0;
  4133. if ((flags & FLAG_ALT) == 0)
  4134. while (nzeroes < ndigits
  4135. && digits[nzeroes] == '0')
  4136. nzeroes++;
  4137. /* The exponent is now determined. */
  4138. if (exponent >= -4
  4139. && exponent < (long)precision)
  4140. {
  4141. /* Fixed-point notation:
  4142. max(exponent,0)+1 digits, then the
  4143. decimal point, then the remaining
  4144. digits without trailing zeroes. */
  4145. if (exponent >= 0)
  4146. {
  4147. size_t count = exponent + 1;
  4148. /* Note: count <= precision = ndigits. */
  4149. for (; count > 0; count--)
  4150. *p++ = digits[--ndigits];
  4151. if ((flags & FLAG_ALT) || ndigits > nzeroes)
  4152. {
  4153. *p++ = decimal_point_char ();
  4154. while (ndigits > nzeroes)
  4155. {
  4156. --ndigits;
  4157. *p++ = digits[ndigits];
  4158. }
  4159. }
  4160. }
  4161. else
  4162. {
  4163. size_t count = -exponent - 1;
  4164. *p++ = '0';
  4165. *p++ = decimal_point_char ();
  4166. for (; count > 0; count--)
  4167. *p++ = '0';
  4168. while (ndigits > nzeroes)
  4169. {
  4170. --ndigits;
  4171. *p++ = digits[ndigits];
  4172. }
  4173. }
  4174. }
  4175. else
  4176. {
  4177. /* Exponential notation. */
  4178. *p++ = digits[--ndigits];
  4179. if ((flags & FLAG_ALT) || ndigits > nzeroes)
  4180. {
  4181. *p++ = decimal_point_char ();
  4182. while (ndigits > nzeroes)
  4183. {
  4184. --ndigits;
  4185. *p++ = digits[ndigits];
  4186. }
  4187. }
  4188. *p++ = dp->conversion - 'G' + 'E'; /* 'e' or 'E' */
  4189. # if WIDE_CHAR_VERSION
  4190. {
  4191. static const wchar_t decimal_format[] =
  4192. /* Produce the same number of exponent digits
  4193. as the native printf implementation. */
  4194. # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
  4195. { '%', '+', '.', '3', 'd', '\0' };
  4196. # else
  4197. { '%', '+', '.', '2', 'd', '\0' };
  4198. # endif
  4199. SNPRINTF (p, 6 + 1, decimal_format, exponent);
  4200. }
  4201. while (*p != '\0')
  4202. p++;
  4203. # else
  4204. {
  4205. static const char decimal_format[] =
  4206. /* Produce the same number of exponent digits
  4207. as the native printf implementation. */
  4208. # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
  4209. "%+.3d";
  4210. # else
  4211. "%+.2d";
  4212. # endif
  4213. if (sizeof (DCHAR_T) == 1)
  4214. {
  4215. sprintf ((char *) p, decimal_format, exponent);
  4216. while (*p != '\0')
  4217. p++;
  4218. }
  4219. else
  4220. {
  4221. char expbuf[6 + 1];
  4222. const char *ep;
  4223. sprintf (expbuf, decimal_format, exponent);
  4224. for (ep = expbuf; (*p = *ep) != '\0'; ep++)
  4225. p++;
  4226. }
  4227. }
  4228. # endif
  4229. }
  4230. free (digits);
  4231. }
  4232. }
  4233. else
  4234. abort ();
  4235. # else
  4236. /* arg is finite. */
  4237. if (!(arg == 0.0))
  4238. abort ();
  4239. pad_ptr = p;
  4240. if (dp->conversion == 'f' || dp->conversion == 'F')
  4241. {
  4242. *p++ = '0';
  4243. if ((flags & FLAG_ALT) || precision > 0)
  4244. {
  4245. *p++ = decimal_point_char ();
  4246. for (; precision > 0; precision--)
  4247. *p++ = '0';
  4248. }
  4249. }
  4250. else if (dp->conversion == 'e' || dp->conversion == 'E')
  4251. {
  4252. *p++ = '0';
  4253. if ((flags & FLAG_ALT) || precision > 0)
  4254. {
  4255. *p++ = decimal_point_char ();
  4256. for (; precision > 0; precision--)
  4257. *p++ = '0';
  4258. }
  4259. *p++ = dp->conversion; /* 'e' or 'E' */
  4260. *p++ = '+';
  4261. /* Produce the same number of exponent digits as
  4262. the native printf implementation. */
  4263. # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
  4264. *p++ = '0';
  4265. # endif
  4266. *p++ = '0';
  4267. *p++ = '0';
  4268. }
  4269. else if (dp->conversion == 'g' || dp->conversion == 'G')
  4270. {
  4271. *p++ = '0';
  4272. if (flags & FLAG_ALT)
  4273. {
  4274. size_t ndigits =
  4275. (precision > 0 ? precision - 1 : 0);
  4276. *p++ = decimal_point_char ();
  4277. for (; ndigits > 0; --ndigits)
  4278. *p++ = '0';
  4279. }
  4280. }
  4281. else
  4282. abort ();
  4283. # endif
  4284. }
  4285. }
  4286. }
  4287. # endif
  4288. /* The generated string now extends from tmp to p, with the
  4289. zero padding insertion point being at pad_ptr. */
  4290. if (has_width && p - tmp < width)
  4291. {
  4292. size_t pad = width - (p - tmp);
  4293. DCHAR_T *end = p + pad;
  4294. if (flags & FLAG_LEFT)
  4295. {
  4296. /* Pad with spaces on the right. */
  4297. for (; pad > 0; pad--)
  4298. *p++ = ' ';
  4299. }
  4300. else if ((flags & FLAG_ZERO) && pad_ptr != NULL)
  4301. {
  4302. /* Pad with zeroes. */
  4303. DCHAR_T *q = end;
  4304. while (p > pad_ptr)
  4305. *--q = *--p;
  4306. for (; pad > 0; pad--)
  4307. *p++ = '0';
  4308. }
  4309. else
  4310. {
  4311. /* Pad with spaces on the left. */
  4312. DCHAR_T *q = end;
  4313. while (p > tmp)
  4314. *--q = *--p;
  4315. for (; pad > 0; pad--)
  4316. *p++ = ' ';
  4317. }
  4318. p = end;
  4319. }
  4320. {
  4321. size_t count = p - tmp;
  4322. if (count >= tmp_length)
  4323. /* tmp_length was incorrectly calculated - fix the
  4324. code above! */
  4325. abort ();
  4326. /* Make room for the result. */
  4327. if (count >= allocated - length)
  4328. {
  4329. size_t n = xsum (length, count);
  4330. ENSURE_ALLOCATION (n);
  4331. }
  4332. /* Append the result. */
  4333. memcpy (result + length, tmp, count * sizeof (DCHAR_T));
  4334. if (tmp != tmpbuf)
  4335. free (tmp);
  4336. length += count;
  4337. }
  4338. }
  4339. #endif
  4340. else
  4341. {
  4342. arg_type type = a.arg[dp->arg_index].type;
  4343. int flags = dp->flags;
  4344. #if !USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_LEFTADJUST || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
  4345. int has_width;
  4346. size_t width;
  4347. #endif
  4348. #if !USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || NEED_PRINTF_UNBOUNDED_PRECISION
  4349. int has_precision;
  4350. size_t precision;
  4351. #endif
  4352. #if NEED_PRINTF_UNBOUNDED_PRECISION
  4353. int prec_ourselves;
  4354. #else
  4355. # define prec_ourselves 0
  4356. #endif
  4357. #if NEED_PRINTF_FLAG_LEFTADJUST
  4358. # define pad_ourselves 1
  4359. #elif !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
  4360. int pad_ourselves;
  4361. #else
  4362. # define pad_ourselves 0
  4363. #endif
  4364. TCHAR_T *fbp;
  4365. unsigned int prefix_count;
  4366. int prefixes[2] IF_LINT (= { 0 });
  4367. int orig_errno;
  4368. #if !USE_SNPRINTF
  4369. size_t tmp_length;
  4370. TCHAR_T tmpbuf[700];
  4371. TCHAR_T *tmp;
  4372. #endif
  4373. #if !USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_LEFTADJUST || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
  4374. has_width = 0;
  4375. width = 0;
  4376. if (dp->width_start != dp->width_end)
  4377. {
  4378. if (dp->width_arg_index != ARG_NONE)
  4379. {
  4380. int arg;
  4381. if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
  4382. abort ();
  4383. arg = a.arg[dp->width_arg_index].a.a_int;
  4384. if (arg < 0)
  4385. {
  4386. /* "A negative field width is taken as a '-' flag
  4387. followed by a positive field width." */
  4388. flags |= FLAG_LEFT;
  4389. width = (unsigned int) (-arg);
  4390. }
  4391. else
  4392. width = arg;
  4393. }
  4394. else
  4395. {
  4396. const FCHAR_T *digitp = dp->width_start;
  4397. do
  4398. width = xsum (xtimes (width, 10), *digitp++ - '0');
  4399. while (digitp != dp->width_end);
  4400. }
  4401. has_width = 1;
  4402. }
  4403. #endif
  4404. #if !USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || NEED_PRINTF_UNBOUNDED_PRECISION
  4405. has_precision = 0;
  4406. precision = 6;
  4407. if (dp->precision_start != dp->precision_end)
  4408. {
  4409. if (dp->precision_arg_index != ARG_NONE)
  4410. {
  4411. int arg;
  4412. if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
  4413. abort ();
  4414. arg = a.arg[dp->precision_arg_index].a.a_int;
  4415. /* "A negative precision is taken as if the precision
  4416. were omitted." */
  4417. if (arg >= 0)
  4418. {
  4419. precision = arg;
  4420. has_precision = 1;
  4421. }
  4422. }
  4423. else
  4424. {
  4425. const FCHAR_T *digitp = dp->precision_start + 1;
  4426. precision = 0;
  4427. while (digitp != dp->precision_end)
  4428. precision = xsum (xtimes (precision, 10), *digitp++ - '0');
  4429. has_precision = 1;
  4430. }
  4431. }
  4432. #endif
  4433. /* Decide whether to handle the precision ourselves. */
  4434. #if NEED_PRINTF_UNBOUNDED_PRECISION
  4435. switch (dp->conversion)
  4436. {
  4437. case 'd': case 'i': case 'u':
  4438. case 'o':
  4439. case 'x': case 'X': case 'p':
  4440. prec_ourselves = has_precision && (precision > 0);
  4441. break;
  4442. default:
  4443. prec_ourselves = 0;
  4444. break;
  4445. }
  4446. #endif
  4447. /* Decide whether to perform the padding ourselves. */
  4448. #if !NEED_PRINTF_FLAG_LEFTADJUST && (!DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION)
  4449. switch (dp->conversion)
  4450. {
  4451. # if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO
  4452. /* If we need conversion from TCHAR_T[] to DCHAR_T[], we need
  4453. to perform the padding after this conversion. Functions
  4454. with unistdio extensions perform the padding based on
  4455. character count rather than element count. */
  4456. case 'c': case 's':
  4457. # endif
  4458. # if NEED_PRINTF_FLAG_ZERO
  4459. case 'f': case 'F': case 'e': case 'E': case 'g': case 'G':
  4460. case 'a': case 'A':
  4461. # endif
  4462. pad_ourselves = 1;
  4463. break;
  4464. default:
  4465. pad_ourselves = prec_ourselves;
  4466. break;
  4467. }
  4468. #endif
  4469. #if !USE_SNPRINTF
  4470. /* Allocate a temporary buffer of sufficient size for calling
  4471. sprintf. */
  4472. tmp_length =
  4473. MAX_ROOM_NEEDED (&a, dp->arg_index, dp->conversion, type,
  4474. flags, width, has_precision, precision,
  4475. pad_ourselves);
  4476. if (tmp_length <= sizeof (tmpbuf) / sizeof (TCHAR_T))
  4477. tmp = tmpbuf;
  4478. else
  4479. {
  4480. size_t tmp_memsize = xtimes (tmp_length, sizeof (TCHAR_T));
  4481. if (size_overflow_p (tmp_memsize))
  4482. /* Overflow, would lead to out of memory. */
  4483. goto out_of_memory;
  4484. tmp = (TCHAR_T *) malloc (tmp_memsize);
  4485. if (tmp == NULL)
  4486. /* Out of memory. */
  4487. goto out_of_memory;
  4488. }
  4489. #endif
  4490. /* Construct the format string for calling snprintf or
  4491. sprintf. */
  4492. fbp = buf;
  4493. *fbp++ = '%';
  4494. #if NEED_PRINTF_FLAG_GROUPING
  4495. /* The underlying implementation doesn't support the ' flag.
  4496. Produce no grouping characters in this case; this is
  4497. acceptable because the grouping is locale dependent. */
  4498. #else
  4499. if (flags & FLAG_GROUP)
  4500. *fbp++ = '\'';
  4501. #endif
  4502. if (flags & FLAG_LEFT)
  4503. *fbp++ = '-';
  4504. if (flags & FLAG_SHOWSIGN)
  4505. *fbp++ = '+';
  4506. if (flags & FLAG_SPACE)
  4507. *fbp++ = ' ';
  4508. if (flags & FLAG_ALT)
  4509. *fbp++ = '#';
  4510. #if __GLIBC__ >= 2 && !defined __UCLIBC__
  4511. if (flags & FLAG_LOCALIZED)
  4512. *fbp++ = 'I';
  4513. #endif
  4514. if (!pad_ourselves)
  4515. {
  4516. if (flags & FLAG_ZERO)
  4517. *fbp++ = '0';
  4518. if (dp->width_start != dp->width_end)
  4519. {
  4520. size_t n = dp->width_end - dp->width_start;
  4521. /* The width specification is known to consist only
  4522. of standard ASCII characters. */
  4523. if (sizeof (FCHAR_T) == sizeof (TCHAR_T))
  4524. {
  4525. memcpy (fbp, dp->width_start, n * sizeof (TCHAR_T));
  4526. fbp += n;
  4527. }
  4528. else
  4529. {
  4530. const FCHAR_T *mp = dp->width_start;
  4531. do
  4532. *fbp++ = (unsigned char) *mp++;
  4533. while (--n > 0);
  4534. }
  4535. }
  4536. }
  4537. if (!prec_ourselves)
  4538. {
  4539. if (dp->precision_start != dp->precision_end)
  4540. {
  4541. size_t n = dp->precision_end - dp->precision_start;
  4542. /* The precision specification is known to consist only
  4543. of standard ASCII characters. */
  4544. if (sizeof (FCHAR_T) == sizeof (TCHAR_T))
  4545. {
  4546. memcpy (fbp, dp->precision_start, n * sizeof (TCHAR_T));
  4547. fbp += n;
  4548. }
  4549. else
  4550. {
  4551. const FCHAR_T *mp = dp->precision_start;
  4552. do
  4553. *fbp++ = (unsigned char) *mp++;
  4554. while (--n > 0);
  4555. }
  4556. }
  4557. }
  4558. switch (type)
  4559. {
  4560. #if HAVE_LONG_LONG_INT
  4561. case TYPE_LONGLONGINT:
  4562. case TYPE_ULONGLONGINT:
  4563. # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
  4564. *fbp++ = 'I';
  4565. *fbp++ = '6';
  4566. *fbp++ = '4';
  4567. break;
  4568. # else
  4569. *fbp++ = 'l';
  4570. /*FALLTHROUGH*/
  4571. # endif
  4572. #endif
  4573. case TYPE_LONGINT:
  4574. case TYPE_ULONGINT:
  4575. #if HAVE_WINT_T
  4576. case TYPE_WIDE_CHAR:
  4577. #endif
  4578. #if HAVE_WCHAR_T
  4579. case TYPE_WIDE_STRING:
  4580. #endif
  4581. *fbp++ = 'l';
  4582. break;
  4583. case TYPE_LONGDOUBLE:
  4584. *fbp++ = 'L';
  4585. break;
  4586. default:
  4587. break;
  4588. }
  4589. #if NEED_PRINTF_DIRECTIVE_F
  4590. if (dp->conversion == 'F')
  4591. *fbp = 'f';
  4592. else
  4593. #endif
  4594. *fbp = dp->conversion;
  4595. #if USE_SNPRINTF
  4596. # if !(((__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)) && !defined __UCLIBC__) || ((defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__))
  4597. fbp[1] = '%';
  4598. fbp[2] = 'n';
  4599. fbp[3] = '\0';
  4600. # else
  4601. /* On glibc2 systems from glibc >= 2.3 - probably also older
  4602. ones - we know that snprintf's return value conforms to
  4603. ISO C 99: the tests gl_SNPRINTF_RETVAL_C99 and
  4604. gl_SNPRINTF_TRUNCATION_C99 pass.
  4605. Therefore we can avoid using %n in this situation.
  4606. On glibc2 systems from 2004-10-18 or newer, the use of %n
  4607. in format strings in writable memory may crash the program
  4608. (if compiled with _FORTIFY_SOURCE=2), so we should avoid it
  4609. in this situation. */
  4610. /* On native Win32 systems (such as mingw), we can avoid using
  4611. %n because:
  4612. - Although the gl_SNPRINTF_TRUNCATION_C99 test fails,
  4613. snprintf does not write more than the specified number
  4614. of bytes. (snprintf (buf, 3, "%d %d", 4567, 89) writes
  4615. '4', '5', '6' into buf, not '4', '5', '\0'.)
  4616. - Although the gl_SNPRINTF_RETVAL_C99 test fails, snprintf
  4617. allows us to recognize the case of an insufficient
  4618. buffer size: it returns -1 in this case.
  4619. On native Win32 systems (such as mingw) where the OS is
  4620. Windows Vista, the use of %n in format strings by default
  4621. crashes the program. See
  4622. <http://gcc.gnu.org/ml/gcc/2007-06/msg00122.html> and
  4623. <http://msdn2.microsoft.com/en-us/library/ms175782(VS.80).aspx>
  4624. So we should avoid %n in this situation. */
  4625. fbp[1] = '\0';
  4626. # endif
  4627. #else
  4628. fbp[1] = '\0';
  4629. #endif
  4630. /* Construct the arguments for calling snprintf or sprintf. */
  4631. prefix_count = 0;
  4632. if (!pad_ourselves && dp->width_arg_index != ARG_NONE)
  4633. {
  4634. if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
  4635. abort ();
  4636. prefixes[prefix_count++] = a.arg[dp->width_arg_index].a.a_int;
  4637. }
  4638. if (!prec_ourselves && dp->precision_arg_index != ARG_NONE)
  4639. {
  4640. if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
  4641. abort ();
  4642. prefixes[prefix_count++] = a.arg[dp->precision_arg_index].a.a_int;
  4643. }
  4644. #if USE_SNPRINTF
  4645. /* The SNPRINTF result is appended after result[0..length].
  4646. The latter is an array of DCHAR_T; SNPRINTF appends an
  4647. array of TCHAR_T to it. This is possible because
  4648. sizeof (TCHAR_T) divides sizeof (DCHAR_T) and
  4649. alignof (TCHAR_T) <= alignof (DCHAR_T). */
  4650. # define TCHARS_PER_DCHAR (sizeof (DCHAR_T) / sizeof (TCHAR_T))
  4651. /* Ensure that maxlen below will be >= 2. Needed on BeOS,
  4652. where an snprintf() with maxlen==1 acts like sprintf(). */
  4653. ENSURE_ALLOCATION (xsum (length,
  4654. (2 + TCHARS_PER_DCHAR - 1)
  4655. / TCHARS_PER_DCHAR));
  4656. /* Prepare checking whether snprintf returns the count
  4657. via %n. */
  4658. *(TCHAR_T *) (result + length) = '\0';
  4659. #endif
  4660. orig_errno = errno;
  4661. for (;;)
  4662. {
  4663. int count = -1;
  4664. #if USE_SNPRINTF
  4665. int retcount = 0;
  4666. size_t maxlen = allocated - length;
  4667. /* SNPRINTF can fail if its second argument is
  4668. > INT_MAX. */
  4669. if (maxlen > INT_MAX / TCHARS_PER_DCHAR)
  4670. maxlen = INT_MAX / TCHARS_PER_DCHAR;
  4671. maxlen = maxlen * TCHARS_PER_DCHAR;
  4672. # define SNPRINTF_BUF(arg) \
  4673. switch (prefix_count) \
  4674. { \
  4675. case 0: \
  4676. retcount = SNPRINTF ((TCHAR_T *) (result + length), \
  4677. maxlen, buf, \
  4678. arg, &count); \
  4679. break; \
  4680. case 1: \
  4681. retcount = SNPRINTF ((TCHAR_T *) (result + length), \
  4682. maxlen, buf, \
  4683. prefixes[0], arg, &count); \
  4684. break; \
  4685. case 2: \
  4686. retcount = SNPRINTF ((TCHAR_T *) (result + length), \
  4687. maxlen, buf, \
  4688. prefixes[0], prefixes[1], arg, \
  4689. &count); \
  4690. break; \
  4691. default: \
  4692. abort (); \
  4693. }
  4694. #else
  4695. # define SNPRINTF_BUF(arg) \
  4696. switch (prefix_count) \
  4697. { \
  4698. case 0: \
  4699. count = sprintf (tmp, buf, arg); \
  4700. break; \
  4701. case 1: \
  4702. count = sprintf (tmp, buf, prefixes[0], arg); \
  4703. break; \
  4704. case 2: \
  4705. count = sprintf (tmp, buf, prefixes[0], prefixes[1],\
  4706. arg); \
  4707. break; \
  4708. default: \
  4709. abort (); \
  4710. }
  4711. #endif
  4712. errno = 0;
  4713. switch (type)
  4714. {
  4715. case TYPE_SCHAR:
  4716. {
  4717. int arg = a.arg[dp->arg_index].a.a_schar;
  4718. SNPRINTF_BUF (arg);
  4719. }
  4720. break;
  4721. case TYPE_UCHAR:
  4722. {
  4723. unsigned int arg = a.arg[dp->arg_index].a.a_uchar;
  4724. SNPRINTF_BUF (arg);
  4725. }
  4726. break;
  4727. case TYPE_SHORT:
  4728. {
  4729. int arg = a.arg[dp->arg_index].a.a_short;
  4730. SNPRINTF_BUF (arg);
  4731. }
  4732. break;
  4733. case TYPE_USHORT:
  4734. {
  4735. unsigned int arg = a.arg[dp->arg_index].a.a_ushort;
  4736. SNPRINTF_BUF (arg);
  4737. }
  4738. break;
  4739. case TYPE_INT:
  4740. {
  4741. int arg = a.arg[dp->arg_index].a.a_int;
  4742. SNPRINTF_BUF (arg);
  4743. }
  4744. break;
  4745. case TYPE_UINT:
  4746. {
  4747. unsigned int arg = a.arg[dp->arg_index].a.a_uint;
  4748. SNPRINTF_BUF (arg);
  4749. }
  4750. break;
  4751. case TYPE_LONGINT:
  4752. {
  4753. long int arg = a.arg[dp->arg_index].a.a_longint;
  4754. SNPRINTF_BUF (arg);
  4755. }
  4756. break;
  4757. case TYPE_ULONGINT:
  4758. {
  4759. unsigned long int arg = a.arg[dp->arg_index].a.a_ulongint;
  4760. SNPRINTF_BUF (arg);
  4761. }
  4762. break;
  4763. #if HAVE_LONG_LONG_INT
  4764. case TYPE_LONGLONGINT:
  4765. {
  4766. long long int arg = a.arg[dp->arg_index].a.a_longlongint;
  4767. SNPRINTF_BUF (arg);
  4768. }
  4769. break;
  4770. case TYPE_ULONGLONGINT:
  4771. {
  4772. unsigned long long int arg = a.arg[dp->arg_index].a.a_ulonglongint;
  4773. SNPRINTF_BUF (arg);
  4774. }
  4775. break;
  4776. #endif
  4777. case TYPE_DOUBLE:
  4778. {
  4779. double arg = a.arg[dp->arg_index].a.a_double;
  4780. SNPRINTF_BUF (arg);
  4781. }
  4782. break;
  4783. case TYPE_LONGDOUBLE:
  4784. {
  4785. long double arg = a.arg[dp->arg_index].a.a_longdouble;
  4786. SNPRINTF_BUF (arg);
  4787. }
  4788. break;
  4789. case TYPE_CHAR:
  4790. {
  4791. int arg = a.arg[dp->arg_index].a.a_char;
  4792. SNPRINTF_BUF (arg);
  4793. }
  4794. break;
  4795. #if HAVE_WINT_T
  4796. case TYPE_WIDE_CHAR:
  4797. {
  4798. wint_t arg = a.arg[dp->arg_index].a.a_wide_char;
  4799. SNPRINTF_BUF (arg);
  4800. }
  4801. break;
  4802. #endif
  4803. case TYPE_STRING:
  4804. {
  4805. const char *arg = a.arg[dp->arg_index].a.a_string;
  4806. SNPRINTF_BUF (arg);
  4807. }
  4808. break;
  4809. #if HAVE_WCHAR_T
  4810. case TYPE_WIDE_STRING:
  4811. {
  4812. const wchar_t *arg = a.arg[dp->arg_index].a.a_wide_string;
  4813. SNPRINTF_BUF (arg);
  4814. }
  4815. break;
  4816. #endif
  4817. case TYPE_POINTER:
  4818. {
  4819. void *arg = a.arg[dp->arg_index].a.a_pointer;
  4820. SNPRINTF_BUF (arg);
  4821. }
  4822. break;
  4823. default:
  4824. abort ();
  4825. }
  4826. #if USE_SNPRINTF
  4827. /* Portability: Not all implementations of snprintf()
  4828. are ISO C 99 compliant. Determine the number of
  4829. bytes that snprintf() has produced or would have
  4830. produced. */
  4831. if (count >= 0)
  4832. {
  4833. /* Verify that snprintf() has NUL-terminated its
  4834. result. */
  4835. if (count < maxlen
  4836. && ((TCHAR_T *) (result + length)) [count] != '\0')
  4837. abort ();
  4838. /* Portability hack. */
  4839. if (retcount > count)
  4840. count = retcount;
  4841. }
  4842. else
  4843. {
  4844. /* snprintf() doesn't understand the '%n'
  4845. directive. */
  4846. if (fbp[1] != '\0')
  4847. {
  4848. /* Don't use the '%n' directive; instead, look
  4849. at the snprintf() return value. */
  4850. fbp[1] = '\0';
  4851. continue;
  4852. }
  4853. else
  4854. {
  4855. /* Look at the snprintf() return value. */
  4856. if (retcount < 0)
  4857. {
  4858. # if !HAVE_SNPRINTF_RETVAL_C99
  4859. /* HP-UX 10.20 snprintf() is doubly deficient:
  4860. It doesn't understand the '%n' directive,
  4861. *and* it returns -1 (rather than the length
  4862. that would have been required) when the
  4863. buffer is too small.
  4864. But a failure at this point can also come
  4865. from other reasons than a too small buffer,
  4866. such as an invalid wide string argument to
  4867. the %ls directive, or possibly an invalid
  4868. floating-point argument. */
  4869. size_t tmp_length =
  4870. MAX_ROOM_NEEDED (&a, dp->arg_index,
  4871. dp->conversion, type, flags,
  4872. width, has_precision,
  4873. precision, pad_ourselves);
  4874. if (maxlen < tmp_length)
  4875. {
  4876. /* Make more room. But try to do through
  4877. this reallocation only once. */
  4878. size_t bigger_need =
  4879. xsum (length,
  4880. xsum (tmp_length,
  4881. TCHARS_PER_DCHAR - 1)
  4882. / TCHARS_PER_DCHAR);
  4883. /* And always grow proportionally.
  4884. (There may be several arguments, each
  4885. needing a little more room than the
  4886. previous one.) */
  4887. size_t bigger_need2 =
  4888. xsum (xtimes (allocated, 2), 12);
  4889. if (bigger_need < bigger_need2)
  4890. bigger_need = bigger_need2;
  4891. ENSURE_ALLOCATION (bigger_need);
  4892. continue;
  4893. }
  4894. # endif
  4895. }
  4896. else
  4897. count = retcount;
  4898. }
  4899. }
  4900. #endif
  4901. /* Attempt to handle failure. */
  4902. if (count < 0)
  4903. {
  4904. /* SNPRINTF or sprintf failed. Save and use the errno
  4905. that it has set, if any. */
  4906. int saved_errno = errno;
  4907. if (!(result == resultbuf || result == NULL))
  4908. free (result);
  4909. if (buf_malloced != NULL)
  4910. free (buf_malloced);
  4911. CLEANUP ();
  4912. errno =
  4913. (saved_errno != 0
  4914. ? saved_errno
  4915. : (dp->conversion == 'c' || dp->conversion == 's'
  4916. ? EILSEQ
  4917. : EINVAL));
  4918. return NULL;
  4919. }
  4920. #if USE_SNPRINTF
  4921. /* Handle overflow of the allocated buffer.
  4922. If such an overflow occurs, a C99 compliant snprintf()
  4923. returns a count >= maxlen. However, a non-compliant
  4924. snprintf() function returns only count = maxlen - 1. To
  4925. cover both cases, test whether count >= maxlen - 1. */
  4926. if ((unsigned int) count + 1 >= maxlen)
  4927. {
  4928. /* If maxlen already has attained its allowed maximum,
  4929. allocating more memory will not increase maxlen.
  4930. Instead of looping, bail out. */
  4931. if (maxlen == INT_MAX / TCHARS_PER_DCHAR)
  4932. goto overflow;
  4933. else
  4934. {
  4935. /* Need at least (count + 1) * sizeof (TCHAR_T)
  4936. bytes. (The +1 is for the trailing NUL.)
  4937. But ask for (count + 2) * sizeof (TCHAR_T)
  4938. bytes, so that in the next round, we likely get
  4939. maxlen > (unsigned int) count + 1
  4940. and so we don't get here again.
  4941. And allocate proportionally, to avoid looping
  4942. eternally if snprintf() reports a too small
  4943. count. */
  4944. size_t n =
  4945. xmax (xsum (length,
  4946. ((unsigned int) count + 2
  4947. + TCHARS_PER_DCHAR - 1)
  4948. / TCHARS_PER_DCHAR),
  4949. xtimes (allocated, 2));
  4950. ENSURE_ALLOCATION (n);
  4951. continue;
  4952. }
  4953. }
  4954. #endif
  4955. #if NEED_PRINTF_UNBOUNDED_PRECISION
  4956. if (prec_ourselves)
  4957. {
  4958. /* Handle the precision. */
  4959. TCHAR_T *prec_ptr =
  4960. # if USE_SNPRINTF
  4961. (TCHAR_T *) (result + length);
  4962. # else
  4963. tmp;
  4964. # endif
  4965. size_t prefix_count;
  4966. size_t move;
  4967. prefix_count = 0;
  4968. /* Put the additional zeroes after the sign. */
  4969. if (count >= 1
  4970. && (*prec_ptr == '-' || *prec_ptr == '+'
  4971. || *prec_ptr == ' '))
  4972. prefix_count = 1;
  4973. /* Put the additional zeroes after the 0x prefix if
  4974. (flags & FLAG_ALT) || (dp->conversion == 'p'). */
  4975. else if (count >= 2
  4976. && prec_ptr[0] == '0'
  4977. && (prec_ptr[1] == 'x' || prec_ptr[1] == 'X'))
  4978. prefix_count = 2;
  4979. move = count - prefix_count;
  4980. if (precision > move)
  4981. {
  4982. /* Insert zeroes. */
  4983. size_t insert = precision - move;
  4984. TCHAR_T *prec_end;
  4985. # if USE_SNPRINTF
  4986. size_t n =
  4987. xsum (length,
  4988. (count + insert + TCHARS_PER_DCHAR - 1)
  4989. / TCHARS_PER_DCHAR);
  4990. length += (count + TCHARS_PER_DCHAR - 1) / TCHARS_PER_DCHAR;
  4991. ENSURE_ALLOCATION (n);
  4992. length -= (count + TCHARS_PER_DCHAR - 1) / TCHARS_PER_DCHAR;
  4993. prec_ptr = (TCHAR_T *) (result + length);
  4994. # endif
  4995. prec_end = prec_ptr + count;
  4996. prec_ptr += prefix_count;
  4997. while (prec_end > prec_ptr)
  4998. {
  4999. prec_end--;
  5000. prec_end[insert] = prec_end[0];
  5001. }
  5002. prec_end += insert;
  5003. do
  5004. *--prec_end = '0';
  5005. while (prec_end > prec_ptr);
  5006. count += insert;
  5007. }
  5008. }
  5009. #endif
  5010. #if !USE_SNPRINTF
  5011. if (count >= tmp_length)
  5012. /* tmp_length was incorrectly calculated - fix the
  5013. code above! */
  5014. abort ();
  5015. #endif
  5016. #if !DCHAR_IS_TCHAR
  5017. /* Convert from TCHAR_T[] to DCHAR_T[]. */
  5018. if (dp->conversion == 'c' || dp->conversion == 's')
  5019. {
  5020. /* type = TYPE_CHAR or TYPE_WIDE_CHAR or TYPE_STRING
  5021. TYPE_WIDE_STRING.
  5022. The result string is not certainly ASCII. */
  5023. const TCHAR_T *tmpsrc;
  5024. DCHAR_T *tmpdst;
  5025. size_t tmpdst_len;
  5026. /* This code assumes that TCHAR_T is 'char'. */
  5027. verify (sizeof (TCHAR_T) == 1);
  5028. # if USE_SNPRINTF
  5029. tmpsrc = (TCHAR_T *) (result + length);
  5030. # else
  5031. tmpsrc = tmp;
  5032. # endif
  5033. tmpdst =
  5034. DCHAR_CONV_FROM_ENCODING (locale_charset (),
  5035. iconveh_question_mark,
  5036. tmpsrc, count,
  5037. NULL,
  5038. NULL, &tmpdst_len);
  5039. if (tmpdst == NULL)
  5040. {
  5041. int saved_errno = errno;
  5042. if (!(result == resultbuf || result == NULL))
  5043. free (result);
  5044. if (buf_malloced != NULL)
  5045. free (buf_malloced);
  5046. CLEANUP ();
  5047. errno = saved_errno;
  5048. return NULL;
  5049. }
  5050. ENSURE_ALLOCATION (xsum (length, tmpdst_len));
  5051. DCHAR_CPY (result + length, tmpdst, tmpdst_len);
  5052. free (tmpdst);
  5053. count = tmpdst_len;
  5054. }
  5055. else
  5056. {
  5057. /* The result string is ASCII.
  5058. Simple 1:1 conversion. */
  5059. # if USE_SNPRINTF
  5060. /* If sizeof (DCHAR_T) == sizeof (TCHAR_T), it's a
  5061. no-op conversion, in-place on the array starting
  5062. at (result + length). */
  5063. if (sizeof (DCHAR_T) != sizeof (TCHAR_T))
  5064. # endif
  5065. {
  5066. const TCHAR_T *tmpsrc;
  5067. DCHAR_T *tmpdst;
  5068. size_t n;
  5069. # if USE_SNPRINTF
  5070. if (result == resultbuf)
  5071. {
  5072. tmpsrc = (TCHAR_T *) (result + length);
  5073. /* ENSURE_ALLOCATION will not move tmpsrc
  5074. (because it's part of resultbuf). */
  5075. ENSURE_ALLOCATION (xsum (length, count));
  5076. }
  5077. else
  5078. {
  5079. /* ENSURE_ALLOCATION will move the array
  5080. (because it uses realloc(). */
  5081. ENSURE_ALLOCATION (xsum (length, count));
  5082. tmpsrc = (TCHAR_T *) (result + length);
  5083. }
  5084. # else
  5085. tmpsrc = tmp;
  5086. ENSURE_ALLOCATION (xsum (length, count));
  5087. # endif
  5088. tmpdst = result + length;
  5089. /* Copy backwards, because of overlapping. */
  5090. tmpsrc += count;
  5091. tmpdst += count;
  5092. for (n = count; n > 0; n--)
  5093. *--tmpdst = (unsigned char) *--tmpsrc;
  5094. }
  5095. }
  5096. #endif
  5097. #if DCHAR_IS_TCHAR && !USE_SNPRINTF
  5098. /* Make room for the result. */
  5099. if (count > allocated - length)
  5100. {
  5101. /* Need at least count elements. But allocate
  5102. proportionally. */
  5103. size_t n =
  5104. xmax (xsum (length, count), xtimes (allocated, 2));
  5105. ENSURE_ALLOCATION (n);
  5106. }
  5107. #endif
  5108. /* Here count <= allocated - length. */
  5109. /* Perform padding. */
  5110. #if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_LEFTADJUST || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
  5111. if (pad_ourselves && has_width)
  5112. {
  5113. size_t w;
  5114. # if ENABLE_UNISTDIO
  5115. /* Outside POSIX, it's preferrable to compare the width
  5116. against the number of _characters_ of the converted
  5117. value. */
  5118. w = DCHAR_MBSNLEN (result + length, count);
  5119. # else
  5120. /* The width is compared against the number of _bytes_
  5121. of the converted value, says POSIX. */
  5122. w = count;
  5123. # endif
  5124. if (w < width)
  5125. {
  5126. size_t pad = width - w;
  5127. /* Make room for the result. */
  5128. if (xsum (count, pad) > allocated - length)
  5129. {
  5130. /* Need at least count + pad elements. But
  5131. allocate proportionally. */
  5132. size_t n =
  5133. xmax (xsum3 (length, count, pad),
  5134. xtimes (allocated, 2));
  5135. # if USE_SNPRINTF
  5136. length += count;
  5137. ENSURE_ALLOCATION (n);
  5138. length -= count;
  5139. # else
  5140. ENSURE_ALLOCATION (n);
  5141. # endif
  5142. }
  5143. /* Here count + pad <= allocated - length. */
  5144. {
  5145. # if !DCHAR_IS_TCHAR || USE_SNPRINTF
  5146. DCHAR_T * const rp = result + length;
  5147. # else
  5148. DCHAR_T * const rp = tmp;
  5149. # endif
  5150. DCHAR_T *p = rp + count;
  5151. DCHAR_T *end = p + pad;
  5152. DCHAR_T *pad_ptr;
  5153. # if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO
  5154. if (dp->conversion == 'c'
  5155. || dp->conversion == 's')
  5156. /* No zero-padding for string directives. */
  5157. pad_ptr = NULL;
  5158. else
  5159. # endif
  5160. {
  5161. pad_ptr = (*rp == '-' ? rp + 1 : rp);
  5162. /* No zero-padding of "inf" and "nan". */
  5163. if ((*pad_ptr >= 'A' && *pad_ptr <= 'Z')
  5164. || (*pad_ptr >= 'a' && *pad_ptr <= 'z'))
  5165. pad_ptr = NULL;
  5166. }
  5167. /* The generated string now extends from rp to p,
  5168. with the zero padding insertion point being at
  5169. pad_ptr. */
  5170. count = count + pad; /* = end - rp */
  5171. if (flags & FLAG_LEFT)
  5172. {
  5173. /* Pad with spaces on the right. */
  5174. for (; pad > 0; pad--)
  5175. *p++ = ' ';
  5176. }
  5177. else if ((flags & FLAG_ZERO) && pad_ptr != NULL)
  5178. {
  5179. /* Pad with zeroes. */
  5180. DCHAR_T *q = end;
  5181. while (p > pad_ptr)
  5182. *--q = *--p;
  5183. for (; pad > 0; pad--)
  5184. *p++ = '0';
  5185. }
  5186. else
  5187. {
  5188. /* Pad with spaces on the left. */
  5189. DCHAR_T *q = end;
  5190. while (p > rp)
  5191. *--q = *--p;
  5192. for (; pad > 0; pad--)
  5193. *p++ = ' ';
  5194. }
  5195. }
  5196. }
  5197. }
  5198. #endif
  5199. /* Here still count <= allocated - length. */
  5200. #if !DCHAR_IS_TCHAR || USE_SNPRINTF
  5201. /* The snprintf() result did fit. */
  5202. #else
  5203. /* Append the sprintf() result. */
  5204. memcpy (result + length, tmp, count * sizeof (DCHAR_T));
  5205. #endif
  5206. #if !USE_SNPRINTF
  5207. if (tmp != tmpbuf)
  5208. free (tmp);
  5209. #endif
  5210. #if NEED_PRINTF_DIRECTIVE_F
  5211. if (dp->conversion == 'F')
  5212. {
  5213. /* Convert the %f result to upper case for %F. */
  5214. DCHAR_T *rp = result + length;
  5215. size_t rc;
  5216. for (rc = count; rc > 0; rc--, rp++)
  5217. if (*rp >= 'a' && *rp <= 'z')
  5218. *rp = *rp - 'a' + 'A';
  5219. }
  5220. #endif
  5221. length += count;
  5222. break;
  5223. }
  5224. errno = orig_errno;
  5225. #undef pad_ourselves
  5226. #undef prec_ourselves
  5227. }
  5228. }
  5229. }
  5230. /* Add the final NUL. */
  5231. ENSURE_ALLOCATION (xsum (length, 1));
  5232. result[length] = '\0';
  5233. if (result != resultbuf && length + 1 < allocated)
  5234. {
  5235. /* Shrink the allocated memory if possible. */
  5236. DCHAR_T *memory;
  5237. memory = (DCHAR_T *) realloc (result, (length + 1) * sizeof (DCHAR_T));
  5238. if (memory != NULL)
  5239. result = memory;
  5240. }
  5241. if (buf_malloced != NULL)
  5242. free (buf_malloced);
  5243. CLEANUP ();
  5244. *lengthp = length;
  5245. /* Note that we can produce a big string of a length > INT_MAX. POSIX
  5246. says that snprintf() fails with errno = EOVERFLOW in this case, but
  5247. that's only because snprintf() returns an 'int'. This function does
  5248. not have this limitation. */
  5249. return result;
  5250. #if USE_SNPRINTF
  5251. overflow:
  5252. if (!(result == resultbuf || result == NULL))
  5253. free (result);
  5254. if (buf_malloced != NULL)
  5255. free (buf_malloced);
  5256. CLEANUP ();
  5257. errno = EOVERFLOW;
  5258. return NULL;
  5259. #endif
  5260. out_of_memory:
  5261. if (!(result == resultbuf || result == NULL))
  5262. free (result);
  5263. if (buf_malloced != NULL)
  5264. free (buf_malloced);
  5265. out_of_memory_1:
  5266. CLEANUP ();
  5267. errno = ENOMEM;
  5268. return NULL;
  5269. }
  5270. }
  5271. #undef MAX_ROOM_NEEDED
  5272. #undef TCHARS_PER_DCHAR
  5273. #undef SNPRINTF
  5274. #undef USE_SNPRINTF
  5275. #undef DCHAR_SET
  5276. #undef DCHAR_CPY
  5277. #undef PRINTF_PARSE
  5278. #undef DIRECTIVES
  5279. #undef DIRECTIVE
  5280. #undef DCHAR_IS_TCHAR
  5281. #undef TCHAR_T
  5282. #undef DCHAR_T
  5283. #undef FCHAR_T
  5284. #undef VASNPRINTF