stdio.c 97 KB

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