stdio.c 95 KB

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