stdio.c 93 KB

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