stdio.c 99 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671
  1. /* Copyright (C) 2002 Manuel Novoa III
  2. * My stdio library for linux and (soon) elks.
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Library General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Library General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Library General Public
  15. * License along with this library; if not, write to the Free
  16. * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. /* ATTENTION! ATTENTION! ATTENTION! ATTENTION! ATTENTION!
  19. *
  20. * This code is currently under development. Also, I plan to port
  21. * it to elks which is a 16-bit environment with a fairly limited
  22. * compiler. Therefore, please refrain from modifying this code
  23. * and, instead, pass any bug-fixes, etc. to me. Thanks. Manuel
  24. *
  25. * ATTENTION! ATTENTION! ATTENTION! ATTENTION! ATTENTION! */
  26. /* 8-05-2002
  27. * Changed fflush() behavior to no-op for r/w streams in read-mode.
  28. * This falls under undefined behavior wrt ANSI/ISO C99, but
  29. * SUSv3 seems to treat it as a no-op and it occurs in some apps.
  30. * Fixed a problem with _stdio_fwrite() not checking for underlying
  31. * write() failures.
  32. * Fixed both _stdio_fwrite() and _stdio_fread() to make sure that
  33. * the putc and getc macros were disabled if the stream was in
  34. * and error state.
  35. * The above changes should take care of a problem initially reported
  36. * by "Steven J. Hill" <sjhill@realitydiluted.com>.
  37. *
  38. * 8-25-2002
  39. * Changed fclose behavior when custom streams were enabled. Previously,
  40. * the cookie pointer was set to NULL as a debugging aid. However,
  41. * some of the perl 5.8 test rely on being able to close stderr and
  42. * still try writing to it. So now, the cookie pointer and handler
  43. * function pointers are set to that it is a "normal" file with a
  44. * file descriptor of -1. Note: The cookie pointer is reset to NULL
  45. * if the FILE struct is free'd by fclose.
  46. *
  47. * Nov 21, 2002
  48. * Added internal function _wstdio_fwrite.
  49. * Jan 3, 2003
  50. * Fixed a bug in _wstdio_fwrite.
  51. *
  52. * Jan 22, 2003
  53. * Fixed a bug related file position in append mode. _stdio_fwrite now
  54. * seeks to the end of the stream when append mode is set and we are
  55. * transitioning to write mode, so that subsequent ftell() return
  56. * values are correct.
  57. * Also fix _stdio_fopen to support fdopen() with append specified when
  58. * the underlying file didn't have O_APPEND set. It now sets the
  59. * O_APPEND flag as recommended by SUSv3 and is done by glibc.
  60. *
  61. * May 15, 2003
  62. * Modify __stdio_fread to deal with fake streams used by *sscanf.
  63. * Set EOF to end of buffer when fmemopen used on a readonly stream.
  64. * Note: I really need to run some tests on this to see what the
  65. * glibc code does in each case.
  66. *
  67. * Sept 21, 2003
  68. * Modify _stdio_READ to conform with C99, as stdio input behavior upon
  69. * encountering EOF changed with Defect Report #141. In the current
  70. * standard, the stream's EOF indicator is "sticky". Once it is set,
  71. * all further input from the stream should fail until the application
  72. * explicitly clears the EOF indicator (clearerr(), file positioning),
  73. * even if more data becomes available.
  74. * Fixed a bug in fgets. Wasn't checking for read errors.
  75. * Minor thread locking optimizations to avoid some unnecessary locking.
  76. * Remove the explicit calls to __builtin_* funcs, as we really need to
  77. * implement a more general solution.
  78. */
  79. /* Before we include anything, convert L_ctermid to L_ctermid_function
  80. * and undef L_ctermid if defined. This is necessary as L_ctermid is
  81. * a SUSv3 standard macro defined in stdio.h. */
  82. #ifdef L_ctermid
  83. #define L_ctermid_function
  84. #undef L_ctermid
  85. #endif
  86. #define _ISOC99_SOURCE /* for ULLONG primarily... */
  87. #define _GNU_SOURCE
  88. #define _STDIO_UTILITY /* for _stdio_fdout and _uintmaxtostr. */
  89. #include <stdio.h>
  90. #include <stddef.h>
  91. #include <stdlib.h>
  92. #include <string.h>
  93. #include <limits.h>
  94. #include <stdint.h>
  95. #include <stdarg.h>
  96. #include <errno.h>
  97. #include <assert.h>
  98. #include <stdio_ext.h>
  99. #include <unistd.h>
  100. #include <fcntl.h>
  101. #ifndef O_LARGEFILE /* uClibc undefines this if no large file support. */
  102. #ifdef __STDIO_LARGE_FILES
  103. #error missing define for O_LARGEFILE!
  104. #endif
  105. #define O_LARGEFILE 0
  106. #endif
  107. /**********************************************************************/
  108. /* First deal with some build issues... */
  109. #ifndef __STDIO_THREADSAFE
  110. /* Just build empty object files if any of these were defined. */
  111. /* Note though that we do keep the various *_unlocked names as aliases. */
  112. #undef L___fsetlocking
  113. #undef L___flockfile
  114. #undef L___ftrylockfile
  115. #undef L___funlockfile
  116. #endif
  117. #ifndef __STDIO_LARGE_FILES
  118. /* Just build empty object files if any of these were defined. */
  119. #undef L_fopen64
  120. #undef L_freopen64
  121. #undef L_ftello64
  122. #undef L_fseeko64
  123. #undef L_fsetpos64
  124. #undef L_fgetpos64
  125. #endif
  126. /**********************************************************************/
  127. #ifndef __STDIO_THREADSAFE
  128. #if defined(__BCC__) && 0
  129. #define UNLOCKED_STREAM(RETURNTYPE,NAME,PARAMS,ARGS,STREAM) \
  130. asm(".text\nexport _" "NAME" "_unlocked\n_" "NAME" "_unlocked = _" "NAME"); \
  131. RETURNTYPE NAME PARAMS
  132. #else
  133. #define UNLOCKED_STREAM(RETURNTYPE,NAME,PARAMS,ARGS,STREAM) \
  134. strong_alias(NAME,NAME##_unlocked) \
  135. RETURNTYPE NAME PARAMS
  136. #endif
  137. #define UNLOCKED(RETURNTYPE,NAME,PARAMS,ARGS) \
  138. UNLOCKED_STREAM(RETURNTYPE,NAME,PARAMS,ARGS,stream)
  139. #if defined(__BCC__) && 0
  140. #define UNLOCKED_VOID_RETURN(NAME,PARAMS,ARGS) \
  141. asm(".text\nexport _" "NAME" "_unlocked\n_" "NAME" "_unlocked = _" "NAME"); \
  142. void NAME PARAMS
  143. #else
  144. #define UNLOCKED_VOID_RETURN(NAME,PARAMS,ARGS) \
  145. strong_alias(NAME,NAME##_unlocked) \
  146. void NAME PARAMS
  147. #endif
  148. #define __STDIO_THREADLOCK_OPENLIST
  149. #define __STDIO_THREADUNLOCK_OPENLIST
  150. #else /* __STDIO_THREADSAFE */
  151. #include <pthread.h>
  152. #define UNLOCKED_STREAM(RETURNTYPE,NAME,PARAMS,ARGS,STREAM) \
  153. RETURNTYPE NAME PARAMS \
  154. { \
  155. RETURNTYPE retval; \
  156. __STDIO_THREADLOCK(STREAM); \
  157. retval = NAME##_unlocked ARGS ; \
  158. __STDIO_THREADUNLOCK(STREAM); \
  159. return retval; \
  160. } \
  161. RETURNTYPE NAME##_unlocked PARAMS
  162. #define UNLOCKED(RETURNTYPE,NAME,PARAMS,ARGS) \
  163. UNLOCKED_STREAM(RETURNTYPE,NAME,PARAMS,ARGS,stream)
  164. #define UNLOCKED_VOID_RETURN(NAME,PARAMS,ARGS) \
  165. void NAME PARAMS \
  166. { \
  167. __STDIO_THREADLOCK(stream); \
  168. NAME##_unlocked ARGS ; \
  169. __STDIO_THREADUNLOCK(stream); \
  170. } \
  171. void NAME##_unlocked PARAMS
  172. #define __STDIO_THREADLOCK_OPENLIST \
  173. pthread_mutex_lock(&_stdio_openlist_lock)
  174. #define __STDIO_THREADUNLOCK_OPENLIST \
  175. pthread_mutex_unlock(&_stdio_openlist_lock)
  176. #define __STDIO_THREADTRYLOCK_OPENLIST \
  177. pthread_mutex_trylock(&_stdio_openlist_lock)
  178. #endif /* __STDIO_THREADSAFE */
  179. /**********************************************************************/
  180. #ifdef __STDIO_WIDE
  181. #define __STDIO_FILE_INIT_UNGOT { 0, 0 }, { 0, 0 },
  182. #else
  183. #define __STDIO_FILE_INIT_UNGOT { 0, 0 },
  184. #endif
  185. #ifdef __STDIO_GETC_MACRO
  186. #define __STDIO_FILE_INIT_BUFGETC(x) x,
  187. #else
  188. #define __STDIO_FILE_INIT_BUFGETC(x)
  189. #endif
  190. #ifdef __STDIO_PUTC_MACRO
  191. #define __STDIO_FILE_INIT_BUFPUTC(x) x,
  192. #else
  193. #define __STDIO_FILE_INIT_BUFPUTC(x)
  194. #endif
  195. #if defined(__STDIO_BUFFERS) || defined(__STDIO_GLIBC_CUSTOM_STREAMS)
  196. #define __STDIO_FILE_INIT_NEXT(next) (next),
  197. #else /* defined(__STDIO_BUFFERS) || defined(__STDIO_GLIBC_CUSTOM_STREAMS) */
  198. #define __STDIO_FILE_INIT_NEXT(next)
  199. #endif /* defined(__STDIO_BUFFERS) || defined(__STDIO_GLIBC_CUSTOM_STREAMS) */
  200. #ifdef __STDIO_BUFFERS
  201. #define __STDIO_FILE_INIT_BUFFERS(buf,bufsize) \
  202. (buf), (buf)+(bufsize), (buf), (buf),
  203. #else
  204. #define __STDIO_FILE_INIT_BUFFERS(buf,bufsize)
  205. #endif
  206. #ifdef __STDIO_GLIBC_CUSTOM_STREAMS
  207. #define __STDIO_FILE_INIT_CUSTOM_STREAM(stream) \
  208. &((stream).filedes), { _cs_read, _cs_write, NULL, _cs_close },
  209. #else
  210. #define __STDIO_FILE_INIT_CUSTOM_STREAM(stream)
  211. #endif
  212. #ifdef __STDIO_MBSTATE
  213. #define __STDIO_FILE_INIT_MBSTATE \
  214. { 0, 0 },
  215. #else
  216. #define __STDIO_FILE_INIT_MBSTATE
  217. #endif
  218. #ifdef __STDIO_THREADSAFE
  219. #define __STDIO_FILE_INIT_THREADSAFE \
  220. 0, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP,
  221. #else
  222. #define __STDIO_FILE_INIT_THREADSAFE
  223. #endif
  224. #define __STDIO_INIT_FILE_STRUCT(stream, flags, filedes, next, buf, bufsize) \
  225. { (flags), \
  226. __STDIO_FILE_INIT_UNGOT \
  227. (filedes), \
  228. __STDIO_FILE_INIT_NEXT(next) \
  229. __STDIO_FILE_INIT_BUFFERS(buf,bufsize) \
  230. __STDIO_FILE_INIT_BUFGETC((buf)) \
  231. __STDIO_FILE_INIT_BUFPUTC((buf)) \
  232. __STDIO_FILE_INIT_CUSTOM_STREAM(stream) \
  233. __STDIO_FILE_INIT_MBSTATE \
  234. __STDIO_FILE_INIT_THREADSAFE \
  235. } /* TODO: mbstate and builtin buf */
  236. #ifdef __STDIO_GLIBC_CUSTOM_STREAMS
  237. /* TODO -- what does glibc do for undefined funcs? errno set? */
  238. #define __READ(STREAMPTR,BUF,SIZE) \
  239. ((((STREAMPTR)->gcs.read) == NULL) ? -1 : \
  240. (((STREAMPTR)->gcs.read)((STREAMPTR)->cookie,(BUF),(SIZE))))
  241. #define __WRITE(STREAMPTR,BUF,SIZE) \
  242. ((((STREAMPTR)->gcs.write) == NULL) ? -1 : \
  243. (((STREAMPTR)->gcs.write)((STREAMPTR)->cookie,(BUF),(SIZE))))
  244. #define __CLOSE(STREAMPTR) \
  245. ((((STREAMPTR)->gcs.close) == NULL) ? 0 : \
  246. (((STREAMPTR)->gcs.close)((STREAMPTR)->cookie)))
  247. #else /* __STDIO_GLIBC_CUSTOM_STREAMS */
  248. #define __READ(STREAMPTR,BUF,SIZE) \
  249. (read((STREAMPTR)->filedes,(BUF),(SIZE)))
  250. #define __WRITE(STREAMPTR,BUF,SIZE) \
  251. (write((STREAMPTR)->filedes,(BUF),(SIZE)))
  252. #define __CLOSE(STREAMPTR) \
  253. (close((STREAMPTR)->filedes))
  254. #endif /* __STDIO_GLIBC_CUSTOM_STREAMS */
  255. /**********************************************************************/
  256. /* POSIX functions */
  257. /**********************************************************************/
  258. #ifdef L_getw
  259. /* SUSv2 Legacy function -- need not be reentrant. */
  260. int getw(FILE *stream)
  261. {
  262. int aw[1];
  263. #ifdef __STDIO_WIDE
  264. return (fread_unlocked((void *)aw, sizeof(int), 1, stream) > 0)
  265. ? (*aw) : EOF;
  266. #else /* __STDIO_WIDE */
  267. return (_stdio_fread((unsigned char *)(aw), sizeof(int), stream)
  268. == sizeof(int)) ? (*aw) : EOF;
  269. #endif /* __STDIO_WIDE */
  270. }
  271. #endif
  272. /**********************************************************************/
  273. #ifdef L_putw
  274. /* SUSv2 Legacy function -- need not be reentrant. */
  275. int putw(int w, FILE *stream)
  276. {
  277. int aw[1];
  278. *aw = w; /* In case 'w' is in a register... */
  279. #ifdef __STDIO_WIDE
  280. return (fwrite_unlocked((void *)aw, sizeof(int), 1, stream) == 1)
  281. ? 0 : EOF;
  282. #else /* __STDIO_WIDE */
  283. return (_stdio_fwrite((unsigned char *)aw, sizeof(int), stream)
  284. == sizeof(int)) ? 0 : EOF;
  285. #endif /* __STDIO_WIDE */
  286. }
  287. #endif
  288. /**********************************************************************/
  289. #ifdef L_fileno
  290. /* Reentrancy handled by UNLOCKED() macro. */
  291. UNLOCKED(int,fileno,(register FILE *stream),(stream))
  292. {
  293. #ifdef __STDIO_GLIBC_CUSTOM_STREAMS
  294. return ( (stream && (stream->cookie == &(stream->filedes)) && (stream->filedes >= 0))
  295. ? stream->filedes
  296. : (__set_errno(EBADF), -1) );
  297. #else /* __STDIO_GLIBC_CUSTOM_STREAMS */
  298. return ((stream && stream->filedes >= 0)) ? stream->filedes : (__set_errno(EBADF), -1);
  299. #endif /* __STDIO_GLIBC_CUSTOM_STREAMS */
  300. }
  301. #endif
  302. /**********************************************************************/
  303. #ifdef L_fdopen
  304. /* No reentrancy issues. */
  305. FILE *fdopen(int filedes, const char *mode)
  306. {
  307. register char *cur_mode; /* TODO -- use intptr_t?? (also fopencookie) */
  308. return (((int)(cur_mode = (char *) fcntl(filedes, F_GETFL))) != -1)
  309. ? _stdio_fopen(cur_mode, mode, NULL, filedes)
  310. : NULL;
  311. }
  312. #endif
  313. /**********************************************************************/
  314. #ifdef L_fopen64
  315. /* No reentrancy issues. */
  316. FILE *fopen64(const char * __restrict filename, const char * __restrict mode)
  317. {
  318. return _stdio_fopen(filename, mode, NULL, -2);
  319. }
  320. #endif
  321. /**********************************************************************/
  322. #ifdef L_ctermid_function
  323. /* Not required to be reentrant. */
  324. char *ctermid(register char *s)
  325. {
  326. static char sbuf[L_ctermid];
  327. #ifdef __BCC__
  328. /* Currently elks doesn't support /dev/tty. */
  329. if (!s) {
  330. s = sbuf;
  331. }
  332. *s = 0;
  333. return s;
  334. #else
  335. /* glibc always returns /dev/tty for linux. */
  336. return strcpy((s ? s : sbuf), "/dev/tty");
  337. #endif
  338. }
  339. #endif
  340. /**********************************************************************/
  341. /* BSD functions */
  342. /**********************************************************************/
  343. #ifdef L_setbuffer
  344. /* No reentrancy issues. */
  345. void setbuffer(FILE * __restrict stream, register char * __restrict buf,
  346. size_t size)
  347. {
  348. #ifdef __STDIO_BUFFERS
  349. setvbuf(stream, buf, (buf ? _IOFBF : _IONBF), size);
  350. #else /* __STDIO_BUFFERS */
  351. /* Nothing to do. */
  352. #endif /* __STDIO_BUFFERS */
  353. }
  354. #endif
  355. /**********************************************************************/
  356. #ifdef L_setlinebuf
  357. /* No reentrancy issues. */
  358. void setlinebuf(FILE * __restrict stream)
  359. {
  360. #ifdef __STDIO_BUFFERS
  361. setvbuf(stream, NULL, _IOLBF, (size_t) 0);
  362. #else /* __STDIO_BUFFERS */
  363. /* Nothing to do. */
  364. #endif /* __STDIO_BUFFERS */
  365. }
  366. #endif
  367. /**********************************************************************/
  368. /* GLIBC functions */
  369. /**********************************************************************/
  370. #ifdef L_fcloseall
  371. /* NOTE: GLIBC difference!!! -- fcloseall
  372. * According to the info pages, glibc actually fclose()s all open files.
  373. * Apparently, glibc's new version only fflush()s and unbuffers all
  374. * writing streams to cope with unordered destruction of c++ static
  375. * objects. Here we implement the old behavior as default.
  376. */
  377. /* Not reentrant. */
  378. int fcloseall (void)
  379. {
  380. #if defined(__STDIO_BUFFERS) || defined(__STDIO_GLIBC_CUSTOM_STREAMS)
  381. register FILE *stream;
  382. int rv;
  383. _stdio_term(); /* Let _stdio_term() do all the work. */
  384. rv = 0;
  385. for (stream = _stdio_openlist ; stream ; stream = stream->nextopen) {
  386. if (stream->modeflags & (__FLAG_WRITING|__FLAG_ERROR)) {
  387. /* TODO -- is this correct? Maybe ferror set before flush...
  388. * could check if pending writable but what if term unbuffers?
  389. * in that case, could clear error flag... */
  390. rv = EOF; /* Only care about failed writes. */
  391. }
  392. }
  393. /* Make sure _stdio_term() does nothing on exit. */
  394. _stdio_openlist = NULL;
  395. return rv;
  396. #else /* defined(__STDIO_BUFFERS) || defined(__STDIO_GLIBC_CUSTOM_STREAMS) */
  397. return 0;
  398. #endif /* defined(__STDIO_BUFFERS) || defined(__STDIO_GLIBC_CUSTOM_STREAMS) */
  399. }
  400. #endif
  401. /**********************************************************************/
  402. #ifdef L_fmemopen
  403. #ifdef __STDIO_GLIBC_CUSTOM_STREAMS
  404. typedef struct {
  405. size_t pos;
  406. size_t len;
  407. size_t eof;
  408. int dynbuf;
  409. unsigned char *buf;
  410. FILE *fp;
  411. } __fmo_cookie;
  412. #define COOKIE ((__fmo_cookie *) cookie)
  413. static ssize_t fmo_read(register void *cookie, char *buf, size_t bufsize)
  414. {
  415. size_t count = COOKIE->len - COOKIE->pos;
  416. /* Note: 0 < bufsize < SSIZE_MAX because of _stdio_READ. */
  417. if (!count) { /* EOF! */
  418. return 0;
  419. }
  420. if (bufsize > count) {
  421. bufsize = count;
  422. }
  423. #if 1 /* TODO - choose code option */
  424. memcpy(buf, COOKIE->buf + COOKIE->pos, bufsize);
  425. COOKIE->pos += bufsize;
  426. #else
  427. {
  428. register char *p = COOKIE->buf + COOKIE->pos;
  429. count = bufsize;
  430. while (count) {
  431. *buf++ = *p++;
  432. --count;
  433. }
  434. COOKIE->pos += bufsize;
  435. }
  436. #endif
  437. return bufsize;
  438. }
  439. static ssize_t fmo_write(register void *cookie, const char *buf, size_t bufsize)
  440. {
  441. size_t count;
  442. /* Note: bufsize < SSIZE_MAX because of _stdio_WRITE. */
  443. /* If appending, need to seek to end of file!!!! */
  444. if (COOKIE->fp->modeflags & __FLAG_APPEND) {
  445. COOKIE->pos = COOKIE->eof;
  446. }
  447. count = COOKIE->len - COOKIE->pos;
  448. if (bufsize > count) {
  449. bufsize = count;
  450. if (count == 0) { /* We're at the end of the buffer... */
  451. __set_errno(EFBIG);
  452. return -1;
  453. }
  454. }
  455. #if 1 /* TODO - choose code option */
  456. memcpy(COOKIE->buf + COOKIE->pos, buf, bufsize);
  457. COOKIE->pos += bufsize;
  458. if (COOKIE->pos > COOKIE->eof) {
  459. COOKIE->eof = COOKIE->pos;
  460. if (bufsize < count) { /* New eof and still room in buffer? */
  461. *(COOKIE->buf + COOKIE->pos) = 0;
  462. }
  463. }
  464. #else
  465. {
  466. register char *p = COOKIE->buf + COOKIE->pos;
  467. size_t i = bufsize;
  468. while (i > 0) {
  469. *p++ = *buf++;
  470. --i;
  471. }
  472. COOKIE->pos += bufsize;
  473. if (COOKIE->pos > COOKIE->eof) {
  474. COOKIE->eof = COOKIE->pos;
  475. if (bufsize < count) { /* New eof and still room in buffer? */
  476. *p = 0;
  477. }
  478. }
  479. }
  480. #endif
  481. return bufsize;
  482. }
  483. /* glibc doesn't allow seeking, but it has in-buffer seeks... we don't. */
  484. static int fmo_seek(register void *cookie, __offmax_t *pos, int whence)
  485. {
  486. __offmax_t p = *pos;
  487. /* Note: fseek already checks that whence is legal, so don't check here
  488. * unless debugging. */
  489. assert(((unsigned int) whence) <= 2);
  490. if (whence != SEEK_SET) {
  491. p += (whence == SEEK_CUR) ? COOKIE->pos : /* SEEK_END */ COOKIE->eof;
  492. }
  493. /* Note: glibc only allows seeking in the buffer. We'll actually restrict
  494. * to the data. */
  495. /* Check for offset < 0, offset > eof, or offset overflow... */
  496. if (((uintmax_t) p) > COOKIE->eof) {
  497. return -1;
  498. }
  499. COOKIE->pos = *pos = p;
  500. return 0;
  501. }
  502. static int fmo_close(register void *cookie)
  503. {
  504. if (COOKIE->dynbuf) {
  505. free(COOKIE->buf);
  506. }
  507. free(cookie);
  508. return 0;
  509. }
  510. #undef COOKIE
  511. static const cookie_io_functions_t _fmo_io_funcs = {
  512. fmo_read, fmo_write, fmo_seek, fmo_close
  513. };
  514. /* TODO: If we have buffers enabled, it might be worthwile to add a pointer
  515. * to the FILE in the cookie and have read, write, and seek operate directly
  516. * on the buffer itself (ie replace the FILE buffer with the cookie buffer
  517. * and update FILE bufstart, etc. whenever we seek). */
  518. FILE *fmemopen(void *s, size_t len, const char *modes)
  519. {
  520. FILE *fp;
  521. register __fmo_cookie *cookie;
  522. size_t i;
  523. if ((cookie = malloc(sizeof(__fmo_cookie))) != NULL) {
  524. cookie->len = len;
  525. cookie->eof = cookie->pos = 0; /* pos and eof adjusted below. */
  526. cookie->dynbuf = 0;
  527. if (((cookie->buf = s) == NULL) && (len > 0)) {
  528. if ((cookie->buf = malloc(len)) == NULL) {
  529. goto EXIT_cookie;
  530. }
  531. cookie->dynbuf = 1;
  532. *cookie->buf = 0; /* If we're appending, treat as empty file. */
  533. }
  534. #ifndef __BCC__
  535. fp = fopencookie(cookie, modes, _fmo_io_funcs);
  536. #else
  537. fp = fopencookie(cookie, modes, &_fmo_io_funcs);
  538. #endif
  539. /* Note: We don't need to worry about locking fp in the thread case
  540. * as the only possible access would be a close or flush with
  541. * nothing currently in the FILE's write buffer. */
  542. if (fp != NULL) {
  543. cookie->fp = fp;
  544. if (fp->modeflags & __FLAG_READONLY) {
  545. cookie->eof = len;
  546. }
  547. if ((fp->modeflags & __FLAG_APPEND) && (len > 0)) {
  548. for (i = 0 ; i < len ; i++) {
  549. if (cookie->buf[i] == 0) {
  550. break;
  551. }
  552. }
  553. cookie->eof = cookie->pos = i; /* Adjust eof and pos. */
  554. }
  555. return fp;
  556. }
  557. }
  558. if (!s) {
  559. free(cookie->buf);
  560. }
  561. EXIT_cookie:
  562. free(cookie);
  563. return NULL;
  564. }
  565. #endif /* __STDIO_GLIBC_CUSTOM_STREAMS */
  566. #endif
  567. /**********************************************************************/
  568. #ifdef L_open_memstream
  569. #ifdef __STDIO_GLIBC_CUSTOM_STREAMS
  570. #define COOKIE ((__oms_cookie *) cookie)
  571. typedef struct {
  572. char *buf;
  573. size_t len;
  574. size_t pos;
  575. size_t eof;
  576. char **bufloc;
  577. size_t *sizeloc;
  578. } __oms_cookie;
  579. /* Nothing to do here, as memstreams are write-only. */
  580. /* static ssize_t oms_read(void *cookie, char *buf, size_t bufsize) */
  581. /* { */
  582. /* } */
  583. static ssize_t oms_write(register void *cookie, const char *buf, size_t bufsize)
  584. {
  585. register char *newbuf;
  586. size_t count;
  587. /* Note: we already know bufsize < SSIZE_MAX... */
  588. count = COOKIE->len - COOKIE->pos - 1;
  589. assert(COOKIE->pos < COOKIE->len); /* Always nul-terminate! */
  590. if (bufsize > count) {
  591. newbuf = realloc(COOKIE->buf, COOKIE->len + bufsize - count);
  592. if (newbuf) {
  593. *COOKIE->bufloc = COOKIE->buf = newbuf;
  594. COOKIE->len += (bufsize - count);
  595. } else {
  596. bufsize = count;
  597. if (count == 0) {
  598. __set_errno(EFBIG); /* TODO: check glibc errno setting... */
  599. return -1;
  600. }
  601. }
  602. }
  603. memcpy(COOKIE->buf + COOKIE->pos, buf, bufsize);
  604. COOKIE->pos += bufsize;
  605. if (COOKIE->pos > COOKIE->eof) {
  606. *COOKIE->sizeloc = COOKIE->eof = COOKIE->pos;
  607. COOKIE->buf[COOKIE->eof] = 0; /* Need to nul-terminate. */
  608. }
  609. return bufsize;
  610. }
  611. static int oms_seek(register void *cookie, __offmax_t *pos, int whence)
  612. {
  613. __offmax_t p = *pos;
  614. register char *buf;
  615. size_t leastlen;
  616. /* Note: fseek already checks that whence is legal, so don't check here
  617. * unless debugging. */
  618. assert(((unsigned int) whence) <= 2);
  619. if (whence != SEEK_SET) {
  620. p += (whence == SEEK_CUR) ? COOKIE->pos : /* SEEK_END */ COOKIE->eof;
  621. }
  622. /* Note: glibc only allows seeking in the buffer. We'll actually restrict
  623. * to the data. */
  624. /* Check for offset < 0, offset >= too big (need nul), or overflow... */
  625. if (((uintmax_t) p) >= SIZE_MAX - 1) {
  626. return -1;
  627. }
  628. leastlen = ((size_t) p) + 1; /* New pos + 1 for nul if necessary. */
  629. if (leastlen >= COOKIE->len) { /* Need to grow buffer... */
  630. buf = realloc(COOKIE->buf, leastlen);
  631. if (buf) {
  632. *COOKIE->bufloc = COOKIE->buf = buf;
  633. COOKIE->len = leastlen;
  634. memset(buf + COOKIE->eof, leastlen - COOKIE->eof, 0); /* 0-fill */
  635. } else {
  636. /* TODO: check glibc errno setting... */
  637. return -1;
  638. }
  639. }
  640. *pos = COOKIE->pos = --leastlen;
  641. if (leastlen > COOKIE->eof) {
  642. memset(COOKIE->buf + COOKIE->eof, leastlen - COOKIE->eof, 0);
  643. *COOKIE->sizeloc = COOKIE->eof;
  644. }
  645. return 0;
  646. }
  647. static int oms_close(void *cookie)
  648. {
  649. free(cookie);
  650. return 0;
  651. }
  652. #undef COOKIE
  653. static const cookie_io_functions_t _oms_io_funcs = {
  654. NULL, oms_write, oms_seek, oms_close
  655. };
  656. /* TODO: If we have buffers enabled, it might be worthwile to add a pointer
  657. * to the FILE in the cookie and operate directly on the buffer itself
  658. * (ie replace the FILE buffer with the cookie buffer and update FILE bufstart,
  659. * etc. whenever we seek). */
  660. FILE *open_memstream(char **__restrict bufloc, size_t *__restrict sizeloc)
  661. {
  662. register __oms_cookie *cookie;
  663. register FILE *fp;
  664. if ((cookie = malloc(sizeof(__oms_cookie))) != NULL) {
  665. if ((cookie->buf = malloc(cookie->len = BUFSIZ)) == NULL) {
  666. goto EXIT_cookie;
  667. }
  668. *cookie->buf = 0; /* Set nul terminator for buffer. */
  669. *(cookie->bufloc = bufloc) = cookie->buf;
  670. *(cookie->sizeloc = sizeloc) = cookie->eof = cookie->pos = 0;
  671. #ifndef __BCC__
  672. fp = fopencookie(cookie, "w", _oms_io_funcs);
  673. #else
  674. fp = fopencookie(cookie, "w", &_oms_io_funcs);
  675. #endif
  676. /* Note: We don't need to worry about locking fp in the thread case
  677. * as the only possible access would be a close or flush with
  678. * nothing currently in the FILE's write buffer. */
  679. if (fp != NULL) {
  680. return fp;
  681. }
  682. }
  683. if (cookie->buf != NULL) {
  684. free(cookie->buf);
  685. }
  686. EXIT_cookie:
  687. free(cookie);
  688. return NULL;
  689. }
  690. #endif /* __STDIO_GLIBC_CUSTOM_STREAMS */
  691. #endif
  692. /**********************************************************************/
  693. #ifdef L_fopencookie
  694. #ifdef __STDIO_GLIBC_CUSTOM_STREAMS
  695. /* NOTE: GLIBC difference!!! -- fopencookie
  696. * According to the info pages, glibc allows seeking within buffers even if
  697. * no seek function is supplied. We don't. */
  698. /* NOTE: GLIBC difference!!! -- fopencookie
  699. * When compiled without large file support, the offset pointer for the
  700. * cookie_seek function is off_t * and not off64_t * as for glibc. */
  701. /* Currently no real reentrancy issues other than a possible double close(). */
  702. #ifndef __BCC__
  703. FILE *fopencookie(void * __restrict cookie, const char * __restrict mode,
  704. cookie_io_functions_t io_functions)
  705. {
  706. FILE *stream;
  707. /* Fake an fdopen guaranteed to pass the _stdio_fopen basic agreement
  708. * check without an fcntl call. */
  709. if ((stream = _stdio_fopen(((char *)(INT_MAX-1)),
  710. mode, NULL, INT_MAX)) /* TODO: use intptr_t? */
  711. != NULL
  712. ) {
  713. stream->filedes = -1;
  714. stream->gcs = io_functions;
  715. stream->cookie = cookie;
  716. }
  717. #if !defined(__STDIO_BUFFERS) && !defined(__STDIO_THREADSAFE)
  718. /* I we don't have buffers or threads, we only need to worry about
  719. * custom streams on the open list, as no flushing is necessary and
  720. * no locking of possible underlying normal streams need be done.
  721. * We do need to explicitly close custom streams on termination of stdio,
  722. * and we need to lock the list as it can be modified by fclose(). */
  723. __STDIO_THREADLOCK_OPENLIST;
  724. stream->nextopen = _stdio_openlist; /* New files are inserted at */
  725. _stdio_openlist = stream; /* the head of the list. */
  726. __STDIO_THREADUNLOCK_OPENLIST;
  727. #endif /* !defined(__STDIO_BUFFERS) && !defined(__STDIO_THREADSAFE) */
  728. return stream;
  729. }
  730. #else /* __BCC__ */
  731. /* NOTE: GLIBC difference!!! -- fopencookie (bcc only)
  732. * Since bcc doesn't support passing of structs, we define fopencookie as a
  733. * macro in terms of _fopencookie which takes a struct * for the io functions
  734. * instead.
  735. */
  736. FILE *_fopencookie(void * __restrict cookie, const char * __restrict mode,
  737. register cookie_io_functions_t *io_functions)
  738. {
  739. register FILE *stream;
  740. /* Fake an fdopen guaranteed to pass the _stdio_fopen basic agreement
  741. * check without an fcntl call. */
  742. if ((stream = _stdio_fopen(((char *)(INT_MAX-1)),
  743. mode, NULL, INT_MAX)) /* TODO: use intptr_t? */
  744. != NULL
  745. ) {
  746. stream->filedes = -1;
  747. stream->gcs.read = io_functions->read;
  748. stream->gcs.write = io_functions->write;
  749. stream->gcs.seek = io_functions->seek;
  750. stream->gcs.close = io_functions->close;
  751. stream->cookie = cookie;
  752. }
  753. #if !defined(__STDIO_BUFFERS) && !defined(__STDIO_THREADSAFE)
  754. /* I we don't have buffers or threads, we only need to worry about
  755. * custom streams on the open list, as no flushing is necessary and
  756. * no locking of possible underlying normal streams need be done.
  757. * We do need to explicitly close custom streams on termination of stdio,
  758. * and we need to lock the list as it can be modified by fclose(). */
  759. __STDIO_THREADLOCK_OPENLIST;
  760. stream->nextopen = _stdio_openlist; /* New files are inserted at */
  761. _stdio_openlist = stream; /* the head of the list. */
  762. __STDIO_THREADUNLOCK_OPENLIST;
  763. #endif /* !defined(__STDIO_BUFFERS) && !defined(__STDIO_THREADSAFE) */
  764. return stream;
  765. }
  766. #endif /* __BCC__ */
  767. #endif /* __STDIO_GLIBC_CUSTOM_STREAMS */
  768. #endif
  769. /**********************************************************************/
  770. #ifdef L___fbufsize
  771. /* Not reentrant. */
  772. size_t __fbufsize(register FILE * __restrict stream)
  773. {
  774. #ifdef __STDIO_BUFFERS
  775. return (stream->modeflags & __FLAG_NBF)
  776. ? 0 : (stream->bufend - stream->bufstart);
  777. #else /* __STDIO_BUFFERS */
  778. return 0;
  779. #endif /* __STDIO_BUFFERS */
  780. }
  781. #endif
  782. /**********************************************************************/
  783. #ifdef L___freading
  784. /* No reentrancy issues. */
  785. int __freading(FILE * __restrict stream)
  786. {
  787. return stream->modeflags & (__FLAG_READING|__FLAG_READONLY);
  788. }
  789. #endif
  790. /**********************************************************************/
  791. #ifdef L___fwriting
  792. /* No reentrancy issues. */
  793. int __fwriting(FILE * __restrict stream)
  794. {
  795. return stream->modeflags & (__FLAG_WRITING|__FLAG_WRITEONLY);
  796. }
  797. #endif
  798. /**********************************************************************/
  799. #ifdef L___freadable
  800. /* No reentrancy issues. */
  801. int __freadable(FILE * __restrict stream)
  802. {
  803. return ~(stream->modeflags & __FLAG_WRITEONLY);
  804. }
  805. #endif
  806. /**********************************************************************/
  807. #ifdef L___fwritable
  808. /* No reentrancy issues. */
  809. int __fwritable(FILE * __restrict stream)
  810. {
  811. return ~(stream->modeflags & __FLAG_READONLY);
  812. }
  813. #endif
  814. /**********************************************************************/
  815. #ifdef L___flbf
  816. /* No reentrancy issues. */
  817. int __flbf(FILE * __restrict stream)
  818. {
  819. #ifdef __STDIO_BUFFERS
  820. return (stream->modeflags & __FLAG_LBF);
  821. #else /* __STDIO_BUFFERS */
  822. /* TODO -- Even though there is no buffer, return flag setting? */
  823. return __FLAG_NBF;
  824. #endif /* __STDIO_BUFFERS */
  825. }
  826. #endif
  827. /**********************************************************************/
  828. #ifdef L___fpurge
  829. /* Not reentrant. */
  830. void __fpurge(register FILE * __restrict stream)
  831. {
  832. #ifdef __STDIO_BUFFERS
  833. #ifdef __STDIO_PUTC_MACRO
  834. stream->bufputc = /* Must disable putc. */
  835. #endif /* __STDIO_PUTC_MACRO */
  836. #ifdef __STDIO_GETC_MACRO
  837. stream->bufgetc = /* Must disable getc. */
  838. #endif
  839. stream->bufpos = stream->bufread = stream->bufstart; /* Reset pointers. */
  840. #endif /* __STDIO_BUFFERS */
  841. /* Reset r/w flags and clear ungots. */
  842. stream->modeflags &= ~(__FLAG_READING|__FLAG_WRITING|__MASK_UNGOT);
  843. }
  844. #endif
  845. /**********************************************************************/
  846. #ifdef L___fpending
  847. /* Not reentrant. */
  848. #ifdef __STDIO_WIDE
  849. #warning Unlike the glibc version, this __fpending returns bytes in buffer for wide streams too!
  850. link_warning(__fpending, "This version of __fpending returns bytes remaining in buffer for both narrow and wide streams. glibc's version returns wide chars in buffer for the wide stream case.")
  851. #endif /* __STDIO_WIDE */
  852. size_t __fpending(register FILE * __restrict stream)
  853. {
  854. #ifdef __STDIO_BUFFERS
  855. /* TODO -- should we check this? should we set errno? just assert? */
  856. return (stream->modeflags & (__FLAG_READING|__FLAG_READONLY))
  857. ? 0 : (stream->bufpos - stream->bufstart);
  858. #else /* __STDIO_BUFFERS */
  859. return 0;
  860. #endif /* __STDIO_BUFFERS */
  861. }
  862. #endif
  863. /**********************************************************************/
  864. #ifdef L__flushlbf
  865. /* No reentrancy issues. */
  866. void _flushlbf(void)
  867. {
  868. #if defined(__STDIO_BUFFERS) || defined(__STDIO_GLIBC_CUSTOM_STREAMS)
  869. fflush((FILE *) &_stdio_openlist); /* Uses an implementation hack!!! */
  870. #else /* defined(__STDIO_BUFFERS) || defined(__STDIO_GLIBC_CUSTOM_STREAMS) */
  871. /* Nothing to do. */
  872. #endif /* defined(__STDIO_BUFFERS) || defined(__STDIO_GLIBC_CUSTOM_STREAMS) */
  873. }
  874. #endif
  875. /**********************************************************************/
  876. #ifdef L___fsetlocking
  877. /* NOT threadsafe!!! (I don't think glibc's is either)
  878. *
  879. * This interacts badly with internal locking/unlocking. If you use this routine,
  880. * make sure the file isn't being accessed by any other threads. Typical use would
  881. * be to change to user locking immediately after opening the stream.
  882. */
  883. #ifdef __UCLIBC_MJN3_ONLY__
  884. link_warning(__fsetlocking, "Oddly enough, __fsetlocking() is NOT threadsafe.")
  885. #endif
  886. int __fsetlocking(FILE *stream, int locking_mode)
  887. {
  888. #ifdef __STDIO_THREADSAFE
  889. int old_mode;
  890. #endif
  891. assert((FSETLOCKING_QUERY == 0) && (FSETLOCKING_INTERNAL == 1)
  892. && (FSETLOCKING_BYCALLER == 2));
  893. assert(((unsigned int) locking_mode) <= 2);
  894. #ifdef __STDIO_THREADSAFE
  895. old_mode = stream->user_locking;
  896. assert(((unsigned int) old_mode) <= 1); /* Must be 0 (internal) or 1 (user). */
  897. if (locking_mode != FSETLOCKING_QUERY) {
  898. /* In case we're not debugging, treat any unknown as a request to
  899. * set internal locking, in order to match glibc behavior. */
  900. stream->user_locking = (locking_mode == FSETLOCKING_BYCALLER);
  901. }
  902. return 2 - old_mode;
  903. #else
  904. return FSETLOCKING_BYCALLER; /* Well, without thread support... */
  905. #endif
  906. }
  907. #endif
  908. /**********************************************************************/
  909. #ifdef L_flockfile
  910. void flockfile(FILE *stream)
  911. {
  912. #ifdef __STDIO_THREADSAFE
  913. pthread_mutex_lock(&stream->lock);
  914. #endif
  915. }
  916. #endif
  917. /**********************************************************************/
  918. #ifdef L_ftrylockfile
  919. int ftrylockfile(FILE *stream)
  920. {
  921. #ifdef __STDIO_THREADSAFE
  922. return pthread_mutex_trylock(&stream->lock);
  923. #else
  924. return 1;
  925. #endif
  926. }
  927. #endif
  928. /**********************************************************************/
  929. #ifdef L_funlockfile
  930. void funlockfile(FILE *stream)
  931. {
  932. #ifdef __STDIO_THREADSAFE
  933. pthread_mutex_unlock(&stream->lock);
  934. #endif
  935. }
  936. #endif
  937. /**********************************************************************/
  938. #ifdef L_getline
  939. ssize_t getline(char **__restrict lineptr, size_t *__restrict n,
  940. FILE *__restrict stream)
  941. {
  942. return __getdelim(lineptr, n, '\n', stream);
  943. }
  944. #endif
  945. /**********************************************************************/
  946. #ifdef L_getdelim
  947. weak_alias(__getdelim,getdelim);
  948. #define GETDELIM_GROWBY 64
  949. ssize_t __getdelim(char **__restrict lineptr, size_t *__restrict n,
  950. int delimiter, register FILE *__restrict stream)
  951. {
  952. register char *buf;
  953. size_t pos;
  954. int c;
  955. if (!lineptr || !n || !stream) { /* Be compatable with glibc... even */
  956. __set_errno(EINVAL); /* though I think we should assert here */
  957. return -1; /* if anything. */
  958. }
  959. if (!(buf = *lineptr)) { /* If passed NULL for buffer, */
  960. *n = 0; /* ignore value passed and treat size as 0. */
  961. }
  962. pos = 1; /* Make sure we have space for terminating nul. */
  963. __STDIO_THREADLOCK(stream);
  964. do {
  965. if (pos >= *n) {
  966. if (!(buf = realloc(buf, *n + GETDELIM_GROWBY))) {
  967. __set_errno(ENOMEM); /* Emulate old uClibc implementation. */
  968. break;
  969. }
  970. *n += GETDELIM_GROWBY;
  971. *lineptr = buf;
  972. }
  973. } while (((c = (getc_unlocked)(stream)) != EOF) /* Disable the macro */
  974. && ((buf[pos++ - 1] = c) != delimiter));
  975. __STDIO_THREADUNLOCK(stream);
  976. if (--pos) {
  977. buf[pos] = 0;
  978. return pos;
  979. }
  980. return -1; /* Either initial realloc failed or first read was EOF. */
  981. }
  982. #endif
  983. /**********************************************************************/
  984. /* my extension functions */
  985. /**********************************************************************/
  986. #ifdef L__stdio_fsfopen
  987. /*
  988. * Stack|Static File open -- open a file where the FILE is either
  989. * stack or staticly allocated.
  990. */
  991. /* No reentrancy issues. */
  992. FILE *_stdio_fsfopen(const char * __restrict filename,
  993. const char * __restrict mode,
  994. register FILE * __restrict stream)
  995. {
  996. #ifdef __STDIO_BUFFERS
  997. stream->modeflags = __FLAG_FBF;
  998. #if __STDIO_BUILTIN_BUF_SIZE > 0
  999. stream->bufstart = stream->builtinbuf;
  1000. stream->bufend = stream->builtinbuf + sizeof(stream->builtinbuf);
  1001. #else /* __STDIO_BUILTIN_BUF_SIZE > 0 */
  1002. stream->bufend = stream->bufstart = NULL;
  1003. #endif /* __STDIO_BUILTIN_BUF_SIZE > 0 */
  1004. #endif /* __STDIO_BUFFERS */
  1005. return _stdio_fopen(filename, mode, stream, -1);
  1006. }
  1007. #endif
  1008. /**********************************************************************/
  1009. /* stdio internal functions */
  1010. /**********************************************************************/
  1011. #ifdef L__stdio_adjpos
  1012. /* According to the ANSI/ISO C99 definition of ungetwc()
  1013. * For a text or binary stream, the value of its file position indicator
  1014. * after a successful call to the ungetwc function is unspecified until
  1015. * all pushed­back wide characters are read or discarded.
  1016. * Note however, that this applies only to _user_ calls to ungetwc. We
  1017. * need to allow for internal calls by scanf.
  1018. * So we store the byte count
  1019. * of the first ungot wide char in ungot_width. If it is 0 (user case)
  1020. * then the file position is treated as unknown.
  1021. */
  1022. /* Internal function -- not reentrant. */
  1023. int _stdio_adjpos(register FILE * __restrict stream, register __offmax_t *pos)
  1024. {
  1025. __offmax_t r;
  1026. int cor = stream->modeflags & __MASK_UNGOT; /* handle ungots */
  1027. assert(cor <= 2);
  1028. #ifdef __STDIO_WIDE
  1029. /* Assumed narrow stream so correct if wide. */
  1030. if (cor && (stream->modeflags & __FLAG_WIDE)) {
  1031. if ((((stream->modeflags & __MASK_UNGOT) > 1) || stream->ungot[1])) {
  1032. return -1; /* App did ungetwc, so position is indeterminate. */
  1033. }
  1034. if (stream->modeflags & __MASK_UNGOT) {
  1035. cor = cor - 1 + stream->ungot_width[1];
  1036. }
  1037. if (stream->state.mask > 0) { /* Incomplete character (possible bad) */
  1038. cor -= stream->ungot_width[0];
  1039. }
  1040. }
  1041. #endif /* __STDIO_WIDE */
  1042. #ifdef __STDIO_BUFFERS
  1043. if (stream->modeflags & __FLAG_WRITING) {
  1044. cor -= (stream->bufpos - stream->bufstart); /* pending writes */
  1045. }
  1046. if (stream->modeflags & __FLAG_READING) {
  1047. cor += (stream->bufread - stream->bufpos); /* extra's read */
  1048. }
  1049. #endif /* __STDIO_BUFFERS */
  1050. r = *pos;
  1051. return ((*pos -= cor) > r) ? -cor : cor;
  1052. }
  1053. #endif
  1054. /**********************************************************************/
  1055. #ifdef L__stdio_lseek
  1056. /*
  1057. * This function is only called by fseek and ftell.
  1058. * fseek -- doesn't care about pos val, just success or failure.
  1059. * ftell -- needs pos val but offset == 0 and whence == SET_CUR.
  1060. */
  1061. /* Internal function -- not reentrant. */
  1062. int _stdio_lseek(register FILE *stream, register __offmax_t *pos, int whence)
  1063. {
  1064. __offmax_t res;
  1065. #ifdef __STDIO_GLIBC_CUSTOM_STREAMS
  1066. if (stream->cookie != &stream->filedes) {
  1067. return (((stream->gcs.seek == NULL)
  1068. || ((stream->gcs.seek)(stream->cookie, pos, whence) < 0))
  1069. ? -1 : 0);
  1070. }
  1071. #endif /* __STDIO_GLIBC_CUSTOM_STREAMS */
  1072. #ifdef __STDIO_LARGE_FILES
  1073. res = lseek64(stream->filedes, *pos, whence);
  1074. #else
  1075. res = lseek(stream->filedes, *pos, whence);
  1076. #endif /* __STDIO_LARGE_FILES */
  1077. return (res >= 0) ? ((*pos = res), 0) : -1;
  1078. }
  1079. #endif
  1080. /**********************************************************************/
  1081. #ifdef L__stdio_fread
  1082. /*
  1083. * NOTE!!! This routine is meant to be callable by both narrow and wide
  1084. * functions. However, if called by a wide function, there must be
  1085. * NO pending ungetwc()s!!!
  1086. */
  1087. /* Unlike write, it's ok for read to return fewer than bufsize, since
  1088. * we may not need all of them. */
  1089. static ssize_t _stdio_READ(register FILE *stream, unsigned char *buf, size_t bufsize)
  1090. {
  1091. ssize_t rv;
  1092. /* NOTE: C99 change: Input fails once the stream's EOF indicator is set. */
  1093. if ((bufsize == 0) || (stream->modeflags & __FLAG_EOF)) {
  1094. return 0;
  1095. }
  1096. if (bufsize > SSIZE_MAX) {
  1097. bufsize = SSIZE_MAX;
  1098. }
  1099. #ifdef __BCC__
  1100. TRY_READ:
  1101. #endif
  1102. rv = __READ(stream, buf, bufsize);
  1103. if (rv > 0) {
  1104. #ifdef __STDIO_GLIBC_CUSTOM_STREAMS
  1105. assert(rv <= bufsize); /* buggy user handler... TODO: check? */
  1106. if (rv > bufsize) { /* Num reported written > number requested */
  1107. rv = bufsize; /* Treat as a full read??? */
  1108. }
  1109. #endif
  1110. } else if (rv == 0) {
  1111. stream->modeflags |= __FLAG_EOF;
  1112. } else {
  1113. #ifdef __BCC__
  1114. if (errno == EINTR) {
  1115. goto TRY_READ;
  1116. }
  1117. #endif
  1118. stream->modeflags |= __FLAG_ERROR;
  1119. rv = 0;
  1120. }
  1121. return rv;
  1122. }
  1123. /* Internal function -- not reentrant. */
  1124. size_t _stdio_fread(unsigned char *buffer, size_t bytes, register FILE *stream)
  1125. {
  1126. __stdio_validate_FILE(stream); /* debugging only */
  1127. #ifdef __STDIO_BUFFERS
  1128. if (stream->modeflags
  1129. #ifdef __STDIO_AUTO_RW_TRANSITION
  1130. & (__FLAG_WRITEONLY)
  1131. #else /* __STDIO_AUTO_RW_TRANSITION */
  1132. /* ANSI/ISO and SUSv3 require not currently writing. */
  1133. & (__FLAG_WRITEONLY|__FLAG_WRITING)
  1134. #endif /* __STDIO_AUTO_RW_TRANSITION */
  1135. ) {
  1136. #ifdef __STDIO_PUTC_MACRO
  1137. stream->bufputc = stream->bufstart; /* Must disable putc. */
  1138. #endif /* __STDIO_PUTC_MACRO */
  1139. stream->modeflags |= __FLAG_ERROR;
  1140. /* TODO: This is for posix behavior if writeonly. To save space, we
  1141. * use this errno for read attempt while writing, as no errno is
  1142. * specified by posix for this case, even though the restriction is
  1143. * mentioned in fopen(). */
  1144. __set_errno(EBADF);
  1145. return 0;
  1146. }
  1147. /* We need to disable putc and getc macros in case of error */
  1148. #if defined(__STDIO_PUTC_MACRO) || defined(__STDIO_GETC_MACRO)
  1149. #ifdef __STDIO_PUTC_MACRO
  1150. stream->bufputc =
  1151. #endif /* __STDIO_GETC_MACRO */
  1152. #ifdef __STDIO_GETC_MACRO
  1153. stream->bufgetc =
  1154. #endif /* __STDIO_GETC_MACRO */
  1155. stream->bufstart;
  1156. #endif /* defined(__STDIO_PUTC_MACRO) || defined(__STDIO_GETC_MACRO) */
  1157. if (stream->modeflags & __MASK_BUFMODE) {
  1158. /* If the stream is readable and not fully buffered, we must first
  1159. * flush all line buffered output streams. Do this before the
  1160. * error check as this may be a read/write line-buffered stream.
  1161. * Note: Uses an implementation-specific hack!!! */
  1162. fflush_unlocked((FILE *) &_stdio_openlist);
  1163. }
  1164. #ifdef __STDIO_AUTO_RW_TRANSITION
  1165. if ((stream->modeflags & __FLAG_WRITING)
  1166. && (fflush_unlocked(stream) == EOF)
  1167. ) {
  1168. return 0; /* Fail if we need to fflush but can't. */
  1169. }
  1170. #endif /* __STDIO_AUTO_RW_TRANSITION */
  1171. stream->modeflags |= __FLAG_READING; /* Make sure Reading flag is set. */
  1172. {
  1173. register unsigned char *p = (unsigned char *) buffer;
  1174. /* First, grab appropriate ungetc() chars. NOT FOR WIDE ORIENTATED! */
  1175. while (bytes && (stream->modeflags & __MASK_UNGOT)) {
  1176. #ifdef __STDIO_WIDE
  1177. assert(stream->modeflags & __FLAG_NARROW);
  1178. #endif /* __STDIO_WIDE */
  1179. *p++ = stream->ungot[(--stream->modeflags) & __MASK_UNGOT];
  1180. stream->ungot[1] = 0;
  1181. --bytes;
  1182. }
  1183. /* Now get any other needed chars from the buffer or the file. */
  1184. FROM_BUF:
  1185. while (bytes && (stream->bufpos < stream->bufread)) {
  1186. --bytes;
  1187. *p++ = *stream->bufpos++;
  1188. }
  1189. if (bytes > 0) {
  1190. ssize_t len;
  1191. if (stream->filedes == -2) {
  1192. stream->modeflags |= __FLAG_EOF;
  1193. goto DONE;
  1194. }
  1195. /* The buffer is exhausted, but we still need chars. */
  1196. stream->bufpos = stream->bufread = stream->bufstart;
  1197. if (bytes <= stream->bufend - stream->bufread) {
  1198. /* We have sufficient space in the buffer. */
  1199. len = _stdio_READ(stream, stream->bufread,
  1200. stream->bufend - stream->bufread);
  1201. if (len > 0) {
  1202. stream->bufread += len;
  1203. goto FROM_BUF;
  1204. }
  1205. } else {
  1206. /* More bytes needed than fit in the buffer, so read */
  1207. /* directly into caller's buffer. */
  1208. len = _stdio_READ(stream, p, bytes);
  1209. if (len > 0) {
  1210. p += len;
  1211. bytes -= len;
  1212. goto FROM_BUF; /* Redundant work, but stops extra read. */
  1213. }
  1214. }
  1215. }
  1216. #ifdef __STDIO_GETC_MACRO
  1217. if (!(stream->modeflags
  1218. & (__FLAG_WIDE|__MASK_UNGOT|__MASK_BUFMODE|__FLAG_ERROR))
  1219. ) {
  1220. stream->bufgetc = stream->bufread; /* Enable getc macro. */
  1221. }
  1222. #endif
  1223. DONE:
  1224. __stdio_validate_FILE(stream); /* debugging only */
  1225. return (p - (unsigned char *)buffer);
  1226. }
  1227. #else /* __STDIO_BUFFERS --------------------------------------- */
  1228. if (stream->modeflags
  1229. #ifdef __STDIO_AUTO_RW_TRANSITION
  1230. & (__FLAG_WRITEONLY)
  1231. #else /* __STDIO_AUTO_RW_TRANSITION */
  1232. /* ANSI/ISO and SUSv3 require not currently writing. */
  1233. & (__FLAG_WRITEONLY|__FLAG_WRITING)
  1234. #endif /* __STDIO_AUTO_RW_TRANSITION */
  1235. ) {
  1236. stream->modeflags |= __FLAG_ERROR;
  1237. /* TODO: This is for posix behavior if writeonly. To save space, we
  1238. * use this errno for read attempt while writing, as no errno is
  1239. * specified by posix for this case, even though the restriction is
  1240. * mentioned in fopen(). */
  1241. __set_errno(EBADF);
  1242. return 0;
  1243. }
  1244. #ifdef __STDIO_AUTO_RW_TRANSITION
  1245. stream->modeflags &= ~(__FLAG_WRITING); /* Make sure Writing flag clear. */
  1246. #endif /* __STDIO_AUTO_RW_TRANSITION */
  1247. stream->modeflags |= __FLAG_READING; /* Make sure Reading flag is set. */
  1248. {
  1249. register unsigned char *p = (unsigned char *) buffer;
  1250. /* First, grab appropriate ungetc() chars. NOT FOR WIDE ORIENTATED! */
  1251. while (bytes && (stream->modeflags & __MASK_UNGOT)) {
  1252. #ifdef __STDIO_WIDE
  1253. assert(stream->modeflags & __FLAG_NARROW);
  1254. #endif /* __STDIO_WIDE */
  1255. *p++ = stream->ungot[(--stream->modeflags) & __MASK_UNGOT];
  1256. stream->ungot[1] = 0;
  1257. --bytes;
  1258. }
  1259. while (bytes > 0) {
  1260. ssize_t len = _stdio_READ(stream, p, (unsigned) bytes);
  1261. if (len == 0) {
  1262. break;
  1263. }
  1264. p += len;
  1265. bytes -= len;
  1266. }
  1267. __stdio_validate_FILE(stream); /* debugging only */
  1268. return (p - (unsigned char *)buffer);
  1269. }
  1270. #endif /* __STDIO_BUFFERS */
  1271. }
  1272. #endif
  1273. /**********************************************************************/
  1274. #ifdef L__stdio_fwrite
  1275. /*
  1276. * If buffer == NULL, attempt to fflush and return number of chars
  1277. * remaining in buffer (0 if successful fflush).
  1278. */
  1279. /* WARNING!!!! Current standards say that termination due to an asyncronous
  1280. * signal may not result in stdio streams being flushed. This libary makes
  1281. * an effort to do so but there is no way, short of blocking signals for
  1282. * each _stdio_fwrite call, that we can maintain the correct state if a
  1283. * signal is recieved mid-call. So any stream in mid-_stdio_fwrite could
  1284. * not some flush data or even duplicate-flush some data. It is possible
  1285. * to avoid the duplicate-flush case by setting/clearing the stream
  1286. * error flag before/after the write process, but it doesn't seem worth
  1287. * the trouble. */
  1288. /* Like standard write, but always does a full write unless error, plus
  1289. * deals correctly with bufsize > SSIZE_MAX... not much on an issue on linux
  1290. * but definitly could be on Elks. Also on Elks, always loops for EINTR..
  1291. * Returns number of bytes written, so a short write indicates an error */
  1292. static size_t _stdio_WRITE(register FILE *stream,
  1293. register const unsigned char *buf, size_t bufsize)
  1294. {
  1295. size_t todo;
  1296. ssize_t rv, stodo;
  1297. todo = bufsize;
  1298. while (todo) {
  1299. stodo = (todo <= SSIZE_MAX) ? todo : SSIZE_MAX;
  1300. rv = __WRITE(stream, buf, stodo);
  1301. if (rv >= 0) {
  1302. #ifdef __STDIO_GLIBC_CUSTOM_STREAMS
  1303. assert(rv <= stodo); /* buggy user handler... TODO: check? */
  1304. if (rv > stodo) { /* Num reported written > number requested */
  1305. rv = stodo; /* Treat as a full write??? */
  1306. }
  1307. #endif
  1308. todo -= rv;
  1309. buf += rv;
  1310. } else
  1311. #ifdef __BCC__
  1312. if (errno != EINTR)
  1313. #endif
  1314. {
  1315. stream->modeflags |= __FLAG_ERROR;
  1316. break;
  1317. }
  1318. }
  1319. return bufsize - todo;
  1320. }
  1321. /* Internal function -- not reentrant. */
  1322. size_t _stdio_fwrite(const unsigned char *buffer, size_t bytes,
  1323. register FILE *stream)
  1324. {
  1325. #ifdef __STDIO_BUFFERS
  1326. register const unsigned char *p;
  1327. __stdio_validate_FILE(stream); /* debugging only */
  1328. if ((stream->modeflags & __FLAG_READONLY)
  1329. #ifndef __STDIO_AUTO_RW_TRANSITION
  1330. /* ANSI/ISO requires either at EOF or currently not reading. */
  1331. || ((stream->modeflags & (__FLAG_READING|__FLAG_EOF))
  1332. == __FLAG_READING)
  1333. #endif /* __STDIO_AUTO_RW_TRANSITION */
  1334. ) {
  1335. stream->modeflags |= __FLAG_ERROR;
  1336. /* TODO: This is for posix behavior if readonly. To save space, we
  1337. * use this errno for write attempt while reading, as no errno is
  1338. * specified by posix for this case, even though the restriction is
  1339. * mentioned in fopen(). */
  1340. __set_errno(EBADF);
  1341. return 0;
  1342. }
  1343. #ifdef __STDIO_AUTO_RW_TRANSITION
  1344. /* If reading, deal with ungots and read-buffered chars. */
  1345. if (stream->modeflags & __FLAG_READING) {
  1346. if (((stream->bufpos < stream->bufread)
  1347. || (stream->modeflags & __MASK_UNGOT))
  1348. /* If appending, we might as well seek to end to save a seek. */
  1349. /* TODO: set EOF in fseek when appropriate? */
  1350. && fseek(stream, 0L,
  1351. ((stream->modeflags & __FLAG_APPEND)
  1352. ? SEEK_END : SEEK_CUR))
  1353. ) {
  1354. /* Note: This differs from glibc's apparent behavior of
  1355. not setting the error flag and discarding the buffered
  1356. read data. */
  1357. stream->modeflags |= __FLAG_ERROR; /* fseek may not set this. */
  1358. return 0; /* Fail if we need to fseek but can't. */
  1359. }
  1360. /* Always reset even if fseek called (saves a test). */
  1361. #ifdef __STDIO_GETC_MACRO
  1362. stream->bufgetc =
  1363. #endif /* __STDIO_GETC_MACRO */
  1364. stream->bufpos = stream->bufread = stream->bufstart;
  1365. } else
  1366. #endif
  1367. if ((stream->modeflags & (__FLAG_WRITING|__FLAG_APPEND)) == __FLAG_APPEND) {
  1368. /* Append mode, but not currently writing. Need to seek to end for proper
  1369. * ftell() return values. Don't worry if the stream isn't seekable. */
  1370. __offmax_t pos[1];
  1371. *pos = 0;
  1372. if (_stdio_lseek(stream, pos, SEEK_END) && (errno != EPIPE)) { /* Too big? */
  1373. stream->modeflags |= __FLAG_ERROR;
  1374. return 0;
  1375. }
  1376. }
  1377. #ifdef __STDIO_PUTC_MACRO
  1378. /* We need to disable putc macro in case of error */
  1379. stream->bufputc = stream->bufstart;
  1380. #endif /* __STDIO_GETC_MACRO */
  1381. /* Clear both reading and writing flags. We need to clear the writing
  1382. * flag in case we're fflush()ing or in case of an error. */
  1383. stream->modeflags &= ~(__FLAG_READING|__FLAG_WRITING);
  1384. {
  1385. const unsigned char *buf0 = buffer;
  1386. size_t write_count = 1; /* 0 means a write failed */
  1387. if (!buffer) { /* fflush the stream */
  1388. FFLUSH:
  1389. {
  1390. size_t count = stream->bufpos - stream->bufstart;
  1391. p = stream->bufstart;
  1392. if (stream->filedes == -2) { /* TODO -- document this hack! */
  1393. stream->modeflags |= __FLAG_WRITING;
  1394. return (!buffer) ? 0 : ((buffer - buf0) + bytes);
  1395. }
  1396. {
  1397. write_count = _stdio_WRITE(stream, p, count);
  1398. p += write_count;
  1399. count -= write_count;
  1400. }
  1401. stream->bufpos = stream->bufstart;
  1402. while (count) {
  1403. *stream->bufpos++ = *p++;
  1404. --count;
  1405. }
  1406. if (!buffer) { /* fflush case... */
  1407. __stdio_validate_FILE(stream); /* debugging only */
  1408. return stream->bufpos - stream->bufstart;
  1409. }
  1410. }
  1411. }
  1412. #if 1
  1413. /* TODO: If the stream is buffered, we may be able to omit. */
  1414. if ((stream->bufpos == stream->bufstart) /* buf empty */
  1415. && (stream->bufend - stream->bufstart <= bytes) /* fills */
  1416. && (stream->filedes != -2)) { /* not strinf fake file */
  1417. /* so want to do a direct write of supplied buffer */
  1418. {
  1419. size_t rv = _stdio_WRITE(stream, buffer, bytes);
  1420. buffer += rv;
  1421. bytes -= rv;
  1422. }
  1423. } else
  1424. #endif
  1425. /* otherwise buffer not empty and/or data fits */
  1426. {
  1427. size_t count = stream->bufend - stream->bufpos;
  1428. p = buffer;
  1429. if (count > bytes) {
  1430. count = bytes;
  1431. }
  1432. bytes -= count;
  1433. while (count) {
  1434. *stream->bufpos++ = *buffer++;
  1435. --count;
  1436. }
  1437. if (write_count) { /* no write errors */
  1438. if (bytes) {
  1439. goto FFLUSH;
  1440. }
  1441. if (stream->modeflags & __FLAG_LBF) {
  1442. while (p < buffer) { /* check for newline. */
  1443. if (*p++ == '\n') {
  1444. goto FFLUSH;
  1445. }
  1446. }
  1447. }
  1448. }
  1449. }
  1450. #ifdef __STDIO_PUTC_MACRO
  1451. if (!(stream->modeflags & (__FLAG_WIDE|__MASK_BUFMODE|__FLAG_ERROR))) {
  1452. /* Not wide, no errors and fully buffered, so enable putc macro. */
  1453. stream->bufputc = stream->bufend;
  1454. }
  1455. #endif /* __STDIO_GETC_MACRO */
  1456. stream->modeflags |= __FLAG_WRITING; /* Ensure Writing flag is set. */
  1457. __stdio_validate_FILE(stream); /* debugging only */
  1458. return buffer - buf0;
  1459. }
  1460. #else /* __STDIO_BUFFERS --------------------------------------- */
  1461. __stdio_validate_FILE(stream); /* debugging only */
  1462. if ((stream->modeflags & __FLAG_READONLY)
  1463. #ifndef __STDIO_AUTO_RW_TRANSITION
  1464. /* ANSI/ISO requires either at EOF or currently not reading. */
  1465. || ((stream->modeflags & (__FLAG_READING|__FLAG_EOF))
  1466. == __FLAG_READING)
  1467. #endif /* __STDIO_AUTO_RW_TRANSITION */
  1468. ) {
  1469. stream->modeflags |= __FLAG_ERROR;
  1470. /* TODO: This is for posix behavior if readonly. To save space, we
  1471. * use this errno for write attempt while reading, as no errno is
  1472. * specified by posix for this case, even though the restriction is
  1473. * mentioned in fopen(). */
  1474. __set_errno(EBADF);
  1475. return 0;
  1476. }
  1477. /* We always clear the reading flag in case at EOF. */
  1478. stream->modeflags &= ~(__FLAG_READING);
  1479. /* Unlike the buffered case, we set the writing flag now since we don't
  1480. * need to do anything here for fflush(). */
  1481. stream->modeflags |= __FLAG_WRITING;
  1482. {
  1483. register unsigned char *p = (unsigned char *) buffer;
  1484. ssize_t rv = _stdio_WRITE(stream, p, bytes);
  1485. p += rv;
  1486. bytes -= rv;
  1487. __stdio_validate_FILE(stream); /* debugging only */
  1488. return (p - (unsigned char *)buffer);
  1489. }
  1490. #endif /* __STDIO_BUFFERS *****************************************/
  1491. }
  1492. #endif
  1493. /**********************************************************************/
  1494. #ifdef L__stdio_init
  1495. /* Internal functions -- _stdio_init() and __stdio_validate_FILE
  1496. * are not reentrant, but _stdio_term() is through fflush().
  1497. * Also, the _cs_{read|write|close} functions are not reentrant. */
  1498. #ifndef NDEBUG
  1499. void __stdio_validate_FILE(FILE *stream)
  1500. {
  1501. if (stream->filedes == -2) { /* fake FILE for sprintf, scanf, etc. */
  1502. return;
  1503. }
  1504. __STDIO_THREADLOCK(stream);
  1505. #ifdef __STDIO_BUFFERS
  1506. assert(stream->bufstart <= stream->bufread);
  1507. if (stream->modeflags & (__FLAG_READING)) {
  1508. assert(stream->bufpos <= stream->bufread);
  1509. }
  1510. assert(stream->bufread <= stream->bufend);
  1511. assert(stream->bufpos <= stream->bufend);
  1512. if ((stream->modeflags & __MASK_BUFMODE) == __FLAG_NBF) {
  1513. assert(stream->bufstart == stream->bufend);
  1514. }
  1515. assert((stream->modeflags & __MASK_BUFMODE) <= __FLAG_NBF);
  1516. #endif
  1517. #ifdef __STDIO_PUTC_MACRO
  1518. assert(stream->bufstart <= stream->bufputc);
  1519. assert(stream->bufputc <= stream->bufend);
  1520. if (stream->bufstart < stream->bufputc) {
  1521. assert(stream->bufputc == stream->bufend);
  1522. assert(stream->modeflags & (__FLAG_WRITING));
  1523. assert(!(stream->modeflags
  1524. & (__FLAG_WIDE|__MASK_BUFMODE|__MASK_UNGOT|__FLAG_READONLY))
  1525. );
  1526. }
  1527. #endif
  1528. #ifdef __STDIO_GETC_MACRO
  1529. assert(stream->bufstart <= stream->bufgetc);
  1530. assert(stream->bufgetc <= stream->bufread);
  1531. if (stream->bufstart < stream->bufgetc) {
  1532. assert(stream->modeflags & (__FLAG_READING));
  1533. assert(!(stream->modeflags
  1534. & (__FLAG_WIDE|__MASK_BUFMODE|__MASK_UNGOT|__FLAG_WRITEONLY))
  1535. );
  1536. }
  1537. #endif
  1538. assert((stream->modeflags & __MASK_UNGOT) != __MASK_UNGOT);
  1539. if (stream->modeflags & __MASK_UNGOT1) {
  1540. assert(stream->ungot[1] <= 1);
  1541. }
  1542. if (stream->modeflags & __MASK_UNGOT) {
  1543. assert(!(stream->modeflags & __FLAG_EOF));
  1544. }
  1545. assert((stream->modeflags & (__FLAG_READONLY|__FLAG_WRITEONLY))
  1546. != (__FLAG_READONLY|__FLAG_WRITEONLY));
  1547. /* TODO -- filepos? ungot_width? filedes? nextopen? */
  1548. __STDIO_THREADUNLOCK(stream);
  1549. }
  1550. #endif
  1551. #ifdef __STDIO_GLIBC_CUSTOM_STREAMS
  1552. ssize_t _cs_read(void *cookie, char *buf, size_t bufsize)
  1553. {
  1554. return read(*((int *) cookie), buf, bufsize);
  1555. }
  1556. ssize_t _cs_write(void *cookie, const char *buf, size_t bufsize)
  1557. {
  1558. return write(*((int *) cookie), (char *) buf, bufsize);
  1559. }
  1560. int _cs_close(void *cookie)
  1561. {
  1562. return close(*((int *) cookie));
  1563. }
  1564. #endif /* __STDIO_GLIBC_CUSTOM_STREAMS */
  1565. #ifdef __STDIO_BUFFERS
  1566. static unsigned char _fixed_buffers[2 * BUFSIZ];
  1567. #define bufin (_fixed_buffers)
  1568. #define bufout (_fixed_buffers + BUFSIZ)
  1569. #endif /* __STDIO_BUFFERS */
  1570. static FILE _stdio_streams[] = {
  1571. __STDIO_INIT_FILE_STRUCT(_stdio_streams[0], __FLAG_LBF|__FLAG_READONLY, \
  1572. 0, _stdio_streams + 1, bufin, BUFSIZ ),
  1573. __STDIO_INIT_FILE_STRUCT(_stdio_streams[1], __FLAG_LBF|__FLAG_WRITEONLY, \
  1574. 1, _stdio_streams + 2, bufout, BUFSIZ ),
  1575. __STDIO_INIT_FILE_STRUCT(_stdio_streams[2], __FLAG_NBF|__FLAG_WRITEONLY, \
  1576. 2, 0, 0, 0 )
  1577. };
  1578. FILE *stdin = _stdio_streams + 0;
  1579. FILE *stdout = _stdio_streams + 1;
  1580. FILE *stderr = _stdio_streams + 2;
  1581. #if defined(__STDIO_BUFFERS) || defined(__STDIO_GLIBC_CUSTOM_STREAMS)
  1582. #ifdef __STDIO_BUFFERS
  1583. FILE *_stdio_openlist = _stdio_streams;
  1584. #else /* __STDIO_BUFFERS */
  1585. FILE *_stdio_openlist = NULL;
  1586. #endif /* __STDIO_BUFFERS */
  1587. #ifdef __STDIO_THREADSAFE
  1588. pthread_mutex_t _stdio_openlist_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
  1589. void __stdio_init_mutex(pthread_mutex_t *m)
  1590. {
  1591. static const pthread_mutex_t __stdio_mutex_initializer
  1592. = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
  1593. memcpy(m, &__stdio_mutex_initializer, sizeof(__stdio_mutex_initializer));
  1594. }
  1595. #endif /* __STDIO_THREADSAFE */
  1596. /* TODO - do we need to lock things, or do we just assume we're the only
  1597. * remaining thread? */
  1598. /* Note: We assume here that we are the only remaining thread. */
  1599. void _stdio_term(void)
  1600. {
  1601. #if defined(__STDIO_GLIBC_CUSTOM_STREAMS) || defined(__STDIO_THREADSAFE)
  1602. register FILE *ptr;
  1603. #endif
  1604. /* TODO: if called via a signal handler for a signal mid _stdio_fwrite,
  1605. * the stream may be in an unstable state... what do we do?
  1606. * perhaps set error flag before and clear when done if successful? */
  1607. #if defined(__STDIO_BUFFERS) || defined(__STDIO_GLIBC_CUSTOM_STREAMS)
  1608. #ifdef __STDIO_THREADSAFE
  1609. /* First, forceably unlock the open file list and all files.
  1610. * Note: Set locking mode to "by caller" to save some overhead later. */
  1611. __stdio_init_mutex(&_stdio_openlist_lock);
  1612. for (ptr = _stdio_openlist ; ptr ; ptr = ptr->nextopen ) {
  1613. ptr->user_locking = 1;
  1614. __stdio_init_mutex(&ptr->lock);
  1615. }
  1616. #endif /* __STDIO_THREADSAFE */
  1617. #endif /* defined(__STDIO_BUFFERS) || defined(__STDIO_GLIBC_CUSTOM_STREAMS) */
  1618. #ifdef __STDIO_BUFFERS
  1619. /* TODO -- set an alarm and flush each file "by hand"? to avoid blocking? */
  1620. /* Now flush all streams. */
  1621. fflush_unlocked(NULL);
  1622. #endif /* __STDIO_BUFFERS */
  1623. /* Next close all custom streams in case of any special cleanup, but
  1624. * don't use fclose() because that pulls in free and malloc. Also,
  1625. * don't worry about removing them from the list. Just set the cookie
  1626. * pointer to NULL so that an error will be generated if someone tries
  1627. * to use the stream. */
  1628. #ifdef __STDIO_GLIBC_CUSTOM_STREAMS
  1629. for (ptr = _stdio_openlist ; ptr ; ptr = ptr->nextopen ) {
  1630. if (ptr->cookie != &ptr->filedes) { /* custom stream */
  1631. __CLOSE(ptr);
  1632. ptr->cookie = NULL; /* Generate an error if used later. */
  1633. #if 0
  1634. /* #ifdef __STDIO_BUFFERS */
  1635. } else {
  1636. /* TODO: "unbuffer" files like glibc does? Inconsistent with
  1637. * custom stream handling above, but that's necessary to deal
  1638. * with special user-defined close behavior. */
  1639. stream->bufpos = stream->bufread = stream->bufend
  1640. #ifdef __STDIO_GETC_MACRO
  1641. = stream->bufgetc
  1642. #endif
  1643. #ifdef __STDIO_PUTC_MACRO
  1644. = stream->bufputc
  1645. #endif
  1646. = stream->bufstart;
  1647. #endif /* __STDIO_BUFFERS */
  1648. }
  1649. }
  1650. #endif /* __STDIO_GLIBC_CUSTOM_STREAMS */
  1651. }
  1652. #if defined(__STDIO_BUFFERS) || !defined(__UCLIBC__)
  1653. void _stdio_init(void)
  1654. {
  1655. #ifdef __STDIO_BUFFERS
  1656. int old_errno = errno;
  1657. /* stdin and stdout uses line buffering when connected to a tty. */
  1658. _stdio_streams[0].modeflags ^= (1-isatty(0)) * __FLAG_LBF;
  1659. _stdio_streams[1].modeflags ^= (1-isatty(1)) * __FLAG_LBF;
  1660. __set_errno(old_errno);
  1661. #endif /* __STDIO_BUFFERS */
  1662. #ifndef __UCLIBC__
  1663. /* __stdio_term is automatically when exiting if stdio is used.
  1664. * See misc/internals/__uClibc_main.c and and stdlib/atexit.c. */
  1665. atexit(_stdio_term);
  1666. #endif /* __UCLIBC__ */
  1667. }
  1668. #endif /* defined(__STDIO_BUFFERS) || !defined(__UCLIBC__) */
  1669. #endif /* defined(__STDIO_BUFFERS) || defined(__STDIO_GLIBC_CUSTOM_STREAMS) */
  1670. #endif
  1671. /**********************************************************************/
  1672. /* ANSI/ISO functions. */
  1673. /**********************************************************************/
  1674. #ifdef L_remove
  1675. #include <unistd.h>
  1676. #include <errno.h>
  1677. /* No reentrancy issues. */
  1678. int remove(register const char *filename)
  1679. {
  1680. int old_errno = errno;
  1681. /* SUSv3 says equivalent to rmdir() if a directory, and unlink()
  1682. * otherwise. Hence, we need to try rmdir() first. */
  1683. return (((rmdir(filename) == 0)
  1684. || ((errno == ENOTDIR)
  1685. && ((__set_errno(old_errno), unlink(filename)) == 0)))
  1686. ? 0 : -1);
  1687. }
  1688. #endif
  1689. /**********************************************************************/
  1690. /* rename is a syscall
  1691. #ifdef L_rename
  1692. int rename(const char *old, const char *new);
  1693. #endif
  1694. */
  1695. /**********************************************************************/
  1696. /* TODO: tmpfile */
  1697. /* #ifdef L_tmpfile */
  1698. /* FILE *tmpfile(void); */
  1699. /* #endif */
  1700. /**********************************************************************/
  1701. /* TODO: tmpname */
  1702. /* #ifdef L_tmpname */
  1703. /* char *tmpname(char *s); */
  1704. /* #endif */
  1705. /**********************************************************************/
  1706. #ifdef L_fclose
  1707. /* We need to be careful here to avoid deadlock when threading, as we
  1708. * need to lock both the file and the open file list. This can clash
  1709. * with fflush. Since fflush is much more common, we do the extra
  1710. * work here. */
  1711. int fclose(register FILE *stream)
  1712. {
  1713. #if defined(__STDIO_BUFFERS) || defined(__STDIO_GLIBC_CUSTOM_STREAMS)
  1714. register FILE *ptr;
  1715. int rv = 0;
  1716. #ifdef __STDIO_THREADSAFE
  1717. /* Need two non-heirchal mutexs... be careful to avoid deadlock*/
  1718. do {
  1719. __STDIO_THREADLOCK(stream);
  1720. if (__STDIO_THREADTRYLOCK_OPENLIST == 0) {
  1721. break;
  1722. }
  1723. __STDIO_THREADUNLOCK(stream);
  1724. usleep(10000);
  1725. } while (1);
  1726. #endif /* __STDIO_THREADSAFE */
  1727. __stdio_validate_FILE(stream); /* debugging only */
  1728. #ifdef __STDIO_BUFFERS
  1729. if (stream->modeflags & __FLAG_WRITING) {
  1730. rv = fflush_unlocked(stream); /* Write any pending buffered chars. */
  1731. } /* Also disables putc macro if used. */
  1732. #ifdef __STDIO_GETC_MACRO
  1733. /* Not necessary after fflush, but always do this to reduce size. */
  1734. stream->bufgetc = stream->bufstart; /* Disable getc macro for safety. */
  1735. #endif /* __STDIO_GETC_MACRO */
  1736. #endif /* __STDIO_BUFFERS */
  1737. /* Remove file from open list before closing file descriptor. */
  1738. ptr = _stdio_openlist;
  1739. if (ptr == stream) {
  1740. _stdio_openlist = stream->nextopen;
  1741. } else {
  1742. while (ptr) {
  1743. if (ptr->nextopen == stream) {
  1744. ptr->nextopen = stream->nextopen;
  1745. break;
  1746. }
  1747. ptr = ptr->nextopen;
  1748. }
  1749. }
  1750. __STDIO_THREADUNLOCK_OPENLIST; /* We're done with the open file list. */
  1751. if (__CLOSE(stream) < 0) { /* Must close even if fflush failed. */
  1752. rv = EOF;
  1753. }
  1754. #ifdef __STDIO_GLIBC_CUSTOM_STREAMS
  1755. stream->cookie = &(stream->filedes);
  1756. stream->gcs.read = _cs_read;
  1757. stream->gcs.write = _cs_write;
  1758. stream->gcs.seek = 0; /* The internal seek func handles normals. */
  1759. stream->gcs.close = _cs_close;
  1760. #endif
  1761. stream->filedes = -1; /* To aid debugging... */
  1762. #ifdef __STDIO_BUFFERS
  1763. if (stream->modeflags & __FLAG_FREEBUF) {
  1764. free(stream->bufstart);
  1765. }
  1766. #endif /* __STDIO_BUFFERS */
  1767. /* TODO -- leave the stream locked to catch any dangling refs? */
  1768. __STDIO_THREADUNLOCK(stream);
  1769. /* At this point, any dangling refs to the stream are the result of
  1770. * a programming bug... so free the unlocked stream. */
  1771. if (stream->modeflags & __FLAG_FREEFILE) {
  1772. #ifdef __STDIO_GLIBC_CUSTOM_STREAMS
  1773. stream->cookie = NULL; /* To aid debugging... */
  1774. #endif
  1775. free(stream);
  1776. }
  1777. return rv;
  1778. #else /* defined(__STDIO_BUFFERS) || defined(__STDIO_GLIBC_CUSTOM_STREAMS) */
  1779. int rv = 0;
  1780. __STDIO_THREADLOCK(stream);
  1781. __stdio_validate_FILE(stream); /* debugging only */
  1782. if (__CLOSE(stream) < 0) { /* Must close even if fflush failed. */
  1783. rv = EOF;
  1784. }
  1785. #ifdef __STDIO_GLIBC_CUSTOM_STREAMS
  1786. stream->cookie = &(stream->filedes);
  1787. stream->gcs.read = _cs_read;
  1788. stream->gcs.write = _cs_write;
  1789. stream->gcs.seek = 0; /* The internal seek func handles normals. */
  1790. stream->gcs.close = _cs_close;
  1791. #endif
  1792. stream->filedes = -1; /* To aid debugging... */
  1793. __STDIO_THREADUNLOCK(stream);
  1794. /* At this point, any dangling refs to the stream are the result of
  1795. * a programming bug... so free the unlocked stream. */
  1796. if (stream->modeflags & __FLAG_FREEFILE) {
  1797. #ifdef __STDIO_GLIBC_CUSTOM_STREAMS
  1798. stream->cookie = NULL; /* To aid debugging... */
  1799. #endif
  1800. free(stream);
  1801. }
  1802. return rv;
  1803. #endif /* defined(__STDIO_BUFFERS) || defined(__STDIO_GLIBC_CUSTOM_STREAMS )*/
  1804. }
  1805. #endif
  1806. /**********************************************************************/
  1807. #ifdef L_fflush
  1808. /*
  1809. * Special cases:
  1810. * stream == NULL means fflush all writing streams (ANSI/ISO).
  1811. * stream == (FILE *) &_stdio_openlist -- implementation-specific hack
  1812. * meaning fflush all line buffered writing streams
  1813. */
  1814. /*
  1815. * NOTE: ANSI/ISO difference!!! According to the standard, fflush is only
  1816. * defined for write-only streams, or read/write streams whose last op
  1817. * was a write. However, reading is allowed for a read/write stream if
  1818. * a file positioning operation was done (fseek, fsetpos) even though there
  1819. * is no guarantee of flushing the write data in that case. Hence, for
  1820. * this case we keep a flag to indicate whether or not the buffer needs to
  1821. * be flushed even if the last operation was a read. This falls under the
  1822. * implementation-defined behavior. Otherwise, we would need to flush
  1823. * every time we did fseek, etc. even if we were still in the buffer's range.
  1824. */
  1825. /* Since the stream pointer arg is allowed to be NULL, or the address of the
  1826. * stdio open file list if stdio is buffered in this implementation, we can't
  1827. * use the UNLOCKED() macro here. */
  1828. #ifndef __STDIO_THREADSAFE
  1829. strong_alias(fflush_unlocked,fflush)
  1830. #else /* __STDIO_THREADSAFE */
  1831. int fflush(register FILE *stream)
  1832. {
  1833. int retval;
  1834. if ((stream != NULL)
  1835. #if defined(__STDIO_BUFFERS) || defined(__STDIO_GLIBC_CUSTOM_STREAMS)
  1836. && (stream != (FILE *) &_stdio_openlist)
  1837. #endif /* defined(__STDIO_BUFFERS) || defined(__STDIO_GLIBC_CUSTOM_STREAMS) */
  1838. ) {
  1839. __STDIO_THREADLOCK(stream);
  1840. retval = fflush_unlocked(stream);
  1841. __STDIO_THREADUNLOCK(stream);
  1842. } else {
  1843. retval = fflush_unlocked(stream);
  1844. }
  1845. return retval;
  1846. }
  1847. #endif /* __STDIO_THREADSAFE */
  1848. int fflush_unlocked(register FILE *stream)
  1849. {
  1850. #ifdef __STDIO_BUFFERS
  1851. int rv = 0;
  1852. unsigned short mask = (__FLAG_NBF|__FLAG_LBF);
  1853. #ifndef NDEBUG
  1854. if ((stream != NULL) && (stream != (FILE *) &_stdio_openlist)) {
  1855. __stdio_validate_FILE(stream); /* debugging only */
  1856. }
  1857. #endif
  1858. if (stream == (FILE *) &_stdio_openlist) { /* fflush all line-buffered */
  1859. stream = NULL;
  1860. mask = __FLAG_LBF;
  1861. }
  1862. if (stream == NULL) { /* flush all (line) buffered writing streams */
  1863. /* Note -- We have to lock the list even in the unlocked function. */
  1864. __STDIO_THREADLOCK_OPENLIST;
  1865. /* TODO -- Can we work around locking the list to avoid keeping it
  1866. * locked if the write blocks? */
  1867. for (stream = _stdio_openlist; stream; stream = stream->nextopen) {
  1868. if (((stream->modeflags ^ __FLAG_NBF) & mask)
  1869. && (stream->modeflags & __FLAG_WRITING)
  1870. && fflush(stream)) {
  1871. rv = EOF;
  1872. }
  1873. }
  1874. __STDIO_THREADUNLOCK_OPENLIST;
  1875. } else if (stream->modeflags & __FLAG_WRITING) {
  1876. if (_stdio_fwrite(NULL, 0, stream) > 0) { /* flush buffer contents. */
  1877. rv = -1; /* Not all chars written. */
  1878. }
  1879. #ifdef __UCLIBC_MJN3_ONLY__
  1880. #warning WISHLIST: Add option to test for undefined behavior of fflush.
  1881. #endif /* __UCLIBC_MJN3_ONLY__ */
  1882. #if 0
  1883. } else if (stream->modeflags & (__FLAG_READING|__FLAG_READONLY)) {
  1884. /* ANSI/ISO says behavior in this case is undefined but also says you
  1885. * shouldn't flush a stream you were reading from. As usual, glibc
  1886. * caters to broken programs and simply ignores this. */
  1887. stream->modeflags |= __FLAG_ERROR;
  1888. __set_errno(EBADF);
  1889. rv = -1;
  1890. #endif
  1891. }
  1892. #ifndef NDEBUG
  1893. if ((stream != NULL) && (stream != (FILE *) &_stdio_openlist)) {
  1894. __stdio_validate_FILE(stream); /* debugging only */
  1895. }
  1896. #endif
  1897. return rv;
  1898. #else /* __STDIO_BUFFERS --------------------------------------- */
  1899. #ifndef NDEBUG
  1900. if ((stream != NULL)
  1901. #if defined(__STDIO_BUFFERS) || defined(__STDIO_GLIBC_CUSTOM_STREAMS)
  1902. && (stream != (FILE *) &_stdio_openlist)
  1903. #endif /* defined(__STDIO_BUFFERS) || defined(__STDIO_GLIBC_CUSTOM_STREAMS) */
  1904. ) {
  1905. __stdio_validate_FILE(stream); /* debugging only */
  1906. }
  1907. #endif
  1908. #ifdef __UCLIBC_MJN3_ONLY__
  1909. #warning WISHLIST: Add option to test for undefined behavior of fflush.
  1910. #endif /* __UCLIBC_MJN3_ONLY__ */
  1911. #if 0
  1912. return ((stream != NULL)
  1913. && (stream->modeflags & (__FLAG_READING|__FLAG_READONLY))
  1914. ? ((stream->modeflags |= __FLAG_ERROR), __set_errno(EBADF), EOF)
  1915. : 0 );
  1916. #else
  1917. return 0;
  1918. #endif
  1919. #endif /* __STDIO_BUFFERS */
  1920. }
  1921. #endif
  1922. /**********************************************************************/
  1923. #ifdef L_fopen
  1924. /* No reentrancy issues. */
  1925. FILE *fopen(const char * __restrict filename, const char * __restrict mode)
  1926. {
  1927. return _stdio_fopen(filename, mode, NULL, -1);
  1928. }
  1929. #endif
  1930. /**********************************************************************/
  1931. #ifdef L__stdio_fopen
  1932. /*
  1933. * Cases:
  1934. * fopen : filename != NULL, stream == NULL, filedes == -1
  1935. * freopen : filename != NULL, stream != NULL, filedes == -1
  1936. * fdopen : filename == NULL, stream == NULL, filedes valid
  1937. * fsfopen : filename != NULL, stream != NULL, filedes == -1
  1938. * fopen64 : filename != NULL, stream == NULL, filedes == -2
  1939. */
  1940. #if O_ACCMODE != 3 || O_RDONLY != 0 || O_WRONLY != 1 || O_RDWR != 2
  1941. #error Assumption violated - mode constants
  1942. #endif
  1943. /* Internal function -- reentrant (locks open file list) */
  1944. FILE *_stdio_fopen(const char * __restrict filename,
  1945. register const char * __restrict mode,
  1946. register FILE * __restrict stream, int filedes)
  1947. {
  1948. __mode_t open_mode;
  1949. /* parse mode */
  1950. open_mode = O_RDONLY;
  1951. if (*mode != 'r') { /* not read */
  1952. open_mode = (O_WRONLY | O_CREAT | O_TRUNC);
  1953. if (*mode != 'w') { /* not write (create or truncate)*/
  1954. open_mode = (O_WRONLY | O_CREAT | O_APPEND);
  1955. if (*mode != 'a') { /* not write (create or append) */
  1956. __set_errno(EINVAL); /* then illegal mode */
  1957. if (stream) { /* If this is freopen, free the stream. */
  1958. goto FREE_STREAM;
  1959. }
  1960. return NULL;
  1961. }
  1962. }
  1963. }
  1964. if ((*++mode == 'b')) { /* binary mode (NOP currently) */
  1965. ++mode;
  1966. }
  1967. if (*mode == '+') { /* read-write */
  1968. ++mode;
  1969. open_mode &= ~(O_RDONLY | O_WRONLY);
  1970. open_mode |= O_RDWR;
  1971. }
  1972. #if defined(__STDIO_FOPEN_EXCLUSIVE_MODE) || defined(__STDIO_FOPEN_LARGEFILE_MODE)
  1973. for ( ; *mode ; ++mode) { /* ignore everything else except ... */
  1974. #ifdef __STDIO_FOPEN_EXCLUSIVE_MODE
  1975. if (*mode == 'x') { /* open exclusive -- glibc extension */
  1976. open_mode |= O_EXCL;
  1977. continue;
  1978. }
  1979. #endif /* __STDIO_FOPEN_EXCLUSIVE_MODE */
  1980. #ifdef __STDIO_FOPEN_LARGEFILE_MODE
  1981. if (*mode == 'F') { /* open large file */
  1982. open_mode |= O_LARGEFILE;
  1983. continue;
  1984. }
  1985. #endif /* __STDIO_FOPEN_LARGEFILE_MODE */
  1986. }
  1987. #endif /* __STDIO_FOPEN_EXCLUSIVE_MODE or __STDIO_FOPEN_LARGEFILE_MODE def'd */
  1988. #ifdef __BCC__
  1989. mode = filename; /* TODO: help BCC with register allocation. */
  1990. #define filename mode
  1991. #endif /* __BCC__ */
  1992. if (!stream) { /* Need to allocate a FILE. */
  1993. #ifdef __STDIO_BUFFERS
  1994. if ((stream = malloc(sizeof(FILE))) == NULL) {
  1995. return stream;
  1996. }
  1997. stream->modeflags = __FLAG_FREEFILE;
  1998. if ((stream->bufstart = malloc(BUFSIZ)) != 0) {
  1999. stream->bufend = stream->bufstart + BUFSIZ;
  2000. stream->modeflags |= __FLAG_FREEBUF;
  2001. } else {
  2002. #if __STDIO_BUILTIN_BUF_SIZE > 0
  2003. stream->bufstart = stream->unbuf;
  2004. stream->bufend = stream->unbuf + sizeof(stream->unbuf);
  2005. #else /* __STDIO_BUILTIN_BUF_SIZE > 0 */
  2006. stream->bufstart = stream->bufend = NULL;
  2007. #endif /* __STDIO_BUILTIN_BUF_SIZE > 0 */
  2008. }
  2009. #else /* __STDIO_BUFFERS */
  2010. if ((stream = malloc(sizeof(FILE))) == NULL) {
  2011. return stream;
  2012. }
  2013. stream->modeflags = __FLAG_FREEFILE;
  2014. #endif /* __STDIO_BUFFERS */
  2015. }
  2016. if (filedes >= 0) { /* Handle fdopen trickery. */
  2017. /* NOTE: it is insufficient to just check R/W/RW agreement.
  2018. * We must also check largefile compatibility if applicable.
  2019. * Also, if append mode is desired for fdopen but O_APPEND isn't
  2020. * currently set, then set it as recommended by SUSv3. However,
  2021. * if append mode is not specified for fdopen but O_APPEND is set,
  2022. * leave it set (glibc compat). */
  2023. int i = (open_mode & (O_ACCMODE|O_LARGEFILE)) + 1;
  2024. /* NOTE: fopencookie needs changing if the basic check changes! */
  2025. if (((i & (((int) filename) + 1)) != i) /* Check basic agreement. */
  2026. || (((open_mode & O_APPEND)
  2027. && !(((int) filename) & O_APPEND)
  2028. && fcntl(filedes, F_SETFL, O_APPEND))) /* Need O_APPEND. */
  2029. ) {
  2030. __set_errno(EINVAL);
  2031. filedes = -1;
  2032. }
  2033. #ifdef __STDIO_LARGE_FILES
  2034. /* For later... to reflect largefile setting in stream flags. */
  2035. open_mode |= (((int) filename) & O_LARGEFILE);
  2036. #endif /* __STDIO_LARGE_FILES */
  2037. stream->filedes = filedes;
  2038. } else {
  2039. #ifdef __STDIO_LARGE_FILES
  2040. if (filedes < -1) {
  2041. open_mode |= O_LARGEFILE;
  2042. }
  2043. #endif /* __STDIO_LARGE_FILES */
  2044. stream->filedes = open(filename, open_mode, 0666);
  2045. }
  2046. if (stream->filedes < 0) {
  2047. FREE_STREAM:
  2048. #ifdef __STDIO_BUFFERS
  2049. if (stream->modeflags & __FLAG_FREEBUF) {
  2050. free(stream->bufstart);
  2051. }
  2052. #endif /* __STDIO_BUFFERS */
  2053. if (stream->modeflags & __FLAG_FREEFILE) {
  2054. free(stream);
  2055. }
  2056. return NULL;
  2057. }
  2058. #ifdef __STDIO_BUFFERS
  2059. {
  2060. /* Do not let isatty mess up errno */
  2061. int old_errno = errno;
  2062. stream->modeflags |= (isatty(stream->filedes) * __FLAG_LBF);
  2063. __set_errno(old_errno);
  2064. }
  2065. #endif
  2066. stream->modeflags |=
  2067. #if (O_APPEND == __FLAG_APPEND) \
  2068. && ((O_LARGEFILE == __FLAG_LARGEFILE) || (O_LARGEFILE == 0))
  2069. (open_mode & (O_APPEND|O_LARGEFILE)) | /* i386 linux and elks */
  2070. #else /* (O_APPEND == __FLAG_APPEND) && (O_LARGEFILE == __FLAG_LARGEFILE) */
  2071. ((open_mode & O_APPEND) ? __FLAG_APPEND : 0) |
  2072. #ifdef __STDIO_LARGE_FILES
  2073. ((open_mode & O_LARGEFILE) ? __FLAG_LARGEFILE : 0) |
  2074. #endif /* __STDIO_LARGE_FILES */
  2075. #endif /* (O_APPEND == __FLAG_APPEND) && (O_LARGEFILE == __FLAG_LARGEFILE) */
  2076. ((((open_mode & O_ACCMODE) + 1) ^ 0x03) * __FLAG_WRITEONLY);
  2077. #ifdef __STDIO_BUFFERS
  2078. #ifdef __STDIO_GETC_MACRO
  2079. stream->bufgetc =
  2080. #endif
  2081. #ifdef __STDIO_PUTC_MACRO
  2082. stream->bufputc =
  2083. #endif
  2084. stream->bufpos = stream->bufread = stream->bufstart;
  2085. #endif /* __STDIO_BUFFERS */
  2086. #ifdef __STDIO_GLIBC_CUSTOM_STREAMS
  2087. stream->cookie = &(stream->filedes);
  2088. stream->gcs.read = _cs_read;
  2089. stream->gcs.write = _cs_write;
  2090. stream->gcs.seek = 0; /* The internal seek func handles normals. */
  2091. stream->gcs.close = _cs_close;
  2092. #endif /* __STDIO_GLIBC_CUSTOM_STREAMS */
  2093. #ifdef __STDIO_WIDE
  2094. stream->ungot_width[0] = 0;
  2095. #endif /* __STDIO_WIDE */
  2096. #ifdef __STDIO_MBSTATE
  2097. __INIT_MBSTATE(&(stream->state));
  2098. #endif /* __STDIO_MBSTATE */
  2099. #ifdef __STDIO_THREADSAFE
  2100. stream->user_locking = 0;
  2101. __stdio_init_mutex(&stream->lock);
  2102. #endif /* __STDIO_THREADSAFE */
  2103. #if defined(__STDIO_BUFFERS) \
  2104. || (defined(__STDIO_THREADSAFE) && defined(__STDIO_GLIBC_CUSTOM_STREAMS))
  2105. __STDIO_THREADLOCK_OPENLIST;
  2106. stream->nextopen = _stdio_openlist; /* New files are inserted at */
  2107. _stdio_openlist = stream; /* the head of the list. */
  2108. __STDIO_THREADUNLOCK_OPENLIST;
  2109. #endif
  2110. __stdio_validate_FILE(stream); /* debugging only */
  2111. return stream;
  2112. #ifdef __BCC__
  2113. #undef filename
  2114. #endif /* __BCC__ */
  2115. }
  2116. #endif
  2117. /**********************************************************************/
  2118. #ifdef L_freopen
  2119. /* Reentrant. */
  2120. FILE *freopen(const char * __restrict filename, const char * __restrict mode,
  2121. register FILE * __restrict stream)
  2122. {
  2123. /*
  2124. * ANSI/ISO allow (implementation-defined) change of mode for an
  2125. * existing file if filename is NULL. It doesn't look like Linux
  2126. * supports this, so we don't here.
  2127. *
  2128. * NOTE: Whether or not the stream is free'd on failure is unclear
  2129. * w.r.t. ANSI/ISO. This implementation chooses to free the
  2130. * stream and associated buffer if they were dynamically
  2131. * allocated.
  2132. * TODO: Check the above.
  2133. * TODO: Apparently linux allows setting append mode. Implement?
  2134. */
  2135. unsigned short dynmode;
  2136. register FILE *fp;
  2137. __STDIO_THREADLOCK(stream);
  2138. /* First, flush and close, but don't deallocate, the stream. */
  2139. /* This also removes the stream for the open file list. */
  2140. dynmode =
  2141. #ifdef __STDIO_BUFFERS
  2142. /* __MASK_BUFMODE | */ /* TODO: check */
  2143. #endif /* __STDIO_BUFFERS */
  2144. (stream->modeflags & (__FLAG_FREEBUF|__FLAG_FREEFILE));
  2145. stream->modeflags &= ~(__FLAG_FREEBUF|__FLAG_FREEFILE);
  2146. fclose(stream); /* Failures are ignored. */
  2147. stream->modeflags = dynmode;
  2148. fp = _stdio_fopen(filename, mode, stream, -1);
  2149. __STDIO_THREADUNLOCK(stream);
  2150. return fp;
  2151. }
  2152. #endif
  2153. /**********************************************************************/
  2154. #ifdef L_freopen64
  2155. /* Reentrant. */
  2156. /* TODO -- is it worth collecting the common work (40 bytes) in a function? */
  2157. FILE *freopen64(const char * __restrict filename, const char * __restrict mode,
  2158. register FILE * __restrict stream)
  2159. {
  2160. unsigned short dynmode;
  2161. register FILE *fp;
  2162. __STDIO_THREADLOCK(stream);
  2163. /* First, flush and close, but don't deallocate, the stream. */
  2164. /* This also removes the stream for the open file list. */
  2165. dynmode =
  2166. #ifdef __STDIO_BUFFERS
  2167. /* __MASK_BUFMODE | */ /* TODO: check */
  2168. #endif /* __STDIO_BUFFERS */
  2169. (stream->modeflags & (__FLAG_FREEBUF|__FLAG_FREEFILE));
  2170. stream->modeflags &= ~(__FLAG_FREEBUF|__FLAG_FREEFILE);
  2171. fclose(stream); /* Failures are ignored. */
  2172. stream->modeflags = dynmode;
  2173. fp = _stdio_fopen(filename, mode, stream, -2); /* TODO -- magic const */
  2174. __STDIO_THREADUNLOCK(stream);
  2175. return fp;
  2176. }
  2177. #endif
  2178. /**********************************************************************/
  2179. #ifdef L_setbuf
  2180. /* Reentrant through setvbuf(). */
  2181. void setbuf(FILE * __restrict stream, register char * __restrict buf)
  2182. {
  2183. #ifdef __STDIO_BUFFERS
  2184. int mode;
  2185. mode = (buf != NULL) ? _IOFBF : _IONBF;
  2186. setvbuf(stream, buf, mode, BUFSIZ);
  2187. #else /* __STDIO_BUFFERS */
  2188. /* TODO -- assert on stream? */
  2189. /* Nothing to do. */
  2190. #endif /* __STDIO_BUFFERS */
  2191. }
  2192. #endif
  2193. /**********************************************************************/
  2194. #ifdef L_setvbuf
  2195. /* Reentrant. */
  2196. int setvbuf(register FILE * __restrict stream, register char * __restrict buf,
  2197. int mode, size_t size)
  2198. {
  2199. #ifdef __STDIO_BUFFERS
  2200. int allocated_buf_flag;
  2201. int rv = EOF;
  2202. __STDIO_THREADLOCK(stream);
  2203. __stdio_validate_FILE(stream); /* debugging only */
  2204. if (((unsigned int) mode) > 2) { /* Illegal mode. */
  2205. /* TODO -- set an errno? */
  2206. goto DONE;
  2207. }
  2208. #ifdef __STDIO_FLEXIBLE_SETVBUF
  2209. /* C89 standard requires no ops before setvbuf, but we can be flexible. */
  2210. /* NOTE: This will trash any chars ungetc'd!!! */
  2211. /* TODO: hmm could preserve unget count since ungot slots aren't changed (true?)
  2212. * but this will fail when buffered chars read from a pipe unless the user buf
  2213. * is big enough to copy everything over. */
  2214. if (fseek(stream, 0L, SEEK_CUR)) {
  2215. goto DONE;
  2216. }
  2217. #else /* __STDIO_FLEXIBLE_SETVBUF */
  2218. /*
  2219. * Note: ANSI/ISO requires setvbuf to be called after opening the file
  2220. * but before any other operation other than a failed setvbuf call.
  2221. * We'll cheat here and only test if the wide or narrow mode flag has
  2222. * been set; i.e. no read or write (or unget or fwide) operations have
  2223. * taken place.
  2224. */
  2225. #ifdef __STDIO_WIDE
  2226. if (stream->modeflags & (__FLAG_WIDE|__FLAG_NARROW)) {
  2227. goto DONE;
  2228. }
  2229. #else /* __STDIO_WIDE */
  2230. /* Note: This only checks if not currently reading or writing. */
  2231. if (stream->modeflags & (__FLAG_READING|__FLAG_WRITING)) {
  2232. goto DONE;
  2233. }
  2234. #endif /* __STDIO_WIDE */
  2235. #endif /* __STDIO_FLEXIBLE_SETVBUF */
  2236. if (mode == _IONBF) {
  2237. size = 0;
  2238. buf = NULL;
  2239. } else if (!buf && !size) {
  2240. /* If buf==NULL && size==0 && either _IOFBF or _IOLBF, keep
  2241. * current buffer and only set buffering mode. */
  2242. size = stream->bufend - stream->bufstart;
  2243. }
  2244. stream->modeflags &= ~(__MASK_BUFMODE); /* Clear current mode */
  2245. stream->modeflags |= mode * __FLAG_LBF; /* and set new one. */
  2246. allocated_buf_flag = 0;
  2247. if ((!buf) && (size != (stream->bufend - stream->bufstart))) {
  2248. /* No buffer supplied and requested size different from current. */
  2249. /* If size == 0, create a (hopefully) bogus non-null pointer... */
  2250. if (!(buf = ((size > 0)
  2251. ? ((allocated_buf_flag = __FLAG_FREEBUF), malloc(size))
  2252. : ((char *)NULL) + 1))
  2253. ) {
  2254. /* TODO -- should we really keep current buffer if it was passed
  2255. * to us earlier by the app? */
  2256. goto DONE; /* Keep current buffer. */
  2257. }
  2258. }
  2259. /* TODO: setvbuf "signal" safety */
  2260. if (buf && (buf != (char *) stream->bufstart)) { /* Want new buffer. */
  2261. if (stream->modeflags & __FLAG_FREEBUF) {
  2262. stream->modeflags &= ~(__FLAG_FREEBUF);
  2263. free(stream->bufstart);
  2264. }
  2265. stream->modeflags |= allocated_buf_flag; /* Free-able buffer? */
  2266. #ifdef __STDIO_GETC_MACRO
  2267. stream->bufgetc =
  2268. #endif
  2269. #ifdef __STDIO_PUTC_MACRO
  2270. stream->bufputc =
  2271. #endif
  2272. stream->bufpos = stream->bufread = stream->bufstart = buf;
  2273. stream->bufend = buf + size;
  2274. }
  2275. __stdio_validate_FILE(stream); /* debugging only */
  2276. rv = 0;
  2277. DONE:
  2278. __STDIO_THREADUNLOCK(stream);
  2279. return rv;
  2280. #else /* __STDIO_BUFFERS */
  2281. __stdio_validate_FILE(stream); /* debugging only */
  2282. /* TODO -- set errno for illegal mode? */
  2283. return EOF;
  2284. #endif /* __STDIO_BUFFERS */
  2285. }
  2286. #endif
  2287. /**********************************************************************
  2288. int fprintf(FILE * __restrict stream, const char * __restrict format, ...);
  2289. int fscanf(FILE * __restrict stream, const char * __restrict format, ...);
  2290. int printf(const char * __restrict format, ...);
  2291. int scanf(const char * __restrict format, ...);
  2292. int snprintf(char * __restrict s, size_t n,
  2293. const char * __restrict format, ...);
  2294. int sprintf(char * __restrict s, const char * __restrict format, ...);
  2295. int sscanf(char * __restrict s, const char * __restrict format, ...);
  2296. int vfprintf(FILE * __restrict stream, const char * __restrict format,
  2297. va_list arg);
  2298. int vfscanf(FILE * __restrict stream, const char * __restrict format,
  2299. va_list arg);
  2300. int vprintf(const char * __restrict format, va_list arg);
  2301. int vscanf(const char * __restrict format, va_list arg);
  2302. int vsnprintf(char * __restrict s, size_t n,
  2303. const char * __restrict format, va_list arg);
  2304. int vsprintf(char * __restrict s, const char * __restrict format,
  2305. va_list arg);
  2306. int vsscanf(char * __restrict s, const char * __restrict format,
  2307. va_list arg);
  2308. **********************************************************************/
  2309. #ifdef L_fgetc
  2310. /* Reentrancy handled by UNLOCKED() macro. */
  2311. UNLOCKED(int,fgetc,(FILE *stream),(stream))
  2312. {
  2313. unsigned char buf[1];
  2314. #ifdef __STDIO_WIDE
  2315. return (fread_unlocked(buf, (size_t) 1, (size_t) 1, stream) > 0)
  2316. ? *buf : EOF;
  2317. #else /* __STDIO_WIDE */
  2318. return (_stdio_fread(buf, (size_t) 1, stream) > 0) ? *buf : EOF;
  2319. #endif /* __STDIO_WIDE */
  2320. }
  2321. #endif
  2322. /**********************************************************************/
  2323. #ifdef L_fgets
  2324. /* Reentrancy handled by UNLOCKED() macro. */
  2325. UNLOCKED(char *,fgets,
  2326. (char *__restrict s, int n, register FILE * __restrict stream),
  2327. (s, n, stream))
  2328. {
  2329. register char *p;
  2330. int c;
  2331. #ifdef __UCLIBC_MJN3_ONLY__
  2332. #warning CONSIDER: What should fgets do if n <= 0?
  2333. #endif /* __UCLIBC_MJN3_ONLY__ */
  2334. /* Should we assert here? Or set errno? Or just fail... */
  2335. if (n <= 0) {
  2336. /* __set_errno(EINVAL); */
  2337. goto ERROR;
  2338. }
  2339. p = s;
  2340. while (--n) {
  2341. if ((c = (getc_unlocked)(stream)) == EOF) { /* Disable the macro. */
  2342. if (__FERROR(stream)) {
  2343. goto ERROR;
  2344. }
  2345. break;
  2346. }
  2347. if ((*p++ = c) == '\n') {
  2348. break;
  2349. }
  2350. }
  2351. #ifdef __UCLIBC_MJN3_ONLY__
  2352. #warning CONSIDER: If n==1 and not at EOF, should fgets return an empty string?
  2353. #endif /* __UCLIBC_MJN3_ONLY__ */
  2354. if (p > s) {
  2355. *p = 0;
  2356. return s;
  2357. }
  2358. ERROR:
  2359. return NULL;
  2360. }
  2361. #endif
  2362. /**********************************************************************/
  2363. #ifdef L_fputc
  2364. /* Reentrancy handled by UNLOCKED() macro. */
  2365. UNLOCKED(int,fputc,(int c, FILE *stream),(c,stream))
  2366. {
  2367. unsigned char buf[1];
  2368. *buf = (unsigned char) c;
  2369. #ifdef __STDIO_WIDE
  2370. return (fwrite_unlocked(buf, (size_t) 1, (size_t) 1, stream) > 0)
  2371. ? (*buf) : EOF;
  2372. #else /* __STDIO_WIDE */
  2373. return (_stdio_fwrite(buf, (size_t) 1, stream) > 0) ? (*buf) : EOF;
  2374. #endif /* __STDIO_WIDE */
  2375. }
  2376. #endif
  2377. /**********************************************************************/
  2378. #ifdef L_fputs
  2379. /* Reentrancy handled by UNLOCKED() macro. */
  2380. UNLOCKED(int,fputs,
  2381. (register const char * __restrict s, FILE * __restrict stream),
  2382. (s, stream))
  2383. {
  2384. size_t n = strlen(s);
  2385. #ifdef __STDIO_WIDE
  2386. return (fwrite_unlocked(s, n, (size_t) 1, stream) > 0) ? n : EOF;
  2387. #else /* __STDIO_WIDE */
  2388. return (_stdio_fwrite(s, n, stream) == n) ? n : EOF;
  2389. #endif /* __STDIO_WIDE */
  2390. }
  2391. #endif
  2392. /**********************************************************************/
  2393. #ifdef L_getc
  2394. #undef getc
  2395. #undef getc_unlocked
  2396. /* Reentrancy handled by UNLOCKED() macro. */
  2397. UNLOCKED(int,getc,(register FILE *stream),(stream))
  2398. {
  2399. return __GETC(stream); /* Invoke the macro. */
  2400. }
  2401. #endif
  2402. /**********************************************************************/
  2403. #ifdef L_getchar
  2404. #undef getchar /* Just in case. */
  2405. /* Reentrancy handled by UNLOCKED() macro. */
  2406. UNLOCKED_STREAM(int,getchar,(void),(),stdin)
  2407. {
  2408. register FILE *stream = stdin; /* This helps bcc optimize. */
  2409. return __GETC(stream);
  2410. }
  2411. #endif
  2412. /**********************************************************************/
  2413. #ifdef L_gets
  2414. link_warning(gets, "the 'gets' function is dangerous and should not be used.")
  2415. /* Reentrant. */
  2416. char *gets(char *s) /* WARNING!!! UNSAFE FUNCTION!!! */
  2417. {
  2418. register FILE *stream = stdin; /* This helps bcc optimize. */
  2419. register char *p = s;
  2420. int c;
  2421. __STDIO_THREADLOCK(stream);
  2422. /* Note: don't worry about performance here... this shouldn't be used!
  2423. * Therefore, force actual function call. */
  2424. while (((c = (getc_unlocked)(stream)) != EOF) && ((*p = c) != '\n')) {
  2425. ++p;
  2426. }
  2427. if ((c == EOF) || (s == p)) {
  2428. s = NULL;
  2429. } else {
  2430. *p = 0;
  2431. }
  2432. __STDIO_THREADUNLOCK(stream);
  2433. return s;
  2434. }
  2435. #endif
  2436. /**********************************************************************/
  2437. #ifdef L_putc
  2438. #undef putc
  2439. #undef putc_unlocked
  2440. /* Reentrancy handled by UNLOCKED() macro. */
  2441. UNLOCKED(int,putc,(int c, register FILE *stream),(c,stream))
  2442. {
  2443. return __PUTC(c, stream); /* Invoke the macro. */
  2444. }
  2445. #endif
  2446. /**********************************************************************/
  2447. #ifdef L_putchar
  2448. #undef putchar /* Just in case. */
  2449. /* Reentrancy handled by UNLOCKED() macro. */
  2450. UNLOCKED_STREAM(int,putchar,(int c),(c),stdout)
  2451. {
  2452. register FILE *stream = stdout; /* This helps bcc optimize. */
  2453. return __PUTC(c, stream);
  2454. }
  2455. #endif
  2456. /**********************************************************************/
  2457. #ifdef L_puts
  2458. /* Reentrant. */
  2459. int puts(register const char *s)
  2460. {
  2461. register FILE *stream = stdout; /* This helps bcc optimize. */
  2462. int n;
  2463. __STDIO_THREADLOCK(stream);
  2464. n = fputs_unlocked(s,stream) + 1;
  2465. if (
  2466. #if 1
  2467. fputc_unlocked('\n',stream)
  2468. #else
  2469. fputs_unlocked("\n",stream)
  2470. #endif
  2471. == EOF) {
  2472. n = EOF;
  2473. }
  2474. __STDIO_THREADUNLOCK(stream);
  2475. return n;
  2476. }
  2477. #endif
  2478. /**********************************************************************/
  2479. #ifdef L_ungetc
  2480. /*
  2481. * Note: This is the application-callable ungetc. If scanf calls this, it
  2482. * should also set stream->ungot[1] to 0 if this is the only ungot.
  2483. */
  2484. /* Reentrant. */
  2485. int ungetc(int c, register FILE *stream)
  2486. {
  2487. __STDIO_THREADLOCK(stream);
  2488. __stdio_validate_FILE(stream); /* debugging only */
  2489. #ifdef __STDIO_WIDE
  2490. if (stream->modeflags & __FLAG_WIDE) {
  2491. stream->modeflags |= __FLAG_ERROR;
  2492. c = EOF;
  2493. goto DONE;
  2494. }
  2495. stream->modeflags |= __FLAG_NARROW;
  2496. #endif /* __STDIO_WIDE */
  2497. /* If can't read or c == EOF or ungot slots already filled, then fail. */
  2498. if ((stream->modeflags
  2499. & (__MASK_UNGOT2|__FLAG_WRITEONLY
  2500. #ifndef __STDIO_AUTO_RW_TRANSITION
  2501. |__FLAG_WRITING /* Note: technically no, but yes in spirit */
  2502. #endif /* __STDIO_AUTO_RW_TRANSITION */
  2503. ))
  2504. || ((stream->modeflags & __MASK_UNGOT1) && (stream->ungot[1]))
  2505. || (c == EOF) ) {
  2506. c = EOF;
  2507. goto DONE;;
  2508. }
  2509. #ifdef __STDIO_BUFFERS
  2510. #ifdef __STDIO_AUTO_RW_TRANSITION
  2511. if (stream->modeflags & __FLAG_WRITING) {
  2512. fflush_unlocked(stream); /* Commit any write-buffered chars. */
  2513. }
  2514. #endif /* __STDIO_AUTO_RW_TRANSITION */
  2515. #endif /* __STDIO_BUFFERS */
  2516. /* Clear EOF and WRITING flags, and set READING FLAG */
  2517. stream->modeflags &= ~(__FLAG_EOF|__FLAG_WRITING);
  2518. #ifdef __UCLIBC_MJN3_ONLY__
  2519. #warning CONSIDER: Is setting the reading flag after an ungetc necessary?
  2520. #endif /* __UCLIBC_MJN3_ONLY__ */
  2521. stream->modeflags |= __FLAG_READING;
  2522. stream->ungot[1] = 1; /* Flag as app ungetc call; scanf fixes up. */
  2523. stream->ungot[(stream->modeflags++) & __MASK_UNGOT] = c;
  2524. #ifdef __STDIO_GETC_MACRO
  2525. stream->bufgetc = stream->bufstart; /* Must disable getc macro. */
  2526. #endif
  2527. __stdio_validate_FILE(stream); /* debugging only */
  2528. DONE:
  2529. __STDIO_THREADUNLOCK(stream);
  2530. return c;
  2531. }
  2532. #endif
  2533. /**********************************************************************/
  2534. #ifdef L_fread
  2535. /* NOTE: Surely ptr cannot point to a buffer of size > SIZE_MAX.
  2536. * Therefore, we treat the case size * nmemb > SIZE_MAX as an error,
  2537. * and include an assert() for it. */
  2538. /* Reentrancy handled by UNLOCKED() macro. */
  2539. UNLOCKED(size_t,fread,
  2540. (void * __restrict ptr, size_t size, size_t nmemb,
  2541. FILE * __restrict stream),
  2542. (ptr,size,nmemb,stream))
  2543. {
  2544. #ifdef __STDIO_WIDE
  2545. if (stream->modeflags & __FLAG_WIDE) {
  2546. stream->modeflags |= __FLAG_ERROR;
  2547. /* TODO -- errno? it this correct? */
  2548. return 0;
  2549. }
  2550. stream->modeflags |= __FLAG_NARROW;
  2551. #endif /* __STDIO_WIDE */
  2552. return (size == 0)
  2553. ? 0
  2554. : ( assert( ((size_t)(-1)) / size >= nmemb ),
  2555. _stdio_fread(ptr, nmemb * size, stream) / size );
  2556. }
  2557. #endif
  2558. /**********************************************************************/
  2559. #ifdef L_fwrite
  2560. /* NOTE: Surely ptr cannot point to a buffer of size > SIZE_MAX.
  2561. * Therefore, we treat the case size * nmemb > SIZE_MAX as an error,
  2562. * and include an assert() for it. */
  2563. /* Reentrancy handled by UNLOCKED() macro. */
  2564. UNLOCKED(size_t,fwrite,
  2565. (const void * __restrict ptr, size_t size, size_t nmemb,
  2566. FILE * __restrict stream),
  2567. (ptr,size,nmemb,stream))
  2568. {
  2569. #ifdef __STDIO_WIDE
  2570. if (stream->modeflags & __FLAG_WIDE) {
  2571. stream->modeflags |= __FLAG_ERROR;
  2572. /* TODO -- errno? it this correct? */
  2573. return 0;
  2574. }
  2575. stream->modeflags |= __FLAG_NARROW;
  2576. #endif /* __STDIO_WIDE */
  2577. return (size == 0)
  2578. ? 0
  2579. : ( assert( ((size_t)(-1)) / size >= nmemb ),
  2580. _stdio_fwrite(ptr, nmemb * size, stream) / size );
  2581. }
  2582. #endif
  2583. /**********************************************************************/
  2584. #if defined(L_fgetpos) || defined(L_fgetpos64)
  2585. /* Reentrant -- fgetpos() and fgetpos64(). */
  2586. #if defined(L_fgetpos) && defined(L_fgetpos64)
  2587. #error L_fgetpos and L_fgetpos64 are defined simultaneously!
  2588. #endif
  2589. #ifndef L_fgetpos64
  2590. #define fgetpos64 fgetpos
  2591. #define fpos64_t fpos_t
  2592. #define ftello64 ftell
  2593. #endif
  2594. int fgetpos64(FILE * __restrict stream, register fpos64_t * __restrict pos)
  2595. {
  2596. int retval = -1;
  2597. #ifdef __STDIO_WIDE
  2598. if (pos == NULL) {
  2599. __set_errno(EINVAL);
  2600. } else {
  2601. __STDIO_THREADLOCK(stream);
  2602. if ((pos->__pos = ftello64(stream)) >= 0) {
  2603. __COPY_MBSTATE(&(pos->__mbstate), &(stream->state));
  2604. pos->mblen_pending = stream->ungot_width[0];
  2605. retval = 0;
  2606. }
  2607. __STDIO_THREADUNLOCK(stream);
  2608. }
  2609. #else /* __STDIO_WIDE */
  2610. if (pos == NULL) {
  2611. __set_errno(EINVAL);
  2612. } else if ((pos->__pos = ftello64(stream)) >= 0) {
  2613. retval = 0;
  2614. }
  2615. #endif /* __STDIO_WIDE */
  2616. return retval;
  2617. }
  2618. #ifndef L_fgetpos64
  2619. #undef fgetpos64
  2620. #undef fpos64_t
  2621. #undef ftello64
  2622. #endif
  2623. #endif
  2624. /**********************************************************************/
  2625. #ifdef L_fseek
  2626. strong_alias(fseek,fseeko);
  2627. #endif
  2628. #if defined(L_fseek) && defined(__STDIO_LARGE_FILES)
  2629. int fseek(register FILE *stream, long int offset, int whence)
  2630. {
  2631. return fseeko64(stream, offset, whence);
  2632. }
  2633. #endif
  2634. #if defined(L_fseeko64) || (defined(L_fseek) && !defined(__STDIO_LARGE_FILES))
  2635. #ifndef L_fseeko64
  2636. #define fseeko64 fseek
  2637. #define __off64_t long int
  2638. #endif
  2639. /* Reentrant -- fseek(), fseeko(), fseeko64() */
  2640. int fseeko64(register FILE *stream, __off64_t offset, int whence)
  2641. {
  2642. #if SEEK_SET != 0 || SEEK_CUR != 1 || SEEK_END != 2
  2643. #error Assumption violated -- values of SEEK_SET, SEEK_CUR, SEEK_END
  2644. #endif
  2645. __offmax_t pos[1];
  2646. int retval;
  2647. if ( ((unsigned int) whence) > 2 ) {
  2648. __set_errno(EINVAL);
  2649. return -1;
  2650. }
  2651. __STDIO_THREADLOCK(stream);
  2652. __stdio_validate_FILE(stream); /* debugging only */
  2653. retval = -1;
  2654. *pos = offset;
  2655. if (
  2656. #ifdef __STDIO_BUFFERS
  2657. /* First commit any pending buffered writes. */
  2658. ((stream->modeflags & __FLAG_WRITING) && fflush_unlocked(stream)) ||
  2659. #endif /* __STDIO_BUFFERS */
  2660. ((whence == SEEK_CUR) && (_stdio_adjpos(stream, pos) < 0))
  2661. || (_stdio_lseek(stream, pos, whence) < 0)
  2662. ) {
  2663. __stdio_validate_FILE(stream); /* debugging only */
  2664. goto DONE;
  2665. }
  2666. #ifdef __STDIO_BUFFERS
  2667. /* only needed if reading but do it anyway to avoid test */
  2668. #ifdef __STDIO_GETC_MACRO
  2669. stream->bufgetc = /* Must disable getc. */
  2670. #endif
  2671. stream->bufpos = stream->bufread = stream->bufstart;
  2672. #endif /* __STDIO_BUFFERS */
  2673. stream->modeflags &=
  2674. ~(__FLAG_READING|__FLAG_WRITING|__FLAG_EOF|__MASK_UNGOT);
  2675. #ifdef __UCLIBC_MJN3_ONLY__
  2676. #warning CONSIDER: How do we deal with fseek to an ftell position for incomplete or error wide? Right now, we clear all multibyte state info. If we do not clear, then fix rewind() to do so if fseek() succeeds.
  2677. #endif /* __UCLIBC_MJN3_ONLY__ */
  2678. #ifdef __STDIO_WIDE
  2679. /* TODO: don't clear state if don't move? */
  2680. stream->ungot_width[0] = 0;
  2681. #endif /* __STDIO_WIDE */
  2682. #ifdef __STDIO_MBSTATE
  2683. /* TODO: don't clear state if don't move? */
  2684. __INIT_MBSTATE(&(stream->state));
  2685. #endif /* __STDIO_MBSTATE */
  2686. __stdio_validate_FILE(stream); /* debugging only */
  2687. retval = 0;
  2688. DONE:
  2689. __STDIO_THREADUNLOCK(stream);
  2690. return retval;
  2691. }
  2692. #ifndef L_fseeko64
  2693. #undef fseeko64
  2694. #undef __off64_t
  2695. #endif
  2696. #endif
  2697. /**********************************************************************/
  2698. #if defined(L_fsetpos) || defined(L_fsetpos64)
  2699. #if defined(L_fsetpos) && defined(L_fsetpos64)
  2700. #error L_fsetpos and L_fsetpos64 are defined simultaneously!
  2701. #endif
  2702. #ifndef L_fsetpos64
  2703. #define fsetpos64 fsetpos
  2704. #define fpos64_t fpos_t
  2705. #define fseeko64 fseek
  2706. #endif
  2707. /* Reentrant -- fgetpos{64}() through fseek{64}(). */
  2708. int fsetpos64(FILE *stream, register const fpos64_t *pos)
  2709. {
  2710. if (!pos) {
  2711. __set_errno(EINVAL);
  2712. return EOF;
  2713. }
  2714. #ifdef __STDIO_WIDE
  2715. {
  2716. int retval;
  2717. __STDIO_THREADLOCK(stream);
  2718. if ((retval = fseeko64(stream, pos->__pos, SEEK_SET)) == 0) {
  2719. __COPY_MBSTATE(&(stream->state), &(pos->__mbstate));
  2720. #ifdef __UCLIBC_MJN3_ONLY__
  2721. #warning CONSIDER: Moving mblen_pending into some mbstate field might be useful. But we would need to modify all the mb<->wc funcs.
  2722. #endif /* __UCLIBC_MJN3_ONLY__ */
  2723. stream->ungot_width[0] = pos->mblen_pending;
  2724. }
  2725. __STDIO_THREADUNLOCK(stream);
  2726. return retval;
  2727. }
  2728. #else /* __STDIO_WIDE */
  2729. return fseeko64(stream, pos->__pos, SEEK_SET);
  2730. #endif /* __STDIO_WIDE */
  2731. }
  2732. #ifndef L_fsetpos64
  2733. #undef fsetpos64
  2734. #undef fpos64_t
  2735. #undef fseeko64
  2736. #endif
  2737. #endif
  2738. /**********************************************************************/
  2739. #ifdef L_ftell
  2740. strong_alias(ftell,ftello);
  2741. #endif
  2742. #if defined(L_ftell) && defined(__STDIO_LARGE_FILES)
  2743. long int ftell(register FILE *stream)
  2744. {
  2745. __offmax_t pos = ftello64(stream);
  2746. return (pos == ((long int) pos)) ? pos : (__set_errno(EOVERFLOW), -1);
  2747. }
  2748. #endif
  2749. #if defined(L_ftello64) || (defined(L_ftell) && !defined(__STDIO_LARGE_FILES))
  2750. #ifndef L_ftello64
  2751. #define ftello64 ftell
  2752. #define __off64_t long int
  2753. #endif
  2754. /* Reentrant -- ftell, ftello, ftello64. */
  2755. __off64_t ftello64(register FILE *stream)
  2756. {
  2757. __offmax_t pos[1];
  2758. __off64_t retval;
  2759. __STDIO_THREADLOCK(stream);
  2760. retval = (((*pos = 0), (_stdio_lseek(stream, pos, SEEK_CUR) < 0))
  2761. || (_stdio_adjpos(stream, pos) < 0)) ? -1 : *pos;
  2762. __STDIO_THREADUNLOCK(stream);
  2763. return retval;
  2764. }
  2765. #ifndef L_ftello64
  2766. #undef ftello64
  2767. #undef __off64_t
  2768. #endif
  2769. #endif
  2770. /**********************************************************************/
  2771. #ifdef L_rewind
  2772. void rewind(register FILE *stream)
  2773. {
  2774. __STDIO_THREADLOCK(stream);
  2775. __CLEARERR(stream); /* Clear errors first and then seek */
  2776. fseek(stream, 0L, SEEK_SET); /* in case there is an error seeking. */
  2777. __STDIO_THREADUNLOCK(stream);
  2778. }
  2779. #endif
  2780. /**********************************************************************/
  2781. #ifdef L_clearerr
  2782. #undef clearerr
  2783. /* Reentrancy handled by UNLOCKED_VOID_RETURN() macro. */
  2784. UNLOCKED_VOID_RETURN(clearerr,(FILE *stream),(stream))
  2785. {
  2786. __CLEARERR(stream);
  2787. }
  2788. #endif
  2789. /**********************************************************************/
  2790. #ifdef L_feof
  2791. #undef feof
  2792. /* Reentrancy handled by UNLOCKED() macro. */
  2793. UNLOCKED(int,feof,(FILE *stream),(stream))
  2794. {
  2795. return __FEOF(stream);
  2796. }
  2797. #endif
  2798. /**********************************************************************/
  2799. #ifdef L_ferror
  2800. #undef ferror
  2801. /* Reentrancy handled by UNLOCKED() macro. */
  2802. UNLOCKED(int,ferror,(FILE *stream),(stream))
  2803. {
  2804. return __FERROR(stream);
  2805. }
  2806. #endif
  2807. /**********************************************************************/
  2808. #ifdef L_perror
  2809. void perror(register const char *s)
  2810. {
  2811. /* If the program is calling perror, it's a safe bet that printf and
  2812. * friends are used as well. It is also possible that the calling
  2813. * program could buffer stderr, or reassign it. */
  2814. register const char *sep;
  2815. sep = ": ";
  2816. if (!(s && *s)) { /* Caller did not supply a prefix message */
  2817. s = (sep += 2); /* or passed an empty string. */
  2818. }
  2819. #if 1
  2820. #ifdef __STDIO_PRINTF_M_SPEC
  2821. fprintf(stderr, "%s%s%m\n", s, sep); /* Use the gnu %m feature. */
  2822. #else
  2823. {
  2824. char buf[64];
  2825. fprintf(stderr, "%s%s%s\n", s, sep,
  2826. _glibc_strerror_r(errno, buf, sizeof(buf)));
  2827. }
  2828. #endif
  2829. #else
  2830. /* Note: Assumes stderr not closed or buffered. */
  2831. {
  2832. char buf[64];
  2833. __STDIO_THREADLOCK(stderr);
  2834. _stdio_fdout(STDERR_FILENO, s, sep,
  2835. _glibc_strerror_r(errno, buf, sizeof(buf)));
  2836. __STDIO_THREADUNLOCK(stderr);
  2837. }
  2838. #endif
  2839. }
  2840. #endif
  2841. /**********************************************************************/
  2842. /* UTILITY funcs */
  2843. /**********************************************************************/
  2844. #ifdef L__stdio_fdout
  2845. /* Not reentrant -- TODO: lock associated stream if a know file descriptor? */
  2846. void _stdio_fdout(int fd, ...)
  2847. {
  2848. va_list arg;
  2849. register const char *p;
  2850. va_start(arg, fd);
  2851. while ((p = va_arg(arg, const char *)) != NULL) {
  2852. write(fd, p, strlen(p));
  2853. }
  2854. va_end(arg);
  2855. }
  2856. #endif
  2857. /**********************************************************************/
  2858. #ifdef L__uintmaxtostr
  2859. /* Avoid using long long / and % operations to cut down dependencies on
  2860. * libgcc.a. Definitely helps on i386 at least. */
  2861. #if (INTMAX_MAX > INT_MAX) && (((INTMAX_MAX/INT_MAX)/2) - 2 <= INT_MAX)
  2862. #define INTERNAL_DIV_MOD
  2863. #endif
  2864. #include <locale.h>
  2865. char *_uintmaxtostr(register char * __restrict bufend, uintmax_t uval,
  2866. int base, __UIM_CASE alphacase)
  2867. {
  2868. int negative;
  2869. unsigned int digit;
  2870. #ifdef INTERNAL_DIV_MOD
  2871. unsigned int H, L, high, low, rh;
  2872. #endif
  2873. #ifndef __LOCALE_C_ONLY
  2874. int grouping, outdigit;
  2875. const char *g; /* This does not need to be initialized. */
  2876. #endif /* __LOCALE_C_ONLY */
  2877. negative = 0;
  2878. if (base < 0) { /* signed value */
  2879. base = -base;
  2880. if (uval > INTMAX_MAX) {
  2881. uval = -uval;
  2882. negative = 1;
  2883. }
  2884. }
  2885. /* this is an internal routine -- we shouldn't need to check this */
  2886. assert(!((base < 2) || (base > 36)));
  2887. #ifndef __LOCALE_C_ONLY
  2888. grouping = -1;
  2889. outdigit = 0x80 & alphacase;
  2890. alphacase ^= outdigit;
  2891. if (alphacase == __UIM_GROUP) {
  2892. assert(base == 10);
  2893. if (*(g = __UCLIBC_CURLOCALE_DATA.grouping)) {
  2894. grouping = *g;
  2895. }
  2896. }
  2897. #endif /* __LOCALE_C_ONLY */
  2898. *bufend = '\0';
  2899. #ifndef INTERNAL_DIV_MOD
  2900. do {
  2901. #ifndef __LOCALE_C_ONLY
  2902. if (!grouping) { /* Finished a group. */
  2903. bufend -= __UCLIBC_CURLOCALE_DATA.thousands_sep_len;
  2904. memcpy(bufend, __UCLIBC_CURLOCALE_DATA.thousands_sep,
  2905. __UCLIBC_CURLOCALE_DATA.thousands_sep_len);
  2906. if (g[1] != 0) { /* g[1] == 0 means repeat last grouping. */
  2907. /* Note: g[1] == -1 means no further grouping. But since
  2908. * we'll never wrap around, we can set grouping to -1 without
  2909. * fear of */
  2910. ++g;
  2911. }
  2912. grouping = *g;
  2913. }
  2914. --grouping;
  2915. #endif /* __LOCALE_C_ONLY */
  2916. digit = uval % base;
  2917. uval /= base;
  2918. #ifndef __LOCALE_C_ONLY
  2919. if (unlikely(outdigit)) {
  2920. bufend -= __UCLIBC_CURLOCALE_DATA.outdigit_length[digit];
  2921. memcpy(bufend,
  2922. (&__UCLIBC_CURLOCALE_DATA.outdigit0_mb)[digit],
  2923. __UCLIBC_CURLOCALE_DATA.outdigit_length[digit]);
  2924. } else
  2925. #endif
  2926. {
  2927. *--bufend = ( (digit < 10) ? digit + '0' : digit + alphacase );
  2928. }
  2929. } while (uval);
  2930. #else /* ************************************************** */
  2931. H = (UINT_MAX / base);
  2932. L = UINT_MAX % base + 1;
  2933. if (L == base) {
  2934. ++H;
  2935. L = 0;
  2936. }
  2937. low = (unsigned int) uval;
  2938. high = (unsigned int) (uval >> (sizeof(unsigned int) * CHAR_BIT));
  2939. do {
  2940. #ifndef __LOCALE_C_ONLY
  2941. if (!grouping) { /* Finished a group. */
  2942. bufend -= __UCLIBC_CURLOCALE_DATA.thousands_sep_len;
  2943. memcpy(bufend, __UCLIBC_CURLOCALE_DATA.thousands_sep,
  2944. __UCLIBC_CURLOCALE_DATA.thousands_sep_len);
  2945. if (g[1] != 0) { /* g[1] == 0 means repeat last grouping. */
  2946. /* Note: g[1] == -1 means no further grouping. But since
  2947. * we'll never wrap around, we can set grouping to -1 without
  2948. * fear of */
  2949. ++g;
  2950. }
  2951. grouping = *g;
  2952. }
  2953. --grouping;
  2954. #endif /* __LOCALE_C_ONLY */
  2955. if (unlikely(high)) {
  2956. rh = high % base;
  2957. high /= base;
  2958. digit = (low % base) + (L * rh);
  2959. low = (low / base) + (H * rh) + (digit / base);
  2960. digit %= base;
  2961. } else {
  2962. digit = low % base;
  2963. low /= base;
  2964. }
  2965. #ifndef __LOCALE_C_ONLY
  2966. if (unlikely(outdigit)) {
  2967. bufend -= __UCLIBC_CURLOCALE_DATA.outdigit_length[digit];
  2968. memcpy(bufend,
  2969. (&__UCLIBC_CURLOCALE_DATA.outdigit0_mb)[digit],
  2970. __UCLIBC_CURLOCALE_DATA.outdigit_length[digit]);
  2971. } else
  2972. #endif
  2973. {
  2974. *--bufend = ( (digit < 10) ? digit + '0' : digit + alphacase );
  2975. }
  2976. } while (low | high);
  2977. #endif /******************************************************/
  2978. if (negative) {
  2979. *--bufend = '-';
  2980. }
  2981. return bufend;
  2982. }
  2983. #undef INTERNAL_DIV_MOD
  2984. #endif
  2985. /**********************************************************************/
  2986. #ifdef L__wstdio_fwrite
  2987. #include <wchar.h>
  2988. size_t _wstdio_fwrite(const wchar_t *__restrict ws, size_t n,
  2989. register FILE *__restrict stream)
  2990. {
  2991. size_t r, count;
  2992. char buf[64];
  2993. const wchar_t *pw;
  2994. #if defined(__STDIO_WIDE) && defined(__STDIO_BUFFERS)
  2995. if (stream->filedes == -3) { /* Special case to support {v}swprintf. */
  2996. count = ((wchar_t *)(stream->bufend)) - ((wchar_t *)(stream->bufpos));
  2997. if (count > n) {
  2998. count = n;
  2999. }
  3000. if (count) {
  3001. wmemcpy((wchar_t *)(stream->bufpos), ws, count);
  3002. stream->bufpos = (char *)(((wchar_t *)(stream->bufpos)) + count);
  3003. }
  3004. return n;
  3005. }
  3006. #endif /* defined(__STDIO_WIDE) && defined(__STDIO_BUFFERS) */
  3007. if (stream->modeflags & __FLAG_NARROW) {
  3008. stream->modeflags |= __FLAG_ERROR;
  3009. __set_errno(EBADF);
  3010. return 0;
  3011. }
  3012. stream->modeflags |= __FLAG_WIDE;
  3013. pw = ws;
  3014. count = 0;
  3015. while (n > count) {
  3016. r = wcsnrtombs(buf, &pw, n-count, sizeof(buf), &stream->state);
  3017. if (r != ((size_t) -1)) { /* No encoding errors */
  3018. if (!r) {
  3019. ++r; /* 0 is returned when nul is reached. */
  3020. pw = ws + count + r; /* pw was set to NULL, so correct. */
  3021. }
  3022. if (_stdio_fwrite(buf, r, stream) == r) {
  3023. count = pw - ws;
  3024. continue;
  3025. }
  3026. }
  3027. break;
  3028. }
  3029. /* Note: The count is incorrect if 0 < _stdio_fwrite return < r!!! */
  3030. return count;
  3031. }
  3032. #endif
  3033. /**********************************************************************/