stdio.c 88 KB

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