stdio.c 84 KB

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