12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743 |
- /*++
- Copyright (c) 2013 Minoca Corp.
- This file is licensed under the terms of the GNU General Public License
- version 3. Alternative licensing terms are available. Contact
- info@minocacorp.com for details. See the LICENSE file at the root of this
- project for complete licensing information.
- Module Name:
- stream.c
- Abstract:
- This module implements the higher level file stream interface.
- Author:
- Evan Green 18-Jun-2013
- Environment:
- User Mode C Library
- --*/
- //
- // ------------------------------------------------------------------- Includes
- //
- #include "libcp.h"
- #include <assert.h>
- #include <errno.h>
- #include <fcntl.h>
- #include <stdio.h>
- #include <stdio_ext.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/stat.h>
- //
- // ---------------------------------------------------------------- Definitions
- //
- //
- // Define the number of standard handles.
- //
- #define STANDARD_HANDLE_COUNT 3
- //
- // Define the creation mask for stream files.
- //
- #define STREAM_FILE_CREATION_MASK \
- (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)
- //
- // Define the maximum size of the stack-allocated print buffer.
- //
- #define STREAM_PRINT_BUFFER_SIZE 128
- //
- // ------------------------------------------------------ Data Type Definitions
- //
- /*++
- Structure Description:
- This structure defines the print context used for streams. This allows
- unbuffered streams to batch prints, rather than write them out character by
- character.
- Members:
- Stream - Store a pointer to the file stream to print to.
- Buffer - Stores a pointer to the buffered print output for unbuffered
- streams.
- BufferNextIndex - Stores the index into the buffer to store the next
- character.
- CharactersWritten - Stores the number of characters actually written to the
- stream.
- --*/
- typedef struct _STREAM_PRINT_CONTEXT {
- FILE *Stream;
- CHAR Buffer[STREAM_PRINT_BUFFER_SIZE];
- ULONG BufferNextIndex;
- ULONG CharactersWritten;
- } STREAM_PRINT_CONTEXT, *PSTREAM_PRINT_CONTEXT;
- //
- // ----------------------------------------------- Internal Function Prototypes
- //
- PFILE
- ClpCreateFileStructure (
- ULONG Descriptor,
- ULONG OpenFlags,
- ULONG BufferMode
- );
- VOID
- ClpDestroyFileStructure (
- PFILE File
- );
- BOOL
- ClpFileFormatWriteCharacter (
- INT Character,
- PPRINT_FORMAT_CONTEXT Context
- );
- INT
- ClpConvertStreamModeStringToOpenFlags (
- PSTR ModeString,
- INT *OpenFlags
- );
- //
- // -------------------------------------------------------------------- Globals
- //
- //
- // Define the standard file handle pointers.
- //
- LIBC_API FILE *stdin;
- LIBC_API FILE *stdout;
- LIBC_API FILE *stderr;
- //
- // Store the global list of streams, protected by a single global spin lock.
- //
- LIST_ENTRY ClStreamList;
- pthread_mutex_t ClStreamListLock;
- //
- // ------------------------------------------------------------------ Functions
- //
- LIBC_API
- FILE *
- fopen (
- const char *FileName,
- const char *Mode
- )
- /*++
- Routine Description:
- This routine opens the given file and associates a stream with it.
- Arguments:
- FileName - Supplies a pointer to a string containing the path to the file
- to open.
- Mode - Supplies a pointer to a null terminated string specifying the mode
- to open the file with. Valid value are "r", "w", "a", and may optionally
- have a + or b appended to it. The + symbol opens the file for updating,
- meaning that the system does not flush immediately after writes. The "b"
- option has no effect.
- Return Value:
- Returns a pointer to the file stream on success.
- NULL on failure, and errno contains more information.
- --*/
- {
- mode_t CreatePermissions;
- int Descriptor;
- int Error;
- FILE *NewFile;
- int OpenFlags;
- BOOL Success;
- CreatePermissions = STREAM_FILE_CREATION_MASK;
- Descriptor = -1;
- NewFile = NULL;
- OpenFlags = 0;
- Success = FALSE;
- //
- // Get the open flags.
- //
- Error = ClpConvertStreamModeStringToOpenFlags((PSTR)Mode, &OpenFlags);
- if (Error != 0) {
- goto fopenEnd;
- }
- //
- // Open up the file with the operating system.
- //
- Descriptor = open(FileName, OpenFlags, CreatePermissions);
- if (Descriptor == -1) {
- goto fopenEnd;
- }
- NewFile = ClpCreateFileStructure(Descriptor, OpenFlags, _IOFBF);
- if (NewFile == NULL) {
- goto fopenEnd;
- }
- Success = TRUE;
- fopenEnd:
- if (Success == FALSE) {
- if (NewFile != NULL) {
- if (NewFile->Descriptor != -1) {
- close(NewFile->Descriptor);
- }
- if (NewFile->Buffer != NULL) {
- free(NewFile->Buffer);
- }
- free(NewFile);
- NewFile = NULL;
- }
- }
- if (Error != 0) {
- errno = Error;
- }
- return NewFile;
- }
- LIBC_API
- FILE *
- fdopen (
- int OpenFileDescriptor,
- const char *Mode
- )
- /*++
- Routine Description:
- This routine associates a stream with the given file descriptor. The mode
- argument must agree with the flags the original descriptor was opened with.
- On success, the stream now "owns" the file descriptor, a call to fclose
- on the stream will also call close on the underlying descriptor.
- Arguments:
- OpenFileDescriptor - Supplies the open file descriptor to create a stream
- around.
- Mode - Supplies a pointer to a null terminated string specifying the mode
- to open the file with. Valid value are "r", "w", "a", and may optionally
- have a + or b appended to it. The + symbol opens the file for updating,
- meaning that the system does not flush immediately after writes. The "b"
- option has no effect.
- Return Value:
- Returns a pointer to the file stream on success.
- NULL on failure, and errno contains more information.
- --*/
- {
- int Error;
- FILE *NewFile;
- int OpenFlags;
- NewFile = NULL;
- OpenFlags = 0;
- if (OpenFileDescriptor == -1) {
- Error = EBADF;
- goto fdopenEnd;
- }
- Error = ClpConvertStreamModeStringToOpenFlags((PSTR)Mode, &OpenFlags);
- if (Error != 0) {
- goto fdopenEnd;
- }
- NewFile = ClpCreateFileStructure(OpenFileDescriptor, OpenFlags, _IOFBF);
- if (NewFile == NULL) {
- Error = ENOMEM;
- goto fdopenEnd;
- }
- Error = 0;
- fdopenEnd:
- if (Error != 0) {
- if (NewFile != NULL) {
- if (NewFile->Buffer != NULL) {
- free(NewFile->Buffer);
- }
- free(NewFile);
- NewFile = NULL;
- }
- }
- if (Error != 0) {
- errno = Error;
- }
- return NewFile;
- }
- LIBC_API
- FILE *
- freopen (
- const char *FileName,
- const char *Mode,
- FILE *Stream
- )
- /*++
- Routine Description:
- This routine attempts to flush the given stream and close any file
- descriptor associated with the stream. Failure to flush or close the
- file descriptor is ignored. The error and end-of-file indicators are
- cleared. This routine then attempts to open the given file with the given
- mode and associate it with the stream. The previous file descriptor
- associated with this stream is closed whether or not the new descriptor
- could be opened.
- The standard says that passing in NULL for the file name will change the
- permissions of the existing descriptor. This implementation currently does
- not support that and sets errno to EBADF if attempted.
- Arguments:
- FileName - Supplies a pointer to the path to open and associate with this
- stream.
- Mode - Supplies a pointer to the string describing the desired access to
- this path. This takes the same format as the fopen mode string.
- Stream - Supplies a pointer to the open stream.
- Return Value:
- Returns a pointer to the given stream on success, now with a different
- file descriptor.
- NULL on failure, and errno will be set to contain more information.
- --*/
- {
- int Error;
- int NewDescriptor;
- int OpenFlags;
- NewDescriptor = -1;
- if (Stream == NULL) {
- errno = EBADF;
- return NULL;
- }
- ClpLockStream(Stream);
- //
- // Flush and close the original descriptor.
- //
- fflush_unlocked(Stream);
- if (Stream->Descriptor != -1) {
- close(Stream->Descriptor);
- Stream->Descriptor = -1;
- }
- //
- // Get the open flags.
- //
- OpenFlags = 0;
- Error = ClpConvertStreamModeStringToOpenFlags((PSTR)Mode, &OpenFlags);
- if (Error != 0) {
- goto freopenEnd;
- }
- //
- // Consider implementing support for changing permissions on the currently
- // open file if people trying to use that.
- //
- assert(FileName != NULL);
- //
- // Open up the new descriptor.
- //
- NewDescriptor = open(FileName, OpenFlags);
- if (NewDescriptor < 0) {
- NewDescriptor = -1;
- Error = errno;
- goto freopenEnd;
- }
- freopenEnd:
- //
- // Set the underlying descriptor to the new descriptor, which may or may
- // not have failed to open.
- //
- Stream->Descriptor = NewDescriptor;
- Stream->OpenFlags = OpenFlags;
- Stream->BufferNextIndex = 0;
- Stream->BufferValidSize = 0;
- Stream->Flags &= (FILE_FLAG_BUFFER_ALLOCATED | FILE_FLAG_STANDARD_IO);
- if ((OpenFlags & O_ACCMODE) != O_WRONLY) {
- Stream->Flags |= FILE_FLAG_CAN_READ;
- }
- ClpUnlockStream(Stream);
- return Stream;
- }
- LIBC_API
- int
- fclose (
- FILE *Stream
- )
- /*++
- Routine Description:
- This routine closes an open file stream.
- Arguments:
- Stream - Supplies a pointer to the open stream.
- Return Value:
- 0 on success.
- Returns EOF if there was an error flushing or closing the stream.
- --*/
- {
- int Result;
- Result = fflush(Stream);
- if (Stream->Descriptor != -1) {
- Result |= close(Stream->Descriptor);
- Stream->Descriptor = -1;
- }
- //
- // Don't actually free the stream if it's one of the standard ones.
- // Applications have come to expect to be able to fclose stdout and then
- // freopen it.
- //
- if ((Stream->Flags & FILE_FLAG_STANDARD_IO) == 0) {
- ClpDestroyFileStructure(Stream);
- }
- return Result;
- }
- LIBC_API
- size_t
- fread (
- void *Buffer,
- size_t Size,
- size_t ItemCount,
- FILE *Stream
- )
- /*++
- Routine Description:
- This routine reads from a file stream.
- Arguments:
- Buffer - Supplies a pointer to the buffer where the data will be returned.
- Size - Supplies the size of each element to read.
- ItemCount - Supplies the number of elements to read.
- Stream - Supplies a pointer to the file stream object to read from.
- Return Value:
- Returns the number of elements successfully read from the file. On failure,
- the error indicator for the stream will be set, and errno will set to
- provide details on the error that occurred.
- --*/
- {
- size_t Result;
- ClpLockStream(Stream);
- Result = fread_unlocked(Buffer, Size, ItemCount, Stream);
- ClpUnlockStream(Stream);
- return Result;
- }
- LIBC_API
- size_t
- fread_unlocked (
- void *Buffer,
- size_t Size,
- size_t ItemCount,
- FILE *Stream
- )
- /*++
- Routine Description:
- This routine reads from a file stream without acquiring the internal file
- lock.
- Arguments:
- Buffer - Supplies a pointer to the buffer where the data will be returned.
- Size - Supplies the size of each element to read.
- ItemCount - Supplies the number of elements to read.
- Stream - Supplies a pointer to the file stream object to read from.
- Return Value:
- Returns the number of elements successfully read from the file. On failure,
- the error indicator for the stream will be set, and errno will set to
- provide details on the error that occurred.
- --*/
- {
- size_t BytesToRead;
- ssize_t Result;
- size_t TotalBytesRead;
- size_t TotalBytesToRead;
- TotalBytesRead = 0;
- TotalBytesToRead = Size * ItemCount;
- if ((Stream->Flags & FILE_FLAG_CAN_READ) == 0) {
- Stream->Flags |= FILE_FLAG_ERROR;
- errno = EACCES;
- return 0;
- }
- if (Stream->Descriptor == -1) {
- errno = EBADF;
- return 0;
- }
- if ((Size == 0) || (ItemCount == 0)) {
- return 0;
- }
- //
- // Set the last operation to be a read.
- //
- Stream->Flags |= FILE_FLAG_READ_LAST;
- //
- // If the unget character is valid, stick that in there first.
- //
- if ((Stream->Flags & FILE_FLAG_UNGET_VALID) != 0) {
- *((PUCHAR)Buffer) = Stream->UngetCharacter;
- Stream->Flags &= ~FILE_FLAG_UNGET_VALID;
- TotalBytesRead += 1;
- if (TotalBytesRead == TotalBytesToRead) {
- return TotalBytesRead / Size;
- }
- }
- //
- // For unbuffered streams, just read the file contents directly.
- //
- if (Stream->BufferMode == _IONBF) {
- ClpFlushAllStreams(FALSE, Stream);
- while (TotalBytesRead != TotalBytesToRead) {
- do {
- Result = read(Stream->Descriptor,
- Buffer + TotalBytesRead,
- TotalBytesToRead - TotalBytesRead);
- } while ((Result < 0) && (errno == EINTR));
- if (Result <= 0) {
- if (Result < 0) {
- Stream->Flags |= FILE_FLAG_ERROR;
- } else {
- Stream->Flags |= FILE_FLAG_END_OF_FILE;
- }
- break;
- }
- TotalBytesRead += Result;
- }
- return TotalBytesRead / Size;
- }
- assert(Stream->Buffer != NULL);
- //
- // Grab as much as needed out of the buffer.
- //
- BytesToRead = Stream->BufferValidSize - Stream->BufferNextIndex;
- if (BytesToRead > (TotalBytesToRead - TotalBytesRead)) {
- BytesToRead = TotalBytesToRead - TotalBytesRead;
- }
- memcpy(Buffer + TotalBytesRead,
- Stream->Buffer + Stream->BufferNextIndex,
- BytesToRead);
- TotalBytesRead += BytesToRead;
- Stream->BufferNextIndex += BytesToRead;
- if (Stream->BufferNextIndex == Stream->BufferValidSize) {
- Stream->BufferNextIndex = 0;
- Stream->BufferValidSize = 0;
- }
- //
- // Do direct reads to the caller's buffer if they're as large as the
- // buffer itself to avoid silly copies.
- //
- if (TotalBytesToRead >= Stream->BufferSize) {
- while (TotalBytesRead != TotalBytesToRead) {
- BytesToRead = TotalBytesToRead - TotalBytesRead;
- do {
- Result = read(Stream->Descriptor,
- Buffer + TotalBytesRead,
- BytesToRead);
- } while ((Result == -1) && (errno == EINTR));
- if (Result <= 0) {
- if (Result < 0) {
- Stream->Flags |= FILE_FLAG_ERROR;
- } else {
- Stream->Flags |= FILE_FLAG_END_OF_FILE;
- }
- break;
- }
- TotalBytesRead += Result;
- }
- //
- // This is a smaller read, use the buffer.
- //
- } else {
- while (TotalBytesRead != TotalBytesToRead) {
- //
- // The buffer should have been cleared out by the first portion of
- // this function or fully satisfied by it.
- //
- assert((Stream->BufferValidSize == 0) &&
- (Stream->BufferNextIndex == 0));
- do {
- Result = read(Stream->Descriptor,
- Stream->Buffer,
- Stream->BufferSize);
- } while ((Result == -1) && (errno == EINTR));
- if (Result <= 0) {
- if (Result < 0) {
- Stream->Flags |= FILE_FLAG_ERROR;
- } else {
- Stream->Flags |= FILE_FLAG_END_OF_FILE;
- }
- break;
- }
- BytesToRead = Result;
- if (BytesToRead > (TotalBytesToRead - TotalBytesRead)) {
- BytesToRead = TotalBytesToRead - TotalBytesRead;
- Stream->BufferValidSize = Result;
- Stream->BufferNextIndex = BytesToRead;
- }
- memcpy(Buffer + TotalBytesRead, Stream->Buffer, BytesToRead);
- TotalBytesRead += BytesToRead;
- }
- }
- return TotalBytesRead / Size;
- }
- LIBC_API
- size_t
- fwrite (
- const void *Buffer,
- size_t Size,
- size_t ItemCount,
- FILE *Stream
- )
- /*++
- Routine Description:
- This routine writes to a file stream.
- Arguments:
- Buffer - Supplies a pointer to the buffer containing the data to write.
- Size - Supplies the size of each element to write.
- ItemCount - Supplies the number of elements to write.
- Stream - Supplies a pointer to the file stream object to write to.
- Return Value:
- Returns the number of elements successfully written to the file. On failure,
- the error indicator for the stream will be set, and errno will set to
- provide details on the error that occurred.
- --*/
- {
- size_t Result;
- ClpLockStream(Stream);
- Result = fwrite_unlocked(Buffer, Size, ItemCount, Stream);
- ClpUnlockStream(Stream);
- return Result;
- }
- LIBC_API
- size_t
- fwrite_unlocked (
- const void *Buffer,
- size_t Size,
- size_t ItemCount,
- FILE *Stream
- )
- /*++
- Routine Description:
- This routine writes to a file stream without acquiring the internal file
- lock.
- Arguments:
- Buffer - Supplies a pointer to the buffer containing the data to write.
- Size - Supplies the size of each element to write.
- ItemCount - Supplies the number of elements to write.
- Stream - Supplies a pointer to the file stream object to write to.
- Return Value:
- Returns the number of elements successfully written to the file. On failure,
- the error indicator for the stream will be set, and errno will set to
- provide details on the error that occurred.
- --*/
- {
- size_t BytesToWrite;
- ULONG CharacterIndex;
- BOOL Flush;
- ssize_t Result;
- PSTR String;
- size_t TotalBytesToWrite;
- size_t TotalBytesWritten;
- TotalBytesWritten = 0;
- TotalBytesToWrite = Size * ItemCount;
- if ((Stream->OpenFlags & O_WRONLY) == 0) {
- errno = EACCES;
- return 0;
- }
- if (TotalBytesToWrite == 0) {
- return 0;
- }
- if (Stream->Descriptor == -1) {
- errno = EBADF;
- return 0;
- }
- //
- // The unget character isn't valid after things have been written.
- //
- if ((Stream->Flags & FILE_FLAG_UNGET_VALID) != 0) {
- Stream->Flags &= ~FILE_FLAG_UNGET_VALID;
- }
- //
- // For unbuffered streams or large writes, just write the file contents
- // directly.
- //
- if ((Stream->BufferMode == _IONBF) ||
- (TotalBytesToWrite > Stream->BufferSize)) {
- if (fflush_unlocked(Stream) != 0) {
- return -1;
- }
- //
- // Set the last thing that happened to be a write.
- //
- Stream->Flags &= ~FILE_FLAG_READ_LAST;
- while (TotalBytesWritten != TotalBytesToWrite) {
- do {
- Result = write(Stream->Descriptor,
- Buffer + TotalBytesWritten,
- TotalBytesToWrite - TotalBytesWritten);
- } while ((Result < 0) && (errno == EINTR));
- if (Result <= 0) {
- Stream->Flags |= FILE_FLAG_ERROR;
- break;
- }
- TotalBytesWritten += Result;
- }
- return TotalBytesWritten / Size;
- }
- //
- // If the last thing that happened was a read, flush the buffer.
- //
- if ((Stream->Flags & FILE_FLAG_READ_LAST) != 0) {
- if (fflush_unlocked(Stream) != 0) {
- return -1;
- }
- Stream->Flags &= ~FILE_FLAG_READ_LAST;
- }
- //
- // Loop writing stuff to the buffer and flushing the buffer.
- //
- Flush = FALSE;
- while (TotalBytesWritten != TotalBytesToWrite) {
- BytesToWrite = Stream->BufferSize - Stream->BufferNextIndex;
- if (BytesToWrite > (TotalBytesToWrite - TotalBytesWritten)) {
- BytesToWrite = TotalBytesToWrite - TotalBytesWritten;
- }
- //
- // If the buffer is line buffered, look for a newline, which would
- // indicate the need to flush, and cut the copy short if one is found.
- //
- if (Stream->BufferMode == _IOLBF) {
- String = (PSTR)Buffer + TotalBytesWritten;
- for (CharacterIndex = 0;
- CharacterIndex < BytesToWrite;
- CharacterIndex += 1) {
- if (String[CharacterIndex] == '\n') {
- Flush = TRUE;
- BytesToWrite = CharacterIndex + 1;
- break;
- }
- }
- }
- assert(Stream->BufferNextIndex + BytesToWrite <= Stream->BufferSize);
- //
- // If there is any space left, copy the bytes into the buffer.
- //
- if (BytesToWrite != 0) {
- memcpy(Stream->Buffer + Stream->BufferNextIndex,
- (PVOID)Buffer + TotalBytesWritten,
- BytesToWrite);
- assert(Stream->BufferValidSize == Stream->BufferNextIndex);
- Stream->BufferNextIndex += BytesToWrite;
- Stream->BufferValidSize = Stream->BufferNextIndex;
- if (Stream->BufferNextIndex == Stream->BufferSize) {
- Flush = TRUE;
- }
- TotalBytesWritten += BytesToWrite;
- //
- // If there's no space left, flush the buffer to make more.
- //
- } else {
- Flush = TRUE;
- }
- //
- // For the buffer not to want to flush it had better be done.
- //
- assert((Flush != FALSE) || (TotalBytesWritten == TotalBytesToWrite));
- if (Flush != FALSE) {
- if (fflush_unlocked(Stream) < 0) {
- break;
- }
- }
- }
- return TotalBytesWritten / Size;
- }
- LIBC_API
- int
- fflush (
- FILE *Stream
- )
- /*++
- Routine Description:
- This routine flushes any data sitting in the file stream that has not yet
- made it out to the operating system. This is only relevant for output
- streams.
- Arguments:
- Stream - Supplies a pointer to the open file stream to flush.
- Return Value:
- 0 on success.
- EOF on failure, and errno will be set to contain more information.
- --*/
- {
- int Result;
- if (Stream == NULL) {
- ClpFlushAllStreams(FALSE, NULL);
- return 0;
- }
- ClpLockStream(Stream);
- Result = fflush_unlocked(Stream);
- ClpUnlockStream(Stream);
- return Result;
- }
- LIBC_API
- int
- fflush_unlocked (
- FILE *Stream
- )
- /*++
- Routine Description:
- This routine flushes any data sitting in the file stream that has not yet
- made it out to the operating system. This routine does not acquire the
- internal stream lock. This is only relevant for output streams.
- Arguments:
- Stream - Supplies a pointer to the open file stream to flush.
- Return Value:
- 0 on success.
- EOF on failure, and errno will be set to contain more information.
- --*/
- {
- ssize_t BytesWritten;
- off_t Offset;
- int PreviousError;
- ssize_t Result;
- if (Stream == NULL) {
- ClpFlushAllStreams(FALSE, NULL);
- return 0;
- }
- Result = 0;
- if (Stream->BufferMode == _IONBF) {
- return 0;
- }
- if (Stream->Descriptor == -1) {
- errno = EBADF;
- return EOF;
- }
- //
- // If the buffer is full of read data, try and back up the file pointer.
- // Ignore failures.
- //
- if ((Stream->Flags & FILE_FLAG_READ_LAST) != 0) {
- Offset = Stream->BufferValidSize - Stream->BufferNextIndex;
- if ((Stream->Flags & FILE_FLAG_UNGET_VALID) != 0) {
- Offset += 1;
- }
- PreviousError = errno;
- lseek(Stream->Descriptor, -Offset, SEEK_CUR);
- errno = PreviousError;
- //
- // The buffer is full of dirty data. Write it out.
- //
- } else {
- BytesWritten = 0;
- while (BytesWritten < Stream->BufferNextIndex) {
- do {
- Result = write(Stream->Descriptor,
- Stream->Buffer + BytesWritten,
- Stream->BufferNextIndex - BytesWritten);
- } while ((Result < 0) && (errno == EINTR));
- if (Result <= 0) {
- Stream->Flags |= FILE_FLAG_ERROR;
- return EOF;
- }
- BytesWritten += Result;
- }
- }
- Stream->BufferNextIndex = 0;
- Stream->BufferValidSize = 0;
- Stream->Flags &= ~FILE_FLAG_UNGET_VALID;
- return 0;
- }
- LIBC_API
- long
- ftell (
- FILE *Stream
- )
- /*++
- Routine Description:
- This routine returns the given stream's file position.
- Arguments:
- Stream - Supplies a pointer to the open file stream.
- Return Value:
- Returns the current file position on success.
- -1 on failure, and errno will be set to contain more information.
- --*/
- {
- off_t Result;
- Result = ftello(Stream);
- if ((long)Result != Result) {
- errno = ERANGE;
- return -1;
- }
- return (long)Result;
- }
- LIBC_API
- off_t
- ftello (
- FILE *Stream
- )
- /*++
- Routine Description:
- This routine returns the given stream's file position.
- Arguments:
- Stream - Supplies a pointer to the open file stream.
- Return Value:
- Returns the current file position on success.
- -1 on failure, and errno will be set to contain more information.
- --*/
- {
- off_t Result;
- //
- // One might ask why the lock needs to be held for what amounts to just a
- // single read. The answer is that the file position may be larger than
- // the native integer size of the machine, and so the read may not be
- // atomic. Without the lock, a torn read could result. This could be
- // optimized for 64-bit systems where those reads are atomic.
- //
- ClpLockStream(Stream);
- Result = ftello_unlocked(Stream);
- ClpUnlockStream(Stream);
- return Result;
- }
- LIBC_API
- off64_t
- ftello64 (
- FILE *Stream
- )
- /*++
- Routine Description:
- This routine returns the given stream's file position.
- Arguments:
- Stream - Supplies a pointer to the open file stream.
- Return Value:
- Returns the current file position on success.
- -1 on failure, and errno will be set to contain more information.
- --*/
- {
- return ftello(Stream);
- }
- LIBC_API
- off_t
- ftello_unlocked (
- FILE *Stream
- )
- /*++
- Routine Description:
- This routine returns the given stream's file position.
- Arguments:
- Stream - Supplies a pointer to the open file stream.
- Return Value:
- Returns the current file position on success.
- -1 on failure, and errno will be set to contain more information.
- --*/
- {
- off_t NewOffset;
- NewOffset = lseek(Stream->Descriptor, 0, SEEK_CUR);
- if (NewOffset == -1) {
- return -1;
- }
- if ((Stream->Flags & FILE_FLAG_READ_LAST) != 0) {
- NewOffset -= Stream->BufferValidSize - Stream->BufferNextIndex;
- if ((Stream->Flags & FILE_FLAG_UNGET_VALID) != 0) {
- NewOffset -= 1;
- }
- } else {
- NewOffset += Stream->BufferValidSize;
- }
- return NewOffset;
- }
- LIBC_API
- int
- fseek (
- FILE *Stream,
- long Offset,
- int Whence
- )
- /*++
- Routine Description:
- This routine sets the file position indicator for the given stream. If a
- read or write error occurs, the error indicator will be set for the stream
- and fseek fails. This routine will undo any effects of a previous call to
- unget.
- Arguments:
- Stream - Supplies a pointer to the open file stream.
- Offset - Supplies the offset from the reference point given in the Whence
- argument.
- Whence - Supplies the reference location to base the offset off of. Valid
- value are:
- SEEK_SET - The offset will be added to the the beginning of the file.
- SEEK_CUR - The offset will be added to the current file position.
- SEEK_END - The offset will be added to the end of the file.
- Return Value:
- 0 on success.
- -1 on failure, and errno will be set to contain more information.
- --*/
- {
- int Result;
- ClpLockStream(Stream);
- Result = fseeko_unlocked(Stream, Offset, Whence);
- ClpUnlockStream(Stream);
- return Result;
- }
- LIBC_API
- int
- fseeko (
- FILE *Stream,
- off_t Offset,
- int Whence
- )
- /*++
- Routine Description:
- This routine sets the file position indicator for the given stream. If a
- read or write error occurs, the error indicator will be set for the stream
- and fseek fails. This routine will undo any effects of a previous call to
- unget.
- Arguments:
- Stream - Supplies a pointer to the open file stream.
- Offset - Supplies the offset from the reference point given in the Whence
- argument.
- Whence - Supplies the reference location to base the offset off of. Valid
- value are:
- SEEK_SET - The offset will be added to the the beginning of the file.
- SEEK_CUR - The offset will be added to the current file position.
- SEEK_END - The offset will be added to the end of the file.
- Return Value:
- 0 on success.
- -1 on failure, and errno will be set to contain more information.
- --*/
- {
- int Result;
- ClpLockStream(Stream);
- Result = fseeko_unlocked(Stream, Offset, Whence);
- ClpUnlockStream(Stream);
- return Result;
- }
- LIBC_API
- int
- fseeko64 (
- FILE *Stream,
- off64_t Offset,
- int Whence
- )
- /*++
- Routine Description:
- This routine sets the file position indicator for the given stream. If a
- read or write error occurs, the error indicator will be set for the stream
- and fseek fails. This routine will undo any effects of a previous call to
- unget.
- Arguments:
- Stream - Supplies a pointer to the open file stream.
- Offset - Supplies the offset from the reference point given in the Whence
- argument.
- Whence - Supplies the reference location to base the offset off of. Valid
- value are:
- SEEK_SET - The offset will be added to the the beginning of the file.
- SEEK_CUR - The offset will be added to the current file position.
- SEEK_END - The offset will be added to the end of the file.
- Return Value:
- 0 on success.
- -1 on failure, and errno will be set to contain more information.
- --*/
- {
- return fseeko(Stream, Offset, Whence);
- }
- LIBC_API
- int
- fseeko_unlocked (
- FILE *Stream,
- off_t Offset,
- int Whence
- )
- /*++
- Routine Description:
- This routine sets the file position indicator for the given stream. If a
- read or write error occurs, the error indicator will be set for the stream
- and fseek fails. This routine does not acquire the internal stream lock.
- Arguments:
- Stream - Supplies a pointer to the open file stream.
- Offset - Supplies the offset from the reference point given in the Whence
- argument.
- Whence - Supplies the reference location to base the offset off of. Valid
- value are:
- SEEK_SET - The offset will be added to the the beginning of the file.
- SEEK_CUR - The offset will be added to the current file position.
- SEEK_END - The offset will be added to the end of the file.
- Return Value:
- 0 on success.
- -1 on failure, and errno will be set to contain more information.
- --*/
- {
- //
- // It would be great to save the system call (or several) if the seek is
- // currently within the buffer, however apps (like m4 for example) rely on
- // using fseek to determine whether a descriptor is seekable, so ultimately
- // this function has to hit lseek somewhere.
- //
- fflush_unlocked(Stream);
- Stream->BufferNextIndex = 0;
- Stream->BufferValidSize = 0;
- Stream->Flags &= ~(FILE_FLAG_END_OF_FILE | FILE_FLAG_ERROR);
- if (lseek(Stream->Descriptor, Offset, Whence) != -1) {
- return 0;
- }
- return -1;
- }
- LIBC_API
- int
- fgetpos (
- FILE *Stream,
- fpos_t *Position
- )
- /*++
- Routine Description:
- This routine returns an opaque structure representing the current absolute
- position within the given stream.
- Arguments:
- Stream - Supplies a pointer to the open file stream.
- Position - Supplies a pointer where the opaque position will be returned.
- Callers must not presume that they can cast this type to an integer or
- compare these types in any way, they only serve as possible inputs to
- fsetpos to restore a file position to its current location.
- Return Value:
- 0 on success.
- -1 on failure, and errno will contain more information.
- --*/
- {
- off_t Offset;
- int Result;
- ClpLockStream(Stream);
- Offset = ftello_unlocked(Stream);
- if (Offset == -1) {
- Result = -1;
- goto getposEnd;
- }
- Position->Offset = Offset;
- Position->ShiftState = Stream->ShiftState;
- Result = 0;
- getposEnd:
- ClpUnlockStream(Stream);
- return Result;
- }
- LIBC_API
- int
- fsetpos (
- FILE *Stream,
- const fpos_t *Position
- )
- /*++
- Routine Description:
- This routine sets the current file position.
- Arguments:
- Stream - Supplies a pointer to the open file stream.
- Position - Supplies a pointer where the opaque position that was returned
- by a previous call to fgetpos.
- Return Value:
- 0 on success.
- -1 on failure, and errno will contain more information.
- --*/
- {
- int Result;
- ClpLockStream(Stream);
- Result = fseeko_unlocked(Stream, Position->Offset, SEEK_SET);
- if (Result != 0) {
- goto setposEnd;
- }
- Stream->ShiftState = Position->ShiftState;
- Result = 0;
- setposEnd:
- ClpUnlockStream(Stream);
- return Result;
- }
- LIBC_API
- void
- rewind (
- FILE *Stream
- )
- /*++
- Routine Description:
- This routine positions the file indicator back to the beginning. It shall
- be equivalent to fseek(Stream, 0, SEEK_SET) except that it also clears
- the error indicator.
- Arguments:
- Stream - Supplies a pointer to the open file stream.
- Return Value:
- None. Applications wishing to detect an error occurring during this
- function should set errno 0, call the function, and then check errno.
- --*/
- {
- fseek(Stream, 0, SEEK_SET);
- clearerr(Stream);
- return;
- }
- LIBC_API
- int
- fileno (
- FILE *Stream
- )
- /*++
- Routine Description:
- This routine returns the integer file descriptor associated with the given
- stream.
- Arguments:
- Stream - Supplies a pointer to the open file stream.
- Return Value:
- Returns the integer value of the file descriptor associated with the given
- stream on success.
- -1 on failure, and errno will contain more information.
- --*/
- {
- if (Stream == NULL) {
- errno = EBADF;
- return -1;
- }
- return Stream->Descriptor;
- }
- LIBC_API
- int
- fgetc (
- FILE *Stream
- )
- /*++
- Routine Description:
- This routine reads one byte from the given file stream.
- Arguments:
- Stream - Supplies a pointer to the open file stream.
- Return Value:
- Returns the byte on success.
- EOF on failure or the end of the file, and errno will contain more
- information.
- --*/
- {
- int Result;
- ClpLockStream(Stream);
- Result = fgetc_unlocked(Stream);
- ClpUnlockStream(Stream);
- return Result;
- }
- LIBC_API
- int
- fgetc_unlocked (
- FILE *Stream
- )
- /*++
- Routine Description:
- This routine reads one byte from the given file stream without acquiring
- the internal stream lock.
- Arguments:
- Stream - Supplies a pointer to the open file stream.
- Return Value:
- Returns the byte on success.
- EOF on failure or the end of the file, and errno will contain more
- information.
- --*/
- {
- unsigned char Byte;
- ssize_t Result;
- ORIENT_STREAM(Stream, FILE_FLAG_BYTE_ORIENTED);
- Result = fread_unlocked(&Byte, 1, 1, Stream);
- if (Result == 0) {
- return EOF;
- }
- return Byte;
- }
- LIBC_API
- int
- getchar (
- void
- )
- /*++
- Routine Description:
- This routine reads one byte from stdin.
- Arguments:
- None.
- Return Value:
- Returns the byte from stdin on success.
- -1 on failure, and errno will contain more information.
- --*/
- {
- return fgetc(stdin);
- }
- LIBC_API
- int
- getchar_unlocked (
- void
- )
- /*++
- Routine Description:
- This routine reads one byte from stdin without acquiring the file lock.
- Arguments:
- None.
- Return Value:
- Returns the byte from stdin on success.
- -1 on failure, and errno will contain more information.
- --*/
- {
- return fgetc_unlocked(stdin);
- }
- LIBC_API
- int
- getc (
- FILE *Stream
- )
- /*++
- Routine Description:
- This routine reads one byte from the given file stream. It is equivalent to
- the fgetc function.
- Arguments:
- Stream - Supplies a pointer to the open file stream.
- Return Value:
- Returns the byte on success.
- EOF on failure or the end of the file, and errno will contain more
- information.
- --*/
- {
- return fgetc(Stream);
- }
- LIBC_API
- int
- getc_unlocked (
- FILE *Stream
- )
- /*++
- Routine Description:
- This routine reads one byte from the given file stream, without acquiring
- the internal file lock. It is equivalent to the fgetc_unlocked function.
- Arguments:
- Stream - Supplies a pointer to the open file stream.
- Return Value:
- Returns the byte on success.
- EOF on failure or the end of the file, and errno will contain more
- information.
- --*/
- {
- return fgetc_unlocked(Stream);
- }
- LIBC_API
- char *
- gets (
- char *Line
- )
- /*++
- Routine Description:
- This routine gets a line of input from standard in, writing bytes to the
- supplied line until a newline or end of file is reached. The newline (if
- found) will be discarded and the string null terminated.
- Use of this function is highly discouraged, as there is no bound on the
- size of text the user can put on one line. Any use of this function is a
- security hole. Use fgets instead.
- Arguments:
- Line - Supplies a pointer where the line will be returned on success.
- Return Value:
- Returns a pointer to the supplied line buffer on success.
- NULL if EOF was encountered.
- --*/
- {
- int Character;
- int Index;
- Character = EOF;
- if (Line == NULL) {
- return NULL;
- }
- //
- // Loop reading in characters.
- //
- Index = 0;
- while (TRUE) {
- Character = fgetc(stdin);
- if (Character == EOF) {
- break;
- }
- if (Character == '\n') {
- break;
- }
- Line[Index] = Character;
- Index += 1;
- }
- Line[Index] = '\0';
- if (Character == EOF) {
- return NULL;
- }
- return Line;
- }
- LIBC_API
- char *
- fgets (
- char *Buffer,
- int BufferSize,
- FILE *Stream
- )
- /*++
- Routine Description:
- This routine reads bytes from the given stream and places them in the
- given buffer until the buffer fills up, a newline is read and transferred,
- or the end of the file is reached. The string is then terminated with a
- null byte.
- Arguments:
- Buffer - Supplies a pointer to the buffer where the read characters should
- be returned.
- BufferSize - Supplies the size of the supplied buffer in bytes.
- Stream - Supplies a pointer to the file stream to read out of.
- Return Value:
- Returns the given buffer on success. If the stream is at end-of-file, the
- end-of-file indicator shall be set and this routine returns NULL. If a
- read error occurs, the error indicator for the stream shall be set, and
- this routine returns NULL. The errno variable may also be set to contain
- more information.
- --*/
- {
- char *Result;
- ClpLockStream(Stream);
- Result = fgets_unlocked(Buffer, BufferSize, Stream);
- ClpUnlockStream(Stream);
- return Result;
- }
- LIBC_API
- char *
- fgets_unlocked (
- char *Buffer,
- int BufferSize,
- FILE *Stream
- )
- /*++
- Routine Description:
- This routine reads bytes from the given stream and places them in the
- given buffer until the buffer fills up, a newline is read and transferred,
- or the end of the file is reached. The string is then terminated with a
- null byte.
- Arguments:
- Buffer - Supplies a pointer to the buffer where the read characters should
- be returned.
- BufferSize - Supplies the size of the supplied buffer in bytes.
- Stream - Supplies a pointer to the file stream to read out of.
- Return Value:
- Returns the given buffer on success. If the stream is at end-of-file, the
- end-of-file indicator shall be set and this routine returns NULL. If a
- read error occurs, the error indicator for the stream shall be set, and
- this routine returns NULL. The errno variable may also be set to contain
- more information.
- --*/
- {
- int Character;
- int Index;
- Character = EOF;
- if ((Buffer == NULL) || (BufferSize < 1)) {
- return NULL;
- }
- //
- // Loop reading in characters until the buffer is full.
- //
- Index = 0;
- while (Index < BufferSize - 1) {
- Character = fgetc_unlocked(Stream);
- if (Character == EOF) {
- break;
- }
- Buffer[Index] = Character;
- Index += 1;
- if (Character == '\n') {
- break;
- }
- }
- Buffer[Index] = '\0';
- if (Index == 0) {
- return NULL;
- }
- return Buffer;
- }
- LIBC_API
- int
- fputc (
- int Character,
- FILE *Stream
- )
- /*++
- Routine Description:
- This routine writes a byte to the given file stream.
- Arguments:
- Character - Supplies the character (converted to an unsigned char) to
- write.
- Stream - Supplies the stream to write the character to.
- Return Value:
- Returns the byte it has written on success.
- EOF on failure, and errno will contain more information.
- --*/
- {
- int Result;
- ClpLockStream(Stream);
- Result = fputc_unlocked(Character, Stream);
- ClpUnlockStream(Stream);
- return Result;
- }
- LIBC_API
- int
- fputc_unlocked (
- int Character,
- FILE *Stream
- )
- /*++
- Routine Description:
- This routine writes a byte to the given file stream without acquiring the
- internal stream lock.
- Arguments:
- Character - Supplies the character (converted to an unsigned char) to
- write.
- Stream - Supplies the stream to write the character to.
- Return Value:
- Returns the byte it has written on success.
- EOF on failure, and errno will contain more information.
- --*/
- {
- unsigned char Byte;
- int Result;
- ORIENT_STREAM(Stream, FILE_FLAG_BYTE_ORIENTED);
- Byte = (unsigned char)Character;
- Result = fwrite_unlocked(&Byte, 1, 1, Stream);
- if (Result > 0) {
- return Byte;
- }
- return EOF;
- }
- LIBC_API
- int
- putc (
- int Character,
- FILE *Stream
- )
- /*++
- Routine Description:
- This routine writes a byte to the given file stream. It is equivalent to
- the fputc function.
- Arguments:
- Character - Supplies the character (converted to an unsigned char) to
- write.
- Stream - Supplies the stream to write the character to.
- Return Value:
- Returns the byte it has written on success.
- EOF on failure, and errno will contain more information.
- --*/
- {
- return fputc(Character, Stream);
- }
- LIBC_API
- int
- putc_unlocked (
- int Character,
- FILE *Stream
- )
- /*++
- Routine Description:
- This routine writes a byte to the given file stream without acquiring the
- stream lock. It is equivalent to the fputc_unlocked function.
- Arguments:
- Character - Supplies the character (converted to an unsigned char) to
- write.
- Stream - Supplies the stream to write the character to.
- Return Value:
- Returns the byte it has written on success.
- EOF on failure, and errno will contain more information.
- --*/
- {
- return fputc_unlocked(Character, Stream);
- }
- LIBC_API
- int
- putchar (
- int Character
- )
- /*++
- Routine Description:
- This routine writes a byte to standard out. This routine is equivalent to
- fputc(Character, stdout).
- Arguments:
- Character - Supplies the character (converted to an unsigned char) to
- write.
- Return Value:
- Returns the byte it has written on success.
- EOF on failure, and errno will contain more information.
- --*/
- {
- return fputc(Character, stdout);
- }
- LIBC_API
- int
- putchar_unlocked (
- int Character
- )
- /*++
- Routine Description:
- This routine writes a byte to standard out, without acquiring the stream
- lock. This routine is equivalent to fputc_unlocked(Character, stdout).
- Arguments:
- Character - Supplies the character (converted to an unsigned char) to
- write.
- Return Value:
- Returns the byte it has written on success.
- EOF on failure, and errno will contain more information.
- --*/
- {
- return fputc_unlocked(Character, stdout);
- }
- LIBC_API
- int
- puts (
- const char *String
- )
- /*++
- Routine Description:
- This routine writes the given string to standard out. The null terminating
- byte is not written.
- Arguments:
- String - Supplies a pointer to the null terminated string to write to.
- Stream - Supplies the stream to write the character to.
- Return Value:
- Returns a non-negative number on success.
- Returns EOF on failure, and the error indicator will be set for the stream.
- The errno variable will also be set to provide more information.
- --*/
- {
- int Result;
- Result = fputs(String, stdout);
- if (Result == EOF) {
- return Result;
- }
- return fputc('\n', stdout);
- }
- LIBC_API
- int
- fputs (
- const char *String,
- FILE *Stream
- )
- /*++
- Routine Description:
- This routine writes the given string to the given file stream. The null
- terminating byte is not written.
- Arguments:
- String - Supplies a pointer to the null terminated string to write to.
- Stream - Supplies the stream to write the character to.
- Return Value:
- Returns a non-negative number on success.
- Returns EOF on failure, and the error indicator will be set for the stream.
- The errno variable will also be set to provide more information.
- --*/
- {
- int Result;
- ClpLockStream(Stream);
- Result = fputs_unlocked(String, Stream);
- ClpUnlockStream(Stream);
- return Result;
- }
- LIBC_API
- int
- fputs_unlocked (
- const char *String,
- FILE *Stream
- )
- /*++
- Routine Description:
- This routine writes the given string to the given file stream. The null
- terminating byte is not written. This routine does not acquire the stream
- lock.
- Arguments:
- String - Supplies a pointer to the null terminated string to write to.
- Stream - Supplies the stream to write the character to.
- Return Value:
- Returns a non-negative number on success.
- Returns EOF on failure, and the error indicator will be set for the stream.
- The errno variable will also be set to provide more information.
- --*/
- {
- size_t Length;
- int Result;
- Result = 0;
- if (String == NULL) {
- return Result;
- }
- ORIENT_STREAM(Stream, FILE_FLAG_BYTE_ORIENTED);
- Length = strlen(String);
- Result = fwrite_unlocked(String, 1, Length, Stream);
- if (Result == Length) {
- return Result;
- }
- return EOF;
- }
- LIBC_API
- int
- ungetc (
- int Character,
- FILE *Stream
- )
- /*++
- Routine Description:
- This routine pushes the specified character back onto the input stream. The
- pushed back character shall be returned by subsequent reads on that stream
- in the reverse order of their pushing. A successful intervening call to
- seek or flush will discard any pushed back bytes for the stream. One byte
- of push back is provided.
- Arguments:
- Character - Supplies the character (converted to an unsigned char) to
- push back.
- Stream - Supplies the stream to push the character on to.
- Return Value:
- Returns the byte pushed back on success.
- EOF on failure, and errno will contain more information.
- --*/
- {
- int Result;
- ClpLockStream(Stream);
- Result = ungetc_unlocked(Character, Stream);
- ClpUnlockStream(Stream);
- return Result;
- }
- LIBC_API
- int
- ungetc_unlocked (
- int Character,
- FILE *Stream
- )
- /*++
- Routine Description:
- This routine pushes the specified character back onto the input stream. The
- pushed back character shall be returned by subsequent reads on that stream
- in the reverse order of their pushing. A successful intervening call to
- seek or flush will discard any pushed back bytes for the stream. One byte
- of push back is provided. This routine does not acquire the internal
- stream lock.
- Arguments:
- Character - Supplies the character (converted to an unsigned char) to
- push back.
- Stream - Supplies the stream to push the character on to.
- Return Value:
- Returns the byte pushed back on success.
- EOF on failure, and errno will contain more information.
- --*/
- {
- unsigned char Byte;
- if (Character == EOF) {
- return EOF;
- }
- ORIENT_STREAM(Stream, FILE_FLAG_BYTE_ORIENTED);
- Byte = (unsigned char)Character;
- if ((Stream->Flags & FILE_FLAG_UNGET_VALID) == 0) {
- Stream->Flags |= FILE_FLAG_UNGET_VALID;
- Stream->Flags &= ~FILE_FLAG_END_OF_FILE;
- Stream->UngetCharacter = Byte;
- return Byte;
- }
- return EOF;
- }
- LIBC_API
- int
- setvbuf (
- FILE *Stream,
- char *Buffer,
- int Mode,
- size_t BufferSize
- )
- /*++
- Routine Description:
- This routine sets the buffering mode and buffer (optionally) for the given
- file stream.
- Arguments:
- Stream - Supplies a pointer to the file stream whose buffering
- characteristics should be altered.
- Buffer - Supplies an optional pointer to the buffer to use for fully
- buffered or line buffered mode. If either of those two modes are
- supplied and this buffer is not supplied, one will be malloced.
- Mode - Supplies the buffering mode to set for this file stream. Valid
- value are:
- _IONBF - Disable buffering on this file stream. All reads and writes
- will go straight to the low level I/O interface.
- _IOFBF - Fully buffered mode. The stream interface will batch reads
- and writes to the low level I/O interface to optimize performance.
- _IOLBF - Line buffered mode. This mode behaves the same as fully
- buffered mode, except that the buffer is flushed automatically when
- a newline character is written to the stream. For input buffers,
- the behavior is no different from fully buffered mode.
- BufferSize - Supplies the size of the supplied buffer in bytes.
- Return Value:
- 0 on success.
- -1 on failure, and errno will contain more information.
- --*/
- {
- int Result;
- Result = -1;
- ClpLockStream(Stream);
- if ((Mode != _IOLBF) && (Mode != _IOFBF) && (Mode != _IONBF)) {
- errno = EINVAL;
- goto setvbufEnd;
- }
- //
- // Flush the file for safety, even though generally users aren't supposed
- // to call this after they've started doing I/O on the stream.
- //
- fflush_unlocked(Stream);
- //
- // Free the old buffer.
- //
- if (((Stream->Flags & FILE_FLAG_BUFFER_ALLOCATED) != 0) &&
- (Stream->Buffer != NULL)) {
- free(Stream->Buffer);
- Stream->Flags &= ~FILE_FLAG_BUFFER_ALLOCATED;
- }
- Stream->Buffer = NULL;
- //
- // Un-buffered mode is easy, just leave the buffer NULLed out.
- //
- if (Mode == _IONBF) {
- Stream->BufferSize = 0;
- //
- // For buffered mode, either use the buffer they provided or allocate
- // one.
- //
- } else {
- if ((Buffer == NULL) || (BufferSize == 0)) {
- if (BufferSize == 0) {
- BufferSize = BUFSIZ;
- }
- Buffer = malloc(BufferSize);
- if (Buffer == NULL) {
- errno = ENOMEM;
- goto setvbufEnd;
- }
- Stream->Flags |= FILE_FLAG_BUFFER_ALLOCATED;
- }
- Stream->Buffer = Buffer;
- Stream->BufferSize = BufferSize;
- }
- Stream->BufferMode = Mode;
- Result = 0;
- setvbufEnd:
- ClpUnlockStream(Stream);
- return Result;
- }
- LIBC_API
- void
- setbuf (
- FILE *Stream,
- char *Buffer
- )
- /*++
- Routine Description:
- This routine sets the internal buffer on a stream. If the given buffer is
- not NULL, this routine shall be equivalent to:
- setvbuf(Stream, Buffer, _IOFBF, BUFSIZ);
- or,
- setvbuf(Stream, Buffer, _IONBF, BUFSIZ);
- if the given buffer is a NULL pointer.
- Arguments:
- Stream - Supplies a pointer to the file stream.
- Buffer - Supplies the optional buffer to use.
- Return Value:
- None.
- --*/
- {
- if (Buffer != NULL) {
- setvbuf(Stream, Buffer, _IOFBF, BUFSIZ);
- } else {
- setvbuf(Stream, Buffer, _IONBF, BUFSIZ);
- }
- return;
- }
- LIBC_API
- void
- clearerr (
- FILE *Stream
- )
- /*++
- Routine Description:
- This routine clears the error and end-of-file indicators for the given
- stream.
- Arguments:
- Stream - Supplies a pointer to the file stream.
- Return Value:
- None.
- --*/
- {
- ClpLockStream(Stream);
- clearerr_unlocked(Stream);
- ClpUnlockStream(Stream);
- return;
- }
- LIBC_API
- void
- clearerr_unlocked (
- FILE *Stream
- )
- /*++
- Routine Description:
- This routine clears the error and end-of-file indicators for the given
- stream. This routine does not acquire the file lock.
- Arguments:
- Stream - Supplies a pointer to the file stream.
- Return Value:
- None.
- --*/
- {
- Stream->Flags &= ~(FILE_FLAG_ERROR | FILE_FLAG_END_OF_FILE);
- return;
- }
- LIBC_API
- int
- feof (
- FILE *Stream
- )
- /*++
- Routine Description:
- This routine returns whether or not the current stream has attempted to
- read beyond the end of the file.
- Arguments:
- Stream - Supplies a pointer to the file stream.
- Return Value:
- Returns non-zero if the end of file indicator is set for the given stream.
- --*/
- {
- int Result;
- ClpLockStream(Stream);
- Result = feof_unlocked(Stream);
- ClpUnlockStream(Stream);
- return Result;
- }
- LIBC_API
- int
- feof_unlocked (
- FILE *Stream
- )
- /*++
- Routine Description:
- This routine returns whether or not the current stream has attempted to
- read beyond the end of the file, without acquiring the file lock.
- Arguments:
- Stream - Supplies a pointer to the file stream.
- Return Value:
- Returns non-zero if the end of file indicator is set for the given stream.
- --*/
- {
- if ((Stream->Flags & FILE_FLAG_END_OF_FILE) != 0) {
- return TRUE;
- }
- return FALSE;
- }
- LIBC_API
- int
- ferror (
- FILE *Stream
- )
- /*++
- Routine Description:
- This routine returns whether or not the error indicator is set for the
- current stream.
- Arguments:
- Stream - Supplies a pointer to the file stream.
- Return Value:
- Returns non-zero if the error indicator is set for the given stream.
- --*/
- {
- int Result;
- ClpLockStream(Stream);
- Result = ferror_unlocked(Stream);
- ClpUnlockStream(Stream);
- return Result;
- }
- LIBC_API
- int
- ferror_unlocked (
- FILE *Stream
- )
- /*++
- Routine Description:
- This routine returns whether or not the error indicator is set for the
- current stream, without acquiring the file lock.
- Arguments:
- Stream - Supplies a pointer to the file stream.
- Return Value:
- Returns non-zero if the error indicator is set for the given stream.
- --*/
- {
- if ((Stream->Flags & FILE_FLAG_ERROR) != 0) {
- return TRUE;
- }
- return FALSE;
- }
- LIBC_API
- void
- flockfile (
- FILE *Stream
- )
- /*++
- Routine Description:
- This routine explicitly locks a file stream. It can be used on the standard
- I/O streams to group a batch of I/O together.
- Arguments:
- Stream - Supplies a pointer to the file stream.
- Return Value:
- None.
- --*/
- {
- return ClpLockStream(Stream);
- }
- LIBC_API
- int
- ftrylockfile (
- FILE *Stream
- )
- /*++
- Routine Description:
- This routine attempts to acquire the lock for a given stream.
- Arguments:
- Stream - Supplies a pointer to the file stream.
- Return Value:
- 0 if the lock was successfully acquired.
- Non-zero if the file lock could not be acquired.
- --*/
- {
- if (ClpTryToLockStream(Stream) != FALSE) {
- return 0;
- }
- return -1;
- }
- LIBC_API
- void
- funlockfile (
- FILE *Stream
- )
- /*++
- Routine Description:
- This routine explicitly unlocks a file stream that had been previously
- locked with flockfile or ftrylockfile (on a successful attempt).
- Arguments:
- Stream - Supplies a pointer to the file stream.
- Return Value:
- None.
- --*/
- {
- return ClpUnlockStream(Stream);
- }
- LIBC_API
- int
- printf (
- const char *Format,
- ...
- )
- /*++
- Routine Description:
- This routine prints a formatted string to the standard output file stream.
- Arguments:
- Format - Supplies the printf format string.
- ... - Supplies a variable number of arguments, as required by the printf
- format string argument.
- Return Value:
- Returns the number of bytes successfully converted, not including the null
- terminator.
- Returns a negative number if an error was encountered.
- --*/
- {
- va_list Arguments;
- int Result;
- va_start(Arguments, Format);
- Result = vfprintf(stdout, Format, Arguments);
- va_end(Arguments);
- return Result;
- }
- LIBC_API
- int
- fprintf (
- FILE *Stream,
- const char *Format,
- ...
- )
- /*++
- Routine Description:
- This routine prints a formatted string to the given file stream.
- Arguments:
- Stream - Supplies the file stream to print to.
- Format - Supplies the printf format string.
- ... - Supplies a variable number of arguments, as required by the printf
- format string argument.
- Return Value:
- Returns the number of bytes successfully converted, not including the null
- terminator.
- Returns a negative number if an error was encountered.
- --*/
- {
- va_list Arguments;
- int Result;
- va_start(Arguments, Format);
- Result = vfprintf(Stream, Format, Arguments);
- va_end(Arguments);
- return Result;
- }
- LIBC_API
- int
- fprintf_unlocked (
- FILE *Stream,
- const char *Format,
- ...
- )
- /*++
- Routine Description:
- This routine prints a formatted string to the given file stream. This
- routine does not acquire the stream lock.
- Arguments:
- Stream - Supplies the file stream to print to.
- Format - Supplies the printf format string.
- ... - Supplies a variable number of arguments, as required by the printf
- format string argument.
- Return Value:
- Returns the number of bytes successfully converted, not including the null
- terminator.
- Returns a negative number if an error was encountered.
- --*/
- {
- va_list Arguments;
- int Result;
- va_start(Arguments, Format);
- Result = vfprintf_unlocked(Stream, Format, Arguments);
- va_end(Arguments);
- return Result;
- }
- LIBC_API
- int
- vfprintf (
- FILE *File,
- const char *Format,
- va_list Arguments
- )
- /*++
- Routine Description:
- This routine prints a formatted string to the given file pointer.
- Arguments:
- File - Supplies a pointer to the file stream to output to.
- Format - Supplies the printf format string.
- Arguments - Supplies the argument list to the format string. The va_end
- macro is not invoked on this list.
- Return Value:
- Returns the number of bytes successfully converted. A null terminator is
- not written.
- Returns a negative number if an error was encountered.
- --*/
- {
- int Result;
- ClpLockStream(File);
- Result = vfprintf_unlocked(File, Format, Arguments);
- ClpUnlockStream(File);
- return Result;
- }
- LIBC_API
- int
- vfprintf_unlocked (
- FILE *File,
- const char *Format,
- va_list Arguments
- )
- /*++
- Routine Description:
- This routine prints a formatted string to the given file pointer. This
- routine does not acquire the stream lock.
- Arguments:
- File - Supplies a pointer to the file stream to output to.
- Format - Supplies the printf format string.
- Arguments - Supplies the argument list to the format string. The va_end
- macro is not invoked on this list.
- Return Value:
- Returns the number of bytes successfully converted. A null terminator is
- not written.
- Returns a negative number if an error was encountered.
- --*/
- {
- ULONG CharactersWritten;
- PRINT_FORMAT_CONTEXT PrintContext;
- STREAM_PRINT_CONTEXT StreamContext;
- StreamContext.Stream = File;
- StreamContext.BufferNextIndex = 0;
- StreamContext.CharactersWritten = 0;
- memset(&PrintContext, 0, sizeof(PRINT_FORMAT_CONTEXT));
- PrintContext.Context = &StreamContext;
- PrintContext.WriteCharacter = ClpFileFormatWriteCharacter;
- RtlInitializeMultibyteState(&(PrintContext.State),
- CharacterEncodingDefault);
- RtlFormat(&PrintContext, (PSTR)Format, Arguments);
- //
- // Flush the remaining buffered bytes if necessary.
- //
- if (File->BufferMode == _IONBF) {
- if (StreamContext.BufferNextIndex != 0) {
- CharactersWritten = fwrite_unlocked(StreamContext.Buffer,
- 1,
- StreamContext.BufferNextIndex,
- File);
- if (CharactersWritten > 0) {
- StreamContext.CharactersWritten += CharactersWritten;
- }
- }
- CharactersWritten = StreamContext.CharactersWritten;
- assert(CharactersWritten <= PrintContext.CharactersWritten);
- } else {
- CharactersWritten = PrintContext.CharactersWritten;
- }
- return CharactersWritten;
- }
- LIBC_API
- int
- vprintf (
- const char *Format,
- va_list Arguments
- )
- /*++
- Routine Description:
- This routine prints a formatted string to the standard output file stream.
- Arguments:
- Format - Supplies the printf format string.
- Arguments - Supplies the argument list to the format string. The va_end
- macro is not invoked on this list.
- Return Value:
- Returns the number of bytes successfully converted, not including the null
- terminator.
- Returns a negative number if an error was encountered.
- --*/
- {
- return vfprintf(stdout, Format, Arguments);
- }
- BOOL
- ClpInitializeFileIo (
- VOID
- )
- /*++
- Routine Description:
- This routine initializes the file I/O subsystem of the C library.
- Arguments:
- None.
- Return Value:
- TRUE on success.
- FALSE on failure.
- --*/
- {
- pthread_mutexattr_t Attribute;
- ULONG BufferMode;
- BOOL Result;
- Result = FALSE;
- //
- // Initialize the global stream list.
- //
- INITIALIZE_LIST_HEAD(&ClStreamList);
- pthread_mutexattr_init(&Attribute);
- pthread_mutexattr_settype(&Attribute, PTHREAD_MUTEX_RECURSIVE);
- pthread_mutex_init(&ClStreamListLock, &Attribute);
- pthread_mutexattr_destroy(&Attribute);
- //
- // Create file pointers for the three standard streams. Standard in and
- // standard out are fully buffered only if they are not pointing at an
- // interactive device.
- //
- BufferMode = _IOFBF;
- if (isatty(STDIN_FILENO) != 0) {
- BufferMode = _IOLBF;
- }
- stdin = ClpCreateFileStructure(STDIN_FILENO, O_RDONLY, BufferMode);
- if (stdin == NULL) {
- goto InitializeFileIoEnd;
- }
- stdin->Flags |= FILE_FLAG_STANDARD_IO;
- BufferMode = _IOFBF;
- if (isatty(STDOUT_FILENO) != 0) {
- BufferMode = _IOLBF;
- }
- stdout = ClpCreateFileStructure(STDOUT_FILENO, O_WRONLY, BufferMode);
- if (stdout == NULL) {
- goto InitializeFileIoEnd;
- }
- stdout->Flags |= FILE_FLAG_STANDARD_IO;
- //
- // Standard error is never buffered. Default to unbuffered.
- //
- stderr = ClpCreateFileStructure(STDERR_FILENO, O_WRONLY, _IONBF);
- if (stderr == NULL) {
- goto InitializeFileIoEnd;
- }
- stderr->Flags |= FILE_FLAG_STANDARD_IO;
- Result = TRUE;
- InitializeFileIoEnd:
- if (Result == FALSE) {
- if (stdin != NULL) {
- ClpDestroyFileStructure(stdin);
- stdin = NULL;
- }
- if (stdout != NULL) {
- ClpDestroyFileStructure(stdout);
- stdout = NULL;
- }
- if (stderr != NULL) {
- ClpDestroyFileStructure(stderr);
- stderr = NULL;
- }
- }
- return Result;
- }
- VOID
- ClpLockStream (
- FILE *Stream
- )
- /*++
- Routine Description:
- This routine locks the file stream.
- Arguments:
- Stream - Supplies a pointer to the stream to lock.
- Return Value:
- None.
- --*/
- {
- int Status;
- if ((Stream->Flags & FILE_FLAG_DISABLE_LOCKING) != 0) {
- return;
- }
- Status = pthread_mutex_lock(&(Stream->Lock));
- ASSERT(Status == 0);
- return;
- }
- BOOL
- ClpTryToLockStream (
- FILE *Stream
- )
- /*++
- Routine Description:
- This routine makes a single attempt at locking the file stream. If locking
- is disabled on the stream, this always returns TRUE.
- Arguments:
- Stream - Supplies a pointer to the stream to try to lock.
- Return Value:
- TRUE if the lock was successfully acquired.
- FALSE if the lock was not successfully acquired.
- --*/
- {
- int Status;
- if ((Stream->Flags & FILE_FLAG_DISABLE_LOCKING) != 0) {
- return TRUE;
- }
- Status = pthread_mutex_trylock(&(Stream->Lock));
- if (Status == 0) {
- return TRUE;
- }
- return FALSE;
- }
- VOID
- ClpUnlockStream (
- FILE *Stream
- )
- /*++
- Routine Description:
- This routine unlocks the file stream.
- Arguments:
- Stream - Supplies a pointer to the stream to unlock.
- Return Value:
- None.
- --*/
- {
- if ((Stream->Flags & FILE_FLAG_DISABLE_LOCKING) != 0) {
- return;
- }
- pthread_mutex_unlock(&(Stream->Lock));
- return;
- }
- VOID
- ClpFlushAllStreams (
- BOOL AllUnlocked,
- PFILE UnlockedStream
- )
- /*++
- Routine Description:
- This routine flushes every stream in the application.
- Arguments:
- AllUnlocked - Supplies a boolean that if TRUE flushes every stream without
- acquiring the file lock. This is used during aborts.
- UnlockedStream - Supplies a specific stream that when flushed should be
- flushed unlocked.
- Return Value:
- None.
- --*/
- {
- PLIST_ENTRY CurrentEntry;
- PFILE Stream;
- pthread_mutex_lock(&ClStreamListLock);
- CurrentEntry = ClStreamList.Next;
- while (CurrentEntry != &ClStreamList) {
- Stream = LIST_VALUE(CurrentEntry, FILE, ListEntry);
- CurrentEntry = CurrentEntry->Next;
- //
- // Flush any dirty streams.
- //
- if ((Stream->Flags & FILE_FLAG_READ_LAST) == 0) {
- if ((AllUnlocked != FALSE) || (Stream == UnlockedStream)) {
- fflush_unlocked(Stream);
- } else {
- fflush(Stream);
- }
- }
- }
- pthread_mutex_unlock(&ClStreamListLock);
- return;
- }
- //
- // --------------------------------------------------------- Internal Functions
- //
- PFILE
- ClpCreateFileStructure (
- ULONG Descriptor,
- ULONG OpenFlags,
- ULONG BufferMode
- )
- /*++
- Routine Description:
- This routine creates a file stream structure.
- Arguments:
- Descriptor - Supplies the underlying file descriptor number associated with
- this stream.
- OpenFlags - Supplies the flags the file was opened with.
- BufferMode - Supplies the buffering mode for the file.
- Return Value:
- Returns a pointer to the created file on success.
- NULL on allocation failure.
- --*/
- {
- pthread_mutexattr_t Attribute;
- FILE *File;
- BOOL Result;
- Result = FALSE;
- File = malloc(sizeof(FILE));
- if (File == NULL) {
- goto CreateFileEnd;
- }
- RtlZeroMemory(File, sizeof(FILE));
- File->Descriptor = Descriptor;
- File->OpenFlags = OpenFlags;
- pthread_mutexattr_init(&Attribute);
- pthread_mutexattr_settype(&Attribute, PTHREAD_MUTEX_RECURSIVE);
- pthread_mutex_init(&(File->Lock), &Attribute);
- pthread_mutexattr_destroy(&Attribute);
- File->BufferMode = BufferMode;
- if ((OpenFlags & O_ACCMODE) != O_WRONLY) {
- File->Flags |= FILE_FLAG_CAN_READ;
- if ((OpenFlags & O_ACCMODE) == O_RDONLY) {
- File->Flags |= FILE_FLAG_READ_LAST;
- }
- }
- //
- // If the stream is anything other than non-buffered, create a buffer for
- // it.
- //
- if (File->BufferMode != _IONBF) {
- File->Buffer = malloc(BUFSIZ);
- if (File->Buffer == NULL) {
- goto CreateFileEnd;
- }
- File->BufferSize = BUFSIZ;
- File->Flags |= FILE_FLAG_BUFFER_ALLOCATED;
- }
- //
- // Add the file to the global list, making it officially open for business.
- //
- pthread_mutex_lock(&ClStreamListLock);
- INSERT_AFTER(&(File->ListEntry), &ClStreamList);
- pthread_mutex_unlock(&ClStreamListLock);
- Result = TRUE;
- CreateFileEnd:
- if (Result == FALSE) {
- if (File != NULL) {
- if (File->Buffer != NULL) {
- free(File->Buffer);
- }
- free(File);
- File = NULL;
- }
- }
- return File;
- }
- VOID
- ClpDestroyFileStructure (
- PFILE File
- )
- /*++
- Routine Description:
- This routine destroys a file stream structure.
- Arguments:
- File - Supplies a pointer to the file stream structure to destroy.
- Return Value:
- None.
- --*/
- {
- if (File != NULL) {
- if (File->ListEntry.Next != NULL) {
- pthread_mutex_lock(&ClStreamListLock);
- LIST_REMOVE(&(File->ListEntry));
- pthread_mutex_unlock(&ClStreamListLock);
- }
- File->ListEntry.Next = NULL;
- if (((File->Flags & FILE_FLAG_BUFFER_ALLOCATED) != 0) &&
- (File->Buffer != NULL)) {
- free(File->Buffer);
- }
- pthread_mutex_destroy(&(File->Lock));
- free(File);
- }
- return;
- }
- BOOL
- ClpFileFormatWriteCharacter (
- INT Character,
- PPRINT_FORMAT_CONTEXT Context
- )
- /*++
- Routine Description:
- This routine writes a character to the output during a printf-style
- formatting operation.
- Arguments:
- Character - Supplies the character to be written.
- Context - Supplies a pointer to the printf-context.
- Return Value:
- TRUE on success.
- FALSE on failure.
- --*/
- {
- ULONG CharactersWritten;
- PSTREAM_PRINT_CONTEXT StreamContext;
- StreamContext = Context->Context;
- //
- // If the stream is buffered in any way, then pass the character on to the
- // stream.
- //
- if (StreamContext->Stream->BufferMode != _IONBF) {
- if (fputc_unlocked(Character, StreamContext->Stream) == -1) {
- return FALSE;
- }
- //
- // If the stream is unbuffered, then locally buffer some characters
- // together before flushing. This reduces the number of system calls
- // required for fprintf on unbuffered streams.
- //
- } else {
- StreamContext->Buffer[StreamContext->BufferNextIndex] = Character;
- StreamContext->BufferNextIndex += 1;
- //
- // If the local buffer is full, then write to the stream. This will
- // flush the data immediately.
- //
- if (StreamContext->BufferNextIndex == STREAM_PRINT_BUFFER_SIZE) {
- StreamContext->BufferNextIndex = 0;
- CharactersWritten = fwrite_unlocked(StreamContext->Buffer,
- 1,
- STREAM_PRINT_BUFFER_SIZE,
- StreamContext->Stream);
- if (CharactersWritten < 0) {
- return FALSE;
- }
- StreamContext->CharactersWritten += CharactersWritten;
- if (CharactersWritten != STREAM_PRINT_BUFFER_SIZE) {
- return FALSE;
- }
- }
- }
- return TRUE;
- }
- INT
- ClpConvertStreamModeStringToOpenFlags (
- PSTR ModeString,
- INT *OpenFlags
- )
- /*++
- Routine Description:
- This routine converts a mode string supplied with a stream open command to
- a set of open flags.
- Arguments:
- ModeString - Supplies a pointer to the mode string.
- OpenFlags - Supplies a pointer where the open flags will be returned.
- Return Value:
- 0 on success.
- Returns an error number on failure.
- --*/
- {
- INT Flags;
- Flags = 0;
- *OpenFlags = 0;
- //
- // Get the open flags.
- //
- while (*ModeString != '\0') {
- switch (*ModeString) {
- case 'r':
- Flags |= O_RDONLY;
- break;
- case 'w':
- Flags |= O_WRONLY | O_CREAT | O_TRUNC;
- break;
- case 'a':
- Flags |= O_WRONLY | O_CREAT | O_APPEND;
- break;
- case '+':
- Flags &= ~O_ACCMODE;
- Flags |= O_RDWR;
- break;
- case 'b':
- case 't':
- break;
- //
- // TODO: Open the file with O_CLOEXEC.
- //
- case 'e':
- break;
- case 'x':
- Flags |= O_EXCL;
- break;
- default:
- return EINVAL;
- }
- ModeString += 1;
- }
- *OpenFlags = Flags;
- return 0;
- }
|