stdio.c 94 KB

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