stdio.c 97 KB

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