stdio.c 85 KB

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