12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674 |
- #ifdef L_ctermid
- #define L_ctermid_function
- #undef L_ctermid
- #endif
- #define _ISOC99_SOURCE
- #define _GNU_SOURCE
- #define _STDIO_UTILITY
- #include <stdio.h>
- #include <stddef.h>
- #include <stdlib.h>
- #include <string.h>
- #include <limits.h>
- #include <stdint.h>
- #include <stdarg.h>
- #include <errno.h>
- #include <assert.h>
- #include <stdio_ext.h>
- #include <unistd.h>
- #include <fcntl.h>
- #ifndef O_LARGEFILE
- #ifdef __STDIO_LARGE_FILES
- #error missing define for O_LARGEFILE!
- #endif
- #define O_LARGEFILE 0
- #endif
- #ifndef __STDIO_THREADSAFE
- #undef L___fsetlocking
- #undef L___flockfile
- #undef L___ftrylockfile
- #undef L___funlockfile
- #endif
- #ifndef __STDIO_LARGE_FILES
- #undef L_fopen64
- #undef L_freopen64
- #undef L_ftello64
- #undef L_fseeko64
- #undef L_fsetpos64
- #undef L_fgetpos64
- #endif
- #ifndef __STDIO_THREADSAFE
- #if defined(__BCC__) && 0
- #define UNLOCKED_STREAM(RETURNTYPE,NAME,PARAMS,ARGS,STREAM) \
- asm(".text\nexport _" "NAME" "_unlocked\n_" "NAME" "_unlocked = _" "NAME"); \
- RETURNTYPE NAME PARAMS
- #else
- #define UNLOCKED_STREAM(RETURNTYPE,NAME,PARAMS,ARGS,STREAM) \
- strong_alias(NAME,NAME##_unlocked) \
- RETURNTYPE NAME PARAMS
- #endif
- #define UNLOCKED(RETURNTYPE,NAME,PARAMS,ARGS) \
- UNLOCKED_STREAM(RETURNTYPE,NAME,PARAMS,ARGS,stream)
- #if defined(__BCC__) && 0
- #define UNLOCKED_VOID_RETURN(NAME,PARAMS,ARGS) \
- asm(".text\nexport _" "NAME" "_unlocked\n_" "NAME" "_unlocked = _" "NAME"); \
- void NAME PARAMS
- #else
- #define UNLOCKED_VOID_RETURN(NAME,PARAMS,ARGS) \
- strong_alias(NAME,NAME##_unlocked) \
- void NAME PARAMS
- #endif
- #define __STDIO_THREADLOCK_OPENLIST
- #define __STDIO_THREADUNLOCK_OPENLIST
- #else
- #include <pthread.h>
- #define UNLOCKED_STREAM(RETURNTYPE,NAME,PARAMS,ARGS,STREAM) \
- RETURNTYPE NAME PARAMS \
- { \
- RETURNTYPE retval; \
- __STDIO_THREADLOCK(STREAM); \
- retval = NAME##_unlocked ARGS ; \
- __STDIO_THREADUNLOCK(STREAM); \
- return retval; \
- } \
- RETURNTYPE NAME##_unlocked PARAMS
- #define UNLOCKED(RETURNTYPE,NAME,PARAMS,ARGS) \
- UNLOCKED_STREAM(RETURNTYPE,NAME,PARAMS,ARGS,stream)
- #define UNLOCKED_VOID_RETURN(NAME,PARAMS,ARGS) \
- void NAME PARAMS \
- { \
- __STDIO_THREADLOCK(stream); \
- NAME##_unlocked ARGS ; \
- __STDIO_THREADUNLOCK(stream); \
- } \
- void NAME##_unlocked PARAMS
- #define __STDIO_THREADLOCK_OPENLIST \
- __pthread_mutex_lock(&_stdio_openlist_lock)
- #define __STDIO_THREADUNLOCK_OPENLIST \
- __pthread_mutex_unlock(&_stdio_openlist_lock)
- #define __STDIO_THREADTRYLOCK_OPENLIST \
- __pthread_mutex_trylock(&_stdio_openlist_lock)
- #endif
- #ifdef __STDIO_WIDE
- #define __STDIO_FILE_INIT_UNGOT { 0, 0 }, { 0, 0 },
- #else
- #define __STDIO_FILE_INIT_UNGOT { 0, 0 },
- #endif
- #ifdef __STDIO_GETC_MACRO
- #define __STDIO_FILE_INIT_BUFGETC(x) x,
- #else
- #define __STDIO_FILE_INIT_BUFGETC(x)
- #endif
- #ifdef __STDIO_PUTC_MACRO
- #define __STDIO_FILE_INIT_BUFPUTC(x) x,
- #else
- #define __STDIO_FILE_INIT_BUFPUTC(x)
- #endif
- #if defined(__STDIO_BUFFERS) || defined(__STDIO_GLIBC_CUSTOM_STREAMS)
- #define __STDIO_FILE_INIT_NEXT(next) (next),
- #else
- #define __STDIO_FILE_INIT_NEXT(next)
- #endif
- #ifdef __STDIO_BUFFERS
- #define __STDIO_FILE_INIT_BUFFERS(buf,bufsize) \
- (buf), (buf)+(bufsize), (buf), (buf),
- #else
- #define __STDIO_FILE_INIT_BUFFERS(buf,bufsize)
- #endif
- #ifdef __STDIO_GLIBC_CUSTOM_STREAMS
- #define __STDIO_FILE_INIT_CUSTOM_STREAM(stream) \
- &((stream).filedes), { _cs_read, _cs_write, NULL, _cs_close },
- #else
- #define __STDIO_FILE_INIT_CUSTOM_STREAM(stream)
- #endif
- #ifdef __STDIO_MBSTATE
- #define __STDIO_FILE_INIT_MBSTATE \
- { 0, 0 },
- #else
- #define __STDIO_FILE_INIT_MBSTATE
- #endif
- #ifdef __STDIO_THREADSAFE
- #define __STDIO_FILE_INIT_THREADSAFE \
- 0, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP,
- #else
- #define __STDIO_FILE_INIT_THREADSAFE
- #endif
- #define __STDIO_INIT_FILE_STRUCT(stream, flags, filedes, next, buf, bufsize) \
- { (flags), \
- __STDIO_FILE_INIT_UNGOT \
- (filedes), \
- __STDIO_FILE_INIT_NEXT(next) \
- __STDIO_FILE_INIT_BUFFERS(buf,bufsize) \
- __STDIO_FILE_INIT_BUFGETC((buf)) \
- __STDIO_FILE_INIT_BUFPUTC((buf)) \
- __STDIO_FILE_INIT_CUSTOM_STREAM(stream) \
- __STDIO_FILE_INIT_MBSTATE \
- __STDIO_FILE_INIT_THREADSAFE \
- }
- #ifdef __STDIO_GLIBC_CUSTOM_STREAMS
- #define __READ(STREAMPTR,BUF,SIZE) \
- ((((STREAMPTR)->gcs.read) == NULL) ? -1 : \
- (((STREAMPTR)->gcs.read)((STREAMPTR)->cookie,(BUF),(SIZE))))
- #define __WRITE(STREAMPTR,BUF,SIZE) \
- ((((STREAMPTR)->gcs.write) == NULL) ? -1 : \
- (((STREAMPTR)->gcs.write)((STREAMPTR)->cookie,(BUF),(SIZE))))
- #define __CLOSE(STREAMPTR) \
- ((((STREAMPTR)->gcs.close) == NULL) ? 0 : \
- (((STREAMPTR)->gcs.close)((STREAMPTR)->cookie)))
- #else
- #define __READ(STREAMPTR,BUF,SIZE) \
- (read((STREAMPTR)->filedes,(BUF),(SIZE)))
- #define __WRITE(STREAMPTR,BUF,SIZE) \
- (write((STREAMPTR)->filedes,(BUF),(SIZE)))
- #define __CLOSE(STREAMPTR) \
- (close((STREAMPTR)->filedes))
- #endif
- #ifdef L_getw
- int getw(FILE *stream)
- {
- int aw[1];
- #ifdef __STDIO_WIDE
- return (fread_unlocked((void *)aw, sizeof(int), 1, stream) > 0)
- ? (*aw) : EOF;
- #else
- return (_stdio_fread((unsigned char *)(aw), sizeof(int), stream)
- == sizeof(int)) ? (*aw) : EOF;
- #endif
- }
- #endif
- #ifdef L_putw
- int putw(int w, FILE *stream)
- {
- int aw[1];
- *aw = w;
- #ifdef __STDIO_WIDE
- return (fwrite_unlocked((void *)aw, sizeof(int), 1, stream) == 1)
- ? 0 : EOF;
- #else
- return (_stdio_fwrite((unsigned char *)aw, sizeof(int), stream)
- == sizeof(int)) ? 0 : EOF;
- #endif
- }
- #endif
- #ifdef L_fileno
- UNLOCKED(int,fileno,(register FILE *stream),(stream))
- {
- #ifdef __STDIO_GLIBC_CUSTOM_STREAMS
- return ( (stream && (stream->cookie == &(stream->filedes)) && (stream->filedes >= 0))
- ? stream->filedes
- : (__set_errno(EBADF), -1) );
- #else
- return ((stream && stream->filedes >= 0)) ? stream->filedes : (__set_errno(EBADF), -1);
- #endif
- }
- #endif
- #ifdef L_fdopen
- FILE *fdopen(int filedes, const char *mode)
- {
- register char *cur_mode;
- return (((int)(cur_mode = (char *) fcntl(filedes, F_GETFL))) != -1)
- ? _stdio_fopen(cur_mode, mode, NULL, filedes)
- : NULL;
- }
- #endif
- #ifdef L_fopen64
- FILE *fopen64(const char * __restrict filename, const char * __restrict mode)
- {
- return _stdio_fopen(filename, mode, NULL, -2);
- }
- #endif
- #ifdef L_ctermid_function
- char *ctermid(register char *s)
- {
- static char sbuf[L_ctermid];
- #ifdef __BCC__
-
- if (!s) {
- s = sbuf;
- }
- *s = 0;
- return s;
- #else
-
- return strcpy((s ? s : sbuf), "/dev/tty");
- #endif
- }
- #endif
- #ifdef L_setbuffer
- void setbuffer(FILE * __restrict stream, register char * __restrict buf,
- size_t size)
- {
- #ifdef __STDIO_BUFFERS
- setvbuf(stream, buf, (buf ? _IOFBF : _IONBF), size);
- #else
-
- #endif
- }
- #endif
- #ifdef L_setlinebuf
- void setlinebuf(FILE * __restrict stream)
- {
- #ifdef __STDIO_BUFFERS
- setvbuf(stream, NULL, _IOLBF, (size_t) 0);
- #else
-
- #endif
- }
- #endif
- #ifdef L_fcloseall
- int fcloseall (void)
- {
- #if defined(__STDIO_BUFFERS) || defined(__STDIO_GLIBC_CUSTOM_STREAMS)
- register FILE *stream;
- int rv;
- _stdio_term();
- rv = 0;
- for (stream = _stdio_openlist ; stream ; stream = stream->nextopen) {
- if (stream->modeflags & (__FLAG_WRITING|__FLAG_ERROR)) {
-
- rv = EOF;
- }
- }
-
- _stdio_openlist = NULL;
- return rv;
- #else
- return 0;
- #endif
- }
- #endif
- #ifdef L_fmemopen
- #ifdef __STDIO_GLIBC_CUSTOM_STREAMS
- typedef struct {
- size_t pos;
- size_t len;
- size_t eof;
- int dynbuf;
- unsigned char *buf;
- FILE *fp;
- } __fmo_cookie;
- #define COOKIE ((__fmo_cookie *) cookie)
- static ssize_t fmo_read(register void *cookie, char *buf, size_t bufsize)
- {
- size_t count = COOKIE->len - COOKIE->pos;
-
- if (!count) {
- return 0;
- }
- if (bufsize > count) {
- bufsize = count;
- }
- #if 1
- memcpy(buf, COOKIE->buf + COOKIE->pos, bufsize);
- COOKIE->pos += bufsize;
- #else
- {
- register char *p = COOKIE->buf + COOKIE->pos;
- count = bufsize;
- while (count) {
- *buf++ = *p++;
- --count;
- }
- COOKIE->pos += bufsize;
- }
- #endif
- return bufsize;
- }
- static ssize_t fmo_write(register void *cookie, const char *buf, size_t bufsize)
- {
- size_t count;
-
-
- if (COOKIE->fp->modeflags & __FLAG_APPEND) {
- COOKIE->pos = COOKIE->eof;
- }
- count = COOKIE->len - COOKIE->pos;
- if (bufsize > count) {
- bufsize = count;
- if (count == 0) {
- __set_errno(EFBIG);
- return -1;
- }
- }
- #if 1
- memcpy(COOKIE->buf + COOKIE->pos, buf, bufsize);
- COOKIE->pos += bufsize;
- if (COOKIE->pos > COOKIE->eof) {
- COOKIE->eof = COOKIE->pos;
- if (bufsize < count) {
- *(COOKIE->buf + COOKIE->pos) = 0;
- }
- }
- #else
- {
- register char *p = COOKIE->buf + COOKIE->pos;
- size_t i = bufsize;
- while (i > 0) {
- *p++ = *buf++;
- --i;
- }
- COOKIE->pos += bufsize;
- if (COOKIE->pos > COOKIE->eof) {
- COOKIE->eof = COOKIE->pos;
- if (bufsize < count) {
- *p = 0;
- }
- }
- }
- #endif
- return bufsize;
- }
- static int fmo_seek(register void *cookie, __offmax_t *pos, int whence)
- {
- __offmax_t p = *pos;
-
- assert(((unsigned int) whence) <= 2);
- if (whence != SEEK_SET) {
- p += (whence == SEEK_CUR) ? COOKIE->pos : COOKIE->eof;
- }
-
-
- if (((uintmax_t) p) > COOKIE->eof) {
- return -1;
- }
- COOKIE->pos = *pos = p;
- return 0;
- }
- static int fmo_close(register void *cookie)
- {
- if (COOKIE->dynbuf) {
- free(COOKIE->buf);
- }
- free(cookie);
- return 0;
- }
- #undef COOKIE
- static const cookie_io_functions_t _fmo_io_funcs = {
- fmo_read, fmo_write, fmo_seek, fmo_close
- };
- FILE *fmemopen(void *s, size_t len, const char *modes)
- {
- FILE *fp;
- register __fmo_cookie *cookie;
- size_t i;
- if ((cookie = malloc(sizeof(__fmo_cookie))) != NULL) {
- cookie->len = len;
- cookie->eof = cookie->pos = 0;
- cookie->dynbuf = 0;
- if (((cookie->buf = s) == NULL) && (len > 0)) {
- if ((cookie->buf = malloc(len)) == NULL) {
- goto EXIT_cookie;
- }
- cookie->dynbuf = 1;
- *cookie->buf = 0;
- }
-
- #ifndef __BCC__
- fp = fopencookie(cookie, modes, _fmo_io_funcs);
- #else
- fp = fopencookie(cookie, modes, &_fmo_io_funcs);
- #endif
-
- if (fp != NULL) {
- cookie->fp = fp;
- if (fp->modeflags & __FLAG_READONLY) {
- cookie->eof = len;
- }
- if ((fp->modeflags & __FLAG_APPEND) && (len > 0)) {
- for (i = 0 ; i < len ; i++) {
- if (cookie->buf[i] == 0) {
- break;
- }
- }
- cookie->eof = cookie->pos = i;
- }
- return fp;
- }
- }
- if (!s) {
- free(cookie->buf);
- }
- EXIT_cookie:
- free(cookie);
- return NULL;
- }
- #endif
- #endif
- #ifdef L_open_memstream
- #ifdef __STDIO_GLIBC_CUSTOM_STREAMS
- #define COOKIE ((__oms_cookie *) cookie)
- typedef struct {
- char *buf;
- size_t len;
- size_t pos;
- size_t eof;
- char **bufloc;
- size_t *sizeloc;
- } __oms_cookie;
- static ssize_t oms_write(register void *cookie, const char *buf, size_t bufsize)
- {
- register char *newbuf;
- size_t count;
-
- count = COOKIE->len - COOKIE->pos - 1;
- assert(COOKIE->pos < COOKIE->len);
- if (bufsize > count) {
- newbuf = realloc(COOKIE->buf, COOKIE->len + bufsize - count);
- if (newbuf) {
- *COOKIE->bufloc = COOKIE->buf = newbuf;
- COOKIE->len += (bufsize - count);
- } else {
- bufsize = count;
- if (count == 0) {
- __set_errno(EFBIG);
- return -1;
- }
- }
- }
- memcpy(COOKIE->buf + COOKIE->pos, buf, bufsize);
- COOKIE->pos += bufsize;
- if (COOKIE->pos > COOKIE->eof) {
- *COOKIE->sizeloc = COOKIE->eof = COOKIE->pos;
- COOKIE->buf[COOKIE->eof] = 0;
- }
- return bufsize;
- }
- static int oms_seek(register void *cookie, __offmax_t *pos, int whence)
- {
- __offmax_t p = *pos;
- register char *buf;
- size_t leastlen;
-
- assert(((unsigned int) whence) <= 2);
- if (whence != SEEK_SET) {
- p += (whence == SEEK_CUR) ? COOKIE->pos : COOKIE->eof;
- }
-
-
- if (((uintmax_t) p) >= SIZE_MAX - 1) {
- return -1;
- }
- leastlen = ((size_t) p) + 1;
- if (leastlen >= COOKIE->len) {
- buf = realloc(COOKIE->buf, leastlen);
- if (buf) {
- *COOKIE->bufloc = COOKIE->buf = buf;
- COOKIE->len = leastlen;
- memset(buf + COOKIE->eof, leastlen - COOKIE->eof, 0);
- } else {
-
- return -1;
- }
- }
- *pos = COOKIE->pos = --leastlen;
- if (leastlen > COOKIE->eof) {
- memset(COOKIE->buf + COOKIE->eof, leastlen - COOKIE->eof, 0);
- *COOKIE->sizeloc = COOKIE->eof;
- }
- return 0;
- }
- static int oms_close(void *cookie)
- {
- free(cookie);
- return 0;
- }
- #undef COOKIE
- static const cookie_io_functions_t _oms_io_funcs = {
- NULL, oms_write, oms_seek, oms_close
- };
- FILE *open_memstream(char **__restrict bufloc, size_t *__restrict sizeloc)
- {
- register __oms_cookie *cookie;
- register FILE *fp;
- if ((cookie = malloc(sizeof(__oms_cookie))) != NULL) {
- if ((cookie->buf = malloc(cookie->len = BUFSIZ)) == NULL) {
- goto EXIT_cookie;
- }
- *cookie->buf = 0;
- *(cookie->bufloc = bufloc) = cookie->buf;
- *(cookie->sizeloc = sizeloc) = cookie->eof = cookie->pos = 0;
-
- #ifndef __BCC__
- fp = fopencookie(cookie, "w", _oms_io_funcs);
- #else
- fp = fopencookie(cookie, "w", &_oms_io_funcs);
- #endif
-
- if (fp != NULL) {
- return fp;
- }
- }
- if (cookie->buf != NULL) {
- free(cookie->buf);
- }
- EXIT_cookie:
- free(cookie);
- return NULL;
- }
- #endif
- #endif
- #ifdef L_fopencookie
- #ifdef __STDIO_GLIBC_CUSTOM_STREAMS
- #ifndef __BCC__
- FILE *fopencookie(void * __restrict cookie, const char * __restrict mode,
- cookie_io_functions_t io_functions)
- {
- FILE *stream;
-
- if ((stream = _stdio_fopen(((char *)(INT_MAX-1)),
- mode, NULL, INT_MAX))
- != NULL
- ) {
- stream->filedes = -1;
- stream->gcs = io_functions;
- stream->cookie = cookie;
- }
- #if !defined(__STDIO_BUFFERS) && !defined(__STDIO_THREADSAFE)
-
- __STDIO_THREADLOCK_OPENLIST;
- stream->nextopen = _stdio_openlist;
- _stdio_openlist = stream;
- __STDIO_THREADUNLOCK_OPENLIST;
- #endif
- return stream;
- }
- #else
- FILE *_fopencookie(void * __restrict cookie, const char * __restrict mode,
- register cookie_io_functions_t *io_functions)
- {
- register FILE *stream;
-
- if ((stream = _stdio_fopen(((char *)(INT_MAX-1)),
- mode, NULL, INT_MAX))
- != NULL
- ) {
- stream->filedes = -1;
- stream->gcs.read = io_functions->read;
- stream->gcs.write = io_functions->write;
- stream->gcs.seek = io_functions->seek;
- stream->gcs.close = io_functions->close;
- stream->cookie = cookie;
- }
- #if !defined(__STDIO_BUFFERS) && !defined(__STDIO_THREADSAFE)
-
- __STDIO_THREADLOCK_OPENLIST;
- stream->nextopen = _stdio_openlist;
- _stdio_openlist = stream;
- __STDIO_THREADUNLOCK_OPENLIST;
- #endif
- return stream;
- }
- #endif
- #endif
- #endif
- #ifdef L___fbufsize
- size_t __fbufsize(register FILE * __restrict stream)
- {
- #ifdef __STDIO_BUFFERS
- return (stream->modeflags & __FLAG_NBF)
- ? 0 : (stream->bufend - stream->bufstart);
- #else
- return 0;
- #endif
- }
- #endif
- #ifdef L___freading
- int __freading(FILE * __restrict stream)
- {
- return stream->modeflags & (__FLAG_READING|__FLAG_READONLY);
- }
- #endif
- #ifdef L___fwriting
- int __fwriting(FILE * __restrict stream)
- {
- return stream->modeflags & (__FLAG_WRITING|__FLAG_WRITEONLY);
- }
- #endif
- #ifdef L___freadable
- int __freadable(FILE * __restrict stream)
- {
- return ~(stream->modeflags & __FLAG_WRITEONLY);
- }
- #endif
- #ifdef L___fwritable
- int __fwritable(FILE * __restrict stream)
- {
- return ~(stream->modeflags & __FLAG_READONLY);
- }
- #endif
- #ifdef L___flbf
- int __flbf(FILE * __restrict stream)
- {
- #ifdef __STDIO_BUFFERS
- return (stream->modeflags & __FLAG_LBF);
- #else
-
- return __FLAG_NBF;
- #endif
- }
- #endif
- #ifdef L___fpurge
- void __fpurge(register FILE * __restrict stream)
- {
- #ifdef __STDIO_BUFFERS
- #ifdef __STDIO_PUTC_MACRO
- stream->bufputc =
- #endif
- #ifdef __STDIO_GETC_MACRO
- stream->bufgetc =
- #endif
- stream->bufpos = stream->bufread = stream->bufstart;
- #endif
-
- stream->modeflags &= ~(__FLAG_READING|__FLAG_WRITING|__MASK_UNGOT);
- }
- #endif
- #ifdef L___fpending
- #ifdef __STDIO_WIDE
- #warning Unlike the glibc version, this __fpending returns bytes in buffer for wide streams too!
- 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.")
- #endif
- size_t __fpending(register FILE * __restrict stream)
- {
- #ifdef __STDIO_BUFFERS
-
- return (stream->modeflags & (__FLAG_READING|__FLAG_READONLY))
- ? 0 : (stream->bufpos - stream->bufstart);
- #else
- return 0;
- #endif
- }
- #endif
- #ifdef L__flushlbf
- void _flushlbf(void)
- {
- #if defined(__STDIO_BUFFERS) || defined(__STDIO_GLIBC_CUSTOM_STREAMS)
- fflush((FILE *) &_stdio_openlist);
- #else
-
- #endif
- }
- #endif
- #ifdef L___fsetlocking
- #ifdef __UCLIBC_MJN3_ONLY__
- link_warning(__fsetlocking, "Oddly enough, __fsetlocking() is NOT threadsafe.")
- #endif
- int __fsetlocking(FILE *stream, int locking_mode)
- {
- #ifdef __STDIO_THREADSAFE
- int old_mode;
- #endif
- assert((FSETLOCKING_QUERY == 0) && (FSETLOCKING_INTERNAL == 1)
- && (FSETLOCKING_BYCALLER == 2));
- assert(((unsigned int) locking_mode) <= 2);
- #ifdef __STDIO_THREADSAFE
- old_mode = stream->user_locking;
- assert(((unsigned int) old_mode) <= 1);
- if (locking_mode != FSETLOCKING_QUERY) {
-
- stream->user_locking = (locking_mode == FSETLOCKING_BYCALLER);
- }
- return 2 - old_mode;
- #else
- return FSETLOCKING_BYCALLER;
- #endif
- }
- #endif
- #ifdef L_flockfile
- void flockfile(FILE *stream)
- {
- #ifdef __STDIO_THREADSAFE
- __pthread_mutex_lock(&stream->lock);
- #endif
- }
- #endif
- #ifdef L_ftrylockfile
- int ftrylockfile(FILE *stream)
- {
- #ifdef __STDIO_THREADSAFE
- return __pthread_mutex_trylock(&stream->lock);
- #else
- return 1;
- #endif
- }
- #endif
- #ifdef L_funlockfile
- void funlockfile(FILE *stream)
- {
- #ifdef __STDIO_THREADSAFE
- __pthread_mutex_unlock(&stream->lock);
- #endif
- }
- #endif
- #ifdef L_getline
- ssize_t getline(char **__restrict lineptr, size_t *__restrict n,
- FILE *__restrict stream)
- {
- return __getdelim(lineptr, n, '\n', stream);
- }
- #endif
- #ifdef L_getdelim
- weak_alias(__getdelim,getdelim);
- #define GETDELIM_GROWBY 64
- ssize_t __getdelim(char **__restrict lineptr, size_t *__restrict n,
- int delimiter, register FILE *__restrict stream)
- {
- register char *buf;
- size_t pos;
- int c;
- if (!lineptr || !n || !stream) {
- __set_errno(EINVAL);
- return -1;
- }
- if (!(buf = *lineptr)) {
- *n = 0;
- }
- pos = 1;
- __STDIO_THREADLOCK(stream);
- do {
- if (pos >= *n) {
- if (!(buf = realloc(buf, *n + GETDELIM_GROWBY))) {
- __set_errno(ENOMEM);
- break;
- }
- *n += GETDELIM_GROWBY;
- *lineptr = buf;
- }
- } while (((c = (getc_unlocked)(stream)) != EOF)
- && ((buf[pos++ - 1] = c) != delimiter));
- __STDIO_THREADUNLOCK(stream);
- if (--pos) {
- buf[pos] = 0;
- return pos;
- }
- return -1;
- }
- #endif
- #ifdef L__stdio_fsfopen
- FILE *_stdio_fsfopen(const char * __restrict filename,
- const char * __restrict mode,
- register FILE * __restrict stream)
- {
- #ifdef __STDIO_BUFFERS
- stream->modeflags = __FLAG_FBF;
- #if __STDIO_BUILTIN_BUF_SIZE > 0
- stream->bufstart = stream->builtinbuf;
- stream->bufend = stream->builtinbuf + sizeof(stream->builtinbuf);
- #else
- stream->bufend = stream->bufstart = NULL;
- #endif
- #endif
- return _stdio_fopen(filename, mode, stream, -1);
- }
- #endif
- #ifdef L__stdio_adjpos
- int _stdio_adjpos(register FILE * __restrict stream, register __offmax_t *pos)
- {
- __offmax_t r;
- int cor = stream->modeflags & __MASK_UNGOT;
- assert(cor <= 2);
- #ifdef __STDIO_WIDE
-
- if (cor && (stream->modeflags & __FLAG_WIDE)) {
- if ((((stream->modeflags & __MASK_UNGOT) > 1) || stream->ungot[1])) {
- return -1;
- }
- if (stream->modeflags & __MASK_UNGOT) {
- cor = cor - 1 + stream->ungot_width[1];
- }
- if (stream->state.mask > 0) {
- cor -= stream->ungot_width[0];
- }
- }
- #endif
- #ifdef __STDIO_BUFFERS
- if (stream->modeflags & __FLAG_WRITING) {
- cor -= (stream->bufpos - stream->bufstart);
- }
- if (stream->modeflags & __FLAG_READING) {
- cor += (stream->bufread - stream->bufpos);
- }
- #endif
- r = *pos;
- return ((*pos -= cor) > r) ? -cor : cor;
- }
- #endif
- #ifdef L__stdio_lseek
- int _stdio_lseek(register FILE *stream, register __offmax_t *pos, int whence)
- {
- __offmax_t res;
- #ifdef __STDIO_GLIBC_CUSTOM_STREAMS
- if (stream->cookie != &stream->filedes) {
- return (((stream->gcs.seek == NULL)
- || ((stream->gcs.seek)(stream->cookie, pos, whence) < 0))
- ? -1 : 0);
- }
- #endif
- #ifdef __STDIO_LARGE_FILES
- res = lseek64(stream->filedes, *pos, whence);
- #else
- res = lseek(stream->filedes, *pos, whence);
- #endif
- return (res >= 0) ? ((*pos = res), 0) : -1;
- }
- #endif
- #ifdef L__stdio_fread
- static ssize_t _stdio_READ(register FILE *stream, unsigned char *buf, size_t bufsize)
- {
- ssize_t rv;
-
- if ((bufsize == 0) || (stream->modeflags & __FLAG_EOF)) {
- return 0;
- }
- if (bufsize > SSIZE_MAX) {
- bufsize = SSIZE_MAX;
- }
- #ifdef __BCC__
- TRY_READ:
- #endif
- rv = __READ(stream, buf, bufsize);
- if (rv > 0) {
- #ifdef __STDIO_GLIBC_CUSTOM_STREAMS
- assert(rv <= bufsize);
- if (rv > bufsize) {
- rv = bufsize;
- }
- #endif
- } else if (rv == 0) {
- stream->modeflags |= __FLAG_EOF;
- } else {
- #ifdef __BCC__
- if (errno == EINTR) {
- goto TRY_READ;
- }
- #endif
- stream->modeflags |= __FLAG_ERROR;
- rv = 0;
- }
- return rv;
- }
- size_t _stdio_fread(unsigned char *buffer, size_t bytes, register FILE *stream)
- {
- __stdio_validate_FILE(stream);
- #ifdef __STDIO_BUFFERS
- if (stream->modeflags
- #ifdef __STDIO_AUTO_RW_TRANSITION
- & (__FLAG_WRITEONLY)
- #else
-
- & (__FLAG_WRITEONLY|__FLAG_WRITING)
- #endif
- ) {
- #ifdef __STDIO_PUTC_MACRO
- stream->bufputc = stream->bufstart;
- #endif
- stream->modeflags |= __FLAG_ERROR;
-
- __set_errno(EBADF);
- return 0;
- }
-
- #if defined(__STDIO_PUTC_MACRO) || defined(__STDIO_GETC_MACRO)
- #ifdef __STDIO_PUTC_MACRO
- stream->bufputc =
- #endif
- #ifdef __STDIO_GETC_MACRO
- stream->bufgetc =
- #endif
- stream->bufstart;
- #endif
- if (stream->modeflags & __MASK_BUFMODE) {
-
- fflush_unlocked((FILE *) &_stdio_openlist);
- }
- #ifdef __STDIO_AUTO_RW_TRANSITION
- if ((stream->modeflags & __FLAG_WRITING)
- && (fflush_unlocked(stream) == EOF)
- ) {
- return 0;
- }
- #endif
- stream->modeflags |= __FLAG_READING;
- {
- register unsigned char *p = (unsigned char *) buffer;
-
- while (bytes && (stream->modeflags & __MASK_UNGOT)) {
- #ifdef __STDIO_WIDE
- assert(stream->modeflags & __FLAG_NARROW);
- #endif
- *p++ = stream->ungot[(--stream->modeflags) & __MASK_UNGOT];
- stream->ungot[1] = 0;
- --bytes;
- }
-
- FROM_BUF:
- while (bytes && (stream->bufpos < stream->bufread)) {
- --bytes;
- *p++ = *stream->bufpos++;
- }
- if (bytes > 0) {
- ssize_t len;
- if (stream->filedes == -2) {
- stream->modeflags |= __FLAG_EOF;
- goto DONE;
- }
-
- stream->bufpos = stream->bufread = stream->bufstart;
- if (bytes <= stream->bufend - stream->bufread) {
-
- len = _stdio_READ(stream, stream->bufread,
- stream->bufend - stream->bufread);
- if (len > 0) {
- stream->bufread += len;
- goto FROM_BUF;
- }
- } else {
-
-
- len = _stdio_READ(stream, p, bytes);
- if (len > 0) {
- p += len;
- bytes -= len;
- goto FROM_BUF;
- }
- }
- }
- #ifdef __STDIO_GETC_MACRO
- if (!(stream->modeflags
- & (__FLAG_WIDE|__MASK_UNGOT|__MASK_BUFMODE|__FLAG_ERROR))
- ) {
- stream->bufgetc = stream->bufread;
- }
- #endif
- DONE:
- __stdio_validate_FILE(stream);
- return (p - (unsigned char *)buffer);
- }
- #else
- if (stream->modeflags
- #ifdef __STDIO_AUTO_RW_TRANSITION
- & (__FLAG_WRITEONLY)
- #else
-
- & (__FLAG_WRITEONLY|__FLAG_WRITING)
- #endif
- ) {
- stream->modeflags |= __FLAG_ERROR;
-
- __set_errno(EBADF);
- return 0;
- }
- #ifdef __STDIO_AUTO_RW_TRANSITION
- stream->modeflags &= ~(__FLAG_WRITING);
- #endif
- stream->modeflags |= __FLAG_READING;
- {
- register unsigned char *p = (unsigned char *) buffer;
-
- while (bytes && (stream->modeflags & __MASK_UNGOT)) {
- #ifdef __STDIO_WIDE
- assert(stream->modeflags & __FLAG_NARROW);
- #endif
- *p++ = stream->ungot[(--stream->modeflags) & __MASK_UNGOT];
- stream->ungot[1] = 0;
- --bytes;
- }
- while (bytes > 0) {
- ssize_t len = _stdio_READ(stream, p, (unsigned) bytes);
- if (len == 0) {
- break;
- }
- p += len;
- bytes -= len;
- }
- __stdio_validate_FILE(stream);
- return (p - (unsigned char *)buffer);
- }
- #endif
- }
- #endif
- #ifdef L__stdio_fwrite
- static size_t _stdio_WRITE(register FILE *stream,
- register const unsigned char *buf, size_t bufsize)
- {
- size_t todo;
- ssize_t rv, stodo;
- todo = bufsize;
- while (todo) {
- stodo = (todo <= SSIZE_MAX) ? todo : SSIZE_MAX;
- rv = __WRITE(stream, buf, stodo);
- if (rv >= 0) {
- #ifdef __STDIO_GLIBC_CUSTOM_STREAMS
- assert(rv <= stodo);
- if (rv > stodo) {
- rv = stodo;
- }
- #endif
- todo -= rv;
- buf += rv;
- } else
- #ifdef __BCC__
- if (errno != EINTR)
- #endif
- {
- stream->modeflags |= __FLAG_ERROR;
- break;
- }
- }
- return bufsize - todo;
- }
- size_t _stdio_fwrite(const unsigned char *buffer, size_t bytes,
- register FILE *stream)
- {
- #ifdef __STDIO_BUFFERS
- register const unsigned char *p;
- __stdio_validate_FILE(stream);
- if ((stream->modeflags & __FLAG_READONLY)
- #ifndef __STDIO_AUTO_RW_TRANSITION
-
- || ((stream->modeflags & (__FLAG_READING|__FLAG_EOF))
- == __FLAG_READING)
- #endif
- ) {
- stream->modeflags |= __FLAG_ERROR;
-
- __set_errno(EBADF);
- return 0;
- }
- #ifdef __STDIO_AUTO_RW_TRANSITION
-
- if (stream->modeflags & __FLAG_READING) {
- if (((stream->bufpos < stream->bufread)
- || (stream->modeflags & __MASK_UNGOT))
-
-
- && fseek(stream, 0L,
- ((stream->modeflags & __FLAG_APPEND)
- ? SEEK_END : SEEK_CUR))
- ) {
-
- stream->modeflags |= __FLAG_ERROR;
- return 0;
- }
-
- #ifdef __STDIO_GETC_MACRO
- stream->bufgetc =
- #endif
- stream->bufpos = stream->bufread = stream->bufstart;
- } else
- #endif
- if ((stream->modeflags & (__FLAG_WRITING|__FLAG_APPEND)) == __FLAG_APPEND) {
-
- __offmax_t pos[1];
- *pos = 0;
- if (_stdio_lseek(stream, pos, SEEK_END) && (errno != EPIPE)) {
- stream->modeflags |= __FLAG_ERROR;
- return 0;
- }
- }
- #ifdef __STDIO_PUTC_MACRO
-
- stream->bufputc = stream->bufstart;
- #endif
-
- stream->modeflags &= ~(__FLAG_READING|__FLAG_WRITING);
- {
- const unsigned char *buf0 = buffer;
- size_t write_count = 1;
- if (!buffer) {
- FFLUSH:
- {
- size_t count = stream->bufpos - stream->bufstart;
- p = stream->bufstart;
- if (stream->filedes == -2) {
- stream->modeflags |= __FLAG_WRITING;
- return (!buffer) ? 0 : ((buffer - buf0) + bytes);
- }
- {
- write_count = _stdio_WRITE(stream, p, count);
- p += write_count;
- count -= write_count;
- }
-
- stream->bufpos = stream->bufstart;
- while (count) {
- *stream->bufpos++ = *p++;
- --count;
- }
- if (!buffer) {
- __stdio_validate_FILE(stream);
- return stream->bufpos - stream->bufstart;
- }
- }
- }
- #if 1
-
- if ((stream->bufpos == stream->bufstart)
- && (stream->bufend - stream->bufstart <= bytes)
- && (stream->filedes != -2)) {
-
- {
- size_t rv = _stdio_WRITE(stream, buffer, bytes);
- buffer += rv;
- bytes -= rv;
- }
- } else
- #endif
-
- {
- size_t count = stream->bufend - stream->bufpos;
- p = buffer;
- if (count > bytes) {
- count = bytes;
- }
- bytes -= count;
- while (count) {
- *stream->bufpos++ = *buffer++;
- --count;
- }
- if (write_count) {
- if (bytes) {
- goto FFLUSH;
- }
- if (stream->modeflags & __FLAG_LBF) {
- while (p < buffer) {
- if (*p++ == '\n') {
- goto FFLUSH;
- }
- }
- }
- }
- }
- #ifdef __STDIO_PUTC_MACRO
- if (!(stream->modeflags & (__FLAG_WIDE|__MASK_BUFMODE|__FLAG_ERROR))) {
-
- stream->bufputc = stream->bufend;
- }
- #endif
- stream->modeflags |= __FLAG_WRITING;
- __stdio_validate_FILE(stream);
- return buffer - buf0;
- }
- #else
- __stdio_validate_FILE(stream);
- if ((stream->modeflags & __FLAG_READONLY)
- #ifndef __STDIO_AUTO_RW_TRANSITION
-
- || ((stream->modeflags & (__FLAG_READING|__FLAG_EOF))
- == __FLAG_READING)
- #endif
- ) {
- stream->modeflags |= __FLAG_ERROR;
-
- __set_errno(EBADF);
- return 0;
- }
-
- stream->modeflags &= ~(__FLAG_READING);
-
- stream->modeflags |= __FLAG_WRITING;
- {
- register unsigned char *p = (unsigned char *) buffer;
- ssize_t rv = _stdio_WRITE(stream, p, bytes);
- p += rv;
- bytes -= rv;
- __stdio_validate_FILE(stream);
- return (p - (unsigned char *)buffer);
- }
- #endif
- }
- #endif
- #ifdef L__stdio_init
- #ifndef NDEBUG
- void __stdio_validate_FILE(FILE *stream)
- {
- if (stream->filedes == -2) {
- return;
- }
- __STDIO_THREADLOCK(stream);
- #ifdef __STDIO_BUFFERS
- assert(stream->bufstart <= stream->bufread);
- if (stream->modeflags & (__FLAG_READING)) {
- assert(stream->bufpos <= stream->bufread);
- }
- assert(stream->bufread <= stream->bufend);
- assert(stream->bufpos <= stream->bufend);
- if ((stream->modeflags & __MASK_BUFMODE) == __FLAG_NBF) {
- assert(stream->bufstart == stream->bufend);
- }
- assert((stream->modeflags & __MASK_BUFMODE) <= __FLAG_NBF);
- #endif
- #ifdef __STDIO_PUTC_MACRO
- assert(stream->bufstart <= stream->bufputc);
- assert(stream->bufputc <= stream->bufend);
- if (stream->bufstart < stream->bufputc) {
- assert(stream->bufputc == stream->bufend);
- assert(stream->modeflags & (__FLAG_WRITING));
- assert(!(stream->modeflags
- & (__FLAG_WIDE|__MASK_BUFMODE|__MASK_UNGOT|__FLAG_READONLY))
- );
- }
- #endif
- #ifdef __STDIO_GETC_MACRO
- assert(stream->bufstart <= stream->bufgetc);
- assert(stream->bufgetc <= stream->bufread);
- if (stream->bufstart < stream->bufgetc) {
- assert(stream->modeflags & (__FLAG_READING));
- assert(!(stream->modeflags
- & (__FLAG_WIDE|__MASK_BUFMODE|__MASK_UNGOT|__FLAG_WRITEONLY))
- );
- }
- #endif
- assert((stream->modeflags & __MASK_UNGOT) != __MASK_UNGOT);
- if (stream->modeflags & __MASK_UNGOT1) {
- assert(stream->ungot[1] <= 1);
- }
- if (stream->modeflags & __MASK_UNGOT) {
- assert(!(stream->modeflags & __FLAG_EOF));
- }
- assert((stream->modeflags & (__FLAG_READONLY|__FLAG_WRITEONLY))
- != (__FLAG_READONLY|__FLAG_WRITEONLY));
-
- __STDIO_THREADUNLOCK(stream);
- }
- #endif
- #ifdef __STDIO_GLIBC_CUSTOM_STREAMS
- ssize_t _cs_read(void *cookie, char *buf, size_t bufsize)
- {
- return read(*((int *) cookie), buf, bufsize);
- }
- ssize_t _cs_write(void *cookie, const char *buf, size_t bufsize)
- {
- return write(*((int *) cookie), (char *) buf, bufsize);
- }
- int _cs_close(void *cookie)
- {
- return close(*((int *) cookie));
- }
- #endif
- #ifdef __STDIO_BUFFERS
- static unsigned char _fixed_buffers[2 * BUFSIZ];
- #define bufin (_fixed_buffers)
- #define bufout (_fixed_buffers + BUFSIZ)
- #endif
- static FILE _stdio_streams[] = {
- __STDIO_INIT_FILE_STRUCT(_stdio_streams[0], __FLAG_LBF|__FLAG_READONLY, \
- 0, _stdio_streams + 1, bufin, BUFSIZ ),
- __STDIO_INIT_FILE_STRUCT(_stdio_streams[1], __FLAG_LBF|__FLAG_WRITEONLY, \
- 1, _stdio_streams + 2, bufout, BUFSIZ ),
- __STDIO_INIT_FILE_STRUCT(_stdio_streams[2], __FLAG_NBF|__FLAG_WRITEONLY, \
- 2, 0, 0, 0 )
- };
- FILE *stdin = _stdio_streams + 0;
- FILE *stdout = _stdio_streams + 1;
- FILE *stderr = _stdio_streams + 2;
- #if defined(__STDIO_BUFFERS) || defined(__STDIO_GLIBC_CUSTOM_STREAMS)
- #ifdef __STDIO_BUFFERS
- FILE *_stdio_openlist = _stdio_streams;
- #else
- FILE *_stdio_openlist = NULL;
- #endif
- #ifdef __STDIO_THREADSAFE
- pthread_mutex_t _stdio_openlist_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
- void __stdio_init_mutex(pthread_mutex_t *m)
- {
- static const pthread_mutex_t __stdio_mutex_initializer
- = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
- memcpy(m, &__stdio_mutex_initializer, sizeof(__stdio_mutex_initializer));
- }
- #endif
- void _stdio_term(void)
- {
- #if defined(__STDIO_GLIBC_CUSTOM_STREAMS) || defined(__STDIO_THREADSAFE)
- register FILE *ptr;
- #endif
-
- #if defined(__STDIO_BUFFERS) || defined(__STDIO_GLIBC_CUSTOM_STREAMS)
- #ifdef __STDIO_THREADSAFE
-
- __stdio_init_mutex(&_stdio_openlist_lock);
- for (ptr = _stdio_openlist ; ptr ; ptr = ptr->nextopen ) {
- ptr->user_locking = 1;
- __stdio_init_mutex(&ptr->lock);
- }
- #endif
- #endif
- #ifdef __STDIO_BUFFERS
-
-
- fflush_unlocked(NULL);
- #endif
-
- #ifdef __STDIO_GLIBC_CUSTOM_STREAMS
- for (ptr = _stdio_openlist ; ptr ; ptr = ptr->nextopen ) {
- if (ptr->cookie != &ptr->filedes) {
- __CLOSE(ptr);
- ptr->cookie = NULL;
- #if 0
- } else {
-
- stream->bufpos = stream->bufread = stream->bufend
- #ifdef __STDIO_GETC_MACRO
- = stream->bufgetc
- #endif
- #ifdef __STDIO_PUTC_MACRO
- = stream->bufputc
- #endif
- = stream->bufstart;
- #endif
- }
- }
- #endif
- }
- #if defined(__STDIO_BUFFERS) || !defined(__UCLIBC__)
- void _stdio_init(void)
- {
- #ifdef __STDIO_BUFFERS
- int old_errno = errno;
-
- _stdio_streams[0].modeflags ^= (1-isatty(0)) * __FLAG_LBF;
- _stdio_streams[1].modeflags ^= (1-isatty(1)) * __FLAG_LBF;
- __set_errno(old_errno);
- #endif
- #ifndef __UCLIBC__
- atexit(_stdio_term);
- #endif
- }
- #endif
- #endif
- #endif
- #ifdef L_remove
- #include <unistd.h>
- #include <errno.h>
- int remove(register const char *filename)
- {
- int old_errno = errno;
-
- return (((rmdir(filename) == 0)
- || ((errno == ENOTDIR)
- && ((__set_errno(old_errno), unlink(filename)) == 0)))
- ? 0 : -1);
- }
- #endif
- #ifdef L_fclose
- int fclose(register FILE *stream)
- {
- #if defined(__STDIO_BUFFERS) || defined(__STDIO_GLIBC_CUSTOM_STREAMS)
- register FILE *ptr;
- int rv = 0;
- #ifdef __STDIO_THREADSAFE
-
- do {
- __STDIO_THREADLOCK(stream);
- if (__STDIO_THREADTRYLOCK_OPENLIST == 0) {
- break;
- }
- __STDIO_THREADUNLOCK(stream);
- usleep(10000);
- } while (1);
- #endif
- __stdio_validate_FILE(stream);
- #ifdef __STDIO_BUFFERS
- if (stream->modeflags & __FLAG_WRITING) {
- rv = fflush_unlocked(stream);
- }
- #ifdef __STDIO_GETC_MACRO
-
- stream->bufgetc = stream->bufstart;
- #endif
- #endif
-
- ptr = _stdio_openlist;
- if (ptr == stream) {
- _stdio_openlist = stream->nextopen;
- } else {
- while (ptr) {
- if (ptr->nextopen == stream) {
- ptr->nextopen = stream->nextopen;
- break;
- }
- ptr = ptr->nextopen;
- }
- }
- __STDIO_THREADUNLOCK_OPENLIST;
- if (__CLOSE(stream) < 0) {
- rv = EOF;
- }
- #ifdef __STDIO_GLIBC_CUSTOM_STREAMS
- stream->cookie = &(stream->filedes);
- stream->gcs.read = _cs_read;
- stream->gcs.write = _cs_write;
- stream->gcs.seek = 0;
- stream->gcs.close = _cs_close;
- #endif
- stream->filedes = -1;
- #ifdef __STDIO_BUFFERS
- if (stream->modeflags & __FLAG_FREEBUF) {
- free(stream->bufstart);
- }
- #endif
-
- __STDIO_THREADUNLOCK(stream);
-
- if (stream->modeflags & __FLAG_FREEFILE) {
- #ifdef __STDIO_GLIBC_CUSTOM_STREAMS
- stream->cookie = NULL;
- #endif
- free(stream);
- }
- return rv;
- #else
- int rv = 0;
- __STDIO_THREADLOCK(stream);
- __stdio_validate_FILE(stream);
- if (__CLOSE(stream) < 0) {
- rv = EOF;
- }
- #ifdef __STDIO_GLIBC_CUSTOM_STREAMS
- stream->cookie = &(stream->filedes);
- stream->gcs.read = _cs_read;
- stream->gcs.write = _cs_write;
- stream->gcs.seek = 0;
- stream->gcs.close = _cs_close;
- #endif
- stream->filedes = -1;
- __STDIO_THREADUNLOCK(stream);
-
- if (stream->modeflags & __FLAG_FREEFILE) {
- #ifdef __STDIO_GLIBC_CUSTOM_STREAMS
- stream->cookie = NULL;
- #endif
- free(stream);
- }
- return rv;
- #endif
- }
- #endif
- #ifdef L_fflush
- #ifndef __STDIO_THREADSAFE
- strong_alias(fflush_unlocked,fflush)
- #else
- int fflush(register FILE *stream)
- {
- int retval;
- if ((stream != NULL)
- #if defined(__STDIO_BUFFERS) || defined(__STDIO_GLIBC_CUSTOM_STREAMS)
- && (stream != (FILE *) &_stdio_openlist)
- #endif
- ) {
- __STDIO_THREADLOCK(stream);
- retval = fflush_unlocked(stream);
- __STDIO_THREADUNLOCK(stream);
- } else {
- retval = fflush_unlocked(stream);
- }
- return retval;
- }
- #endif
- int fflush_unlocked(register FILE *stream)
- {
- #ifdef __STDIO_BUFFERS
- int rv = 0;
- unsigned short mask = (__FLAG_NBF|__FLAG_LBF);
- #ifndef NDEBUG
- if ((stream != NULL) && (stream != (FILE *) &_stdio_openlist)) {
- __stdio_validate_FILE(stream);
- }
- #endif
- if (stream == (FILE *) &_stdio_openlist) {
- stream = NULL;
- mask = __FLAG_LBF;
- }
- if (stream == NULL) {
-
- __STDIO_THREADLOCK_OPENLIST;
-
- for (stream = _stdio_openlist; stream; stream = stream->nextopen) {
- if (((stream->modeflags ^ __FLAG_NBF) & mask)
- && (stream->modeflags & __FLAG_WRITING)
- && fflush(stream)) {
- rv = EOF;
- }
- }
- __STDIO_THREADUNLOCK_OPENLIST;
- } else if (stream->modeflags & __FLAG_WRITING) {
- if (_stdio_fwrite(NULL, 0, stream) > 0) {
- rv = -1;
- }
- #ifdef __UCLIBC_MJN3_ONLY__
- #warning WISHLIST: Add option to test for undefined behavior of fflush.
- #endif
- #if 0
- } else if (stream->modeflags & (__FLAG_READING|__FLAG_READONLY)) {
-
- stream->modeflags |= __FLAG_ERROR;
- __set_errno(EBADF);
- rv = -1;
- #endif
- }
- #ifndef NDEBUG
- if ((stream != NULL) && (stream != (FILE *) &_stdio_openlist)) {
- __stdio_validate_FILE(stream);
- }
- #endif
- return rv;
- #else
- #ifndef NDEBUG
- if ((stream != NULL)
- #if defined(__STDIO_BUFFERS) || defined(__STDIO_GLIBC_CUSTOM_STREAMS)
- && (stream != (FILE *) &_stdio_openlist)
- #endif
- ) {
- __stdio_validate_FILE(stream);
- }
- #endif
- #ifdef __UCLIBC_MJN3_ONLY__
- #warning WISHLIST: Add option to test for undefined behavior of fflush.
- #endif
- #if 0
- return ((stream != NULL)
- && (stream->modeflags & (__FLAG_READING|__FLAG_READONLY))
- ? ((stream->modeflags |= __FLAG_ERROR), __set_errno(EBADF), EOF)
- : 0 );
- #else
- return 0;
- #endif
- #endif
- }
- #endif
- #ifdef L_fopen
- FILE *fopen(const char * __restrict filename, const char * __restrict mode)
- {
- return _stdio_fopen(filename, mode, NULL, -1);
- }
- #endif
- #ifdef L__stdio_fopen
- #if O_ACCMODE != 3 || O_RDONLY != 0 || O_WRONLY != 1 || O_RDWR != 2
- #error Assumption violated - mode constants
- #endif
- FILE *_stdio_fopen(const char * __restrict filename,
- register const char * __restrict mode,
- register FILE * __restrict stream, int filedes)
- {
- __mode_t open_mode;
-
- open_mode = O_RDONLY;
- if (*mode != 'r') {
- open_mode = (O_WRONLY | O_CREAT | O_TRUNC);
- if (*mode != 'w') {
- open_mode = (O_WRONLY | O_CREAT | O_APPEND);
- if (*mode != 'a') {
- __set_errno(EINVAL);
- if (stream) {
- goto FREE_STREAM;
- }
- return NULL;
- }
- }
- }
- if ((*++mode == 'b')) {
- ++mode;
- }
- if (*mode == '+') {
- ++mode;
- open_mode &= ~(O_RDONLY | O_WRONLY);
- open_mode |= O_RDWR;
- }
- #if defined(__STDIO_FOPEN_EXCLUSIVE_MODE) || defined(__STDIO_FOPEN_LARGEFILE_MODE)
- for ( ; *mode ; ++mode) {
- #ifdef __STDIO_FOPEN_EXCLUSIVE_MODE
- if (*mode == 'x') {
- open_mode |= O_EXCL;
- continue;
- }
- #endif
- #ifdef __STDIO_FOPEN_LARGEFILE_MODE
- if (*mode == 'F') {
- open_mode |= O_LARGEFILE;
- continue;
- }
- #endif
- }
- #endif
- #ifdef __BCC__
- mode = filename;
- #define filename mode
- #endif
- if (!stream) {
- #ifdef __STDIO_BUFFERS
- if ((stream = malloc(sizeof(FILE))) == NULL) {
- return stream;
- }
- stream->modeflags = __FLAG_FREEFILE;
- if ((stream->bufstart = malloc(BUFSIZ)) != 0) {
- stream->bufend = stream->bufstart + BUFSIZ;
- stream->modeflags |= __FLAG_FREEBUF;
- } else {
- #if __STDIO_BUILTIN_BUF_SIZE > 0
- stream->bufstart = stream->unbuf;
- stream->bufend = stream->unbuf + sizeof(stream->unbuf);
- #else
- stream->bufstart = stream->bufend = NULL;
- #endif
- }
- #else
- if ((stream = malloc(sizeof(FILE))) == NULL) {
- return stream;
- }
- stream->modeflags = __FLAG_FREEFILE;
- #endif
- }
- if (filedes >= 0) {
-
- int i = (open_mode & (O_ACCMODE|O_LARGEFILE)) + 1;
-
- if (((i & (((int) filename) + 1)) != i)
- || (((open_mode & O_APPEND)
- && !(((int) filename) & O_APPEND)
- && fcntl(filedes, F_SETFL, O_APPEND)))
- ) {
- __set_errno(EINVAL);
- filedes = -1;
- }
- #ifdef __STDIO_LARGE_FILES
-
- open_mode |= (((int) filename) & O_LARGEFILE);
- #endif
- stream->filedes = filedes;
- } else {
- #ifdef __STDIO_LARGE_FILES
- if (filedes < -1) {
- open_mode |= O_LARGEFILE;
- }
- #endif
- stream->filedes = open(filename, open_mode, 0666);
- }
- if (stream->filedes < 0) {
- FREE_STREAM:
- #ifdef __STDIO_BUFFERS
- if (stream->modeflags & __FLAG_FREEBUF) {
- free(stream->bufstart);
- }
- #endif
- if (stream->modeflags & __FLAG_FREEFILE) {
- free(stream);
- }
- return NULL;
- }
- #ifdef __STDIO_BUFFERS
- {
-
- int old_errno = errno;
- stream->modeflags |= (isatty(stream->filedes) * __FLAG_LBF);
- __set_errno(old_errno);
- }
- #endif
- stream->modeflags |=
- #if (O_APPEND == __FLAG_APPEND) \
- && ((O_LARGEFILE == __FLAG_LARGEFILE) || (O_LARGEFILE == 0))
- (open_mode & (O_APPEND|O_LARGEFILE)) |
- #else
- ((open_mode & O_APPEND) ? __FLAG_APPEND : 0) |
- #ifdef __STDIO_LARGE_FILES
- ((open_mode & O_LARGEFILE) ? __FLAG_LARGEFILE : 0) |
- #endif
- #endif
- ((((open_mode & O_ACCMODE) + 1) ^ 0x03) * __FLAG_WRITEONLY);
- #ifdef __STDIO_BUFFERS
- #ifdef __STDIO_GETC_MACRO
- stream->bufgetc =
- #endif
- #ifdef __STDIO_PUTC_MACRO
- stream->bufputc =
- #endif
- stream->bufpos = stream->bufread = stream->bufstart;
- #endif
- #ifdef __STDIO_GLIBC_CUSTOM_STREAMS
- stream->cookie = &(stream->filedes);
- stream->gcs.read = _cs_read;
- stream->gcs.write = _cs_write;
- stream->gcs.seek = 0;
- stream->gcs.close = _cs_close;
- #endif
- #ifdef __STDIO_WIDE
- stream->ungot_width[0] = 0;
- #endif
- #ifdef __STDIO_MBSTATE
- __INIT_MBSTATE(&(stream->state));
- #endif
- #ifdef __STDIO_THREADSAFE
- stream->user_locking = 0;
- __stdio_init_mutex(&stream->lock);
- #endif
- #if defined(__STDIO_BUFFERS) \
- || (defined(__STDIO_THREADSAFE) && defined(__STDIO_GLIBC_CUSTOM_STREAMS))
- __STDIO_THREADLOCK_OPENLIST;
- stream->nextopen = _stdio_openlist;
- _stdio_openlist = stream;
- __STDIO_THREADUNLOCK_OPENLIST;
- #endif
- __stdio_validate_FILE(stream);
- return stream;
- #ifdef __BCC__
- #undef filename
- #endif
- }
- #endif
- #ifdef L_freopen
- FILE *freopen(const char * __restrict filename, const char * __restrict mode,
- register FILE * __restrict stream)
- {
-
- unsigned short dynmode;
- register FILE *fp;
- __STDIO_THREADLOCK(stream);
-
-
- dynmode =
- #ifdef __STDIO_BUFFERS
-
- #endif
- (stream->modeflags & (__FLAG_FREEBUF|__FLAG_FREEFILE));
- stream->modeflags &= ~(__FLAG_FREEBUF|__FLAG_FREEFILE);
- fclose(stream);
- stream->modeflags = dynmode;
- fp = _stdio_fopen(filename, mode, stream, -1);
- __STDIO_THREADUNLOCK(stream);
- return fp;
- }
- #endif
- #ifdef L_freopen64
- FILE *freopen64(const char * __restrict filename, const char * __restrict mode,
- register FILE * __restrict stream)
- {
- unsigned short dynmode;
- register FILE *fp;
- __STDIO_THREADLOCK(stream);
-
-
- dynmode =
- #ifdef __STDIO_BUFFERS
-
- #endif
- (stream->modeflags & (__FLAG_FREEBUF|__FLAG_FREEFILE));
- stream->modeflags &= ~(__FLAG_FREEBUF|__FLAG_FREEFILE);
- fclose(stream);
- stream->modeflags = dynmode;
- fp = _stdio_fopen(filename, mode, stream, -2);
- __STDIO_THREADUNLOCK(stream);
- return fp;
- }
- #endif
- #ifdef L_setbuf
- void setbuf(FILE * __restrict stream, register char * __restrict buf)
- {
- #ifdef __STDIO_BUFFERS
- int mode;
- mode = (buf != NULL) ? _IOFBF : _IONBF;
- setvbuf(stream, buf, mode, BUFSIZ);
- #else
-
-
- #endif
- }
- #endif
- #ifdef L_setvbuf
- int setvbuf(register FILE * __restrict stream, register char * __restrict buf,
- int mode, size_t size)
- {
- #ifdef __STDIO_BUFFERS
- int allocated_buf_flag;
- int rv = EOF;
- __STDIO_THREADLOCK(stream);
- __stdio_validate_FILE(stream);
- if (((unsigned int) mode) > 2) {
-
- goto DONE;
- }
- #ifdef __STDIO_FLEXIBLE_SETVBUF
-
-
-
- if (fseek(stream, 0L, SEEK_CUR)) {
- goto DONE;
- }
- #else
-
- #ifdef __STDIO_WIDE
- if (stream->modeflags & (__FLAG_WIDE|__FLAG_NARROW)) {
- goto DONE;
- }
- #else
-
- if (stream->modeflags & (__FLAG_READING|__FLAG_WRITING)) {
- goto DONE;
- }
- #endif
- #endif
- if (mode == _IONBF) {
- size = 0;
- buf = NULL;
- } else if (!buf && !size) {
-
- size = stream->bufend - stream->bufstart;
- }
- stream->modeflags &= ~(__MASK_BUFMODE);
- stream->modeflags |= mode * __FLAG_LBF;
- allocated_buf_flag = 0;
- if ((!buf) && (size != (stream->bufend - stream->bufstart))) {
-
-
- if (!(buf = ((size > 0)
- ? ((allocated_buf_flag = __FLAG_FREEBUF), malloc(size))
- : ((char *)NULL) + 1))
- ) {
-
- goto DONE;
- }
- }
-
- if (buf && (buf != (char *) stream->bufstart)) {
- if (stream->modeflags & __FLAG_FREEBUF) {
- stream->modeflags &= ~(__FLAG_FREEBUF);
- free(stream->bufstart);
- }
- stream->modeflags |= allocated_buf_flag;
- #ifdef __STDIO_GETC_MACRO
- stream->bufgetc =
- #endif
- #ifdef __STDIO_PUTC_MACRO
- stream->bufputc =
- #endif
- stream->bufpos = stream->bufread = stream->bufstart = buf;
- stream->bufend = buf + size;
- }
- __stdio_validate_FILE(stream);
- rv = 0;
- DONE:
- __STDIO_THREADUNLOCK(stream);
- return rv;
- #else
- __stdio_validate_FILE(stream);
-
- return EOF;
- #endif
- }
- #endif
- #ifdef L_fgetc
- UNLOCKED(int,fgetc,(FILE *stream),(stream))
- {
- unsigned char buf[1];
- #ifdef __STDIO_WIDE
- return (fread_unlocked(buf, (size_t) 1, (size_t) 1, stream) > 0)
- ? *buf : EOF;
- #else
- return (_stdio_fread(buf, (size_t) 1, stream) > 0) ? *buf : EOF;
- #endif
- }
- #endif
- #ifdef L_fgets
- UNLOCKED(char *,fgets,
- (char *__restrict s, int n, register FILE * __restrict stream),
- (s, n, stream))
- {
- register char *p;
- int c;
- #ifdef __UCLIBC_MJN3_ONLY__
- #warning CONSIDER: What should fgets do if n <= 0?
- #endif
-
- if (n <= 0) {
- goto ERROR;
- }
- p = s;
- while (--n) {
- if ((c = (getc_unlocked)(stream)) == EOF) {
- if (__FERROR(stream)) {
- goto ERROR;
- }
- break;
- }
- if ((*p++ = c) == '\n') {
- break;
- }
- }
- #ifdef __UCLIBC_MJN3_ONLY__
- #warning CONSIDER: If n==1 and not at EOF, should fgets return an empty string?
- #endif
- if (p > s) {
- *p = 0;
- return s;
- }
- ERROR:
- return NULL;
- }
- #endif
- #ifdef L_fputc
- UNLOCKED(int,fputc,(int c, FILE *stream),(c,stream))
- {
- unsigned char buf[1];
- *buf = (unsigned char) c;
- #ifdef __STDIO_WIDE
- return (fwrite_unlocked(buf, (size_t) 1, (size_t) 1, stream) > 0)
- ? (*buf) : EOF;
- #else
- return (_stdio_fwrite(buf, (size_t) 1, stream) > 0) ? (*buf) : EOF;
- #endif
- }
- #endif
- #ifdef L_fputs
- UNLOCKED(int,fputs,
- (register const char * __restrict s, FILE * __restrict stream),
- (s, stream))
- {
- size_t n = strlen(s);
- #ifdef __STDIO_WIDE
- return (fwrite_unlocked(s, (size_t) 1, n, stream) == n) ? n : EOF;
- #else
- return (_stdio_fwrite(s, n, stream) == n) ? n : EOF;
- #endif
- }
- #endif
- #ifdef L_getc
- #undef getc
- #undef getc_unlocked
- UNLOCKED(int,getc,(register FILE *stream),(stream))
- {
- return __GETC(stream);
- }
- #endif
- #ifdef L_getchar
- #undef getchar
- UNLOCKED_STREAM(int,getchar,(void),(),stdin)
- {
- register FILE *stream = stdin;
- return __GETC(stream);
- }
- #endif
- #ifdef L_gets
- link_warning(gets, "the 'gets' function is dangerous and should not be used.")
- char *gets(char *s)
- {
- register FILE *stream = stdin;
- register char *p = s;
- int c;
- __STDIO_THREADLOCK(stream);
-
- while (((c = (getc_unlocked)(stream)) != EOF) && ((*p = c) != '\n')) {
- ++p;
- }
- if ((c == EOF) || (s == p)) {
- s = NULL;
- } else {
- *p = 0;
- }
- __STDIO_THREADUNLOCK(stream);
- return s;
- }
- #endif
- #ifdef L_putc
- #undef putc
- #undef putc_unlocked
- UNLOCKED(int,putc,(int c, register FILE *stream),(c,stream))
- {
- return __PUTC(c, stream);
- }
- #endif
- #ifdef L_putchar
- #undef putchar
- UNLOCKED_STREAM(int,putchar,(int c),(c),stdout)
- {
- register FILE *stream = stdout;
- return __PUTC(c, stream);
- }
- #endif
- #ifdef L_puts
- int puts(register const char *s)
- {
- register FILE *stream = stdout;
- int n;
- __STDIO_THREADLOCK(stream);
- n = fputs_unlocked(s,stream) + 1;
- if (
- #if 1
- fputc_unlocked('\n',stream)
- #else
- fputs_unlocked("\n",stream)
- #endif
- == EOF) {
- n = EOF;
- }
- __STDIO_THREADUNLOCK(stream);
- return n;
- }
- #endif
- #ifdef L_ungetc
- int ungetc(int c, register FILE *stream)
- {
- __STDIO_THREADLOCK(stream);
- __stdio_validate_FILE(stream);
- #ifdef __STDIO_WIDE
- if (stream->modeflags & __FLAG_WIDE) {
- stream->modeflags |= __FLAG_ERROR;
- c = EOF;
- goto DONE;
- }
- stream->modeflags |= __FLAG_NARROW;
- #endif
-
- if ((stream->modeflags
- & (__MASK_UNGOT2|__FLAG_WRITEONLY
- #ifndef __STDIO_AUTO_RW_TRANSITION
- |__FLAG_WRITING
- #endif
- ))
- || ((stream->modeflags & __MASK_UNGOT1) && (stream->ungot[1]))
- || (c == EOF) ) {
- c = EOF;
- goto DONE;;
- }
- #ifdef __STDIO_BUFFERS
- #ifdef __STDIO_AUTO_RW_TRANSITION
- if (stream->modeflags & __FLAG_WRITING) {
- fflush_unlocked(stream);
- }
- #endif
- #endif
-
- stream->modeflags &= ~(__FLAG_EOF|__FLAG_WRITING);
- #ifdef __UCLIBC_MJN3_ONLY__
- #warning CONSIDER: Is setting the reading flag after an ungetc necessary?
- #endif
- stream->modeflags |= __FLAG_READING;
- stream->ungot[1] = 1;
- stream->ungot[(stream->modeflags++) & __MASK_UNGOT] = c;
- #ifdef __STDIO_GETC_MACRO
- stream->bufgetc = stream->bufstart;
- #endif
- __stdio_validate_FILE(stream);
- DONE:
- __STDIO_THREADUNLOCK(stream);
- return c;
- }
- #endif
- #ifdef L_fread
- UNLOCKED(size_t,fread,
- (void * __restrict ptr, size_t size, size_t nmemb,
- FILE * __restrict stream),
- (ptr,size,nmemb,stream))
- {
- #ifdef __STDIO_WIDE
- if (stream->modeflags & __FLAG_WIDE) {
- stream->modeflags |= __FLAG_ERROR;
-
- return 0;
- }
- stream->modeflags |= __FLAG_NARROW;
- #endif
- return (size == 0)
- ? 0
- : ( assert( ((size_t)(-1)) / size >= nmemb ),
- _stdio_fread(ptr, nmemb * size, stream) / size );
- }
- #endif
- #ifdef L_fwrite
- UNLOCKED(size_t,fwrite,
- (const void * __restrict ptr, size_t size, size_t nmemb,
- FILE * __restrict stream),
- (ptr,size,nmemb,stream))
- {
- #ifdef __STDIO_WIDE
- if (stream->modeflags & __FLAG_WIDE) {
- stream->modeflags |= __FLAG_ERROR;
-
- return 0;
- }
- stream->modeflags |= __FLAG_NARROW;
- #endif
- return (size == 0)
- ? 0
- : ( assert( ((size_t)(-1)) / size >= nmemb ),
- _stdio_fwrite(ptr, nmemb * size, stream) / size );
- }
- #endif
- #if defined(L_fgetpos) || defined(L_fgetpos64)
- #if defined(L_fgetpos) && defined(L_fgetpos64)
- #error L_fgetpos and L_fgetpos64 are defined simultaneously!
- #endif
- #ifndef L_fgetpos64
- #define fgetpos64 fgetpos
- #define fpos64_t fpos_t
- #define ftello64 ftell
- #endif
- int fgetpos64(FILE * __restrict stream, register fpos64_t * __restrict pos)
- {
- int retval = -1;
- #ifdef __STDIO_WIDE
- if (pos == NULL) {
- __set_errno(EINVAL);
- } else {
- __STDIO_THREADLOCK(stream);
- if ((pos->__pos = ftello64(stream)) >= 0) {
- __COPY_MBSTATE(&(pos->__mbstate), &(stream->state));
- pos->mblen_pending = stream->ungot_width[0];
- retval = 0;
- }
- __STDIO_THREADUNLOCK(stream);
- }
- #else
- if (pos == NULL) {
- __set_errno(EINVAL);
- } else if ((pos->__pos = ftello64(stream)) >= 0) {
- retval = 0;
- }
- #endif
- return retval;
- }
- #ifndef L_fgetpos64
- #undef fgetpos64
- #undef fpos64_t
- #undef ftello64
- #endif
- #endif
- #ifdef L_fseek
- strong_alias(fseek,fseeko);
- #endif
- #if defined(L_fseek) && defined(__STDIO_LARGE_FILES)
- int fseek(register FILE *stream, long int offset, int whence)
- {
- return fseeko64(stream, offset, whence);
- }
- #endif
- #if defined(L_fseeko64) || (defined(L_fseek) && !defined(__STDIO_LARGE_FILES))
- #ifndef L_fseeko64
- #define fseeko64 fseek
- #define __off64_t long int
- #endif
- int fseeko64(register FILE *stream, __off64_t offset, int whence)
- {
- #if SEEK_SET != 0 || SEEK_CUR != 1 || SEEK_END != 2
- #error Assumption violated -- values of SEEK_SET, SEEK_CUR, SEEK_END
- #endif
- __offmax_t pos[1];
- int retval;
- if ( ((unsigned int) whence) > 2 ) {
- __set_errno(EINVAL);
- return -1;
- }
- __STDIO_THREADLOCK(stream);
- __stdio_validate_FILE(stream);
- retval = -1;
- *pos = offset;
- if (
- #ifdef __STDIO_BUFFERS
-
- ((stream->modeflags & __FLAG_WRITING) && fflush_unlocked(stream)) ||
- #endif
- ((whence == SEEK_CUR) && (_stdio_adjpos(stream, pos) < 0))
- || (_stdio_lseek(stream, pos, whence) < 0)
- ) {
- __stdio_validate_FILE(stream);
- goto DONE;
- }
- #ifdef __STDIO_BUFFERS
-
- #ifdef __STDIO_GETC_MACRO
- stream->bufgetc =
- #endif
- stream->bufpos = stream->bufread = stream->bufstart;
- #endif
- stream->modeflags &=
- ~(__FLAG_READING|__FLAG_WRITING|__FLAG_EOF|__MASK_UNGOT);
- #ifdef __UCLIBC_MJN3_ONLY__
- #warning CONSIDER: How do we deal with fseek to an ftell position for incomplete or error wide? Right now, we clear all multibyte state info. If we do not clear, then fix rewind() to do so if fseek() succeeds.
- #endif
- #ifdef __STDIO_WIDE
-
- stream->ungot_width[0] = 0;
- #endif
- #ifdef __STDIO_MBSTATE
-
- __INIT_MBSTATE(&(stream->state));
- #endif
- __stdio_validate_FILE(stream);
- retval = 0;
- DONE:
- __STDIO_THREADUNLOCK(stream);
- return retval;
- }
- #ifndef L_fseeko64
- #undef fseeko64
- #undef __off64_t
- #endif
- #endif
- #if defined(L_fsetpos) || defined(L_fsetpos64)
- #if defined(L_fsetpos) && defined(L_fsetpos64)
- #error L_fsetpos and L_fsetpos64 are defined simultaneously!
- #endif
- #ifndef L_fsetpos64
- #define fsetpos64 fsetpos
- #define fpos64_t fpos_t
- #define fseeko64 fseek
- #endif
- int fsetpos64(FILE *stream, register const fpos64_t *pos)
- {
- if (!pos) {
- __set_errno(EINVAL);
- return EOF;
- }
- #ifdef __STDIO_WIDE
- {
- int retval;
- __STDIO_THREADLOCK(stream);
- if ((retval = fseeko64(stream, pos->__pos, SEEK_SET)) == 0) {
- __COPY_MBSTATE(&(stream->state), &(pos->__mbstate));
- #ifdef __UCLIBC_MJN3_ONLY__
- #warning CONSIDER: Moving mblen_pending into some mbstate field might be useful. But we would need to modify all the mb<->wc funcs.
- #endif
- stream->ungot_width[0] = pos->mblen_pending;
- }
- __STDIO_THREADUNLOCK(stream);
- return retval;
- }
- #else
- return fseeko64(stream, pos->__pos, SEEK_SET);
- #endif
- }
- #ifndef L_fsetpos64
- #undef fsetpos64
- #undef fpos64_t
- #undef fseeko64
- #endif
- #endif
- #ifdef L_ftell
- strong_alias(ftell,ftello);
- #endif
- #if defined(L_ftell) && defined(__STDIO_LARGE_FILES)
- long int ftell(register FILE *stream)
- {
- __offmax_t pos = ftello64(stream);
- return (pos == ((long int) pos)) ? pos : (__set_errno(EOVERFLOW), -1);
- }
- #endif
- #if defined(L_ftello64) || (defined(L_ftell) && !defined(__STDIO_LARGE_FILES))
- #ifndef L_ftello64
- #define ftello64 ftell
- #define __off64_t long int
- #endif
- __off64_t ftello64(register FILE *stream)
- {
- __offmax_t pos[1];
- __off64_t retval;
- __STDIO_THREADLOCK(stream);
- retval = (((*pos = 0), (_stdio_lseek(stream, pos, SEEK_CUR) < 0))
- || (_stdio_adjpos(stream, pos) < 0)) ? -1 : *pos;
- __STDIO_THREADUNLOCK(stream);
- return retval;
- }
- #ifndef L_ftello64
- #undef ftello64
- #undef __off64_t
- #endif
- #endif
- #ifdef L_rewind
- void rewind(register FILE *stream)
- {
- __STDIO_THREADLOCK(stream);
- __CLEARERR(stream);
- fseek(stream, 0L, SEEK_SET);
- __STDIO_THREADUNLOCK(stream);
- }
- #endif
- #ifdef L_clearerr
- #undef clearerr
- UNLOCKED_VOID_RETURN(clearerr,(FILE *stream),(stream))
- {
- __CLEARERR(stream);
- }
- #endif
- #ifdef L_feof
- #undef feof
- UNLOCKED(int,feof,(FILE *stream),(stream))
- {
- return __FEOF(stream);
- }
- #endif
- #ifdef L_ferror
- #undef ferror
- UNLOCKED(int,ferror,(FILE *stream),(stream))
- {
- return __FERROR(stream);
- }
- #endif
- #ifdef L_perror
- void perror(register const char *s)
- {
-
- register const char *sep;
- sep = ": ";
- if (!(s && *s)) {
- s = (sep += 2);
- }
- #if 1
- #ifdef __STDIO_PRINTF_M_SPEC
- fprintf(stderr, "%s%s%m\n", s, sep);
- #else
- {
- char buf[64];
- fprintf(stderr, "%s%s%s\n", s, sep,
- _glibc_strerror_r(errno, buf, sizeof(buf)));
- }
- #endif
- #else
-
- {
- char buf[64];
- __STDIO_THREADLOCK(stderr);
- _stdio_fdout(STDERR_FILENO, s, sep,
- _glibc_strerror_r(errno, buf, sizeof(buf)));
- __STDIO_THREADUNLOCK(stderr);
- }
- #endif
- }
- #endif
- #ifdef L__stdio_fdout
- void _stdio_fdout(int fd, ...)
- {
- va_list arg;
- register const char *p;
- va_start(arg, fd);
- while ((p = va_arg(arg, const char *)) != NULL) {
- write(fd, p, strlen(p));
- }
- va_end(arg);
- }
- #endif
- #ifdef L__uintmaxtostr
- #if (INTMAX_MAX > INT_MAX) && (((INTMAX_MAX/INT_MAX)/2) - 2 <= INT_MAX)
- #define INTERNAL_DIV_MOD
- #endif
- #include <locale.h>
- char *_uintmaxtostr(register char * __restrict bufend, uintmax_t uval,
- int base, __UIM_CASE alphacase)
- {
- int negative;
- unsigned int digit;
- #ifdef INTERNAL_DIV_MOD
- unsigned int H, L, high, low, rh;
- #endif
- #ifndef __LOCALE_C_ONLY
- int grouping, outdigit;
- const char *g;
- #endif
- negative = 0;
- if (base < 0) {
- base = -base;
- if (uval > INTMAX_MAX) {
- uval = -uval;
- negative = 1;
- }
- }
-
- assert(!((base < 2) || (base > 36)));
- #ifndef __LOCALE_C_ONLY
- grouping = -1;
- outdigit = 0x80 & alphacase;
- alphacase ^= outdigit;
- if (alphacase == __UIM_GROUP) {
- assert(base == 10);
- if (*(g = __UCLIBC_CURLOCALE_DATA.grouping)) {
- grouping = *g;
- }
- }
- #endif
- *bufend = '\0';
- #ifndef INTERNAL_DIV_MOD
- do {
- #ifndef __LOCALE_C_ONLY
- if (!grouping) {
- bufend -= __UCLIBC_CURLOCALE_DATA.thousands_sep_len;
- memcpy(bufend, __UCLIBC_CURLOCALE_DATA.thousands_sep,
- __UCLIBC_CURLOCALE_DATA.thousands_sep_len);
- if (g[1] != 0) {
-
- ++g;
- }
- grouping = *g;
- }
- --grouping;
- #endif
- digit = uval % base;
- uval /= base;
- #ifndef __LOCALE_C_ONLY
- if (unlikely(outdigit)) {
- bufend -= __UCLIBC_CURLOCALE_DATA.outdigit_length[digit];
- memcpy(bufend,
- (&__UCLIBC_CURLOCALE_DATA.outdigit0_mb)[digit],
- __UCLIBC_CURLOCALE_DATA.outdigit_length[digit]);
- } else
- #endif
- {
- *--bufend = ( (digit < 10) ? digit + '0' : digit + alphacase );
- }
- } while (uval);
- #else
- H = (UINT_MAX / base);
- L = UINT_MAX % base + 1;
- if (L == base) {
- ++H;
- L = 0;
- }
- low = (unsigned int) uval;
- high = (unsigned int) (uval >> (sizeof(unsigned int) * CHAR_BIT));
- do {
- #ifndef __LOCALE_C_ONLY
- if (!grouping) {
- bufend -= __UCLIBC_CURLOCALE_DATA.thousands_sep_len;
- memcpy(bufend, __UCLIBC_CURLOCALE_DATA.thousands_sep,
- __UCLIBC_CURLOCALE_DATA.thousands_sep_len);
- if (g[1] != 0) {
-
- ++g;
- }
- grouping = *g;
- }
- --grouping;
- #endif
- if (unlikely(high)) {
- rh = high % base;
- high /= base;
- digit = (low % base) + (L * rh);
- low = (low / base) + (H * rh) + (digit / base);
- digit %= base;
- } else {
- digit = low % base;
- low /= base;
- }
-
- #ifndef __LOCALE_C_ONLY
- if (unlikely(outdigit)) {
- bufend -= __UCLIBC_CURLOCALE_DATA.outdigit_length[digit];
- memcpy(bufend,
- (&__UCLIBC_CURLOCALE_DATA.outdigit0_mb)[digit],
- __UCLIBC_CURLOCALE_DATA.outdigit_length[digit]);
- } else
- #endif
- {
- *--bufend = ( (digit < 10) ? digit + '0' : digit + alphacase );
- }
- } while (low | high);
- #endif
- if (negative) {
- *--bufend = '-';
- }
- return bufend;
- }
- #undef INTERNAL_DIV_MOD
- #endif
- #ifdef L__wstdio_fwrite
- #include <wchar.h>
- size_t _wstdio_fwrite(const wchar_t *__restrict ws, size_t n,
- register FILE *__restrict stream)
- {
- size_t r, count;
- char buf[64];
- const wchar_t *pw;
- #if defined(__STDIO_WIDE) && defined(__STDIO_BUFFERS)
- if (stream->filedes == -3) {
- count = ((wchar_t *)(stream->bufend)) - ((wchar_t *)(stream->bufpos));
- if (count > n) {
- count = n;
- }
- if (count) {
- wmemcpy((wchar_t *)(stream->bufpos), ws, count);
- stream->bufpos = (char *)(((wchar_t *)(stream->bufpos)) + count);
- }
- return n;
- }
- #endif
- if (stream->modeflags & __FLAG_NARROW) {
- stream->modeflags |= __FLAG_ERROR;
- __set_errno(EBADF);
- return 0;
- }
- stream->modeflags |= __FLAG_WIDE;
- pw = ws;
- count = 0;
- while (n > count) {
- r = wcsnrtombs(buf, &pw, n-count, sizeof(buf), &stream->state);
- if (r != ((size_t) -1)) {
- if (!r) {
- ++r;
- pw = ws + count + r;
- }
- if (_stdio_fwrite(buf, r, stream) == r) {
- count = pw - ws;
- continue;
- }
- }
- break;
- }
-
- return count;
- }
- #endif
|