stdio.c 99 KB

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