stdio.c 88 KB

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