stdio.c 84 KB

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