stdio.c 92 KB

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