stdio.c 89 KB

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