stdio.c 90 KB

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