stdio.c 95 KB

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