stdio.c 85 KB

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