stdio.texi 226 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505
  1. @node I/O on Streams, Low-Level I/O, I/O Overview, Top
  2. @c %MENU% High-level, portable I/O facilities
  3. @chapter Input/Output on Streams
  4. @c fix an overfull:
  5. @tex
  6. \hyphenation{which-ever}
  7. @end tex
  8. This chapter describes the functions for creating streams and performing
  9. input and output operations on them. As discussed in @ref{I/O
  10. Overview}, a stream is a fairly abstract, high-level concept
  11. representing a communications channel to a file, device, or process.
  12. @menu
  13. * Streams:: About the data type representing a stream.
  14. * Standard Streams:: Streams to the standard input and output
  15. devices are created for you.
  16. * Opening Streams:: How to create a stream to talk to a file.
  17. * Closing Streams:: Close a stream when you are finished with it.
  18. * Streams and Threads:: Issues with streams in threaded programs.
  19. * Streams and I18N:: Streams in internationalized applications.
  20. * Simple Output:: Unformatted output by characters and lines.
  21. * Character Input:: Unformatted input by characters and words.
  22. * Line Input:: Reading a line or a record from a stream.
  23. * Unreading:: Peeking ahead/pushing back input just read.
  24. * Block Input/Output:: Input and output operations on blocks of data.
  25. * Formatted Output:: @code{printf} and related functions.
  26. * Customizing Printf:: You can define new conversion specifiers for
  27. @code{printf} and friends.
  28. * Formatted Input:: @code{scanf} and related functions.
  29. * EOF and Errors:: How you can tell if an I/O error happens.
  30. * Error Recovery:: What you can do about errors.
  31. * Binary Streams:: Some systems distinguish between text files
  32. and binary files.
  33. * File Positioning:: About random-access streams.
  34. * Portable Positioning:: Random access on peculiar ISO C systems.
  35. * Stream Buffering:: How to control buffering of streams.
  36. * Other Kinds of Streams:: Streams that do not necessarily correspond
  37. to an open file.
  38. * Formatted Messages:: Print strictly formatted messages.
  39. @end menu
  40. @node Streams
  41. @section Streams
  42. For historical reasons, the type of the C data structure that represents
  43. a stream is called @code{FILE} rather than ``stream''. Since most of
  44. the library functions deal with objects of type @code{FILE *}, sometimes
  45. the term @dfn{file pointer} is also used to mean ``stream''. This leads
  46. to unfortunate confusion over terminology in many books on C. This
  47. manual, however, is careful to use the terms ``file'' and ``stream''
  48. only in the technical sense.
  49. @cindex file pointer
  50. @pindex stdio.h
  51. The @code{FILE} type is declared in the header file @file{stdio.h}.
  52. @deftp {Data Type} FILE
  53. @standards{ISO, stdio.h}
  54. This is the data type used to represent stream objects. A @code{FILE}
  55. object holds all of the internal state information about the connection
  56. to the associated file, including such things as the file position
  57. indicator and buffering information. Each stream also has error and
  58. end-of-file status indicators that can be tested with the @code{ferror}
  59. and @code{feof} functions; see @ref{EOF and Errors}.
  60. @end deftp
  61. @code{FILE} objects are allocated and managed internally by the
  62. input/output library functions. Don't try to create your own objects of
  63. type @code{FILE}; let the library do it. Your programs should
  64. deal only with pointers to these objects (that is, @code{FILE *} values)
  65. rather than the objects themselves.
  66. @c !!! should say that FILE's have "No user-serviceable parts inside."
  67. @node Standard Streams
  68. @section Standard Streams
  69. @cindex standard streams
  70. @cindex streams, standard
  71. When the @code{main} function of your program is invoked, it already has
  72. three predefined streams open and available for use. These represent
  73. the ``standard'' input and output channels that have been established
  74. for the process.
  75. These streams are declared in the header file @file{stdio.h}.
  76. @pindex stdio.h
  77. @deftypevar {FILE *} stdin
  78. @standards{ISO, stdio.h}
  79. The @dfn{standard input} stream, which is the normal source of input for the
  80. program.
  81. @end deftypevar
  82. @cindex standard input stream
  83. @deftypevar {FILE *} stdout
  84. @standards{ISO, stdio.h}
  85. The @dfn{standard output} stream, which is used for normal output from
  86. the program.
  87. @end deftypevar
  88. @cindex standard output stream
  89. @deftypevar {FILE *} stderr
  90. @standards{ISO, stdio.h}
  91. The @dfn{standard error} stream, which is used for error messages and
  92. diagnostics issued by the program.
  93. @end deftypevar
  94. @cindex standard error stream
  95. On @gnusystems{}, you can specify what files or processes correspond to
  96. these streams using the pipe and redirection facilities provided by the
  97. shell. (The primitives shells use to implement these facilities are
  98. described in @ref{File System Interface}.) Most other operating systems
  99. provide similar mechanisms, but the details of how to use them can vary.
  100. In @theglibc{}, @code{stdin}, @code{stdout}, and @code{stderr} are
  101. normal variables which you can set just like any others. For example,
  102. to redirect the standard output to a file, you could do:
  103. @smallexample
  104. fclose (stdout);
  105. stdout = fopen ("standard-output-file", "w");
  106. @end smallexample
  107. Note however, that in other systems @code{stdin}, @code{stdout}, and
  108. @code{stderr} are macros that you cannot assign to in the normal way.
  109. But you can use @code{freopen} to get the effect of closing one and
  110. reopening it. @xref{Opening Streams}.
  111. The three streams @code{stdin}, @code{stdout}, and @code{stderr} are not
  112. unoriented at program start (@pxref{Streams and I18N}).
  113. @node Opening Streams
  114. @section Opening Streams
  115. @cindex opening a stream
  116. Opening a file with the @code{fopen} function creates a new stream and
  117. establishes a connection between the stream and a file. This may
  118. involve creating a new file.
  119. @pindex stdio.h
  120. Everything described in this section is declared in the header file
  121. @file{stdio.h}.
  122. @deftypefun {FILE *} fopen (const char *@var{filename}, const char *@var{opentype})
  123. @standards{ISO, stdio.h}
  124. @safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{} @asulock{}}@acunsafe{@acsmem{} @acsfd{} @aculock{}}}
  125. @c fopen may leak the list lock if cancelled within _IO_link_in.
  126. The @code{fopen} function opens a stream for I/O to the file
  127. @var{filename}, and returns a pointer to the stream.
  128. The @var{opentype} argument is a string that controls how the file is
  129. opened and specifies attributes of the resulting stream. It must begin
  130. with one of the following sequences of characters:
  131. @table @samp
  132. @item r
  133. Open an existing file for reading only.
  134. @item w
  135. Open the file for writing only. If the file already exists, it is
  136. truncated to zero length. Otherwise a new file is created.
  137. @item a
  138. Open a file for append access; that is, writing at the end of file only.
  139. If the file already exists, its initial contents are unchanged and
  140. output to the stream is appended to the end of the file.
  141. Otherwise, a new, empty file is created.
  142. @item r+
  143. Open an existing file for both reading and writing. The initial contents
  144. of the file are unchanged and the initial file position is at the
  145. beginning of the file.
  146. @item w+
  147. Open a file for both reading and writing. If the file already exists, it
  148. is truncated to zero length. Otherwise, a new file is created.
  149. @item a+
  150. Open or create file for both reading and appending. If the file exists,
  151. its initial contents are unchanged. Otherwise, a new file is created.
  152. The initial file position for reading is at the beginning of the file,
  153. but output is always appended to the end of the file.
  154. @end table
  155. As you can see, @samp{+} requests a stream that can do both input and
  156. output. When using such a stream, you must call @code{fflush}
  157. (@pxref{Stream Buffering}) or a file positioning function such as
  158. @code{fseek} (@pxref{File Positioning}) when switching from reading
  159. to writing or vice versa. Otherwise, internal buffers might not be
  160. emptied properly.
  161. Additional characters may appear after these to specify flags for the
  162. call. Always put the mode (@samp{r}, @samp{w+}, etc.) first; that is
  163. the only part you are guaranteed will be understood by all systems.
  164. @Theglibc{} defines additional characters for use in @var{opentype}:
  165. @table @samp
  166. @item c
  167. The file is opened with cancellation in the I/O functions disabled.
  168. @item e
  169. The underlying file descriptor will be closed if you use any of the
  170. @code{exec@dots{}} functions (@pxref{Executing a File}). (This is
  171. equivalent to having set @code{FD_CLOEXEC} on that descriptor.
  172. @xref{Descriptor Flags}.)
  173. @item m
  174. The file is opened and accessed using @code{mmap}. This is only
  175. supported with files opened for reading.
  176. @item x
  177. Insist on creating a new file---if a file @var{filename} already
  178. exists, @code{fopen} fails rather than opening it. If you use
  179. @samp{x} you are guaranteed that you will not clobber an existing
  180. file. This is equivalent to the @code{O_EXCL} option to the
  181. @code{open} function (@pxref{Opening and Closing Files}).
  182. The @samp{x} modifier is part of @w{ISO C11}.
  183. @end table
  184. The character @samp{b} in @var{opentype} has a standard meaning; it
  185. requests a binary stream rather than a text stream. But this makes no
  186. difference in POSIX systems (including @gnusystems{}). If both
  187. @samp{+} and @samp{b} are specified, they can appear in either order.
  188. @xref{Binary Streams}.
  189. @cindex stream orientation
  190. @cindex orientation, stream
  191. If the @var{opentype} string contains the sequence
  192. @code{,ccs=@var{STRING}} then @var{STRING} is taken as the name of a
  193. coded character set and @code{fopen} will mark the stream as
  194. wide-oriented with appropriate conversion functions in place to convert
  195. from and to the character set @var{STRING}. Any other stream
  196. is opened initially unoriented and the orientation is decided with the
  197. first file operation. If the first operation is a wide character
  198. operation, the stream is not only marked as wide-oriented, also the
  199. conversion functions to convert to the coded character set used for the
  200. current locale are loaded. This will not change anymore from this point
  201. on even if the locale selected for the @code{LC_CTYPE} category is
  202. changed.
  203. Any other characters in @var{opentype} are simply ignored. They may be
  204. meaningful in other systems.
  205. If the open fails, @code{fopen} returns a null pointer.
  206. When the sources are compiled with @code{_FILE_OFFSET_BITS == 64} on a
  207. 32 bit machine this function is in fact @code{fopen64} since the LFS
  208. interface replaces transparently the old interface.
  209. @end deftypefun
  210. You can have multiple streams (or file descriptors) pointing to the same
  211. file open at the same time. If you do only input, this works
  212. straightforwardly, but you must be careful if any output streams are
  213. included. @xref{Stream/Descriptor Precautions}. This is equally true
  214. whether the streams are in one program (not usual) or in several
  215. programs (which can easily happen). It may be advantageous to use the
  216. file locking facilities to avoid simultaneous access. @xref{File
  217. Locks}.
  218. @deftypefun {FILE *} fopen64 (const char *@var{filename}, const char *@var{opentype})
  219. @standards{Unix98, stdio.h}
  220. @safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{} @asulock{}}@acunsafe{@acsmem{} @acsfd{} @aculock{}}}
  221. This function is similar to @code{fopen} but the stream it returns a
  222. pointer for is opened using @code{open64}. Therefore this stream can be
  223. used even on files larger than @twoexp{31} bytes on 32 bit machines.
  224. Please note that the return type is still @code{FILE *}. There is no
  225. special @code{FILE} type for the LFS interface.
  226. If the sources are compiled with @code{_FILE_OFFSET_BITS == 64} on a 32
  227. bits machine this function is available under the name @code{fopen}
  228. and so transparently replaces the old interface.
  229. @end deftypefun
  230. @deftypevr Macro int FOPEN_MAX
  231. @standards{ISO, stdio.h}
  232. The value of this macro is an integer constant expression that
  233. represents the minimum number of streams that the implementation
  234. guarantees can be open simultaneously. You might be able to open more
  235. than this many streams, but that is not guaranteed. The value of this
  236. constant is at least eight, which includes the three standard streams
  237. @code{stdin}, @code{stdout}, and @code{stderr}. In POSIX.1 systems this
  238. value is determined by the @code{OPEN_MAX} parameter; @pxref{General
  239. Limits}. In BSD and GNU, it is controlled by the @code{RLIMIT_NOFILE}
  240. resource limit; @pxref{Limits on Resources}.
  241. @end deftypevr
  242. @deftypefun {FILE *} freopen (const char *@var{filename}, const char *@var{opentype}, FILE *@var{stream})
  243. @standards{ISO, stdio.h}
  244. @safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{} @acsfd{}}}
  245. @c Like most I/O operations, this one is guarded by a recursive lock,
  246. @c released even upon cancellation, but cancellation may leak file
  247. @c descriptors and leave the stream in an inconsistent state (e.g.,
  248. @c still bound to the closed descriptor). Also, if the stream is
  249. @c part-way through a significant update (say running freopen) when a
  250. @c signal handler calls freopen again on the same stream, the result is
  251. @c likely to be an inconsistent stream, and the possibility of closing
  252. @c twice file descriptor number that the stream used to use, the second
  253. @c time when it might have already been reused by another thread.
  254. This function is like a combination of @code{fclose} and @code{fopen}.
  255. It first closes the stream referred to by @var{stream}, ignoring any
  256. errors that are detected in the process. (Because errors are ignored,
  257. you should not use @code{freopen} on an output stream if you have
  258. actually done any output using the stream.) Then the file named by
  259. @var{filename} is opened with mode @var{opentype} as for @code{fopen},
  260. and associated with the same stream object @var{stream}.
  261. If the operation fails, a null pointer is returned; otherwise,
  262. @code{freopen} returns @var{stream}. On Linux, @code{freopen} may also
  263. fail and set @code{errno} to @code{EBUSY} when the kernel structure for
  264. the old file descriptor was not initialized completely before @code{freopen}
  265. was called. This can only happen in multi-threaded programs, when two
  266. threads race to allocate the same file descriptor number. To avoid the
  267. possibility of this race, do not use @code{close} to close the underlying
  268. file descriptor for a @code{FILE}; either use @code{freopen} while the
  269. file is still open, or use @code{open} and then @code{dup2} to install
  270. the new file descriptor.
  271. @code{freopen} has traditionally been used to connect a standard stream
  272. such as @code{stdin} with a file of your own choice. This is useful in
  273. programs in which use of a standard stream for certain purposes is
  274. hard-coded. In @theglibc{}, you can simply close the standard
  275. streams and open new ones with @code{fopen}. But other systems lack
  276. this ability, so using @code{freopen} is more portable.
  277. When the sources are compiled with @code{_FILE_OFFSET_BITS == 64} on a
  278. 32 bit machine this function is in fact @code{freopen64} since the LFS
  279. interface replaces transparently the old interface.
  280. @end deftypefun
  281. @deftypefun {FILE *} freopen64 (const char *@var{filename}, const char *@var{opentype}, FILE *@var{stream})
  282. @standards{Unix98, stdio.h}
  283. @safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{} @acsfd{}}}
  284. This function is similar to @code{freopen}. The only difference is that
  285. on 32 bit machine the stream returned is able to read beyond the
  286. @twoexp{31} bytes limits imposed by the normal interface. It should be
  287. noted that the stream pointed to by @var{stream} need not be opened
  288. using @code{fopen64} or @code{freopen64} since its mode is not important
  289. for this function.
  290. If the sources are compiled with @code{_FILE_OFFSET_BITS == 64} on a 32
  291. bits machine this function is available under the name @code{freopen}
  292. and so transparently replaces the old interface.
  293. @end deftypefun
  294. In some situations it is useful to know whether a given stream is
  295. available for reading or writing. This information is normally not
  296. available and would have to be remembered separately. Solaris
  297. introduced a few functions to get this information from the stream
  298. descriptor and these functions are also available in @theglibc{}.
  299. @deftypefun int __freadable (FILE *@var{stream})
  300. @standards{GNU, stdio_ext.h}
  301. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  302. The @code{__freadable} function determines whether the stream
  303. @var{stream} was opened to allow reading. In this case the return value
  304. is nonzero. For write-only streams the function returns zero.
  305. This function is declared in @file{stdio_ext.h}.
  306. @end deftypefun
  307. @deftypefun int __fwritable (FILE *@var{stream})
  308. @standards{GNU, stdio_ext.h}
  309. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  310. The @code{__fwritable} function determines whether the stream
  311. @var{stream} was opened to allow writing. In this case the return value
  312. is nonzero. For read-only streams the function returns zero.
  313. This function is declared in @file{stdio_ext.h}.
  314. @end deftypefun
  315. For slightly different kinds of problems there are two more functions.
  316. They provide even finer-grained information.
  317. @deftypefun int __freading (FILE *@var{stream})
  318. @standards{GNU, stdio_ext.h}
  319. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  320. The @code{__freading} function determines whether the stream
  321. @var{stream} was last read from or whether it is opened read-only. In
  322. this case the return value is nonzero, otherwise it is zero.
  323. Determining whether a stream opened for reading and writing was last
  324. used for writing allows to draw conclusions about the content about the
  325. buffer, among other things.
  326. This function is declared in @file{stdio_ext.h}.
  327. @end deftypefun
  328. @deftypefun int __fwriting (FILE *@var{stream})
  329. @standards{GNU, stdio_ext.h}
  330. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  331. The @code{__fwriting} function determines whether the stream
  332. @var{stream} was last written to or whether it is opened write-only. In
  333. this case the return value is nonzero, otherwise it is zero.
  334. This function is declared in @file{stdio_ext.h}.
  335. @end deftypefun
  336. @node Closing Streams
  337. @section Closing Streams
  338. @cindex closing a stream
  339. When a stream is closed with @code{fclose}, the connection between the
  340. stream and the file is canceled. After you have closed a stream, you
  341. cannot perform any additional operations on it.
  342. @deftypefun int fclose (FILE *@var{stream})
  343. @standards{ISO, stdio.h}
  344. @safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{} @asulock{}}@acunsafe{@aculock{} @acsmem{} @acsfd{}}}
  345. @c After fclose, it is undefined behavior to use the stream it points
  346. @c to. Therefore, one must only call fclose when the stream is
  347. @c otherwise unused. Concurrent uses started before will complete
  348. @c successfully because of the lock, which makes it MT-Safe. Calling it
  349. @c from a signal handler is perfectly safe if the stream is known to be
  350. @c no longer used, which is a precondition for fclose to be safe in the
  351. @c first place; since this is no further requirement, fclose is safe for
  352. @c use in async signals too. After calling fclose, you can no longer
  353. @c use the stream, not even to fclose it again, so its memory and file
  354. @c descriptor may leak if fclose is canceled before @c releasing them.
  355. @c That the stream must be unused and it becomes unused after the call
  356. @c is what would enable fclose to be AS- and AC-Safe while freopen
  357. @c isn't. However, because of the possibility of leaving __gconv_lock
  358. @c taken upon cancellation, AC-Safety is lost.
  359. This function causes @var{stream} to be closed and the connection to
  360. the corresponding file to be broken. Any buffered output is written
  361. and any buffered input is discarded. The @code{fclose} function returns
  362. a value of @code{0} if the file was closed successfully, and @code{EOF}
  363. if an error was detected.
  364. It is important to check for errors when you call @code{fclose} to close
  365. an output stream, because real, everyday errors can be detected at this
  366. time. For example, when @code{fclose} writes the remaining buffered
  367. output, it might get an error because the disk is full. Even if you
  368. know the buffer is empty, errors can still occur when closing a file if
  369. you are using NFS.
  370. The function @code{fclose} is declared in @file{stdio.h}.
  371. @end deftypefun
  372. To close all streams currently available @theglibc{} provides
  373. another function.
  374. @deftypefun int fcloseall (void)
  375. @standards{GNU, stdio.h}
  376. @safety{@prelim{}@mtunsafe{@mtasurace{:streams}}@asunsafe{}@acsafe{}}
  377. @c Like fclose, using any previously-opened streams after fcloseall is
  378. @c undefined. However, the implementation of fcloseall isn't equivalent
  379. @c to calling fclose for all streams: it just flushes and unbuffers all
  380. @c streams, without any locking. It's the flushing without locking that
  381. @c makes it unsafe.
  382. This function causes all open streams of the process to be closed and
  383. the connections to corresponding files to be broken. All buffered data
  384. is written and any buffered input is discarded. The @code{fcloseall}
  385. function returns a value of @code{0} if all the files were closed
  386. successfully, and @code{EOF} if an error was detected.
  387. This function should be used only in special situations, e.g., when an
  388. error occurred and the program must be aborted. Normally each single
  389. stream should be closed separately so that problems with individual
  390. streams can be identified. It is also problematic since the standard
  391. streams (@pxref{Standard Streams}) will also be closed.
  392. The function @code{fcloseall} is declared in @file{stdio.h}.
  393. @end deftypefun
  394. If the @code{main} function to your program returns, or if you call the
  395. @code{exit} function (@pxref{Normal Termination}), all open streams are
  396. automatically closed properly. If your program terminates in any other
  397. manner, such as by calling the @code{abort} function (@pxref{Aborting a
  398. Program}) or from a fatal signal (@pxref{Signal Handling}), open streams
  399. might not be closed properly. Buffered output might not be flushed and
  400. files may be incomplete. For more information on buffering of streams,
  401. see @ref{Stream Buffering}.
  402. @node Streams and Threads
  403. @section Streams and Threads
  404. @cindex threads
  405. @cindex multi-threaded application
  406. Streams can be used in multi-threaded applications in the same way they
  407. are used in single-threaded applications. But the programmer must be
  408. aware of the possible complications. It is important to know about
  409. these also if the program one writes never use threads since the design
  410. and implementation of many stream functions are heavily influenced by the
  411. requirements added by multi-threaded programming.
  412. The POSIX standard requires that by default the stream operations are
  413. atomic. I.e., issuing two stream operations for the same stream in two
  414. threads at the same time will cause the operations to be executed as if
  415. they were issued sequentially. The buffer operations performed while
  416. reading or writing are protected from other uses of the same stream. To
  417. do this each stream has an internal lock object which has to be
  418. (implicitly) acquired before any work can be done.
  419. But there are situations where this is not enough and there are also
  420. situations where this is not wanted. The implicit locking is not enough
  421. if the program requires more than one stream function call to happen
  422. atomically. One example would be if an output line a program wants to
  423. generate is created by several function calls. The functions by
  424. themselves would ensure only atomicity of their own operation, but not
  425. atomicity over all the function calls. For this it is necessary to
  426. perform the stream locking in the application code.
  427. @deftypefun void flockfile (FILE *@var{stream})
  428. @standards{POSIX, stdio.h}
  429. @safety{@prelim{}@mtsafe{}@assafe{}@acunsafe{@aculock{}}}
  430. @c There's no way to tell whether the lock was acquired before or after
  431. @c cancellation so as to unlock only when appropriate.
  432. The @code{flockfile} function acquires the internal locking object
  433. associated with the stream @var{stream}. This ensures that no other
  434. thread can explicitly through @code{flockfile}/@code{ftrylockfile} or
  435. implicitly through the call of a stream function lock the stream. The
  436. thread will block until the lock is acquired. An explicit call to
  437. @code{funlockfile} has to be used to release the lock.
  438. @end deftypefun
  439. @deftypefun int ftrylockfile (FILE *@var{stream})
  440. @standards{POSIX, stdio.h}
  441. @safety{@prelim{}@mtsafe{}@assafe{}@acunsafe{@aculock{}}}
  442. The @code{ftrylockfile} function tries to acquire the internal locking
  443. object associated with the stream @var{stream} just like
  444. @code{flockfile}. But unlike @code{flockfile} this function does not
  445. block if the lock is not available. @code{ftrylockfile} returns zero if
  446. the lock was successfully acquired. Otherwise the stream is locked by
  447. another thread.
  448. @end deftypefun
  449. @deftypefun void funlockfile (FILE *@var{stream})
  450. @standards{POSIX, stdio.h}
  451. @safety{@prelim{}@mtsafe{}@assafe{}@acunsafe{@aculock{}}}
  452. The @code{funlockfile} function releases the internal locking object of
  453. the stream @var{stream}. The stream must have been locked before by a
  454. call to @code{flockfile} or a successful call of @code{ftrylockfile}.
  455. The implicit locking performed by the stream operations do not count.
  456. The @code{funlockfile} function does not return an error status and the
  457. behavior of a call for a stream which is not locked by the current
  458. thread is undefined.
  459. @end deftypefun
  460. The following example shows how the functions above can be used to
  461. generate an output line atomically even in multi-threaded applications
  462. (yes, the same job could be done with one @code{fprintf} call but it is
  463. sometimes not possible):
  464. @smallexample
  465. FILE *fp;
  466. @{
  467. @dots{}
  468. flockfile (fp);
  469. fputs ("This is test number ", fp);
  470. fprintf (fp, "%d\n", test);
  471. funlockfile (fp)
  472. @}
  473. @end smallexample
  474. Without the explicit locking it would be possible for another thread to
  475. use the stream @var{fp} after the @code{fputs} call returns and before
  476. @code{fprintf} was called with the result that the number does not
  477. follow the word @samp{number}.
  478. From this description it might already be clear that the locking objects
  479. in streams are no simple mutexes. Since locking the same stream twice
  480. in the same thread is allowed the locking objects must be equivalent to
  481. recursive mutexes. These mutexes keep track of the owner and the number
  482. of times the lock is acquired. The same number of @code{funlockfile}
  483. calls by the same threads is necessary to unlock the stream completely.
  484. For instance:
  485. @smallexample
  486. void
  487. foo (FILE *fp)
  488. @{
  489. ftrylockfile (fp);
  490. fputs ("in foo\n", fp);
  491. /* @r{This is very wrong!!!} */
  492. funlockfile (fp);
  493. @}
  494. @end smallexample
  495. It is important here that the @code{funlockfile} function is only called
  496. if the @code{ftrylockfile} function succeeded in locking the stream. It
  497. is therefore always wrong to ignore the result of @code{ftrylockfile}.
  498. And it makes no sense since otherwise one would use @code{flockfile}.
  499. The result of code like that above is that either @code{funlockfile}
  500. tries to free a stream that hasn't been locked by the current thread or it
  501. frees the stream prematurely. The code should look like this:
  502. @smallexample
  503. void
  504. foo (FILE *fp)
  505. @{
  506. if (ftrylockfile (fp) == 0)
  507. @{
  508. fputs ("in foo\n", fp);
  509. funlockfile (fp);
  510. @}
  511. @}
  512. @end smallexample
  513. Now that we covered why it is necessary to have locking it is
  514. necessary to talk about situations when locking is unwanted and what can
  515. be done. The locking operations (explicit or implicit) don't come for
  516. free. Even if a lock is not taken the cost is not zero. The operations
  517. which have to be performed require memory operations that are safe in
  518. multi-processor environments. With the many local caches involved in
  519. such systems this is quite costly. So it is best to avoid the locking
  520. completely if it is not needed -- because the code in question is never
  521. used in a context where two or more threads may use a stream at a time.
  522. This can be determined most of the time for application code; for
  523. library code which can be used in many contexts one should default to be
  524. conservative and use locking.
  525. There are two basic mechanisms to avoid locking. The first is to use
  526. the @code{_unlocked} variants of the stream operations. The POSIX
  527. standard defines quite a few of those and @theglibc{} adds a few
  528. more. These variants of the functions behave just like the functions
  529. with the name without the suffix except that they do not lock the
  530. stream. Using these functions is very desirable since they are
  531. potentially much faster. This is not only because the locking
  532. operation itself is avoided. More importantly, functions like
  533. @code{putc} and @code{getc} are very simple and traditionally (before the
  534. introduction of threads) were implemented as macros which are very fast
  535. if the buffer is not empty. With the addition of locking requirements
  536. these functions are no longer implemented as macros since they would
  537. expand to too much code.
  538. But these macros are still available with the same functionality under the new
  539. names @code{putc_unlocked} and @code{getc_unlocked}. This possibly huge
  540. difference of speed also suggests the use of the @code{_unlocked}
  541. functions even if locking is required. The difference is that the
  542. locking then has to be performed in the program:
  543. @smallexample
  544. void
  545. foo (FILE *fp, char *buf)
  546. @{
  547. flockfile (fp);
  548. while (*buf != '/')
  549. putc_unlocked (*buf++, fp);
  550. funlockfile (fp);
  551. @}
  552. @end smallexample
  553. If in this example the @code{putc} function would be used and the
  554. explicit locking would be missing the @code{putc} function would have to
  555. acquire the lock in every call, potentially many times depending on when
  556. the loop terminates. Writing it the way illustrated above allows the
  557. @code{putc_unlocked} macro to be used which means no locking and direct
  558. manipulation of the buffer of the stream.
  559. A second way to avoid locking is by using a non-standard function which
  560. was introduced in Solaris and is available in @theglibc{} as well.
  561. @deftypefun int __fsetlocking (FILE *@var{stream}, int @var{type})
  562. @standards{GNU, stdio_ext.h}
  563. @safety{@prelim{}@mtsafe{@mtsrace{:stream}}@asunsafe{@asulock{}}@acsafe{}}
  564. @c Changing the implicit-locking status of a stream while it's in use by
  565. @c another thread may cause a lock to be implicitly acquired and not
  566. @c released, or vice-versa. This function should probably hold the lock
  567. @c while changing this setting, to make sure we don't change it while
  568. @c there are any concurrent uses. Meanwhile, callers should acquire the
  569. @c lock themselves to be safe, and even concurrent uses with external
  570. @c locking will be fine, as long as functions that require external
  571. @c locking are not called without holding locks.
  572. The @code{__fsetlocking} function can be used to select whether the
  573. stream operations will implicitly acquire the locking object of the
  574. stream @var{stream}. By default this is done but it can be disabled and
  575. reinstated using this function. There are three values defined for the
  576. @var{type} parameter.
  577. @vtable @code
  578. @item FSETLOCKING_INTERNAL
  579. The stream @code{stream} will from now on use the default internal
  580. locking. Every stream operation with exception of the @code{_unlocked}
  581. variants will implicitly lock the stream.
  582. @item FSETLOCKING_BYCALLER
  583. After the @code{__fsetlocking} function returns, the user is responsible
  584. for locking the stream. None of the stream operations will implicitly
  585. do this anymore until the state is set back to
  586. @code{FSETLOCKING_INTERNAL}.
  587. @item FSETLOCKING_QUERY
  588. @code{__fsetlocking} only queries the current locking state of the
  589. stream. The return value will be @code{FSETLOCKING_INTERNAL} or
  590. @code{FSETLOCKING_BYCALLER} depending on the state.
  591. @end vtable
  592. The return value of @code{__fsetlocking} is either
  593. @code{FSETLOCKING_INTERNAL} or @code{FSETLOCKING_BYCALLER} depending on
  594. the state of the stream before the call.
  595. This function and the values for the @var{type} parameter are declared
  596. in @file{stdio_ext.h}.
  597. @end deftypefun
  598. This function is especially useful when program code has to be used
  599. which is written without knowledge about the @code{_unlocked} functions
  600. (or if the programmer was too lazy to use them).
  601. @node Streams and I18N
  602. @section Streams in Internationalized Applications
  603. @w{ISO C90} introduced the new type @code{wchar_t} to allow handling
  604. larger character sets. What was missing was a possibility to output
  605. strings of @code{wchar_t} directly. One had to convert them into
  606. multibyte strings using @code{mbstowcs} (there was no @code{mbsrtowcs}
  607. yet) and then use the normal stream functions. While this is doable it
  608. is very cumbersome since performing the conversions is not trivial and
  609. greatly increases program complexity and size.
  610. The Unix standard early on (I think in XPG4.2) introduced two additional
  611. format specifiers for the @code{printf} and @code{scanf} families of
  612. functions. Printing and reading of single wide characters was made
  613. possible using the @code{%C} specifier and wide character strings can be
  614. handled with @code{%S}. These modifiers behave just like @code{%c} and
  615. @code{%s} only that they expect the corresponding argument to have the
  616. wide character type and that the wide character and string are
  617. transformed into/from multibyte strings before being used.
  618. This was a beginning but it is still not good enough. Not always is it
  619. desirable to use @code{printf} and @code{scanf}. The other, smaller and
  620. faster functions cannot handle wide characters. Second, it is not
  621. possible to have a format string for @code{printf} and @code{scanf}
  622. consisting of wide characters. The result is that format strings would
  623. have to be generated if they have to contain non-basic characters.
  624. @cindex C++ streams
  625. @cindex streams, C++
  626. In the @w{Amendment 1} to @w{ISO C90} a whole new set of functions was
  627. added to solve the problem. Most of the stream functions got a
  628. counterpart which take a wide character or wide character string instead
  629. of a character or string respectively. The new functions operate on the
  630. same streams (like @code{stdout}). This is different from the model of
  631. the C++ runtime library where separate streams for wide and normal I/O
  632. are used.
  633. @cindex orientation, stream
  634. @cindex stream orientation
  635. Being able to use the same stream for wide and normal operations comes
  636. with a restriction: a stream can be used either for wide operations or
  637. for normal operations. Once it is decided there is no way back. Only a
  638. call to @code{freopen} or @code{freopen64} can reset the
  639. @dfn{orientation}. The orientation can be decided in three ways:
  640. @itemize @bullet
  641. @item
  642. If any of the normal character functions are used (this includes the
  643. @code{fread} and @code{fwrite} functions) the stream is marked as not
  644. wide oriented.
  645. @item
  646. If any of the wide character functions are used the stream is marked as
  647. wide oriented.
  648. @item
  649. The @code{fwide} function can be used to set the orientation either way.
  650. @end itemize
  651. It is important to never mix the use of wide and not wide operations on
  652. a stream. There are no diagnostics issued. The application behavior
  653. will simply be strange or the application will simply crash. The
  654. @code{fwide} function can help avoid this.
  655. @deftypefun int fwide (FILE *@var{stream}, int @var{mode})
  656. @standards{ISO, wchar.h}
  657. @safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{}}}
  658. @c Querying is always safe, but changing the stream when it's in use
  659. @c upthread may be problematic. Like most lock-acquiring functions,
  660. @c this one may leak the lock if canceled.
  661. The @code{fwide} function can be used to set and query the state of the
  662. orientation of the stream @var{stream}. If the @var{mode} parameter has
  663. a positive value the streams get wide oriented, for negative values
  664. narrow oriented. It is not possible to overwrite previous orientations
  665. with @code{fwide}. I.e., if the stream @var{stream} was already
  666. oriented before the call nothing is done.
  667. If @var{mode} is zero the current orientation state is queried and
  668. nothing is changed.
  669. The @code{fwide} function returns a negative value, zero, or a positive
  670. value if the stream is narrow, not at all, or wide oriented
  671. respectively.
  672. This function was introduced in @w{Amendment 1} to @w{ISO C90} and is
  673. declared in @file{wchar.h}.
  674. @end deftypefun
  675. It is generally a good idea to orient a stream as early as possible.
  676. This can prevent surprise especially for the standard streams
  677. @code{stdin}, @code{stdout}, and @code{stderr}. If some library
  678. function in some situations uses one of these streams and this use
  679. orients the stream in a different way the rest of the application
  680. expects it one might end up with hard to reproduce errors. Remember
  681. that no errors are signal if the streams are used incorrectly. Leaving
  682. a stream unoriented after creation is normally only necessary for
  683. library functions which create streams which can be used in different
  684. contexts.
  685. When writing code which uses streams and which can be used in different
  686. contexts it is important to query the orientation of the stream before
  687. using it (unless the rules of the library interface demand a specific
  688. orientation). The following little, silly function illustrates this.
  689. @smallexample
  690. void
  691. print_f (FILE *fp)
  692. @{
  693. if (fwide (fp, 0) > 0)
  694. /* @r{Positive return value means wide orientation.} */
  695. fputwc (L'f', fp);
  696. else
  697. fputc ('f', fp);
  698. @}
  699. @end smallexample
  700. Note that in this case the function @code{print_f} decides about the
  701. orientation of the stream if it was unoriented before (will not happen
  702. if the advice above is followed).
  703. The encoding used for the @code{wchar_t} values is unspecified and the
  704. user must not make any assumptions about it. For I/O of @code{wchar_t}
  705. values this means that it is impossible to write these values directly
  706. to the stream. This is not what follows from the @w{ISO C} locale model
  707. either. What happens instead is that the bytes read from or written to
  708. the underlying media are first converted into the internal encoding
  709. chosen by the implementation for @code{wchar_t}. The external encoding
  710. is determined by the @code{LC_CTYPE} category of the current locale or
  711. by the @samp{ccs} part of the mode specification given to @code{fopen},
  712. @code{fopen64}, @code{freopen}, or @code{freopen64}. How and when the
  713. conversion happens is unspecified and it happens invisibly to the user.
  714. Since a stream is created in the unoriented state it has at that point
  715. no conversion associated with it. The conversion which will be used is
  716. determined by the @code{LC_CTYPE} category selected at the time the
  717. stream is oriented. If the locales are changed at the runtime this
  718. might produce surprising results unless one pays attention. This is
  719. just another good reason to orient the stream explicitly as soon as
  720. possible, perhaps with a call to @code{fwide}.
  721. @node Simple Output
  722. @section Simple Output by Characters or Lines
  723. @cindex writing to a stream, by characters
  724. This section describes functions for performing character- and
  725. line-oriented output.
  726. These narrow stream functions are declared in the header file
  727. @file{stdio.h} and the wide stream functions in @file{wchar.h}.
  728. @pindex stdio.h
  729. @pindex wchar.h
  730. @deftypefun int fputc (int @var{c}, FILE *@var{stream})
  731. @standards{ISO, stdio.h}
  732. @safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{} @aculock{}}}
  733. @c If the stream is in use when interrupted by a signal, the recursive
  734. @c lock won't help ensure the stream is consistent; indeed, if fputc
  735. @c gets a signal precisely before the post-incremented _IO_write_ptr
  736. @c value is stored, we may overwrite the interrupted write. Conversely,
  737. @c depending on compiler optimizations, the incremented _IO_write_ptr
  738. @c may be stored before the character is stored in the buffer,
  739. @c corrupting the stream if async cancel hits between the two stores.
  740. @c There may be other reasons for AS- and AC-unsafety in the overflow
  741. @c cases.
  742. The @code{fputc} function converts the character @var{c} to type
  743. @code{unsigned char}, and writes it to the stream @var{stream}.
  744. @code{EOF} is returned if a write error occurs; otherwise the
  745. character @var{c} is returned.
  746. @end deftypefun
  747. @deftypefun wint_t fputwc (wchar_t @var{wc}, FILE *@var{stream})
  748. @standards{ISO, wchar.h}
  749. @safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{} @aculock{}}}
  750. The @code{fputwc} function writes the wide character @var{wc} to the
  751. stream @var{stream}. @code{WEOF} is returned if a write error occurs;
  752. otherwise the character @var{wc} is returned.
  753. @end deftypefun
  754. @deftypefun int fputc_unlocked (int @var{c}, FILE *@var{stream})
  755. @standards{POSIX, stdio.h}
  756. @safety{@prelim{}@mtsafe{@mtsrace{:stream}}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{}}}
  757. @c The unlocked functions can't possibly satisfy the MT-Safety
  758. @c requirements on their own, because they require external locking for
  759. @c safety.
  760. The @code{fputc_unlocked} function is equivalent to the @code{fputc}
  761. function except that it does not implicitly lock the stream.
  762. @end deftypefun
  763. @deftypefun wint_t fputwc_unlocked (wchar_t @var{wc}, FILE *@var{stream})
  764. @standards{POSIX, wchar.h}
  765. @safety{@prelim{}@mtsafe{@mtsrace{:stream}}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{}}}
  766. The @code{fputwc_unlocked} function is equivalent to the @code{fputwc}
  767. function except that it does not implicitly lock the stream.
  768. This function is a GNU extension.
  769. @end deftypefun
  770. @deftypefun int putc (int @var{c}, FILE *@var{stream})
  771. @standards{ISO, stdio.h}
  772. @safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{} @aculock{}}}
  773. This is just like @code{fputc}, except that most systems implement it as
  774. a macro, making it faster. One consequence is that it may evaluate the
  775. @var{stream} argument more than once, which is an exception to the
  776. general rule for macros. @code{putc} is usually the best function to
  777. use for writing a single character.
  778. @end deftypefun
  779. @deftypefun wint_t putwc (wchar_t @var{wc}, FILE *@var{stream})
  780. @standards{ISO, wchar.h}
  781. @safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{} @aculock{}}}
  782. This is just like @code{fputwc}, except that it can be implement as
  783. a macro, making it faster. One consequence is that it may evaluate the
  784. @var{stream} argument more than once, which is an exception to the
  785. general rule for macros. @code{putwc} is usually the best function to
  786. use for writing a single wide character.
  787. @end deftypefun
  788. @deftypefun int putc_unlocked (int @var{c}, FILE *@var{stream})
  789. @standards{POSIX, stdio.h}
  790. @safety{@prelim{}@mtsafe{@mtsrace{:stream}}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{}}}
  791. The @code{putc_unlocked} function is equivalent to the @code{putc}
  792. function except that it does not implicitly lock the stream.
  793. @end deftypefun
  794. @deftypefun wint_t putwc_unlocked (wchar_t @var{wc}, FILE *@var{stream})
  795. @standards{GNU, wchar.h}
  796. @safety{@prelim{}@mtsafe{@mtsrace{:stream}}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{}}}
  797. The @code{putwc_unlocked} function is equivalent to the @code{putwc}
  798. function except that it does not implicitly lock the stream.
  799. This function is a GNU extension.
  800. @end deftypefun
  801. @deftypefun int putchar (int @var{c})
  802. @standards{ISO, stdio.h}
  803. @safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{} @aculock{}}}
  804. The @code{putchar} function is equivalent to @code{putc} with
  805. @code{stdout} as the value of the @var{stream} argument.
  806. @end deftypefun
  807. @deftypefun wint_t putwchar (wchar_t @var{wc})
  808. @standards{ISO, wchar.h}
  809. @safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{} @aculock{}}}
  810. The @code{putwchar} function is equivalent to @code{putwc} with
  811. @code{stdout} as the value of the @var{stream} argument.
  812. @end deftypefun
  813. @deftypefun int putchar_unlocked (int @var{c})
  814. @standards{POSIX, stdio.h}
  815. @safety{@prelim{}@mtunsafe{@mtasurace{:stdout}}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{}}}
  816. The @code{putchar_unlocked} function is equivalent to the @code{putchar}
  817. function except that it does not implicitly lock the stream.
  818. @end deftypefun
  819. @deftypefun wint_t putwchar_unlocked (wchar_t @var{wc})
  820. @standards{GNU, wchar.h}
  821. @safety{@prelim{}@mtunsafe{@mtasurace{:stdout}}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{}}}
  822. The @code{putwchar_unlocked} function is equivalent to the @code{putwchar}
  823. function except that it does not implicitly lock the stream.
  824. This function is a GNU extension.
  825. @end deftypefun
  826. @deftypefun int fputs (const char *@var{s}, FILE *@var{stream})
  827. @standards{ISO, stdio.h}
  828. @safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{} @aculock{}}}
  829. The function @code{fputs} writes the string @var{s} to the stream
  830. @var{stream}. The terminating null character is not written.
  831. This function does @emph{not} add a newline character, either.
  832. It outputs only the characters in the string.
  833. This function returns @code{EOF} if a write error occurs, and otherwise
  834. a non-negative value.
  835. For example:
  836. @smallexample
  837. fputs ("Are ", stdout);
  838. fputs ("you ", stdout);
  839. fputs ("hungry?\n", stdout);
  840. @end smallexample
  841. @noindent
  842. outputs the text @samp{Are you hungry?} followed by a newline.
  843. @end deftypefun
  844. @deftypefun int fputws (const wchar_t *@var{ws}, FILE *@var{stream})
  845. @standards{ISO, wchar.h}
  846. @safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{} @aculock{}}}
  847. The function @code{fputws} writes the wide character string @var{ws} to
  848. the stream @var{stream}. The terminating null character is not written.
  849. This function does @emph{not} add a newline character, either. It
  850. outputs only the characters in the string.
  851. This function returns @code{WEOF} if a write error occurs, and otherwise
  852. a non-negative value.
  853. @end deftypefun
  854. @deftypefun int fputs_unlocked (const char *@var{s}, FILE *@var{stream})
  855. @standards{GNU, stdio.h}
  856. @safety{@prelim{}@mtsafe{@mtsrace{:stream}}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{}}}
  857. The @code{fputs_unlocked} function is equivalent to the @code{fputs}
  858. function except that it does not implicitly lock the stream.
  859. This function is a GNU extension.
  860. @end deftypefun
  861. @deftypefun int fputws_unlocked (const wchar_t *@var{ws}, FILE *@var{stream})
  862. @standards{GNU, wchar.h}
  863. @safety{@prelim{}@mtsafe{@mtsrace{:stream}}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{}}}
  864. The @code{fputws_unlocked} function is equivalent to the @code{fputws}
  865. function except that it does not implicitly lock the stream.
  866. This function is a GNU extension.
  867. @end deftypefun
  868. @deftypefun int puts (const char *@var{s})
  869. @standards{ISO, stdio.h}
  870. @safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{} @acucorrupt{}}}
  871. The @code{puts} function writes the string @var{s} to the stream
  872. @code{stdout} followed by a newline. The terminating null character of
  873. the string is not written. (Note that @code{fputs} does @emph{not}
  874. write a newline as this function does.)
  875. @code{puts} is the most convenient function for printing simple
  876. messages. For example:
  877. @smallexample
  878. puts ("This is a message.");
  879. @end smallexample
  880. @noindent
  881. outputs the text @samp{This is a message.} followed by a newline.
  882. @end deftypefun
  883. @deftypefun int putw (int @var{w}, FILE *@var{stream})
  884. @standards{SVID, stdio.h}
  885. @safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{} @acucorrupt{}}}
  886. This function writes the word @var{w} (that is, an @code{int}) to
  887. @var{stream}. It is provided for compatibility with SVID, but we
  888. recommend you use @code{fwrite} instead (@pxref{Block Input/Output}).
  889. @end deftypefun
  890. @node Character Input
  891. @section Character Input
  892. @cindex reading from a stream, by characters
  893. This section describes functions for performing character-oriented
  894. input. These narrow stream functions are declared in the header file
  895. @file{stdio.h} and the wide character functions are declared in
  896. @file{wchar.h}.
  897. @pindex stdio.h
  898. @pindex wchar.h
  899. These functions return an @code{int} or @code{wint_t} value (for narrow
  900. and wide stream functions respectively) that is either a character of
  901. input, or the special value @code{EOF}/@code{WEOF} (usually -1). For
  902. the narrow stream functions it is important to store the result of these
  903. functions in a variable of type @code{int} instead of @code{char}, even
  904. when you plan to use it only as a character. Storing @code{EOF} in a
  905. @code{char} variable truncates its value to the size of a character, so
  906. that it is no longer distinguishable from the valid character
  907. @samp{(char) -1}. So always use an @code{int} for the result of
  908. @code{getc} and friends, and check for @code{EOF} after the call; once
  909. you've verified that the result is not @code{EOF}, you can be sure that
  910. it will fit in a @samp{char} variable without loss of information.
  911. @deftypefun int fgetc (FILE *@var{stream})
  912. @standards{ISO, stdio.h}
  913. @safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{} @acucorrupt{}}}
  914. @c Same caveats as fputc, but instead of losing a write in case of async
  915. @c signals, we may read the same character more than once, and the
  916. @c stream may be left in odd states due to cancellation in the underflow
  917. @c cases.
  918. This function reads the next character as an @code{unsigned char} from
  919. the stream @var{stream} and returns its value, converted to an
  920. @code{int}. If an end-of-file condition or read error occurs,
  921. @code{EOF} is returned instead.
  922. @end deftypefun
  923. @deftypefun wint_t fgetwc (FILE *@var{stream})
  924. @standards{ISO, wchar.h}
  925. @safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{} @acucorrupt{}}}
  926. This function reads the next wide character from the stream @var{stream}
  927. and returns its value. If an end-of-file condition or read error
  928. occurs, @code{WEOF} is returned instead.
  929. @end deftypefun
  930. @deftypefun int fgetc_unlocked (FILE *@var{stream})
  931. @standards{POSIX, stdio.h}
  932. @safety{@prelim{}@mtsafe{@mtsrace{:stream}}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{}}}
  933. The @code{fgetc_unlocked} function is equivalent to the @code{fgetc}
  934. function except that it does not implicitly lock the stream.
  935. @end deftypefun
  936. @deftypefun wint_t fgetwc_unlocked (FILE *@var{stream})
  937. @standards{GNU, wchar.h}
  938. @safety{@prelim{}@mtsafe{@mtsrace{:stream}}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{}}}
  939. The @code{fgetwc_unlocked} function is equivalent to the @code{fgetwc}
  940. function except that it does not implicitly lock the stream.
  941. This function is a GNU extension.
  942. @end deftypefun
  943. @deftypefun int getc (FILE *@var{stream})
  944. @standards{ISO, stdio.h}
  945. @safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{} @acucorrupt{}}}
  946. This is just like @code{fgetc}, except that it is permissible (and
  947. typical) for it to be implemented as a macro that evaluates the
  948. @var{stream} argument more than once. @code{getc} is often highly
  949. optimized, so it is usually the best function to use to read a single
  950. character.
  951. @end deftypefun
  952. @deftypefun wint_t getwc (FILE *@var{stream})
  953. @standards{ISO, wchar.h}
  954. @safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{} @acucorrupt{}}}
  955. This is just like @code{fgetwc}, except that it is permissible for it to
  956. be implemented as a macro that evaluates the @var{stream} argument more
  957. than once. @code{getwc} can be highly optimized, so it is usually the
  958. best function to use to read a single wide character.
  959. @end deftypefun
  960. @deftypefun int getc_unlocked (FILE *@var{stream})
  961. @standards{POSIX, stdio.h}
  962. @safety{@prelim{}@mtsafe{@mtsrace{:stream}}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{}}}
  963. The @code{getc_unlocked} function is equivalent to the @code{getc}
  964. function except that it does not implicitly lock the stream.
  965. @end deftypefun
  966. @deftypefun wint_t getwc_unlocked (FILE *@var{stream})
  967. @standards{GNU, wchar.h}
  968. @safety{@prelim{}@mtsafe{@mtsrace{:stream}}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{}}}
  969. The @code{getwc_unlocked} function is equivalent to the @code{getwc}
  970. function except that it does not implicitly lock the stream.
  971. This function is a GNU extension.
  972. @end deftypefun
  973. @deftypefun int getchar (void)
  974. @standards{ISO, stdio.h}
  975. @safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{} @acucorrupt{}}}
  976. The @code{getchar} function is equivalent to @code{getc} with @code{stdin}
  977. as the value of the @var{stream} argument.
  978. @end deftypefun
  979. @deftypefun wint_t getwchar (void)
  980. @standards{ISO, wchar.h}
  981. @safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{} @acucorrupt{}}}
  982. The @code{getwchar} function is equivalent to @code{getwc} with @code{stdin}
  983. as the value of the @var{stream} argument.
  984. @end deftypefun
  985. @deftypefun int getchar_unlocked (void)
  986. @standards{POSIX, stdio.h}
  987. @safety{@prelim{}@mtunsafe{@mtasurace{:stdin}}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{}}}
  988. The @code{getchar_unlocked} function is equivalent to the @code{getchar}
  989. function except that it does not implicitly lock the stream.
  990. @end deftypefun
  991. @deftypefun wint_t getwchar_unlocked (void)
  992. @standards{GNU, wchar.h}
  993. @safety{@prelim{}@mtunsafe{@mtasurace{:stdin}}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{}}}
  994. The @code{getwchar_unlocked} function is equivalent to the @code{getwchar}
  995. function except that it does not implicitly lock the stream.
  996. This function is a GNU extension.
  997. @end deftypefun
  998. Here is an example of a function that does input using @code{fgetc}. It
  999. would work just as well using @code{getc} instead, or using
  1000. @code{getchar ()} instead of @w{@code{fgetc (stdin)}}. The code would
  1001. also work the same for the wide character stream functions.
  1002. @smallexample
  1003. int
  1004. y_or_n_p (const char *question)
  1005. @{
  1006. fputs (question, stdout);
  1007. while (1)
  1008. @{
  1009. int c, answer;
  1010. /* @r{Write a space to separate answer from question.} */
  1011. fputc (' ', stdout);
  1012. /* @r{Read the first character of the line.}
  1013. @r{This should be the answer character, but might not be.} */
  1014. c = tolower (fgetc (stdin));
  1015. answer = c;
  1016. /* @r{Discard rest of input line.} */
  1017. while (c != '\n' && c != EOF)
  1018. c = fgetc (stdin);
  1019. /* @r{Obey the answer if it was valid.} */
  1020. if (answer == 'y')
  1021. return 1;
  1022. if (answer == 'n')
  1023. return 0;
  1024. /* @r{Answer was invalid: ask for valid answer.} */
  1025. fputs ("Please answer y or n:", stdout);
  1026. @}
  1027. @}
  1028. @end smallexample
  1029. @deftypefun int getw (FILE *@var{stream})
  1030. @standards{SVID, stdio.h}
  1031. @safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{} @acucorrupt{}}}
  1032. This function reads a word (that is, an @code{int}) from @var{stream}.
  1033. It's provided for compatibility with SVID. We recommend you use
  1034. @code{fread} instead (@pxref{Block Input/Output}). Unlike @code{getc},
  1035. any @code{int} value could be a valid result. @code{getw} returns
  1036. @code{EOF} when it encounters end-of-file or an error, but there is no
  1037. way to distinguish this from an input word with value -1.
  1038. @end deftypefun
  1039. @node Line Input
  1040. @section Line-Oriented Input
  1041. Since many programs interpret input on the basis of lines, it is
  1042. convenient to have functions to read a line of text from a stream.
  1043. Standard C has functions to do this, but they aren't very safe: null
  1044. characters and even (for @code{gets}) long lines can confuse them. So
  1045. @theglibc{} provides the nonstandard @code{getline} function that
  1046. makes it easy to read lines reliably.
  1047. Another GNU extension, @code{getdelim}, generalizes @code{getline}. It
  1048. reads a delimited record, defined as everything through the next
  1049. occurrence of a specified delimiter character.
  1050. All these functions are declared in @file{stdio.h}.
  1051. @deftypefun ssize_t getline (char **@var{lineptr}, size_t *@var{n}, FILE *@var{stream})
  1052. @standards{GNU, stdio.h}
  1053. @safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{} @ascuheap{}}@acunsafe{@aculock{} @acucorrupt{} @acsmem{}}}
  1054. @c Besides the usual possibility of getting an inconsistent stream in a
  1055. @c signal handler or leaving it inconsistent in case of cancellation,
  1056. @c the possibility of leaving a dangling pointer upon cancellation
  1057. @c between reallocing the buffer at *lineptr and updating the pointer
  1058. @c brings about another case of @acucorrupt.
  1059. This function reads an entire line from @var{stream}, storing the text
  1060. (including the newline and a terminating null character) in a buffer
  1061. and storing the buffer address in @code{*@var{lineptr}}.
  1062. Before calling @code{getline}, you should place in @code{*@var{lineptr}}
  1063. the address of a buffer @code{*@var{n}} bytes long, allocated with
  1064. @code{malloc}. If this buffer is long enough to hold the line,
  1065. @code{getline} stores the line in this buffer. Otherwise,
  1066. @code{getline} makes the buffer bigger using @code{realloc}, storing the
  1067. new buffer address back in @code{*@var{lineptr}} and the increased size
  1068. back in @code{*@var{n}}.
  1069. @xref{Unconstrained Allocation}.
  1070. If you set @code{*@var{lineptr}} to a null pointer, and @code{*@var{n}}
  1071. to zero, before the call, then @code{getline} allocates the initial
  1072. buffer for you by calling @code{malloc}. This buffer remains allocated
  1073. even if @code{getline} encounters errors and is unable to read any bytes.
  1074. In either case, when @code{getline} returns, @code{*@var{lineptr}} is
  1075. a @code{char *} which points to the text of the line.
  1076. When @code{getline} is successful, it returns the number of characters
  1077. read (including the newline, but not including the terminating null).
  1078. This value enables you to distinguish null characters that are part of
  1079. the line from the null character inserted as a terminator.
  1080. This function is a GNU extension, but it is the recommended way to read
  1081. lines from a stream. The alternative standard functions are unreliable.
  1082. If an error occurs or end of file is reached without any bytes read,
  1083. @code{getline} returns @code{-1}.
  1084. @end deftypefun
  1085. @deftypefun ssize_t getdelim (char **@var{lineptr}, size_t *@var{n}, int @var{delimiter}, FILE *@var{stream})
  1086. @standards{GNU, stdio.h}
  1087. @safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{} @ascuheap{}}@acunsafe{@aculock{} @acucorrupt{} @acsmem{}}}
  1088. @c See the getline @acucorrupt note.
  1089. This function is like @code{getline} except that the character which
  1090. tells it to stop reading is not necessarily newline. The argument
  1091. @var{delimiter} specifies the delimiter character; @code{getdelim} keeps
  1092. reading until it sees that character (or end of file).
  1093. The text is stored in @var{lineptr}, including the delimiter character
  1094. and a terminating null. Like @code{getline}, @code{getdelim} makes
  1095. @var{lineptr} bigger if it isn't big enough.
  1096. @code{getline} is in fact implemented in terms of @code{getdelim}, just
  1097. like this:
  1098. @smallexample
  1099. ssize_t
  1100. getline (char **lineptr, size_t *n, FILE *stream)
  1101. @{
  1102. return getdelim (lineptr, n, '\n', stream);
  1103. @}
  1104. @end smallexample
  1105. @end deftypefun
  1106. @deftypefun {char *} fgets (char *@var{s}, int @var{count}, FILE *@var{stream})
  1107. @standards{ISO, stdio.h}
  1108. @safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{} @acucorrupt{}}}
  1109. The @code{fgets} function reads characters from the stream @var{stream}
  1110. up to and including a newline character and stores them in the string
  1111. @var{s}, adding a null character to mark the end of the string. You
  1112. must supply @var{count} characters worth of space in @var{s}, but the
  1113. number of characters read is at most @var{count} @minus{} 1. The extra
  1114. character space is used to hold the null character at the end of the
  1115. string.
  1116. If the system is already at end of file when you call @code{fgets}, then
  1117. the contents of the array @var{s} are unchanged and a null pointer is
  1118. returned. A null pointer is also returned if a read error occurs.
  1119. Otherwise, the return value is the pointer @var{s}.
  1120. @strong{Warning:} If the input data has a null character, you can't tell.
  1121. So don't use @code{fgets} unless you know the data cannot contain a null.
  1122. Don't use it to read files edited by the user because, if the user inserts
  1123. a null character, you should either handle it properly or print a clear
  1124. error message. We recommend using @code{getline} instead of @code{fgets}.
  1125. @end deftypefun
  1126. @deftypefun {wchar_t *} fgetws (wchar_t *@var{ws}, int @var{count}, FILE *@var{stream})
  1127. @standards{ISO, wchar.h}
  1128. @safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{} @acucorrupt{}}}
  1129. The @code{fgetws} function reads wide characters from the stream
  1130. @var{stream} up to and including a newline character and stores them in
  1131. the string @var{ws}, adding a null wide character to mark the end of the
  1132. string. You must supply @var{count} wide characters worth of space in
  1133. @var{ws}, but the number of characters read is at most @var{count}
  1134. @minus{} 1. The extra character space is used to hold the null wide
  1135. character at the end of the string.
  1136. If the system is already at end of file when you call @code{fgetws}, then
  1137. the contents of the array @var{ws} are unchanged and a null pointer is
  1138. returned. A null pointer is also returned if a read error occurs.
  1139. Otherwise, the return value is the pointer @var{ws}.
  1140. @strong{Warning:} If the input data has a null wide character (which are
  1141. null bytes in the input stream), you can't tell. So don't use
  1142. @code{fgetws} unless you know the data cannot contain a null. Don't use
  1143. it to read files edited by the user because, if the user inserts a null
  1144. character, you should either handle it properly or print a clear error
  1145. message.
  1146. @comment XXX We need getwline!!!
  1147. @end deftypefun
  1148. @deftypefun {char *} fgets_unlocked (char *@var{s}, int @var{count}, FILE *@var{stream})
  1149. @standards{GNU, stdio.h}
  1150. @safety{@prelim{}@mtsafe{@mtsrace{:stream}}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{}}}
  1151. The @code{fgets_unlocked} function is equivalent to the @code{fgets}
  1152. function except that it does not implicitly lock the stream.
  1153. This function is a GNU extension.
  1154. @end deftypefun
  1155. @deftypefun {wchar_t *} fgetws_unlocked (wchar_t *@var{ws}, int @var{count}, FILE *@var{stream})
  1156. @standards{GNU, wchar.h}
  1157. @safety{@prelim{}@mtsafe{@mtsrace{:stream}}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{}}}
  1158. The @code{fgetws_unlocked} function is equivalent to the @code{fgetws}
  1159. function except that it does not implicitly lock the stream.
  1160. This function is a GNU extension.
  1161. @end deftypefun
  1162. @deftypefn {Deprecated function} {char *} gets (char *@var{s})
  1163. @standards{ISO, stdio.h}
  1164. @safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{} @acucorrupt{}}}
  1165. The function @code{gets} reads characters from the stream @code{stdin}
  1166. up to the next newline character, and stores them in the string @var{s}.
  1167. The newline character is discarded (note that this differs from the
  1168. behavior of @code{fgets}, which copies the newline character into the
  1169. string). If @code{gets} encounters a read error or end-of-file, it
  1170. returns a null pointer; otherwise it returns @var{s}.
  1171. @strong{Warning:} The @code{gets} function is @strong{very dangerous}
  1172. because it provides no protection against overflowing the string
  1173. @var{s}. @Theglibc{} includes it for compatibility only. You
  1174. should @strong{always} use @code{fgets} or @code{getline} instead. To
  1175. remind you of this, the linker (if using GNU @code{ld}) will issue a
  1176. warning whenever you use @code{gets}.
  1177. @end deftypefn
  1178. @node Unreading
  1179. @section Unreading
  1180. @cindex peeking at input
  1181. @cindex unreading characters
  1182. @cindex pushing input back
  1183. In parser programs it is often useful to examine the next character in
  1184. the input stream without removing it from the stream. This is called
  1185. ``peeking ahead'' at the input because your program gets a glimpse of
  1186. the input it will read next.
  1187. Using stream I/O, you can peek ahead at input by first reading it and
  1188. then @dfn{unreading} it (also called @dfn{pushing it back} on the stream).
  1189. Unreading a character makes it available to be input again from the stream,
  1190. by the next call to @code{fgetc} or other input function on that stream.
  1191. @menu
  1192. * Unreading Idea:: An explanation of unreading with pictures.
  1193. * How Unread:: How to call @code{ungetc} to do unreading.
  1194. @end menu
  1195. @node Unreading Idea
  1196. @subsection What Unreading Means
  1197. Here is a pictorial explanation of unreading. Suppose you have a
  1198. stream reading a file that contains just six characters, the letters
  1199. @samp{foobar}. Suppose you have read three characters so far. The
  1200. situation looks like this:
  1201. @smallexample
  1202. f o o b a r
  1203. ^
  1204. @end smallexample
  1205. @noindent
  1206. so the next input character will be @samp{b}.
  1207. @c @group Invalid outside @example
  1208. If instead of reading @samp{b} you unread the letter @samp{o}, you get a
  1209. situation like this:
  1210. @smallexample
  1211. f o o b a r
  1212. |
  1213. o--
  1214. ^
  1215. @end smallexample
  1216. @noindent
  1217. so that the next input characters will be @samp{o} and @samp{b}.
  1218. @c @end group
  1219. @c @group
  1220. If you unread @samp{9} instead of @samp{o}, you get this situation:
  1221. @smallexample
  1222. f o o b a r
  1223. |
  1224. 9--
  1225. ^
  1226. @end smallexample
  1227. @noindent
  1228. so that the next input characters will be @samp{9} and @samp{b}.
  1229. @c @end group
  1230. @node How Unread
  1231. @subsection Using @code{ungetc} To Do Unreading
  1232. The function to unread a character is called @code{ungetc}, because it
  1233. reverses the action of @code{getc}.
  1234. @deftypefun int ungetc (int @var{c}, FILE *@var{stream})
  1235. @standards{ISO, stdio.h}
  1236. @safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{} @acucorrupt{}}}
  1237. The @code{ungetc} function pushes back the character @var{c} onto the
  1238. input stream @var{stream}. So the next input from @var{stream} will
  1239. read @var{c} before anything else.
  1240. If @var{c} is @code{EOF}, @code{ungetc} does nothing and just returns
  1241. @code{EOF}. This lets you call @code{ungetc} with the return value of
  1242. @code{getc} without needing to check for an error from @code{getc}.
  1243. The character that you push back doesn't have to be the same as the last
  1244. character that was actually read from the stream. In fact, it isn't
  1245. necessary to actually read any characters from the stream before
  1246. unreading them with @code{ungetc}! But that is a strange way to write a
  1247. program; usually @code{ungetc} is used only to unread a character that
  1248. was just read from the same stream. @Theglibc{} supports this
  1249. even on files opened in binary mode, but other systems might not.
  1250. @Theglibc{} only supports one character of pushback---in other
  1251. words, it does not work to call @code{ungetc} twice without doing input
  1252. in between. Other systems might let you push back multiple characters;
  1253. then reading from the stream retrieves the characters in the reverse
  1254. order that they were pushed.
  1255. Pushing back characters doesn't alter the file; only the internal
  1256. buffering for the stream is affected. If a file positioning function
  1257. (such as @code{fseek}, @code{fseeko} or @code{rewind}; @pxref{File
  1258. Positioning}) is called, any pending pushed-back characters are
  1259. discarded.
  1260. Unreading a character on a stream that is at end of file clears the
  1261. end-of-file indicator for the stream, because it makes the character of
  1262. input available. After you read that character, trying to read again
  1263. will encounter end of file.
  1264. @end deftypefun
  1265. @deftypefun wint_t ungetwc (wint_t @var{wc}, FILE *@var{stream})
  1266. @standards{ISO, wchar.h}
  1267. @safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{} @acucorrupt{}}}
  1268. The @code{ungetwc} function behaves just like @code{ungetc} just that it
  1269. pushes back a wide character.
  1270. @end deftypefun
  1271. Here is an example showing the use of @code{getc} and @code{ungetc} to
  1272. skip over whitespace characters. When this function reaches a
  1273. non-whitespace character, it unreads that character to be seen again on
  1274. the next read operation on the stream.
  1275. @smallexample
  1276. #include <stdio.h>
  1277. #include <ctype.h>
  1278. void
  1279. skip_whitespace (FILE *stream)
  1280. @{
  1281. int c;
  1282. do
  1283. /* @r{No need to check for @code{EOF} because it is not}
  1284. @r{@code{isspace}, and @code{ungetc} ignores @code{EOF}.} */
  1285. c = getc (stream);
  1286. while (isspace (c));
  1287. ungetc (c, stream);
  1288. @}
  1289. @end smallexample
  1290. @node Block Input/Output
  1291. @section Block Input/Output
  1292. This section describes how to do input and output operations on blocks
  1293. of data. You can use these functions to read and write binary data, as
  1294. well as to read and write text in fixed-size blocks instead of by
  1295. characters or lines.
  1296. @cindex binary I/O to a stream
  1297. @cindex block I/O to a stream
  1298. @cindex reading from a stream, by blocks
  1299. @cindex writing to a stream, by blocks
  1300. Binary files are typically used to read and write blocks of data in the
  1301. same format as is used to represent the data in a running program. In
  1302. other words, arbitrary blocks of memory---not just character or string
  1303. objects---can be written to a binary file, and meaningfully read in
  1304. again by the same program.
  1305. Storing data in binary form is often considerably more efficient than
  1306. using the formatted I/O functions. Also, for floating-point numbers,
  1307. the binary form avoids possible loss of precision in the conversion
  1308. process. On the other hand, binary files can't be examined or modified
  1309. easily using many standard file utilities (such as text editors), and
  1310. are not portable between different implementations of the language, or
  1311. different kinds of computers.
  1312. These functions are declared in @file{stdio.h}.
  1313. @pindex stdio.h
  1314. @deftypefun size_t fread (void *@var{data}, size_t @var{size}, size_t @var{count}, FILE *@var{stream})
  1315. @standards{ISO, stdio.h}
  1316. @safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{} @acucorrupt{}}}
  1317. This function reads up to @var{count} objects of size @var{size} into
  1318. the array @var{data}, from the stream @var{stream}. It returns the
  1319. number of objects actually read, which might be less than @var{count} if
  1320. a read error occurs or the end of the file is reached. This function
  1321. returns a value of zero (and doesn't read anything) if either @var{size}
  1322. or @var{count} is zero.
  1323. If @code{fread} encounters end of file in the middle of an object, it
  1324. returns the number of complete objects read, and discards the partial
  1325. object. Therefore, the stream remains at the actual end of the file.
  1326. @end deftypefun
  1327. @deftypefun size_t fread_unlocked (void *@var{data}, size_t @var{size}, size_t @var{count}, FILE *@var{stream})
  1328. @standards{GNU, stdio.h}
  1329. @safety{@prelim{}@mtsafe{@mtsrace{:stream}}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{}}}
  1330. The @code{fread_unlocked} function is equivalent to the @code{fread}
  1331. function except that it does not implicitly lock the stream.
  1332. This function is a GNU extension.
  1333. @end deftypefun
  1334. @deftypefun size_t fwrite (const void *@var{data}, size_t @var{size}, size_t @var{count}, FILE *@var{stream})
  1335. @standards{ISO, stdio.h}
  1336. @safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{} @acucorrupt{}}}
  1337. This function writes up to @var{count} objects of size @var{size} from
  1338. the array @var{data}, to the stream @var{stream}. The return value is
  1339. normally @var{count}, if the call succeeds. Any other value indicates
  1340. some sort of error, such as running out of space.
  1341. @end deftypefun
  1342. @deftypefun size_t fwrite_unlocked (const void *@var{data}, size_t @var{size}, size_t @var{count}, FILE *@var{stream})
  1343. @standards{GNU, stdio.h}
  1344. @safety{@prelim{}@mtsafe{@mtsrace{:stream}}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{}}}
  1345. The @code{fwrite_unlocked} function is equivalent to the @code{fwrite}
  1346. function except that it does not implicitly lock the stream.
  1347. This function is a GNU extension.
  1348. @end deftypefun
  1349. @node Formatted Output
  1350. @section Formatted Output
  1351. @cindex format string, for @code{printf}
  1352. @cindex template, for @code{printf}
  1353. @cindex formatted output to a stream
  1354. @cindex writing to a stream, formatted
  1355. The functions described in this section (@code{printf} and related
  1356. functions) provide a convenient way to perform formatted output. You
  1357. call @code{printf} with a @dfn{format string} or @dfn{template string}
  1358. that specifies how to format the values of the remaining arguments.
  1359. Unless your program is a filter that specifically performs line- or
  1360. character-oriented processing, using @code{printf} or one of the other
  1361. related functions described in this section is usually the easiest and
  1362. most concise way to perform output. These functions are especially
  1363. useful for printing error messages, tables of data, and the like.
  1364. @menu
  1365. * Formatted Output Basics:: Some examples to get you started.
  1366. * Output Conversion Syntax:: General syntax of conversion
  1367. specifications.
  1368. * Table of Output Conversions:: Summary of output conversions and
  1369. what they do.
  1370. * Integer Conversions:: Details about formatting of integers.
  1371. * Floating-Point Conversions:: Details about formatting of
  1372. floating-point numbers.
  1373. * Other Output Conversions:: Details about formatting of strings,
  1374. characters, pointers, and the like.
  1375. * Formatted Output Functions:: Descriptions of the actual functions.
  1376. * Dynamic Output:: Functions that allocate memory for the output.
  1377. * Variable Arguments Output:: @code{vprintf} and friends.
  1378. * Parsing a Template String:: What kinds of args does a given template
  1379. call for?
  1380. * Example of Parsing:: Sample program using @code{parse_printf_format}.
  1381. @end menu
  1382. @node Formatted Output Basics
  1383. @subsection Formatted Output Basics
  1384. The @code{printf} function can be used to print any number of arguments.
  1385. The template string argument you supply in a call provides
  1386. information not only about the number of additional arguments, but also
  1387. about their types and what style should be used for printing them.
  1388. Ordinary characters in the template string are simply written to the
  1389. output stream as-is, while @dfn{conversion specifications} introduced by
  1390. a @samp{%} character in the template cause subsequent arguments to be
  1391. formatted and written to the output stream. For example,
  1392. @cindex conversion specifications (@code{printf})
  1393. @smallexample
  1394. int pct = 37;
  1395. char filename[] = "foo.txt";
  1396. printf ("Processing of `%s' is %d%% finished.\nPlease be patient.\n",
  1397. filename, pct);
  1398. @end smallexample
  1399. @noindent
  1400. produces output like
  1401. @smallexample
  1402. Processing of `foo.txt' is 37% finished.
  1403. Please be patient.
  1404. @end smallexample
  1405. This example shows the use of the @samp{%d} conversion to specify that
  1406. an @code{int} argument should be printed in decimal notation, the
  1407. @samp{%s} conversion to specify printing of a string argument, and
  1408. the @samp{%%} conversion to print a literal @samp{%} character.
  1409. There are also conversions for printing an integer argument as an
  1410. unsigned value in octal, decimal, or hexadecimal radix (@samp{%o},
  1411. @samp{%u}, or @samp{%x}, respectively); or as a character value
  1412. (@samp{%c}).
  1413. Floating-point numbers can be printed in normal, fixed-point notation
  1414. using the @samp{%f} conversion or in exponential notation using the
  1415. @samp{%e} conversion. The @samp{%g} conversion uses either @samp{%e}
  1416. or @samp{%f} format, depending on what is more appropriate for the
  1417. magnitude of the particular number.
  1418. You can control formatting more precisely by writing @dfn{modifiers}
  1419. between the @samp{%} and the character that indicates which conversion
  1420. to apply. These slightly alter the ordinary behavior of the conversion.
  1421. For example, most conversion specifications permit you to specify a
  1422. minimum field width and a flag indicating whether you want the result
  1423. left- or right-justified within the field.
  1424. The specific flags and modifiers that are permitted and their
  1425. interpretation vary depending on the particular conversion. They're all
  1426. described in more detail in the following sections. Don't worry if this
  1427. all seems excessively complicated at first; you can almost always get
  1428. reasonable free-format output without using any of the modifiers at all.
  1429. The modifiers are mostly used to make the output look ``prettier'' in
  1430. tables.
  1431. @node Output Conversion Syntax
  1432. @subsection Output Conversion Syntax
  1433. This section provides details about the precise syntax of conversion
  1434. specifications that can appear in a @code{printf} template
  1435. string.
  1436. Characters in the template string that are not part of a conversion
  1437. specification are printed as-is to the output stream. Multibyte
  1438. character sequences (@pxref{Character Set Handling}) are permitted in a
  1439. template string.
  1440. The conversion specifications in a @code{printf} template string have
  1441. the general form:
  1442. @smallexample
  1443. % @r{[} @var{param-no} @r{$]} @var{flags} @var{width} @r{[} . @var{precision} @r{]} @var{type} @var{conversion}
  1444. @end smallexample
  1445. @noindent
  1446. or
  1447. @smallexample
  1448. % @r{[} @var{param-no} @r{$]} @var{flags} @var{width} . @r{*} @r{[} @var{param-no} @r{$]} @var{type} @var{conversion}
  1449. @end smallexample
  1450. For example, in the conversion specifier @samp{%-10.8ld}, the @samp{-}
  1451. is a flag, @samp{10} specifies the field width, the precision is
  1452. @samp{8}, the letter @samp{l} is a type modifier, and @samp{d} specifies
  1453. the conversion style. (This particular type specifier says to
  1454. print a @code{long int} argument in decimal notation, with a minimum of
  1455. 8 digits left-justified in a field at least 10 characters wide.)
  1456. In more detail, output conversion specifications consist of an
  1457. initial @samp{%} character followed in sequence by:
  1458. @itemize @bullet
  1459. @item
  1460. An optional specification of the parameter used for this format.
  1461. Normally the parameters to the @code{printf} function are assigned to the
  1462. formats in the order of appearance in the format string. But in some
  1463. situations (such as message translation) this is not desirable and this
  1464. extension allows an explicit parameter to be specified.
  1465. The @var{param-no} parts of the format must be integers in the range of
  1466. 1 to the maximum number of arguments present to the function call. Some
  1467. implementations limit this number to a certain upper bound. The exact
  1468. limit can be retrieved by the following constant.
  1469. @defvr Macro NL_ARGMAX
  1470. The value of @code{NL_ARGMAX} is the maximum value allowed for the
  1471. specification of a positional parameter in a @code{printf} call. The
  1472. actual value in effect at runtime can be retrieved by using
  1473. @code{sysconf} using the @code{_SC_NL_ARGMAX} parameter @pxref{Sysconf
  1474. Definition}.
  1475. Some systems have a quite low limit such as @math{9} for @w{System V}
  1476. systems. @Theglibc{} has no real limit.
  1477. @end defvr
  1478. If any of the formats has a specification for the parameter position all
  1479. of them in the format string shall have one. Otherwise the behavior is
  1480. undefined.
  1481. @item
  1482. Zero or more @dfn{flag characters} that modify the normal behavior of
  1483. the conversion specification.
  1484. @cindex flag character (@code{printf})
  1485. @item
  1486. An optional decimal integer specifying the @dfn{minimum field width}.
  1487. If the normal conversion produces fewer characters than this, the field
  1488. is padded with spaces to the specified width. This is a @emph{minimum}
  1489. value; if the normal conversion produces more characters than this, the
  1490. field is @emph{not} truncated. Normally, the output is right-justified
  1491. within the field.
  1492. @cindex minimum field width (@code{printf})
  1493. You can also specify a field width of @samp{*}. This means that the
  1494. next argument in the argument list (before the actual value to be
  1495. printed) is used as the field width. The value must be an @code{int}.
  1496. If the value is negative, this means to set the @samp{-} flag (see
  1497. below) and to use the absolute value as the field width.
  1498. @item
  1499. An optional @dfn{precision} to specify the number of digits to be
  1500. written for the numeric conversions. If the precision is specified, it
  1501. consists of a period (@samp{.}) followed optionally by a decimal integer
  1502. (which defaults to zero if omitted).
  1503. @cindex precision (@code{printf})
  1504. You can also specify a precision of @samp{*}. This means that the next
  1505. argument in the argument list (before the actual value to be printed) is
  1506. used as the precision. The value must be an @code{int}, and is ignored
  1507. if it is negative. If you specify @samp{*} for both the field width and
  1508. precision, the field width argument precedes the precision argument.
  1509. Other C library versions may not recognize this syntax.
  1510. @item
  1511. An optional @dfn{type modifier character}, which is used to specify the
  1512. data type of the corresponding argument if it differs from the default
  1513. type. (For example, the integer conversions assume a type of @code{int},
  1514. but you can specify @samp{h}, @samp{l}, or @samp{L} for other integer
  1515. types.)
  1516. @cindex type modifier character (@code{printf})
  1517. @item
  1518. A character that specifies the conversion to be applied.
  1519. @end itemize
  1520. The exact options that are permitted and how they are interpreted vary
  1521. between the different conversion specifiers. See the descriptions of the
  1522. individual conversions for information about the particular options that
  1523. they use.
  1524. With the @samp{-Wformat} option, the GNU C compiler checks calls to
  1525. @code{printf} and related functions. It examines the format string and
  1526. verifies that the correct number and types of arguments are supplied.
  1527. There is also a GNU C syntax to tell the compiler that a function you
  1528. write uses a @code{printf}-style format string.
  1529. @xref{Function Attributes, , Declaring Attributes of Functions,
  1530. gcc, Using GNU CC}, for more information.
  1531. @node Table of Output Conversions
  1532. @subsection Table of Output Conversions
  1533. @cindex output conversions, for @code{printf}
  1534. Here is a table summarizing what all the different conversions do:
  1535. @table @asis
  1536. @item @samp{%d}, @samp{%i}
  1537. Print an integer as a signed decimal number. @xref{Integer
  1538. Conversions}, for details. @samp{%d} and @samp{%i} are synonymous for
  1539. output, but are different when used with @code{scanf} for input
  1540. (@pxref{Table of Input Conversions}).
  1541. @item @samp{%o}
  1542. Print an integer as an unsigned octal number. @xref{Integer
  1543. Conversions}, for details.
  1544. @item @samp{%u}
  1545. Print an integer as an unsigned decimal number. @xref{Integer
  1546. Conversions}, for details.
  1547. @item @samp{%x}, @samp{%X}
  1548. Print an integer as an unsigned hexadecimal number. @samp{%x} uses
  1549. lower-case letters and @samp{%X} uses upper-case. @xref{Integer
  1550. Conversions}, for details.
  1551. @item @samp{%f}
  1552. Print a floating-point number in normal (fixed-point) notation.
  1553. @xref{Floating-Point Conversions}, for details.
  1554. @item @samp{%e}, @samp{%E}
  1555. Print a floating-point number in exponential notation. @samp{%e} uses
  1556. lower-case letters and @samp{%E} uses upper-case. @xref{Floating-Point
  1557. Conversions}, for details.
  1558. @item @samp{%g}, @samp{%G}
  1559. Print a floating-point number in either normal or exponential notation,
  1560. whichever is more appropriate for its magnitude. @samp{%g} uses
  1561. lower-case letters and @samp{%G} uses upper-case. @xref{Floating-Point
  1562. Conversions}, for details.
  1563. @item @samp{%a}, @samp{%A}
  1564. Print a floating-point number in a hexadecimal fractional notation with
  1565. the exponent to base 2 represented in decimal digits. @samp{%a} uses
  1566. lower-case letters and @samp{%A} uses upper-case. @xref{Floating-Point
  1567. Conversions}, for details.
  1568. @item @samp{%c}
  1569. Print a single character. @xref{Other Output Conversions}.
  1570. @item @samp{%C}
  1571. This is an alias for @samp{%lc} which is supported for compatibility
  1572. with the Unix standard.
  1573. @item @samp{%s}
  1574. Print a string. @xref{Other Output Conversions}.
  1575. @item @samp{%S}
  1576. This is an alias for @samp{%ls} which is supported for compatibility
  1577. with the Unix standard.
  1578. @item @samp{%p}
  1579. Print the value of a pointer. @xref{Other Output Conversions}.
  1580. @item @samp{%n}
  1581. Get the number of characters printed so far. @xref{Other Output Conversions}.
  1582. Note that this conversion specification never produces any output.
  1583. @item @samp{%m}
  1584. Print the string corresponding to the value of @code{errno}.
  1585. (This is a GNU extension.)
  1586. @xref{Other Output Conversions}.
  1587. @item @samp{%%}
  1588. Print a literal @samp{%} character. @xref{Other Output Conversions}.
  1589. @end table
  1590. If the syntax of a conversion specification is invalid, unpredictable
  1591. things will happen, so don't do this. If there aren't enough function
  1592. arguments provided to supply values for all the conversion
  1593. specifications in the template string, or if the arguments are not of
  1594. the correct types, the results are unpredictable. If you supply more
  1595. arguments than conversion specifications, the extra argument values are
  1596. simply ignored; this is sometimes useful.
  1597. @node Integer Conversions
  1598. @subsection Integer Conversions
  1599. This section describes the options for the @samp{%d}, @samp{%i},
  1600. @samp{%o}, @samp{%u}, @samp{%x}, and @samp{%X} conversion
  1601. specifications. These conversions print integers in various formats.
  1602. The @samp{%d} and @samp{%i} conversion specifications both print an
  1603. @code{int} argument as a signed decimal number; while @samp{%o},
  1604. @samp{%u}, and @samp{%x} print the argument as an unsigned octal,
  1605. decimal, or hexadecimal number (respectively). The @samp{%X} conversion
  1606. specification is just like @samp{%x} except that it uses the characters
  1607. @samp{ABCDEF} as digits instead of @samp{abcdef}.
  1608. The following flags are meaningful:
  1609. @table @asis
  1610. @item @samp{-}
  1611. Left-justify the result in the field (instead of the normal
  1612. right-justification).
  1613. @item @samp{+}
  1614. For the signed @samp{%d} and @samp{%i} conversions, print a
  1615. plus sign if the value is positive.
  1616. @item @samp{ }
  1617. For the signed @samp{%d} and @samp{%i} conversions, if the result
  1618. doesn't start with a plus or minus sign, prefix it with a space
  1619. character instead. Since the @samp{+} flag ensures that the result
  1620. includes a sign, this flag is ignored if you supply both of them.
  1621. @item @samp{#}
  1622. For the @samp{%o} conversion, this forces the leading digit to be
  1623. @samp{0}, as if by increasing the precision. For @samp{%x} or
  1624. @samp{%X}, this prefixes a leading @samp{0x} or @samp{0X} (respectively)
  1625. to the result. This doesn't do anything useful for the @samp{%d},
  1626. @samp{%i}, or @samp{%u} conversions. Using this flag produces output
  1627. which can be parsed by the @code{strtoul} function (@pxref{Parsing of
  1628. Integers}) and @code{scanf} with the @samp{%i} conversion
  1629. (@pxref{Numeric Input Conversions}).
  1630. @item @samp{'}
  1631. Separate the digits into groups as specified by the locale specified for
  1632. the @code{LC_NUMERIC} category; @pxref{General Numeric}. This flag is a
  1633. GNU extension.
  1634. @item @samp{0}
  1635. Pad the field with zeros instead of spaces. The zeros are placed after
  1636. any indication of sign or base. This flag is ignored if the @samp{-}
  1637. flag is also specified, or if a precision is specified.
  1638. @end table
  1639. If a precision is supplied, it specifies the minimum number of digits to
  1640. appear; leading zeros are produced if necessary. If you don't specify a
  1641. precision, the number is printed with as many digits as it needs. If
  1642. you convert a value of zero with an explicit precision of zero, then no
  1643. characters at all are produced.
  1644. Without a type modifier, the corresponding argument is treated as an
  1645. @code{int} (for the signed conversions @samp{%i} and @samp{%d}) or
  1646. @code{unsigned int} (for the unsigned conversions @samp{%o}, @samp{%u},
  1647. @samp{%x}, and @samp{%X}). Recall that since @code{printf} and friends
  1648. are variadic, any @code{char} and @code{short} arguments are
  1649. automatically converted to @code{int} by the default argument
  1650. promotions. For arguments of other integer types, you can use these
  1651. modifiers:
  1652. @table @samp
  1653. @item hh
  1654. Specifies that the argument is a @code{signed char} or @code{unsigned
  1655. char}, as appropriate. A @code{char} argument is converted to an
  1656. @code{int} or @code{unsigned int} by the default argument promotions
  1657. anyway, but the @samp{hh} modifier says to convert it back to a
  1658. @code{char} again.
  1659. This modifier was introduced in @w{ISO C99}.
  1660. @item h
  1661. Specifies that the argument is a @code{short int} or @code{unsigned
  1662. short int}, as appropriate. A @code{short} argument is converted to an
  1663. @code{int} or @code{unsigned int} by the default argument promotions
  1664. anyway, but the @samp{h} modifier says to convert it back to a
  1665. @code{short} again.
  1666. @item j
  1667. Specifies that the argument is a @code{intmax_t} or @code{uintmax_t}, as
  1668. appropriate.
  1669. This modifier was introduced in @w{ISO C99}.
  1670. @item l
  1671. Specifies that the argument is a @code{long int} or @code{unsigned long
  1672. int}, as appropriate. Two @samp{l} characters are like the @samp{L}
  1673. modifier, below.
  1674. If used with @samp{%c} or @samp{%s} the corresponding parameter is
  1675. considered as a wide character or wide character string respectively.
  1676. This use of @samp{l} was introduced in @w{Amendment 1} to @w{ISO C90}.
  1677. @item L
  1678. @itemx ll
  1679. @itemx q
  1680. Specifies that the argument is a @code{long long int}. (This type is
  1681. an extension supported by the GNU C compiler. On systems that don't
  1682. support extra-long integers, this is the same as @code{long int}.)
  1683. The @samp{q} modifier is another name for the same thing, which comes
  1684. from 4.4 BSD; a @w{@code{long long int}} is sometimes called a ``quad''
  1685. @code{int}.
  1686. @item t
  1687. Specifies that the argument is a @code{ptrdiff_t}.
  1688. This modifier was introduced in @w{ISO C99}.
  1689. @item z
  1690. @itemx Z
  1691. Specifies that the argument is a @code{size_t}.
  1692. @samp{z} was introduced in @w{ISO C99}. @samp{Z} is a GNU extension
  1693. predating this addition and should not be used in new code.
  1694. @end table
  1695. Here is an example. Using the template string:
  1696. @smallexample
  1697. "|%5d|%-5d|%+5d|%+-5d|% 5d|%05d|%5.0d|%5.2d|%d|\n"
  1698. @end smallexample
  1699. @noindent
  1700. to print numbers using the different options for the @samp{%d}
  1701. conversion gives results like:
  1702. @smallexample
  1703. | 0|0 | +0|+0 | 0|00000| | 00|0|
  1704. | 1|1 | +1|+1 | 1|00001| 1| 01|1|
  1705. | -1|-1 | -1|-1 | -1|-0001| -1| -01|-1|
  1706. |100000|100000|+100000|+100000| 100000|100000|100000|100000|100000|
  1707. @end smallexample
  1708. In particular, notice what happens in the last case where the number
  1709. is too large to fit in the minimum field width specified.
  1710. Here are some more examples showing how unsigned integers print under
  1711. various format options, using the template string:
  1712. @smallexample
  1713. "|%5u|%5o|%5x|%5X|%#5o|%#5x|%#5X|%#10.8x|\n"
  1714. @end smallexample
  1715. @smallexample
  1716. | 0| 0| 0| 0| 0| 0| 0| 00000000|
  1717. | 1| 1| 1| 1| 01| 0x1| 0X1|0x00000001|
  1718. |100000|303240|186a0|186A0|0303240|0x186a0|0X186A0|0x000186a0|
  1719. @end smallexample
  1720. @node Floating-Point Conversions
  1721. @subsection Floating-Point Conversions
  1722. This section discusses the conversion specifications for floating-point
  1723. numbers: the @samp{%f}, @samp{%e}, @samp{%E}, @samp{%g}, and @samp{%G}
  1724. conversions.
  1725. The @samp{%f} conversion prints its argument in fixed-point notation,
  1726. producing output of the form
  1727. @w{[@code{-}]@var{ddd}@code{.}@var{ddd}},
  1728. where the number of digits following the decimal point is controlled
  1729. by the precision you specify.
  1730. The @samp{%e} conversion prints its argument in exponential notation,
  1731. producing output of the form
  1732. @w{[@code{-}]@var{d}@code{.}@var{ddd}@code{e}[@code{+}|@code{-}]@var{dd}}.
  1733. Again, the number of digits following the decimal point is controlled by
  1734. the precision. The exponent always contains at least two digits. The
  1735. @samp{%E} conversion is similar but the exponent is marked with the letter
  1736. @samp{E} instead of @samp{e}.
  1737. The @samp{%g} and @samp{%G} conversions print the argument in the style
  1738. of @samp{%e} or @samp{%E} (respectively) if the exponent would be less
  1739. than -4 or greater than or equal to the precision; otherwise they use
  1740. the @samp{%f} style. A precision of @code{0}, is taken as 1.
  1741. Trailing zeros are removed from the fractional portion of the result and
  1742. a decimal-point character appears only if it is followed by a digit.
  1743. The @samp{%a} and @samp{%A} conversions are meant for representing
  1744. floating-point numbers exactly in textual form so that they can be
  1745. exchanged as texts between different programs and/or machines. The
  1746. numbers are represented in the form
  1747. @w{[@code{-}]@code{0x}@var{h}@code{.}@var{hhh}@code{p}[@code{+}|@code{-}]@var{dd}}.
  1748. At the left of the decimal-point character exactly one digit is print.
  1749. This character is only @code{0} if the number is denormalized.
  1750. Otherwise the value is unspecified; it is implementation dependent how many
  1751. bits are used. The number of hexadecimal digits on the right side of
  1752. the decimal-point character is equal to the precision. If the precision
  1753. is zero it is determined to be large enough to provide an exact
  1754. representation of the number (or it is large enough to distinguish two
  1755. adjacent values if the @code{FLT_RADIX} is not a power of 2,
  1756. @pxref{Floating Point Parameters}). For the @samp{%a} conversion
  1757. lower-case characters are used to represent the hexadecimal number and
  1758. the prefix and exponent sign are printed as @code{0x} and @code{p}
  1759. respectively. Otherwise upper-case characters are used and @code{0X}
  1760. and @code{P} are used for the representation of prefix and exponent
  1761. string. The exponent to the base of two is printed as a decimal number
  1762. using at least one digit but at most as many digits as necessary to
  1763. represent the value exactly.
  1764. If the value to be printed represents infinity or a NaN, the output is
  1765. @w{[@code{-}]@code{inf}} or @code{nan} respectively if the conversion
  1766. specifier is @samp{%a}, @samp{%e}, @samp{%f}, or @samp{%g} and it is
  1767. @w{[@code{-}]@code{INF}} or @code{NAN} respectively if the conversion is
  1768. @samp{%A}, @samp{%E}, or @samp{%G}.
  1769. The following flags can be used to modify the behavior:
  1770. @comment We use @asis instead of @samp so we can have ` ' as an item.
  1771. @table @asis
  1772. @item @samp{-}
  1773. Left-justify the result in the field. Normally the result is
  1774. right-justified.
  1775. @item @samp{+}
  1776. Always include a plus or minus sign in the result.
  1777. @item @samp{ }
  1778. If the result doesn't start with a plus or minus sign, prefix it with a
  1779. space instead. Since the @samp{+} flag ensures that the result includes
  1780. a sign, this flag is ignored if you supply both of them.
  1781. @item @samp{#}
  1782. Specifies that the result should always include a decimal point, even
  1783. if no digits follow it. For the @samp{%g} and @samp{%G} conversions,
  1784. this also forces trailing zeros after the decimal point to be left
  1785. in place where they would otherwise be removed.
  1786. @item @samp{'}
  1787. Separate the digits of the integer part of the result into groups as
  1788. specified by the locale specified for the @code{LC_NUMERIC} category;
  1789. @pxref{General Numeric}. This flag is a GNU extension.
  1790. @item @samp{0}
  1791. Pad the field with zeros instead of spaces; the zeros are placed
  1792. after any sign. This flag is ignored if the @samp{-} flag is also
  1793. specified.
  1794. @end table
  1795. The precision specifies how many digits follow the decimal-point
  1796. character for the @samp{%f}, @samp{%e}, and @samp{%E} conversions. For
  1797. these conversions, the default precision is @code{6}. If the precision
  1798. is explicitly @code{0}, this suppresses the decimal point character
  1799. entirely. For the @samp{%g} and @samp{%G} conversions, the precision
  1800. specifies how many significant digits to print. Significant digits are
  1801. the first digit before the decimal point, and all the digits after it.
  1802. If the precision is @code{0} or not specified for @samp{%g} or @samp{%G},
  1803. it is treated like a value of @code{1}. If the value being printed
  1804. cannot be expressed accurately in the specified number of digits, the
  1805. value is rounded to the nearest number that fits.
  1806. Without a type modifier, the floating-point conversions use an argument
  1807. of type @code{double}. (By the default argument promotions, any
  1808. @code{float} arguments are automatically converted to @code{double}.)
  1809. The following type modifier is supported:
  1810. @table @samp
  1811. @item L
  1812. An uppercase @samp{L} specifies that the argument is a @code{long
  1813. double}.
  1814. @end table
  1815. Here are some examples showing how numbers print using the various
  1816. floating-point conversions. All of the numbers were printed using
  1817. this template string:
  1818. @smallexample
  1819. "|%13.4a|%13.4f|%13.4e|%13.4g|\n"
  1820. @end smallexample
  1821. Here is the output:
  1822. @smallexample
  1823. | 0x0.0000p+0| 0.0000| 0.0000e+00| 0|
  1824. | 0x1.0000p-1| 0.5000| 5.0000e-01| 0.5|
  1825. | 0x1.0000p+0| 1.0000| 1.0000e+00| 1|
  1826. | -0x1.0000p+0| -1.0000| -1.0000e+00| -1|
  1827. | 0x1.9000p+6| 100.0000| 1.0000e+02| 100|
  1828. | 0x1.f400p+9| 1000.0000| 1.0000e+03| 1000|
  1829. | 0x1.3880p+13| 10000.0000| 1.0000e+04| 1e+04|
  1830. | 0x1.81c8p+13| 12345.0000| 1.2345e+04| 1.234e+04|
  1831. | 0x1.86a0p+16| 100000.0000| 1.0000e+05| 1e+05|
  1832. | 0x1.e240p+16| 123456.0000| 1.2346e+05| 1.235e+05|
  1833. @end smallexample
  1834. Notice how the @samp{%g} conversion drops trailing zeros.
  1835. @node Other Output Conversions
  1836. @subsection Other Output Conversions
  1837. This section describes miscellaneous conversions for @code{printf}.
  1838. The @samp{%c} conversion prints a single character. In case there is no
  1839. @samp{l} modifier the @code{int} argument is first converted to an
  1840. @code{unsigned char}. Then, if used in a wide stream function, the
  1841. character is converted into the corresponding wide character. The
  1842. @samp{-} flag can be used to specify left-justification in the field,
  1843. but no other flags are defined, and no precision or type modifier can be
  1844. given. For example:
  1845. @smallexample
  1846. printf ("%c%c%c%c%c", 'h', 'e', 'l', 'l', 'o');
  1847. @end smallexample
  1848. @noindent
  1849. prints @samp{hello}.
  1850. If there is an @samp{l} modifier present the argument is expected to be
  1851. of type @code{wint_t}. If used in a multibyte function the wide
  1852. character is converted into a multibyte character before being added to
  1853. the output. In this case more than one output byte can be produced.
  1854. The @samp{%s} conversion prints a string. If no @samp{l} modifier is
  1855. present the corresponding argument must be of type @code{char *} (or
  1856. @code{const char *}). If used in a wide stream function the string is
  1857. first converted to a wide character string. A precision can be
  1858. specified to indicate the maximum number of characters to write;
  1859. otherwise characters in the string up to but not including the
  1860. terminating null character are written to the output stream. The
  1861. @samp{-} flag can be used to specify left-justification in the field,
  1862. but no other flags or type modifiers are defined for this conversion.
  1863. For example:
  1864. @smallexample
  1865. printf ("%3s%-6s", "no", "where");
  1866. @end smallexample
  1867. @noindent
  1868. prints @samp{ nowhere }.
  1869. If there is an @samp{l} modifier present, the argument is expected to
  1870. be of type @code{wchar_t} (or @code{const wchar_t *}).
  1871. If you accidentally pass a null pointer as the argument for a @samp{%s}
  1872. conversion, @theglibc{} prints it as @samp{(null)}. We think this
  1873. is more useful than crashing. But it's not good practice to pass a null
  1874. argument intentionally.
  1875. The @samp{%m} conversion prints the string corresponding to the error
  1876. code in @code{errno}. @xref{Error Messages}. Thus:
  1877. @smallexample
  1878. fprintf (stderr, "can't open `%s': %m\n", filename);
  1879. @end smallexample
  1880. @noindent
  1881. is equivalent to:
  1882. @smallexample
  1883. fprintf (stderr, "can't open `%s': %s\n", filename, strerror (errno));
  1884. @end smallexample
  1885. @noindent
  1886. The @samp{%m} conversion is a @glibcadj{} extension.
  1887. The @samp{%p} conversion prints a pointer value. The corresponding
  1888. argument must be of type @code{void *}. In practice, you can use any
  1889. type of pointer.
  1890. In @theglibc{}, non-null pointers are printed as unsigned integers,
  1891. as if a @samp{%#x} conversion were used. Null pointers print as
  1892. @samp{(nil)}. (Pointers might print differently in other systems.)
  1893. For example:
  1894. @smallexample
  1895. printf ("%p", "testing");
  1896. @end smallexample
  1897. @noindent
  1898. prints @samp{0x} followed by a hexadecimal number---the address of the
  1899. string constant @code{"testing"}. It does not print the word
  1900. @samp{testing}.
  1901. You can supply the @samp{-} flag with the @samp{%p} conversion to
  1902. specify left-justification, but no other flags, precision, or type
  1903. modifiers are defined.
  1904. The @samp{%n} conversion is unlike any of the other output conversions.
  1905. It uses an argument which must be a pointer to an @code{int}, but
  1906. instead of printing anything it stores the number of characters printed
  1907. so far by this call at that location. The @samp{h} and @samp{l} type
  1908. modifiers are permitted to specify that the argument is of type
  1909. @code{short int *} or @code{long int *} instead of @code{int *}, but no
  1910. flags, field width, or precision are permitted.
  1911. For example,
  1912. @smallexample
  1913. int nchar;
  1914. printf ("%d %s%n\n", 3, "bears", &nchar);
  1915. @end smallexample
  1916. @noindent
  1917. prints:
  1918. @smallexample
  1919. 3 bears
  1920. @end smallexample
  1921. @noindent
  1922. and sets @code{nchar} to @code{7}, because @samp{3 bears} is seven
  1923. characters.
  1924. The @samp{%%} conversion prints a literal @samp{%} character. This
  1925. conversion doesn't use an argument, and no flags, field width,
  1926. precision, or type modifiers are permitted.
  1927. @node Formatted Output Functions
  1928. @subsection Formatted Output Functions
  1929. This section describes how to call @code{printf} and related functions.
  1930. Prototypes for these functions are in the header file @file{stdio.h}.
  1931. Because these functions take a variable number of arguments, you
  1932. @emph{must} declare prototypes for them before using them. Of course,
  1933. the easiest way to make sure you have all the right prototypes is to
  1934. just include @file{stdio.h}.
  1935. @pindex stdio.h
  1936. @deftypefun int printf (const char *@var{template}, @dots{})
  1937. @standards{ISO, stdio.h}
  1938. @safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{}}@acunsafe{@acsmem{} @aculock{} @acucorrupt{}}}
  1939. The @code{printf} function prints the optional arguments under the
  1940. control of the template string @var{template} to the stream
  1941. @code{stdout}. It returns the number of characters printed, or a
  1942. negative value if there was an output error.
  1943. @end deftypefun
  1944. @deftypefun int wprintf (const wchar_t *@var{template}, @dots{})
  1945. @standards{ISO, wchar.h}
  1946. @safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{}}@acunsafe{@acsmem{} @aculock{} @acucorrupt{}}}
  1947. The @code{wprintf} function prints the optional arguments under the
  1948. control of the wide template string @var{template} to the stream
  1949. @code{stdout}. It returns the number of wide characters printed, or a
  1950. negative value if there was an output error.
  1951. @end deftypefun
  1952. @deftypefun int fprintf (FILE *@var{stream}, const char *@var{template}, @dots{})
  1953. @standards{ISO, stdio.h}
  1954. @safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{}}@acunsafe{@acsmem{} @aculock{} @acucorrupt{}}}
  1955. This function is just like @code{printf}, except that the output is
  1956. written to the stream @var{stream} instead of @code{stdout}.
  1957. @end deftypefun
  1958. @deftypefun int fwprintf (FILE *@var{stream}, const wchar_t *@var{template}, @dots{})
  1959. @standards{ISO, wchar.h}
  1960. @safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{}}@acunsafe{@acsmem{} @aculock{} @acucorrupt{}}}
  1961. This function is just like @code{wprintf}, except that the output is
  1962. written to the stream @var{stream} instead of @code{stdout}.
  1963. @end deftypefun
  1964. @deftypefun int sprintf (char *@var{s}, const char *@var{template}, @dots{})
  1965. @standards{ISO, stdio.h}
  1966. @safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
  1967. This is like @code{printf}, except that the output is stored in the character
  1968. array @var{s} instead of written to a stream. A null character is written
  1969. to mark the end of the string.
  1970. The @code{sprintf} function returns the number of characters stored in
  1971. the array @var{s}, not including the terminating null character.
  1972. The behavior of this function is undefined if copying takes place
  1973. between objects that overlap---for example, if @var{s} is also given
  1974. as an argument to be printed under control of the @samp{%s} conversion.
  1975. @xref{Copying Strings and Arrays}.
  1976. @strong{Warning:} The @code{sprintf} function can be @strong{dangerous}
  1977. because it can potentially output more characters than can fit in the
  1978. allocation size of the string @var{s}. Remember that the field width
  1979. given in a conversion specification is only a @emph{minimum} value.
  1980. To avoid this problem, you can use @code{snprintf} or @code{asprintf},
  1981. described below.
  1982. @end deftypefun
  1983. @deftypefun int swprintf (wchar_t *@var{ws}, size_t @var{size}, const wchar_t *@var{template}, @dots{})
  1984. @standards{GNU, wchar.h}
  1985. @safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
  1986. This is like @code{wprintf}, except that the output is stored in the
  1987. wide character array @var{ws} instead of written to a stream. A null
  1988. wide character is written to mark the end of the string. The @var{size}
  1989. argument specifies the maximum number of characters to produce. The
  1990. trailing null character is counted towards this limit, so you should
  1991. allocate at least @var{size} wide characters for the string @var{ws}.
  1992. The return value is the number of characters generated for the given
  1993. input, excluding the trailing null. If not all output fits into the
  1994. provided buffer a negative value is returned. You should try again with
  1995. a bigger output string. @emph{Note:} this is different from how
  1996. @code{snprintf} handles this situation.
  1997. Note that the corresponding narrow stream function takes fewer
  1998. parameters. @code{swprintf} in fact corresponds to the @code{snprintf}
  1999. function. Since the @code{sprintf} function can be dangerous and should
  2000. be avoided the @w{ISO C} committee refused to make the same mistake
  2001. again and decided to not define a function exactly corresponding to
  2002. @code{sprintf}.
  2003. @end deftypefun
  2004. @deftypefun int snprintf (char *@var{s}, size_t @var{size}, const char *@var{template}, @dots{})
  2005. @standards{GNU, stdio.h}
  2006. @safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
  2007. The @code{snprintf} function is similar to @code{sprintf}, except that
  2008. the @var{size} argument specifies the maximum number of characters to
  2009. produce. The trailing null character is counted towards this limit, so
  2010. you should allocate at least @var{size} characters for the string @var{s}.
  2011. If @var{size} is zero, nothing, not even the null byte, shall be written and
  2012. @var{s} may be a null pointer.
  2013. The return value is the number of characters which would be generated
  2014. for the given input, excluding the trailing null. If this value is
  2015. greater than or equal to @var{size}, not all characters from the result have
  2016. been stored in @var{s}. You should try again with a bigger output
  2017. string. Here is an example of doing this:
  2018. @smallexample
  2019. @group
  2020. /* @r{Construct a message describing the value of a variable}
  2021. @r{whose name is @var{name} and whose value is @var{value}.} */
  2022. char *
  2023. make_message (char *name, char *value)
  2024. @{
  2025. /* @r{Guess we need no more than 100 chars of space.} */
  2026. int size = 100;
  2027. char *buffer = (char *) xmalloc (size);
  2028. int nchars;
  2029. @end group
  2030. @group
  2031. if (buffer == NULL)
  2032. return NULL;
  2033. /* @r{Try to print in the allocated space.} */
  2034. nchars = snprintf (buffer, size, "value of %s is %s",
  2035. name, value);
  2036. @end group
  2037. @group
  2038. if (nchars >= size)
  2039. @{
  2040. /* @r{Reallocate buffer now that we know
  2041. how much space is needed.} */
  2042. size = nchars + 1;
  2043. buffer = (char *) xrealloc (buffer, size);
  2044. if (buffer != NULL)
  2045. /* @r{Try again.} */
  2046. snprintf (buffer, size, "value of %s is %s",
  2047. name, value);
  2048. @}
  2049. /* @r{The last call worked, return the string.} */
  2050. return buffer;
  2051. @}
  2052. @end group
  2053. @end smallexample
  2054. In practice, it is often easier just to use @code{asprintf}, below.
  2055. @strong{Attention:} In versions of @theglibc{} prior to 2.1 the
  2056. return value is the number of characters stored, not including the
  2057. terminating null; unless there was not enough space in @var{s} to
  2058. store the result in which case @code{-1} is returned. This was
  2059. changed in order to comply with the @w{ISO C99} standard.
  2060. @end deftypefun
  2061. @node Dynamic Output
  2062. @subsection Dynamically Allocating Formatted Output
  2063. The functions in this section do formatted output and place the results
  2064. in dynamically allocated memory.
  2065. @deftypefun int asprintf (char **@var{ptr}, const char *@var{template}, @dots{})
  2066. @standards{GNU, stdio.h}
  2067. @safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
  2068. This function is similar to @code{sprintf}, except that it dynamically
  2069. allocates a string (as with @code{malloc}; @pxref{Unconstrained
  2070. Allocation}) to hold the output, instead of putting the output in a
  2071. buffer you allocate in advance. The @var{ptr} argument should be the
  2072. address of a @code{char *} object, and a successful call to
  2073. @code{asprintf} stores a pointer to the newly allocated string at that
  2074. location.
  2075. The return value is the number of characters allocated for the buffer, or
  2076. less than zero if an error occurred. Usually this means that the buffer
  2077. could not be allocated.
  2078. Here is how to use @code{asprintf} to get the same result as the
  2079. @code{snprintf} example, but more easily:
  2080. @smallexample
  2081. /* @r{Construct a message describing the value of a variable}
  2082. @r{whose name is @var{name} and whose value is @var{value}.} */
  2083. char *
  2084. make_message (char *name, char *value)
  2085. @{
  2086. char *result;
  2087. if (asprintf (&result, "value of %s is %s", name, value) < 0)
  2088. return NULL;
  2089. return result;
  2090. @}
  2091. @end smallexample
  2092. @end deftypefun
  2093. @deftypefun int obstack_printf (struct obstack *@var{obstack}, const char *@var{template}, @dots{})
  2094. @standards{GNU, stdio.h}
  2095. @safety{@prelim{}@mtsafe{@mtsrace{:obstack} @mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{}}@acunsafe{@acucorrupt{} @acsmem{}}}
  2096. This function is similar to @code{asprintf}, except that it uses the
  2097. obstack @var{obstack} to allocate the space. @xref{Obstacks}.
  2098. The characters are written onto the end of the current object.
  2099. To get at them, you must finish the object with @code{obstack_finish}
  2100. (@pxref{Growing Objects}).@refill
  2101. @end deftypefun
  2102. @node Variable Arguments Output
  2103. @subsection Variable Arguments Output Functions
  2104. The functions @code{vprintf} and friends are provided so that you can
  2105. define your own variadic @code{printf}-like functions that make use of
  2106. the same internals as the built-in formatted output functions.
  2107. The most natural way to define such functions would be to use a language
  2108. construct to say, ``Call @code{printf} and pass this template plus all
  2109. of my arguments after the first five.'' But there is no way to do this
  2110. in C, and it would be hard to provide a way, since at the C language
  2111. level there is no way to tell how many arguments your function received.
  2112. Since that method is impossible, we provide alternative functions, the
  2113. @code{vprintf} series, which lets you pass a @code{va_list} to describe
  2114. ``all of my arguments after the first five.''
  2115. When it is sufficient to define a macro rather than a real function,
  2116. the GNU C compiler provides a way to do this much more easily with macros.
  2117. For example:
  2118. @smallexample
  2119. #define myprintf(a, b, c, d, e, rest...) \
  2120. printf (mytemplate , ## rest)
  2121. @end smallexample
  2122. @noindent
  2123. @xref{Variadic Macros,,, cpp, The C preprocessor}, for details.
  2124. But this is limited to macros, and does not apply to real functions at all.
  2125. Before calling @code{vprintf} or the other functions listed in this
  2126. section, you @emph{must} call @code{va_start} (@pxref{Variadic
  2127. Functions}) to initialize a pointer to the variable arguments. Then you
  2128. can call @code{va_arg} to fetch the arguments that you want to handle
  2129. yourself. This advances the pointer past those arguments.
  2130. Once your @code{va_list} pointer is pointing at the argument of your
  2131. choice, you are ready to call @code{vprintf}. That argument and all
  2132. subsequent arguments that were passed to your function are used by
  2133. @code{vprintf} along with the template that you specified separately.
  2134. @strong{Portability Note:} The value of the @code{va_list} pointer is
  2135. undetermined after the call to @code{vprintf}, so you must not use
  2136. @code{va_arg} after you call @code{vprintf}. Instead, you should call
  2137. @code{va_end} to retire the pointer from service. You can call
  2138. @code{va_start} again and begin fetching the arguments from the start of
  2139. the variable argument list. (Alternatively, you can use @code{va_copy}
  2140. to make a copy of the @code{va_list} pointer before calling
  2141. @code{vfprintf}.) Calling @code{vprintf} does not destroy the argument
  2142. list of your function, merely the particular pointer that you passed to
  2143. it.
  2144. Prototypes for these functions are declared in @file{stdio.h}.
  2145. @pindex stdio.h
  2146. @deftypefun int vprintf (const char *@var{template}, va_list @var{ap})
  2147. @standards{ISO, stdio.h}
  2148. @safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{}}@acunsafe{@acsmem{} @aculock{} @acucorrupt{}}}
  2149. This function is similar to @code{printf} except that, instead of taking
  2150. a variable number of arguments directly, it takes an argument list
  2151. pointer @var{ap}.
  2152. @end deftypefun
  2153. @deftypefun int vwprintf (const wchar_t *@var{template}, va_list @var{ap})
  2154. @standards{ISO, wchar.h}
  2155. @safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{}}@acunsafe{@acsmem{} @aculock{} @acucorrupt{}}}
  2156. This function is similar to @code{wprintf} except that, instead of taking
  2157. a variable number of arguments directly, it takes an argument list
  2158. pointer @var{ap}.
  2159. @end deftypefun
  2160. @deftypefun int vfprintf (FILE *@var{stream}, const char *@var{template}, va_list @var{ap})
  2161. @standards{ISO, stdio.h}
  2162. @safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{}}@acunsafe{@acsmem{} @aculock{} @acucorrupt{}}}
  2163. @c Although vfprintf sets up a cleanup region to release the lock on the
  2164. @c output stream, it doesn't use it to release args_value or string in
  2165. @c case of cancellation. This doesn't make it unsafe, but cancelling it
  2166. @c may leak memory. The unguarded use of __printf_function_table is
  2167. @c also of concern for all callers.
  2168. @c _itoa ok
  2169. @c _udiv_qrnnd_preinv ok
  2170. @c group_number ok
  2171. @c _i18n_number_rewrite
  2172. @c __wctrans ok
  2173. @c __towctrans @mtslocale
  2174. @c __wcrtomb ok? dup below
  2175. @c outdigit_value ok
  2176. @c outdigitwc_value ok
  2177. @c outchar ok
  2178. @c outstring ok
  2179. @c PAD ok
  2180. @c __printf_fp @mtslocale @ascuheap @acsmem
  2181. @c __printf_fphex @mtslocale
  2182. @c __readonly_area
  2183. @c [GNU/Linux] fopen, strtoul, free
  2184. @c __strerror_r ok if no translation, check otherwise
  2185. @c __btowc ? gconv-modules
  2186. @c __wcrtomb ok (not using internal state) gconv-modules
  2187. @c ARGCHECK
  2188. @c UNBUFFERED_P (tested before taking the stream lock)
  2189. @c buffered_vfprintf ok
  2190. @c __find_spec(wc|mb)
  2191. @c read_int
  2192. @c __libc_use_alloca
  2193. @c process_arg
  2194. @c process_string_arg
  2195. @c __parse_one_spec(wc|mb)
  2196. @c *__printf_arginfo_table unguarded
  2197. @c __printf_va_arg_table-> unguarded
  2198. @c *__printf_function_table unguarded
  2199. @c done_add
  2200. @c printf_unknown
  2201. @c outchar
  2202. @c _itoa_word
  2203. This is the equivalent of @code{fprintf} with the variable argument list
  2204. specified directly as for @code{vprintf}.
  2205. @end deftypefun
  2206. @deftypefun int vfwprintf (FILE *@var{stream}, const wchar_t *@var{template}, va_list @var{ap})
  2207. @standards{ISO, wchar.h}
  2208. @safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{}}@acunsafe{@acsmem{} @aculock{} @acucorrupt{}}}
  2209. This is the equivalent of @code{fwprintf} with the variable argument list
  2210. specified directly as for @code{vwprintf}.
  2211. @end deftypefun
  2212. @deftypefun int vsprintf (char *@var{s}, const char *@var{template}, va_list @var{ap})
  2213. @standards{ISO, stdio.h}
  2214. @safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
  2215. This is the equivalent of @code{sprintf} with the variable argument list
  2216. specified directly as for @code{vprintf}.
  2217. @end deftypefun
  2218. @deftypefun int vswprintf (wchar_t *@var{ws}, size_t @var{size}, const wchar_t *@var{template}, va_list @var{ap})
  2219. @standards{GNU, wchar.h}
  2220. @safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
  2221. This is the equivalent of @code{swprintf} with the variable argument list
  2222. specified directly as for @code{vwprintf}.
  2223. @end deftypefun
  2224. @deftypefun int vsnprintf (char *@var{s}, size_t @var{size}, const char *@var{template}, va_list @var{ap})
  2225. @standards{GNU, stdio.h}
  2226. @safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
  2227. This is the equivalent of @code{snprintf} with the variable argument list
  2228. specified directly as for @code{vprintf}.
  2229. @end deftypefun
  2230. @deftypefun int vasprintf (char **@var{ptr}, const char *@var{template}, va_list @var{ap})
  2231. @standards{GNU, stdio.h}
  2232. @safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
  2233. The @code{vasprintf} function is the equivalent of @code{asprintf} with the
  2234. variable argument list specified directly as for @code{vprintf}.
  2235. @end deftypefun
  2236. @deftypefun int obstack_vprintf (struct obstack *@var{obstack}, const char *@var{template}, va_list @var{ap})
  2237. @standards{GNU, stdio.h}
  2238. @safety{@prelim{}@mtsafe{@mtsrace{:obstack} @mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{}}@acunsafe{@acucorrupt{} @acsmem{}}}
  2239. @c The obstack is not guarded by mutexes, it might be at an inconsistent
  2240. @c state within a signal handler, and it could be left at an
  2241. @c inconsistent state in case of cancellation.
  2242. The @code{obstack_vprintf} function is the equivalent of
  2243. @code{obstack_printf} with the variable argument list specified directly
  2244. as for @code{vprintf}.@refill
  2245. @end deftypefun
  2246. Here's an example showing how you might use @code{vfprintf}. This is a
  2247. function that prints error messages to the stream @code{stderr}, along
  2248. with a prefix indicating the name of the program
  2249. (@pxref{Error Messages}, for a description of
  2250. @code{program_invocation_short_name}).
  2251. @smallexample
  2252. @group
  2253. #include <stdio.h>
  2254. #include <stdarg.h>
  2255. void
  2256. eprintf (const char *template, ...)
  2257. @{
  2258. va_list ap;
  2259. extern char *program_invocation_short_name;
  2260. fprintf (stderr, "%s: ", program_invocation_short_name);
  2261. va_start (ap, template);
  2262. vfprintf (stderr, template, ap);
  2263. va_end (ap);
  2264. @}
  2265. @end group
  2266. @end smallexample
  2267. @noindent
  2268. You could call @code{eprintf} like this:
  2269. @smallexample
  2270. eprintf ("file `%s' does not exist\n", filename);
  2271. @end smallexample
  2272. In GNU C, there is a special construct you can use to let the compiler
  2273. know that a function uses a @code{printf}-style format string. Then it
  2274. can check the number and types of arguments in each call to the
  2275. function, and warn you when they do not match the format string.
  2276. For example, take this declaration of @code{eprintf}:
  2277. @smallexample
  2278. void eprintf (const char *template, ...)
  2279. __attribute__ ((format (printf, 1, 2)));
  2280. @end smallexample
  2281. @noindent
  2282. This tells the compiler that @code{eprintf} uses a format string like
  2283. @code{printf} (as opposed to @code{scanf}; @pxref{Formatted Input});
  2284. the format string appears as the first argument;
  2285. and the arguments to satisfy the format begin with the second.
  2286. @xref{Function Attributes, , Declaring Attributes of Functions,
  2287. gcc, Using GNU CC}, for more information.
  2288. @node Parsing a Template String
  2289. @subsection Parsing a Template String
  2290. @cindex parsing a template string
  2291. You can use the function @code{parse_printf_format} to obtain
  2292. information about the number and types of arguments that are expected by
  2293. a given template string. This function permits interpreters that
  2294. provide interfaces to @code{printf} to avoid passing along invalid
  2295. arguments from the user's program, which could cause a crash.
  2296. All the symbols described in this section are declared in the header
  2297. file @file{printf.h}.
  2298. @deftypefun size_t parse_printf_format (const char *@var{template}, size_t @var{n}, int *@var{argtypes})
  2299. @standards{GNU, printf.h}
  2300. @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
  2301. This function returns information about the number and types of
  2302. arguments expected by the @code{printf} template string @var{template}.
  2303. The information is stored in the array @var{argtypes}; each element of
  2304. this array describes one argument. This information is encoded using
  2305. the various @samp{PA_} macros, listed below.
  2306. The argument @var{n} specifies the number of elements in the array
  2307. @var{argtypes}. This is the maximum number of elements that
  2308. @code{parse_printf_format} will try to write.
  2309. @code{parse_printf_format} returns the total number of arguments required
  2310. by @var{template}. If this number is greater than @var{n}, then the
  2311. information returned describes only the first @var{n} arguments. If you
  2312. want information about additional arguments, allocate a bigger
  2313. array and call @code{parse_printf_format} again.
  2314. @end deftypefun
  2315. The argument types are encoded as a combination of a basic type and
  2316. modifier flag bits.
  2317. @deftypevr Macro int PA_FLAG_MASK
  2318. @standards{GNU, printf.h}
  2319. This macro is a bitmask for the type modifier flag bits. You can write
  2320. the expression @code{(argtypes[i] & PA_FLAG_MASK)} to extract just the
  2321. flag bits for an argument, or @code{(argtypes[i] & ~PA_FLAG_MASK)} to
  2322. extract just the basic type code.
  2323. @end deftypevr
  2324. Here are symbolic constants that represent the basic types; they stand
  2325. for integer values.
  2326. @vtable @code
  2327. @item PA_INT
  2328. @standards{GNU, printf.h}
  2329. This specifies that the base type is @code{int}.
  2330. @item PA_CHAR
  2331. @standards{GNU, printf.h}
  2332. This specifies that the base type is @code{int}, cast to @code{char}.
  2333. @item PA_STRING
  2334. @standards{GNU, printf.h}
  2335. This specifies that the base type is @code{char *}, a null-terminated string.
  2336. @item PA_POINTER
  2337. @standards{GNU, printf.h}
  2338. This specifies that the base type is @code{void *}, an arbitrary pointer.
  2339. @item PA_FLOAT
  2340. @standards{GNU, printf.h}
  2341. This specifies that the base type is @code{float}.
  2342. @item PA_DOUBLE
  2343. @standards{GNU, printf.h}
  2344. This specifies that the base type is @code{double}.
  2345. @item PA_LAST
  2346. @standards{GNU, printf.h}
  2347. You can define additional base types for your own programs as offsets
  2348. from @code{PA_LAST}. For example, if you have data types @samp{foo}
  2349. and @samp{bar} with their own specialized @code{printf} conversions,
  2350. you could define encodings for these types as:
  2351. @smallexample
  2352. #define PA_FOO PA_LAST
  2353. #define PA_BAR (PA_LAST + 1)
  2354. @end smallexample
  2355. @end vtable
  2356. Here are the flag bits that modify a basic type. They are combined with
  2357. the code for the basic type using inclusive-or.
  2358. @vtable @code
  2359. @item PA_FLAG_PTR
  2360. @standards{GNU, printf.h}
  2361. If this bit is set, it indicates that the encoded type is a pointer to
  2362. the base type, rather than an immediate value.
  2363. For example, @samp{PA_INT|PA_FLAG_PTR} represents the type @samp{int *}.
  2364. @item PA_FLAG_SHORT
  2365. @standards{GNU, printf.h}
  2366. If this bit is set, it indicates that the base type is modified with
  2367. @code{short}. (This corresponds to the @samp{h} type modifier.)
  2368. @item PA_FLAG_LONG
  2369. @standards{GNU, printf.h}
  2370. If this bit is set, it indicates that the base type is modified with
  2371. @code{long}. (This corresponds to the @samp{l} type modifier.)
  2372. @item PA_FLAG_LONG_LONG
  2373. @standards{GNU, printf.h}
  2374. If this bit is set, it indicates that the base type is modified with
  2375. @code{long long}. (This corresponds to the @samp{L} type modifier.)
  2376. @item PA_FLAG_LONG_DOUBLE
  2377. @standards{GNU, printf.h}
  2378. This is a synonym for @code{PA_FLAG_LONG_LONG}, used by convention with
  2379. a base type of @code{PA_DOUBLE} to indicate a type of @code{long double}.
  2380. @end vtable
  2381. @ifinfo
  2382. For an example of using these facilities, see @ref{Example of Parsing}.
  2383. @end ifinfo
  2384. @node Example of Parsing
  2385. @subsection Example of Parsing a Template String
  2386. Here is an example of decoding argument types for a format string. We
  2387. assume this is part of an interpreter which contains arguments of type
  2388. @code{NUMBER}, @code{CHAR}, @code{STRING} and @code{STRUCTURE} (and
  2389. perhaps others which are not valid here).
  2390. @smallexample
  2391. /* @r{Test whether the @var{nargs} specified objects}
  2392. @r{in the vector @var{args} are valid}
  2393. @r{for the format string @var{format}:}
  2394. @r{if so, return 1.}
  2395. @r{If not, return 0 after printing an error message.} */
  2396. int
  2397. validate_args (char *format, int nargs, OBJECT *args)
  2398. @{
  2399. int *argtypes;
  2400. int nwanted;
  2401. /* @r{Get the information about the arguments.}
  2402. @r{Each conversion specification must be at least two characters}
  2403. @r{long, so there cannot be more specifications than half the}
  2404. @r{length of the string.} */
  2405. argtypes = (int *) alloca (strlen (format) / 2 * sizeof (int));
  2406. nwanted = parse_printf_format (string, nelts, argtypes);
  2407. /* @r{Check the number of arguments.} */
  2408. if (nwanted > nargs)
  2409. @{
  2410. error ("too few arguments (at least %d required)", nwanted);
  2411. return 0;
  2412. @}
  2413. /* @r{Check the C type wanted for each argument}
  2414. @r{and see if the object given is suitable.} */
  2415. for (i = 0; i < nwanted; i++)
  2416. @{
  2417. int wanted;
  2418. if (argtypes[i] & PA_FLAG_PTR)
  2419. wanted = STRUCTURE;
  2420. else
  2421. switch (argtypes[i] & ~PA_FLAG_MASK)
  2422. @{
  2423. case PA_INT:
  2424. case PA_FLOAT:
  2425. case PA_DOUBLE:
  2426. wanted = NUMBER;
  2427. break;
  2428. case PA_CHAR:
  2429. wanted = CHAR;
  2430. break;
  2431. case PA_STRING:
  2432. wanted = STRING;
  2433. break;
  2434. case PA_POINTER:
  2435. wanted = STRUCTURE;
  2436. break;
  2437. @}
  2438. if (TYPE (args[i]) != wanted)
  2439. @{
  2440. error ("type mismatch for arg number %d", i);
  2441. return 0;
  2442. @}
  2443. @}
  2444. return 1;
  2445. @}
  2446. @end smallexample
  2447. @node Customizing Printf
  2448. @section Customizing @code{printf}
  2449. @cindex customizing @code{printf}
  2450. @cindex defining new @code{printf} conversions
  2451. @cindex extending @code{printf}
  2452. @Theglibc{} lets you define your own custom conversion specifiers
  2453. for @code{printf} template strings, to teach @code{printf} clever ways
  2454. to print the important data structures of your program.
  2455. The way you do this is by registering the conversion with the function
  2456. @code{register_printf_function}; see @ref{Registering New Conversions}.
  2457. One of the arguments you pass to this function is a pointer to a handler
  2458. function that produces the actual output; see @ref{Defining the Output
  2459. Handler}, for information on how to write this function.
  2460. You can also install a function that just returns information about the
  2461. number and type of arguments expected by the conversion specifier.
  2462. @xref{Parsing a Template String}, for information about this.
  2463. The facilities of this section are declared in the header file
  2464. @file{printf.h}.
  2465. @menu
  2466. * Registering New Conversions:: Using @code{register_printf_function}
  2467. to register a new output conversion.
  2468. * Conversion Specifier Options:: The handler must be able to get
  2469. the options specified in the
  2470. template when it is called.
  2471. * Defining the Output Handler:: Defining the handler and arginfo
  2472. functions that are passed as arguments
  2473. to @code{register_printf_function}.
  2474. * Printf Extension Example:: How to define a @code{printf}
  2475. handler function.
  2476. * Predefined Printf Handlers:: Predefined @code{printf} handlers.
  2477. @end menu
  2478. @strong{Portability Note:} The ability to extend the syntax of
  2479. @code{printf} template strings is a GNU extension. ISO standard C has
  2480. nothing similar. When using the GNU C compiler or any other compiler
  2481. that interprets calls to standard I/O functions according to the rules
  2482. of the language standard it is necessary to disable such handling by
  2483. the appropriate compiler option. Otherwise the behavior of a program
  2484. that relies on the extension is undefined.
  2485. @node Registering New Conversions
  2486. @subsection Registering New Conversions
  2487. The function to register a new output conversion is
  2488. @code{register_printf_function}, declared in @file{printf.h}.
  2489. @pindex printf.h
  2490. @deftypefun int register_printf_function (int @var{spec}, printf_function @var{handler-function}, printf_arginfo_function @var{arginfo-function})
  2491. @standards{GNU, printf.h}
  2492. @safety{@prelim{}@mtunsafe{@mtasuconst{:printfext}}@asunsafe{@ascuheap{} @asulock{}}@acunsafe{@acsmem{} @aculock{}}}
  2493. @c This function is guarded by the global non-recursive libc lock, but
  2494. @c users of the variables it sets aren't, and those should be MT-Safe,
  2495. @c so we're ruling out the use of this extension with threads. Calling
  2496. @c it from a signal handler may self-deadlock, and cancellation may
  2497. @c leave the lock held, besides leaking allocated memory.
  2498. This function defines the conversion specifier character @var{spec}.
  2499. Thus, if @var{spec} is @code{'Y'}, it defines the conversion @samp{%Y}.
  2500. You can redefine the built-in conversions like @samp{%s}, but flag
  2501. characters like @samp{#} and type modifiers like @samp{l} can never be
  2502. used as conversions; calling @code{register_printf_function} for those
  2503. characters has no effect. It is advisable not to use lowercase letters,
  2504. since the ISO C standard warns that additional lowercase letters may be
  2505. standardized in future editions of the standard.
  2506. The @var{handler-function} is the function called by @code{printf} and
  2507. friends when this conversion appears in a template string.
  2508. @xref{Defining the Output Handler}, for information about how to define
  2509. a function to pass as this argument. If you specify a null pointer, any
  2510. existing handler function for @var{spec} is removed.
  2511. The @var{arginfo-function} is the function called by
  2512. @code{parse_printf_format} when this conversion appears in a
  2513. template string. @xref{Parsing a Template String}, for information
  2514. about this.
  2515. @c The following is not true anymore. The `parse_printf_format' function
  2516. @c is now also called from `vfprintf' via `parse_one_spec'.
  2517. @c --drepper@gnu, 1996/11/14
  2518. @c
  2519. @c Normally, you install both functions for a conversion at the same time,
  2520. @c but if you are never going to call @code{parse_printf_format}, you do
  2521. @c not need to define an arginfo function.
  2522. @strong{Attention:} In @theglibc{} versions before 2.0 the
  2523. @var{arginfo-function} function did not need to be installed unless
  2524. the user used the @code{parse_printf_format} function. This has changed.
  2525. Now a call to any of the @code{printf} functions will call this
  2526. function when this format specifier appears in the format string.
  2527. The return value is @code{0} on success, and @code{-1} on failure
  2528. (which occurs if @var{spec} is out of range).
  2529. @strong{Portability Note:} It is possible to redefine the standard output
  2530. conversions but doing so is strongly discouraged because it may interfere
  2531. with the behavior of programs and compiler implementations that assume
  2532. the effects of the conversions conform to the relevant language standards.
  2533. In addition, conforming compilers need not guarantee that the function
  2534. registered for a standard conversion will be called for each such
  2535. conversion in every format string in a program.
  2536. @end deftypefun
  2537. @node Conversion Specifier Options
  2538. @subsection Conversion Specifier Options
  2539. If you define a meaning for @samp{%A}, what if the template contains
  2540. @samp{%+23A} or @samp{%-#A}? To implement a sensible meaning for these,
  2541. the handler when called needs to be able to get the options specified in
  2542. the template.
  2543. Both the @var{handler-function} and @var{arginfo-function} accept an
  2544. argument that points to a @code{struct printf_info}, which contains
  2545. information about the options appearing in an instance of the conversion
  2546. specifier. This data type is declared in the header file
  2547. @file{printf.h}.
  2548. @pindex printf.h
  2549. @deftp {Type} {struct printf_info}
  2550. @standards{GNU, printf.h}
  2551. This structure is used to pass information about the options appearing
  2552. in an instance of a conversion specifier in a @code{printf} template
  2553. string to the handler and arginfo functions for that specifier. It
  2554. contains the following members:
  2555. @table @code
  2556. @item int prec
  2557. This is the precision specified. The value is @code{-1} if no precision
  2558. was specified. If the precision was given as @samp{*}, the
  2559. @code{printf_info} structure passed to the handler function contains the
  2560. actual value retrieved from the argument list. But the structure passed
  2561. to the arginfo function contains a value of @code{INT_MIN}, since the
  2562. actual value is not known.
  2563. @item int width
  2564. This is the minimum field width specified. The value is @code{0} if no
  2565. width was specified. If the field width was given as @samp{*}, the
  2566. @code{printf_info} structure passed to the handler function contains the
  2567. actual value retrieved from the argument list. But the structure passed
  2568. to the arginfo function contains a value of @code{INT_MIN}, since the
  2569. actual value is not known.
  2570. @item wchar_t spec
  2571. This is the conversion specifier character specified. It's stored in
  2572. the structure so that you can register the same handler function for
  2573. multiple characters, but still have a way to tell them apart when the
  2574. handler function is called.
  2575. @item unsigned int is_long_double
  2576. This is a boolean that is true if the @samp{L}, @samp{ll}, or @samp{q}
  2577. type modifier was specified. For integer conversions, this indicates
  2578. @code{long long int}, as opposed to @code{long double} for floating
  2579. point conversions.
  2580. @item unsigned int is_char
  2581. This is a boolean that is true if the @samp{hh} type modifier was specified.
  2582. @item unsigned int is_short
  2583. This is a boolean that is true if the @samp{h} type modifier was specified.
  2584. @item unsigned int is_long
  2585. This is a boolean that is true if the @samp{l} type modifier was specified.
  2586. @item unsigned int alt
  2587. This is a boolean that is true if the @samp{#} flag was specified.
  2588. @item unsigned int space
  2589. This is a boolean that is true if the @samp{ } flag was specified.
  2590. @item unsigned int left
  2591. This is a boolean that is true if the @samp{-} flag was specified.
  2592. @item unsigned int showsign
  2593. This is a boolean that is true if the @samp{+} flag was specified.
  2594. @item unsigned int group
  2595. This is a boolean that is true if the @samp{'} flag was specified.
  2596. @item unsigned int extra
  2597. This flag has a special meaning depending on the context. It could
  2598. be used freely by the user-defined handlers but when called from
  2599. the @code{printf} function this variable always contains the value
  2600. @code{0}.
  2601. @item unsigned int wide
  2602. This flag is set if the stream is wide oriented.
  2603. @item wchar_t pad
  2604. This is the character to use for padding the output to the minimum field
  2605. width. The value is @code{'0'} if the @samp{0} flag was specified, and
  2606. @code{' '} otherwise.
  2607. @end table
  2608. @end deftp
  2609. @node Defining the Output Handler
  2610. @subsection Defining the Output Handler
  2611. Now let's look at how to define the handler and arginfo functions
  2612. which are passed as arguments to @code{register_printf_function}.
  2613. @strong{Compatibility Note:} The interface changed in @theglibc{}
  2614. version 2.0. Previously the third argument was of type
  2615. @code{va_list *}.
  2616. You should define your handler functions with a prototype like:
  2617. @smallexample
  2618. int @var{function} (FILE *stream, const struct printf_info *info,
  2619. const void *const *args)
  2620. @end smallexample
  2621. The @var{stream} argument passed to the handler function is the stream to
  2622. which it should write output.
  2623. The @var{info} argument is a pointer to a structure that contains
  2624. information about the various options that were included with the
  2625. conversion in the template string. You should not modify this structure
  2626. inside your handler function. @xref{Conversion Specifier Options}, for
  2627. a description of this data structure.
  2628. @c The following changes some time back. --drepper@gnu, 1996/11/14
  2629. @c
  2630. @c The @code{ap_pointer} argument is used to pass the tail of the variable
  2631. @c argument list containing the values to be printed to your handler.
  2632. @c Unlike most other functions that can be passed an explicit variable
  2633. @c argument list, this is a @emph{pointer} to a @code{va_list}, rather than
  2634. @c the @code{va_list} itself. Thus, you should fetch arguments by
  2635. @c means of @code{va_arg (*ap_pointer, @var{type})}.
  2636. @c
  2637. @c (Passing a pointer here allows the function that calls your handler
  2638. @c function to update its own @code{va_list} variable to account for the
  2639. @c arguments that your handler processes. @xref{Variadic Functions}.)
  2640. The @var{args} is a vector of pointers to the arguments data.
  2641. The number of arguments was determined by calling the argument
  2642. information function provided by the user.
  2643. Your handler function should return a value just like @code{printf}
  2644. does: it should return the number of characters it has written, or a
  2645. negative value to indicate an error.
  2646. @deftp {Data Type} printf_function
  2647. @standards{GNU, printf.h}
  2648. This is the data type that a handler function should have.
  2649. @end deftp
  2650. If you are going to use @w{@code{parse_printf_format}} in your
  2651. application, you must also define a function to pass as the
  2652. @var{arginfo-function} argument for each new conversion you install with
  2653. @code{register_printf_function}.
  2654. You have to define these functions with a prototype like:
  2655. @smallexample
  2656. int @var{function} (const struct printf_info *info,
  2657. size_t n, int *argtypes)
  2658. @end smallexample
  2659. The return value from the function should be the number of arguments the
  2660. conversion expects. The function should also fill in no more than
  2661. @var{n} elements of the @var{argtypes} array with information about the
  2662. types of each of these arguments. This information is encoded using the
  2663. various @samp{PA_} macros. (You will notice that this is the same
  2664. calling convention @code{parse_printf_format} itself uses.)
  2665. @deftp {Data Type} printf_arginfo_function
  2666. @standards{GNU, printf.h}
  2667. This type is used to describe functions that return information about
  2668. the number and type of arguments used by a conversion specifier.
  2669. @end deftp
  2670. @node Printf Extension Example
  2671. @subsection @code{printf} Extension Example
  2672. Here is an example showing how to define a @code{printf} handler function.
  2673. This program defines a data structure called a @code{Widget} and
  2674. defines the @samp{%W} conversion to print information about @w{@code{Widget *}}
  2675. arguments, including the pointer value and the name stored in the data
  2676. structure. The @samp{%W} conversion supports the minimum field width and
  2677. left-justification options, but ignores everything else.
  2678. @smallexample
  2679. @include rprintf.c.texi
  2680. @end smallexample
  2681. The output produced by this program looks like:
  2682. @smallexample
  2683. |<Widget 0xffeffb7c: mywidget>|
  2684. | <Widget 0xffeffb7c: mywidget>|
  2685. |<Widget 0xffeffb7c: mywidget> |
  2686. @end smallexample
  2687. @node Predefined Printf Handlers
  2688. @subsection Predefined @code{printf} Handlers
  2689. @Theglibc{} also contains a concrete and useful application of the
  2690. @code{printf} handler extension. There are two functions available
  2691. which implement a special way to print floating-point numbers.
  2692. @deftypefun int printf_size (FILE *@var{fp}, const struct printf_info *@var{info}, const void *const *@var{args})
  2693. @standards{GNU, printf.h}
  2694. @safety{@prelim{}@mtsafe{@mtsrace{:fp} @mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{}}@acunsafe{@acsmem{} @acucorrupt{}}}
  2695. @c This is meant to be called by vfprintf, that should hold the lock on
  2696. @c the stream, but if this function is called directly, output will be
  2697. @c racy, besides the uses of the global locale object while other
  2698. @c threads may be changing it and the possbility of leaving the stream
  2699. @c object in an inconsistent state in case of cancellation.
  2700. Print a given floating point number as for the format @code{%f} except
  2701. that there is a postfix character indicating the divisor for the
  2702. number to make this less than 1000. There are two possible divisors:
  2703. powers of 1024 or powers of 1000. Which one is used depends on the
  2704. format character specified while registered this handler. If the
  2705. character is of lower case, 1024 is used. For upper case characters,
  2706. 1000 is used.
  2707. The postfix tag corresponds to bytes, kilobytes, megabytes, gigabytes,
  2708. etc. The full table is:
  2709. @ifinfo
  2710. @multitable {' '} {2^10 (1024)} {zetta} {Upper} {10^24 (1000)}
  2711. @item low @tab Multiplier @tab From @tab Upper @tab Multiplier
  2712. @item ' ' @tab 1 @tab @tab ' ' @tab 1
  2713. @item k @tab 2^10 (1024) @tab kilo @tab K @tab 10^3 (1000)
  2714. @item m @tab 2^20 @tab mega @tab M @tab 10^6
  2715. @item g @tab 2^30 @tab giga @tab G @tab 10^9
  2716. @item t @tab 2^40 @tab tera @tab T @tab 10^12
  2717. @item p @tab 2^50 @tab peta @tab P @tab 10^15
  2718. @item e @tab 2^60 @tab exa @tab E @tab 10^18
  2719. @item z @tab 2^70 @tab zetta @tab Z @tab 10^21
  2720. @item y @tab 2^80 @tab yotta @tab Y @tab 10^24
  2721. @end multitable
  2722. @end ifinfo
  2723. @iftex
  2724. @tex
  2725. \hbox to\hsize{\hfil\vbox{\offinterlineskip
  2726. \hrule
  2727. \halign{\strut#& \vrule#\tabskip=1em plus2em& {\tt#}\hfil& \vrule#& #\hfil& \vrule#& #\hfil& \vrule#& {\tt#}\hfil& \vrule#& #\hfil& \vrule#\tabskip=0pt\cr
  2728. \noalign{\hrule}
  2729. \omit&height2pt&\omit&&\omit&&\omit&&\omit&&\omit&\cr
  2730. && \omit low && Multiplier && From && \omit Upper && Multiplier &\cr
  2731. \omit&height2pt&\omit&&\omit&&\omit&&\omit&&\omit&\cr
  2732. \noalign{\hrule}
  2733. && {\tt\char32} && 1 && && {\tt\char32} && 1 &\cr
  2734. && k && $2^{10} = 1024$ && kilo && K && $10^3 = 1000$ &\cr
  2735. && m && $2^{20}$ && mega && M && $10^6$ &\cr
  2736. && g && $2^{30}$ && giga && G && $10^9$ &\cr
  2737. && t && $2^{40}$ && tera && T && $10^{12}$ &\cr
  2738. && p && $2^{50}$ && peta && P && $10^{15}$ &\cr
  2739. && e && $2^{60}$ && exa && E && $10^{18}$ &\cr
  2740. && z && $2^{70}$ && zetta && Z && $10^{21}$ &\cr
  2741. && y && $2^{80}$ && yotta && Y && $10^{24}$ &\cr
  2742. \noalign{\hrule}}}\hfil}
  2743. @end tex
  2744. @end iftex
  2745. The default precision is 3, i.e., 1024 is printed with a lower-case
  2746. format character as if it were @code{%.3fk} and will yield @code{1.000k}.
  2747. @end deftypefun
  2748. Due to the requirements of @code{register_printf_function} we must also
  2749. provide the function which returns information about the arguments.
  2750. @deftypefun int printf_size_info (const struct printf_info *@var{info}, size_t @var{n}, int *@var{argtypes})
  2751. @standards{GNU, printf.h}
  2752. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  2753. This function will return in @var{argtypes} the information about the
  2754. used parameters in the way the @code{vfprintf} implementation expects
  2755. it. The format always takes one argument.
  2756. @end deftypefun
  2757. To use these functions both functions must be registered with a call like
  2758. @smallexample
  2759. register_printf_function ('B', printf_size, printf_size_info);
  2760. @end smallexample
  2761. Here we register the functions to print numbers as powers of 1000 since
  2762. the format character @code{'B'} is an upper-case character. If we
  2763. would additionally use @code{'b'} in a line like
  2764. @smallexample
  2765. register_printf_function ('b', printf_size, printf_size_info);
  2766. @end smallexample
  2767. @noindent
  2768. we could also print using a power of 1024. Please note that all that is
  2769. different in these two lines is the format specifier. The
  2770. @code{printf_size} function knows about the difference between lower and upper
  2771. case format specifiers.
  2772. The use of @code{'B'} and @code{'b'} is no coincidence. Rather it is
  2773. the preferred way to use this functionality since it is available on
  2774. some other systems which also use format specifiers.
  2775. @node Formatted Input
  2776. @section Formatted Input
  2777. @cindex formatted input from a stream
  2778. @cindex reading from a stream, formatted
  2779. @cindex format string, for @code{scanf}
  2780. @cindex template, for @code{scanf}
  2781. The functions described in this section (@code{scanf} and related
  2782. functions) provide facilities for formatted input analogous to the
  2783. formatted output facilities. These functions provide a mechanism for
  2784. reading arbitrary values under the control of a @dfn{format string} or
  2785. @dfn{template string}.
  2786. @menu
  2787. * Formatted Input Basics:: Some basics to get you started.
  2788. * Input Conversion Syntax:: Syntax of conversion specifications.
  2789. * Table of Input Conversions:: Summary of input conversions and what they do.
  2790. * Numeric Input Conversions:: Details of conversions for reading numbers.
  2791. * String Input Conversions:: Details of conversions for reading strings.
  2792. * Dynamic String Input:: String conversions that @code{malloc} the buffer.
  2793. * Other Input Conversions:: Details of miscellaneous other conversions.
  2794. * Formatted Input Functions:: Descriptions of the actual functions.
  2795. * Variable Arguments Input:: @code{vscanf} and friends.
  2796. @end menu
  2797. @node Formatted Input Basics
  2798. @subsection Formatted Input Basics
  2799. Calls to @code{scanf} are superficially similar to calls to
  2800. @code{printf} in that arbitrary arguments are read under the control of
  2801. a template string. While the syntax of the conversion specifications in
  2802. the template is very similar to that for @code{printf}, the
  2803. interpretation of the template is oriented more towards free-format
  2804. input and simple pattern matching, rather than fixed-field formatting.
  2805. For example, most @code{scanf} conversions skip over any amount of
  2806. ``white space'' (including spaces, tabs, and newlines) in the input
  2807. file, and there is no concept of precision for the numeric input
  2808. conversions as there is for the corresponding output conversions.
  2809. Ordinarily, non-whitespace characters in the template are expected to
  2810. match characters in the input stream exactly, but a matching failure is
  2811. distinct from an input error on the stream.
  2812. @cindex conversion specifications (@code{scanf})
  2813. Another area of difference between @code{scanf} and @code{printf} is
  2814. that you must remember to supply pointers rather than immediate values
  2815. as the optional arguments to @code{scanf}; the values that are read are
  2816. stored in the objects that the pointers point to. Even experienced
  2817. programmers tend to forget this occasionally, so if your program is
  2818. getting strange errors that seem to be related to @code{scanf}, you
  2819. might want to double-check this.
  2820. When a @dfn{matching failure} occurs, @code{scanf} returns immediately,
  2821. leaving the first non-matching character as the next character to be
  2822. read from the stream. The normal return value from @code{scanf} is the
  2823. number of values that were assigned, so you can use this to determine if
  2824. a matching error happened before all the expected values were read.
  2825. @cindex matching failure, in @code{scanf}
  2826. The @code{scanf} function is typically used for things like reading in
  2827. the contents of tables. For example, here is a function that uses
  2828. @code{scanf} to initialize an array of @code{double}:
  2829. @smallexample
  2830. void
  2831. readarray (double *array, int n)
  2832. @{
  2833. int i;
  2834. for (i=0; i<n; i++)
  2835. if (scanf (" %lf", &(array[i])) != 1)
  2836. invalid_input_error ();
  2837. @}
  2838. @end smallexample
  2839. The formatted input functions are not used as frequently as the
  2840. formatted output functions. Partly, this is because it takes some care
  2841. to use them properly. Another reason is that it is difficult to recover
  2842. from a matching error.
  2843. If you are trying to read input that doesn't match a single, fixed
  2844. pattern, you may be better off using a tool such as Flex to generate a
  2845. lexical scanner, or Bison to generate a parser, rather than using
  2846. @code{scanf}. For more information about these tools, see @ref{Top, , ,
  2847. flex.info, Flex: The Lexical Scanner Generator}, and @ref{Top, , ,
  2848. bison.info, The Bison Reference Manual}.
  2849. @node Input Conversion Syntax
  2850. @subsection Input Conversion Syntax
  2851. A @code{scanf} template string is a string that contains ordinary
  2852. multibyte characters interspersed with conversion specifications that
  2853. start with @samp{%}.
  2854. Any whitespace character (as defined by the @code{isspace} function;
  2855. @pxref{Classification of Characters}) in the template causes any number
  2856. of whitespace characters in the input stream to be read and discarded.
  2857. The whitespace characters that are matched need not be exactly the same
  2858. whitespace characters that appear in the template string. For example,
  2859. write @samp{ , } in the template to recognize a comma with optional
  2860. whitespace before and after.
  2861. Other characters in the template string that are not part of conversion
  2862. specifications must match characters in the input stream exactly; if
  2863. this is not the case, a matching failure occurs.
  2864. The conversion specifications in a @code{scanf} template string
  2865. have the general form:
  2866. @smallexample
  2867. % @var{flags} @var{width} @var{type} @var{conversion}
  2868. @end smallexample
  2869. In more detail, an input conversion specification consists of an initial
  2870. @samp{%} character followed in sequence by:
  2871. @itemize @bullet
  2872. @item
  2873. An optional @dfn{flag character} @samp{*}, which says to ignore the text
  2874. read for this specification. When @code{scanf} finds a conversion
  2875. specification that uses this flag, it reads input as directed by the
  2876. rest of the conversion specification, but it discards this input, does
  2877. not use a pointer argument, and does not increment the count of
  2878. successful assignments.
  2879. @cindex flag character (@code{scanf})
  2880. @item
  2881. An optional flag character @samp{a} (valid with string conversions only)
  2882. which requests allocation of a buffer long enough to store the string in.
  2883. (This is a GNU extension.)
  2884. @xref{Dynamic String Input}.
  2885. @item
  2886. An optional decimal integer that specifies the @dfn{maximum field
  2887. width}. Reading of characters from the input stream stops either when
  2888. this maximum is reached or when a non-matching character is found,
  2889. whichever happens first. Most conversions discard initial whitespace
  2890. characters (those that don't are explicitly documented), and these
  2891. discarded characters don't count towards the maximum field width.
  2892. String input conversions store a null character to mark the end of the
  2893. input; the maximum field width does not include this terminator.
  2894. @cindex maximum field width (@code{scanf})
  2895. @item
  2896. An optional @dfn{type modifier character}. For example, you can
  2897. specify a type modifier of @samp{l} with integer conversions such as
  2898. @samp{%d} to specify that the argument is a pointer to a @code{long int}
  2899. rather than a pointer to an @code{int}.
  2900. @cindex type modifier character (@code{scanf})
  2901. @item
  2902. A character that specifies the conversion to be applied.
  2903. @end itemize
  2904. The exact options that are permitted and how they are interpreted vary
  2905. between the different conversion specifiers. See the descriptions of the
  2906. individual conversions for information about the particular options that
  2907. they allow.
  2908. With the @samp{-Wformat} option, the GNU C compiler checks calls to
  2909. @code{scanf} and related functions. It examines the format string and
  2910. verifies that the correct number and types of arguments are supplied.
  2911. There is also a GNU C syntax to tell the compiler that a function you
  2912. write uses a @code{scanf}-style format string.
  2913. @xref{Function Attributes, , Declaring Attributes of Functions,
  2914. gcc, Using GNU CC}, for more information.
  2915. @node Table of Input Conversions
  2916. @subsection Table of Input Conversions
  2917. @cindex input conversions, for @code{scanf}
  2918. Here is a table that summarizes the various conversion specifications:
  2919. @table @asis
  2920. @item @samp{%d}
  2921. Matches an optionally signed integer written in decimal. @xref{Numeric
  2922. Input Conversions}.
  2923. @item @samp{%i}
  2924. Matches an optionally signed integer in any of the formats that the C
  2925. language defines for specifying an integer constant. @xref{Numeric
  2926. Input Conversions}.
  2927. @item @samp{%o}
  2928. Matches an unsigned integer written in octal radix.
  2929. @xref{Numeric Input Conversions}.
  2930. @item @samp{%u}
  2931. Matches an unsigned integer written in decimal radix.
  2932. @xref{Numeric Input Conversions}.
  2933. @item @samp{%x}, @samp{%X}
  2934. Matches an unsigned integer written in hexadecimal radix.
  2935. @xref{Numeric Input Conversions}.
  2936. @item @samp{%e}, @samp{%f}, @samp{%g}, @samp{%E}, @samp{%G}
  2937. Matches an optionally signed floating-point number. @xref{Numeric Input
  2938. Conversions}.
  2939. @item @samp{%s}
  2940. Matches a string containing only non-whitespace characters.
  2941. @xref{String Input Conversions}. The presence of the @samp{l} modifier
  2942. determines whether the output is stored as a wide character string or a
  2943. multibyte string. If @samp{%s} is used in a wide character function the
  2944. string is converted as with multiple calls to @code{wcrtomb} into a
  2945. multibyte string. This means that the buffer must provide room for
  2946. @code{MB_CUR_MAX} bytes for each wide character read. In case
  2947. @samp{%ls} is used in a multibyte function the result is converted into
  2948. wide characters as with multiple calls of @code{mbrtowc} before being
  2949. stored in the user provided buffer.
  2950. @item @samp{%S}
  2951. This is an alias for @samp{%ls} which is supported for compatibility
  2952. with the Unix standard.
  2953. @item @samp{%[}
  2954. Matches a string of characters that belong to a specified set.
  2955. @xref{String Input Conversions}. The presence of the @samp{l} modifier
  2956. determines whether the output is stored as a wide character string or a
  2957. multibyte string. If @samp{%[} is used in a wide character function the
  2958. string is converted as with multiple calls to @code{wcrtomb} into a
  2959. multibyte string. This means that the buffer must provide room for
  2960. @code{MB_CUR_MAX} bytes for each wide character read. In case
  2961. @samp{%l[} is used in a multibyte function the result is converted into
  2962. wide characters as with multiple calls of @code{mbrtowc} before being
  2963. stored in the user provided buffer.
  2964. @item @samp{%c}
  2965. Matches a string of one or more characters; the number of characters
  2966. read is controlled by the maximum field width given for the conversion.
  2967. @xref{String Input Conversions}.
  2968. If @samp{%c} is used in a wide stream function the read value is
  2969. converted from a wide character to the corresponding multibyte character
  2970. before storing it. Note that this conversion can produce more than one
  2971. byte of output and therefore the provided buffer must be large enough for up
  2972. to @code{MB_CUR_MAX} bytes for each character. If @samp{%lc} is used in
  2973. a multibyte function the input is treated as a multibyte sequence (and
  2974. not bytes) and the result is converted as with calls to @code{mbrtowc}.
  2975. @item @samp{%C}
  2976. This is an alias for @samp{%lc} which is supported for compatibility
  2977. with the Unix standard.
  2978. @item @samp{%p}
  2979. Matches a pointer value in the same implementation-defined format used
  2980. by the @samp{%p} output conversion for @code{printf}. @xref{Other Input
  2981. Conversions}.
  2982. @item @samp{%n}
  2983. This conversion doesn't read any characters; it records the number of
  2984. characters read so far by this call. @xref{Other Input Conversions}.
  2985. @item @samp{%%}
  2986. This matches a literal @samp{%} character in the input stream. No
  2987. corresponding argument is used. @xref{Other Input Conversions}.
  2988. @end table
  2989. If the syntax of a conversion specification is invalid, the behavior is
  2990. undefined. If there aren't enough function arguments provided to supply
  2991. addresses for all the conversion specifications in the template strings
  2992. that perform assignments, or if the arguments are not of the correct
  2993. types, the behavior is also undefined. On the other hand, extra
  2994. arguments are simply ignored.
  2995. @node Numeric Input Conversions
  2996. @subsection Numeric Input Conversions
  2997. This section describes the @code{scanf} conversions for reading numeric
  2998. values.
  2999. The @samp{%d} conversion matches an optionally signed integer in decimal
  3000. radix. The syntax that is recognized is the same as that for the
  3001. @code{strtol} function (@pxref{Parsing of Integers}) with the value
  3002. @code{10} for the @var{base} argument.
  3003. The @samp{%i} conversion matches an optionally signed integer in any of
  3004. the formats that the C language defines for specifying an integer
  3005. constant. The syntax that is recognized is the same as that for the
  3006. @code{strtol} function (@pxref{Parsing of Integers}) with the value
  3007. @code{0} for the @var{base} argument. (You can print integers in this
  3008. syntax with @code{printf} by using the @samp{#} flag character with the
  3009. @samp{%x}, @samp{%o}, or @samp{%d} conversion. @xref{Integer Conversions}.)
  3010. For example, any of the strings @samp{10}, @samp{0xa}, or @samp{012}
  3011. could be read in as integers under the @samp{%i} conversion. Each of
  3012. these specifies a number with decimal value @code{10}.
  3013. The @samp{%o}, @samp{%u}, and @samp{%x} conversions match unsigned
  3014. integers in octal, decimal, and hexadecimal radices, respectively. The
  3015. syntax that is recognized is the same as that for the @code{strtoul}
  3016. function (@pxref{Parsing of Integers}) with the appropriate value
  3017. (@code{8}, @code{10}, or @code{16}) for the @var{base} argument.
  3018. The @samp{%X} conversion is identical to the @samp{%x} conversion. They
  3019. both permit either uppercase or lowercase letters to be used as digits.
  3020. The default type of the corresponding argument for the @code{%d} and
  3021. @code{%i} conversions is @code{int *}, and @code{unsigned int *} for the
  3022. other integer conversions. You can use the following type modifiers to
  3023. specify other sizes of integer:
  3024. @table @samp
  3025. @item hh
  3026. Specifies that the argument is a @code{signed char *} or @code{unsigned
  3027. char *}.
  3028. This modifier was introduced in @w{ISO C99}.
  3029. @item h
  3030. Specifies that the argument is a @code{short int *} or @code{unsigned
  3031. short int *}.
  3032. @item j
  3033. Specifies that the argument is a @code{intmax_t *} or @code{uintmax_t *}.
  3034. This modifier was introduced in @w{ISO C99}.
  3035. @item l
  3036. Specifies that the argument is a @code{long int *} or @code{unsigned
  3037. long int *}. Two @samp{l} characters is like the @samp{L} modifier, below.
  3038. If used with @samp{%c} or @samp{%s} the corresponding parameter is
  3039. considered as a pointer to a wide character or wide character string
  3040. respectively. This use of @samp{l} was introduced in @w{Amendment 1} to
  3041. @w{ISO C90}.
  3042. @need 100
  3043. @item ll
  3044. @itemx L
  3045. @itemx q
  3046. Specifies that the argument is a @code{long long int *} or @code{unsigned long long int *}. (The @code{long long} type is an extension supported by the
  3047. GNU C compiler. For systems that don't provide extra-long integers, this
  3048. is the same as @code{long int}.)
  3049. The @samp{q} modifier is another name for the same thing, which comes
  3050. from 4.4 BSD; a @w{@code{long long int}} is sometimes called a ``quad''
  3051. @code{int}.
  3052. @item t
  3053. Specifies that the argument is a @code{ptrdiff_t *}.
  3054. This modifier was introduced in @w{ISO C99}.
  3055. @item z
  3056. Specifies that the argument is a @code{size_t *}.
  3057. This modifier was introduced in @w{ISO C99}.
  3058. @end table
  3059. All of the @samp{%e}, @samp{%f}, @samp{%g}, @samp{%E}, and @samp{%G}
  3060. input conversions are interchangeable. They all match an optionally
  3061. signed floating point number, in the same syntax as for the
  3062. @code{strtod} function (@pxref{Parsing of Floats}).
  3063. For the floating-point input conversions, the default argument type is
  3064. @code{float *}. (This is different from the corresponding output
  3065. conversions, where the default type is @code{double}; remember that
  3066. @code{float} arguments to @code{printf} are converted to @code{double}
  3067. by the default argument promotions, but @code{float *} arguments are
  3068. not promoted to @code{double *}.) You can specify other sizes of float
  3069. using these type modifiers:
  3070. @table @samp
  3071. @item l
  3072. Specifies that the argument is of type @code{double *}.
  3073. @item L
  3074. Specifies that the argument is of type @code{long double *}.
  3075. @end table
  3076. For all the above number parsing formats there is an additional optional
  3077. flag @samp{'}. When this flag is given the @code{scanf} function
  3078. expects the number represented in the input string to be formatted
  3079. according to the grouping rules of the currently selected locale
  3080. (@pxref{General Numeric}).
  3081. If the @code{"C"} or @code{"POSIX"} locale is selected there is no
  3082. difference. But for a locale which specifies values for the appropriate
  3083. fields in the locale the input must have the correct form in the input.
  3084. Otherwise the longest prefix with a correct form is processed.
  3085. @node String Input Conversions
  3086. @subsection String Input Conversions
  3087. This section describes the @code{scanf} input conversions for reading
  3088. string and character values: @samp{%s}, @samp{%S}, @samp{%[}, @samp{%c},
  3089. and @samp{%C}.
  3090. You have two options for how to receive the input from these
  3091. conversions:
  3092. @itemize @bullet
  3093. @item
  3094. Provide a buffer to store it in. This is the default. You should
  3095. provide an argument of type @code{char *} or @code{wchar_t *} (the
  3096. latter if the @samp{l} modifier is present).
  3097. @strong{Warning:} To make a robust program, you must make sure that the
  3098. input (plus its terminating null) cannot possibly exceed the size of the
  3099. buffer you provide. In general, the only way to do this is to specify a
  3100. maximum field width one less than the buffer size. @strong{If you
  3101. provide the buffer, always specify a maximum field width to prevent
  3102. overflow.}
  3103. @item
  3104. Ask @code{scanf} to allocate a big enough buffer, by specifying the
  3105. @samp{a} flag character. This is a GNU extension. You should provide
  3106. an argument of type @code{char **} for the buffer address to be stored
  3107. in. @xref{Dynamic String Input}.
  3108. @end itemize
  3109. The @samp{%c} conversion is the simplest: it matches a fixed number of
  3110. characters, always. The maximum field width says how many characters to
  3111. read; if you don't specify the maximum, the default is 1. This
  3112. conversion doesn't append a null character to the end of the text it
  3113. reads. It also does not skip over initial whitespace characters. It
  3114. reads precisely the next @var{n} characters, and fails if it cannot get
  3115. that many. Since there is always a maximum field width with @samp{%c}
  3116. (whether specified, or 1 by default), you can always prevent overflow by
  3117. making the buffer long enough.
  3118. @comment Is character == byte here??? --drepper
  3119. If the format is @samp{%lc} or @samp{%C} the function stores wide
  3120. characters which are converted using the conversion determined at the
  3121. time the stream was opened from the external byte stream. The number of
  3122. bytes read from the medium is limited by @code{MB_CUR_LEN * @var{n}} but
  3123. at most @var{n} wide characters get stored in the output string.
  3124. The @samp{%s} conversion matches a string of non-whitespace characters.
  3125. It skips and discards initial whitespace, but stops when it encounters
  3126. more whitespace after having read something. It stores a null character
  3127. at the end of the text that it reads.
  3128. For example, reading the input:
  3129. @smallexample
  3130. hello, world
  3131. @end smallexample
  3132. @noindent
  3133. with the conversion @samp{%10c} produces @code{" hello, wo"}, but
  3134. reading the same input with the conversion @samp{%10s} produces
  3135. @code{"hello,"}.
  3136. @strong{Warning:} If you do not specify a field width for @samp{%s},
  3137. then the number of characters read is limited only by where the next
  3138. whitespace character appears. This almost certainly means that invalid
  3139. input can make your program crash---which is a bug.
  3140. The @samp{%ls} and @samp{%S} format are handled just like @samp{%s}
  3141. except that the external byte sequence is converted using the conversion
  3142. associated with the stream to wide characters with their own encoding.
  3143. A width or precision specified with the format do not directly determine
  3144. how many bytes are read from the stream since they measure wide
  3145. characters. But an upper limit can be computed by multiplying the value
  3146. of the width or precision by @code{MB_CUR_MAX}.
  3147. To read in characters that belong to an arbitrary set of your choice,
  3148. use the @samp{%[} conversion. You specify the set between the @samp{[}
  3149. character and a following @samp{]} character, using the same syntax used
  3150. in regular expressions for explicit sets of characters. As special cases:
  3151. @itemize @bullet
  3152. @item
  3153. A literal @samp{]} character can be specified as the first character
  3154. of the set.
  3155. @item
  3156. An embedded @samp{-} character (that is, one that is not the first or
  3157. last character of the set) is used to specify a range of characters.
  3158. @item
  3159. If a caret character @samp{^} immediately follows the initial @samp{[},
  3160. then the set of allowed input characters is everything @emph{except}
  3161. the characters listed.
  3162. @end itemize
  3163. The @samp{%[} conversion does not skip over initial whitespace
  3164. characters.
  3165. Note that the @dfn{character class} syntax available in character sets
  3166. that appear inside regular expressions (such as @samp{[:alpha:]}) is
  3167. @emph{not} available in the @samp{%[} conversion.
  3168. Here are some examples of @samp{%[} conversions and what they mean:
  3169. @table @samp
  3170. @item %25[1234567890]
  3171. Matches a string of up to 25 digits.
  3172. @item %25[][]
  3173. Matches a string of up to 25 square brackets.
  3174. @item %25[^ \f\n\r\t\v]
  3175. Matches a string up to 25 characters long that doesn't contain any of
  3176. the standard whitespace characters. This is slightly different from
  3177. @samp{%s}, because if the input begins with a whitespace character,
  3178. @samp{%[} reports a matching failure while @samp{%s} simply discards the
  3179. initial whitespace.
  3180. @item %25[a-z]
  3181. Matches up to 25 lowercase characters.
  3182. @end table
  3183. As for @samp{%c} and @samp{%s} the @samp{%[} format is also modified to
  3184. produce wide characters if the @samp{l} modifier is present. All what
  3185. is said about @samp{%ls} above is true for @samp{%l[}.
  3186. One more reminder: the @samp{%s} and @samp{%[} conversions are
  3187. @strong{dangerous} if you don't specify a maximum width or use the
  3188. @samp{a} flag, because input too long would overflow whatever buffer you
  3189. have provided for it. No matter how long your buffer is, a user could
  3190. supply input that is longer. A well-written program reports invalid
  3191. input with a comprehensible error message, not with a crash.
  3192. @node Dynamic String Input
  3193. @subsection Dynamically Allocating String Conversions
  3194. A GNU extension to formatted input lets you safely read a string with no
  3195. maximum size. Using this feature, you don't supply a buffer; instead,
  3196. @code{scanf} allocates a buffer big enough to hold the data and gives
  3197. you its address. To use this feature, write @samp{a} as a flag
  3198. character, as in @samp{%as} or @samp{%a[0-9a-z]}.
  3199. The pointer argument you supply for where to store the input should have
  3200. type @code{char **}. The @code{scanf} function allocates a buffer and
  3201. stores its address in the word that the argument points to. You should
  3202. free the buffer with @code{free} when you no longer need it.
  3203. Here is an example of using the @samp{a} flag with the @samp{%[@dots{}]}
  3204. conversion specification to read a ``variable assignment'' of the form
  3205. @samp{@var{variable} = @var{value}}.
  3206. @smallexample
  3207. @{
  3208. char *variable, *value;
  3209. if (2 > scanf ("%a[a-zA-Z0-9] = %a[^\n]\n",
  3210. &variable, &value))
  3211. @{
  3212. invalid_input_error ();
  3213. return 0;
  3214. @}
  3215. @dots{}
  3216. @}
  3217. @end smallexample
  3218. @node Other Input Conversions
  3219. @subsection Other Input Conversions
  3220. This section describes the miscellaneous input conversions.
  3221. The @samp{%p} conversion is used to read a pointer value. It recognizes
  3222. the same syntax used by the @samp{%p} output conversion for
  3223. @code{printf} (@pxref{Other Output Conversions}); that is, a hexadecimal
  3224. number just as the @samp{%x} conversion accepts. The corresponding
  3225. argument should be of type @code{void **}; that is, the address of a
  3226. place to store a pointer.
  3227. The resulting pointer value is not guaranteed to be valid if it was not
  3228. originally written during the same program execution that reads it in.
  3229. The @samp{%n} conversion produces the number of characters read so far
  3230. by this call. The corresponding argument should be of type @code{int *}.
  3231. This conversion works in the same way as the @samp{%n} conversion for
  3232. @code{printf}; see @ref{Other Output Conversions}, for an example.
  3233. The @samp{%n} conversion is the only mechanism for determining the
  3234. success of literal matches or conversions with suppressed assignments.
  3235. If the @samp{%n} follows the locus of a matching failure, then no value
  3236. is stored for it since @code{scanf} returns before processing the
  3237. @samp{%n}. If you store @code{-1} in that argument slot before calling
  3238. @code{scanf}, the presence of @code{-1} after @code{scanf} indicates an
  3239. error occurred before the @samp{%n} was reached.
  3240. Finally, the @samp{%%} conversion matches a literal @samp{%} character
  3241. in the input stream, without using an argument. This conversion does
  3242. not permit any flags, field width, or type modifier to be specified.
  3243. @node Formatted Input Functions
  3244. @subsection Formatted Input Functions
  3245. Here are the descriptions of the functions for performing formatted
  3246. input.
  3247. Prototypes for these functions are in the header file @file{stdio.h}.
  3248. @pindex stdio.h
  3249. @deftypefun int scanf (const char *@var{template}, @dots{})
  3250. @standards{ISO, stdio.h}
  3251. @safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{}}@acunsafe{@acsmem{} @aculock{} @acucorrupt{}}}
  3252. The @code{scanf} function reads formatted input from the stream
  3253. @code{stdin} under the control of the template string @var{template}.
  3254. The optional arguments are pointers to the places which receive the
  3255. resulting values.
  3256. The return value is normally the number of successful assignments. If
  3257. an end-of-file condition is detected before any matches are performed,
  3258. including matches against whitespace and literal characters in the
  3259. template, then @code{EOF} is returned.
  3260. @end deftypefun
  3261. @deftypefun int wscanf (const wchar_t *@var{template}, @dots{})
  3262. @standards{ISO, wchar.h}
  3263. @safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{}}@acunsafe{@acsmem{} @aculock{} @acucorrupt{}}}
  3264. The @code{wscanf} function reads formatted input from the stream
  3265. @code{stdin} under the control of the template string @var{template}.
  3266. The optional arguments are pointers to the places which receive the
  3267. resulting values.
  3268. The return value is normally the number of successful assignments. If
  3269. an end-of-file condition is detected before any matches are performed,
  3270. including matches against whitespace and literal characters in the
  3271. template, then @code{WEOF} is returned.
  3272. @end deftypefun
  3273. @deftypefun int fscanf (FILE *@var{stream}, const char *@var{template}, @dots{})
  3274. @standards{ISO, stdio.h}
  3275. @safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{}}@acunsafe{@acsmem{} @aculock{} @acucorrupt{}}}
  3276. This function is just like @code{scanf}, except that the input is read
  3277. from the stream @var{stream} instead of @code{stdin}.
  3278. @end deftypefun
  3279. @deftypefun int fwscanf (FILE *@var{stream}, const wchar_t *@var{template}, @dots{})
  3280. @standards{ISO, wchar.h}
  3281. @safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{}}@acunsafe{@acsmem{} @aculock{} @acucorrupt{}}}
  3282. This function is just like @code{wscanf}, except that the input is read
  3283. from the stream @var{stream} instead of @code{stdin}.
  3284. @end deftypefun
  3285. @deftypefun int sscanf (const char *@var{s}, const char *@var{template}, @dots{})
  3286. @standards{ISO, stdio.h}
  3287. @safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
  3288. This is like @code{scanf}, except that the characters are taken from the
  3289. null-terminated string @var{s} instead of from a stream. Reaching the
  3290. end of the string is treated as an end-of-file condition.
  3291. The behavior of this function is undefined if copying takes place
  3292. between objects that overlap---for example, if @var{s} is also given
  3293. as an argument to receive a string read under control of the @samp{%s},
  3294. @samp{%S}, or @samp{%[} conversion.
  3295. @end deftypefun
  3296. @deftypefun int swscanf (const wchar_t *@var{ws}, const wchar_t *@var{template}, @dots{})
  3297. @standards{ISO, wchar.h}
  3298. @safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
  3299. This is like @code{wscanf}, except that the characters are taken from the
  3300. null-terminated string @var{ws} instead of from a stream. Reaching the
  3301. end of the string is treated as an end-of-file condition.
  3302. The behavior of this function is undefined if copying takes place
  3303. between objects that overlap---for example, if @var{ws} is also given as
  3304. an argument to receive a string read under control of the @samp{%s},
  3305. @samp{%S}, or @samp{%[} conversion.
  3306. @end deftypefun
  3307. @node Variable Arguments Input
  3308. @subsection Variable Arguments Input Functions
  3309. The functions @code{vscanf} and friends are provided so that you can
  3310. define your own variadic @code{scanf}-like functions that make use of
  3311. the same internals as the built-in formatted output functions.
  3312. These functions are analogous to the @code{vprintf} series of output
  3313. functions. @xref{Variable Arguments Output}, for important
  3314. information on how to use them.
  3315. @strong{Portability Note:} The functions listed in this section were
  3316. introduced in @w{ISO C99} and were before available as GNU extensions.
  3317. @deftypefun int vscanf (const char *@var{template}, va_list @var{ap})
  3318. @standards{ISO, stdio.h}
  3319. @safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{}}@acunsafe{@acsmem{} @aculock{} @acucorrupt{}}}
  3320. This function is similar to @code{scanf}, but instead of taking
  3321. a variable number of arguments directly, it takes an argument list
  3322. pointer @var{ap} of type @code{va_list} (@pxref{Variadic Functions}).
  3323. @end deftypefun
  3324. @deftypefun int vwscanf (const wchar_t *@var{template}, va_list @var{ap})
  3325. @standards{ISO, wchar.h}
  3326. @safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{}}@acunsafe{@acsmem{} @aculock{} @acucorrupt{}}}
  3327. This function is similar to @code{wscanf}, but instead of taking
  3328. a variable number of arguments directly, it takes an argument list
  3329. pointer @var{ap} of type @code{va_list} (@pxref{Variadic Functions}).
  3330. @end deftypefun
  3331. @deftypefun int vfscanf (FILE *@var{stream}, const char *@var{template}, va_list @var{ap})
  3332. @standards{ISO, stdio.h}
  3333. @safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{}}@acunsafe{@acsmem{} @aculock{} @acucorrupt{}}}
  3334. This is the equivalent of @code{fscanf} with the variable argument list
  3335. specified directly as for @code{vscanf}.
  3336. @end deftypefun
  3337. @deftypefun int vfwscanf (FILE *@var{stream}, const wchar_t *@var{template}, va_list @var{ap})
  3338. @standards{ISO, wchar.h}
  3339. @safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{}}@acunsafe{@acsmem{} @aculock{} @acucorrupt{}}}
  3340. This is the equivalent of @code{fwscanf} with the variable argument list
  3341. specified directly as for @code{vwscanf}.
  3342. @end deftypefun
  3343. @deftypefun int vsscanf (const char *@var{s}, const char *@var{template}, va_list @var{ap})
  3344. @standards{ISO, stdio.h}
  3345. @safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
  3346. This is the equivalent of @code{sscanf} with the variable argument list
  3347. specified directly as for @code{vscanf}.
  3348. @end deftypefun
  3349. @deftypefun int vswscanf (const wchar_t *@var{s}, const wchar_t *@var{template}, va_list @var{ap})
  3350. @standards{ISO, wchar.h}
  3351. @safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
  3352. This is the equivalent of @code{swscanf} with the variable argument list
  3353. specified directly as for @code{vwscanf}.
  3354. @end deftypefun
  3355. In GNU C, there is a special construct you can use to let the compiler
  3356. know that a function uses a @code{scanf}-style format string. Then it
  3357. can check the number and types of arguments in each call to the
  3358. function, and warn you when they do not match the format string.
  3359. For details, see @ref{Function Attributes, , Declaring Attributes of Functions,
  3360. gcc, Using GNU CC}.
  3361. @node EOF and Errors
  3362. @section End-Of-File and Errors
  3363. @cindex end of file, on a stream
  3364. Many of the functions described in this chapter return the value of the
  3365. macro @code{EOF} to indicate unsuccessful completion of the operation.
  3366. Since @code{EOF} is used to report both end of file and random errors,
  3367. it's often better to use the @code{feof} function to check explicitly
  3368. for end of file and @code{ferror} to check for errors. These functions
  3369. check indicators that are part of the internal state of the stream
  3370. object, indicators set if the appropriate condition was detected by a
  3371. previous I/O operation on that stream.
  3372. @deftypevr Macro int EOF
  3373. @standards{ISO, stdio.h}
  3374. This macro is an integer value that is returned by a number of narrow
  3375. stream functions to indicate an end-of-file condition, or some other
  3376. error situation. With @theglibc{}, @code{EOF} is @code{-1}. In
  3377. other libraries, its value may be some other negative number.
  3378. This symbol is declared in @file{stdio.h}.
  3379. @end deftypevr
  3380. @deftypevr Macro int WEOF
  3381. @standards{ISO, wchar.h}
  3382. This macro is an integer value that is returned by a number of wide
  3383. stream functions to indicate an end-of-file condition, or some other
  3384. error situation. With @theglibc{}, @code{WEOF} is @code{-1}. In
  3385. other libraries, its value may be some other negative number.
  3386. This symbol is declared in @file{wchar.h}.
  3387. @end deftypevr
  3388. @deftypefun int feof (FILE *@var{stream})
  3389. @standards{ISO, stdio.h}
  3390. @safety{@prelim{}@mtsafe{}@assafe{}@acunsafe{@aculock{}}}
  3391. The @code{feof} function returns nonzero if and only if the end-of-file
  3392. indicator for the stream @var{stream} is set.
  3393. This symbol is declared in @file{stdio.h}.
  3394. @end deftypefun
  3395. @deftypefun int feof_unlocked (FILE *@var{stream})
  3396. @standards{GNU, stdio.h}
  3397. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  3398. @c There isn't much of a thread unsafety risk in reading a flag word and
  3399. @c testing a bit in it.
  3400. The @code{feof_unlocked} function is equivalent to the @code{feof}
  3401. function except that it does not implicitly lock the stream.
  3402. This function is a GNU extension.
  3403. This symbol is declared in @file{stdio.h}.
  3404. @end deftypefun
  3405. @deftypefun int ferror (FILE *@var{stream})
  3406. @standards{ISO, stdio.h}
  3407. @safety{@prelim{}@mtsafe{}@assafe{}@acunsafe{@aculock{}}}
  3408. The @code{ferror} function returns nonzero if and only if the error
  3409. indicator for the stream @var{stream} is set, indicating that an error
  3410. has occurred on a previous operation on the stream.
  3411. This symbol is declared in @file{stdio.h}.
  3412. @end deftypefun
  3413. @deftypefun int ferror_unlocked (FILE *@var{stream})
  3414. @standards{GNU, stdio.h}
  3415. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  3416. The @code{ferror_unlocked} function is equivalent to the @code{ferror}
  3417. function except that it does not implicitly lock the stream.
  3418. This function is a GNU extension.
  3419. This symbol is declared in @file{stdio.h}.
  3420. @end deftypefun
  3421. In addition to setting the error indicator associated with the stream,
  3422. the functions that operate on streams also set @code{errno} in the same
  3423. way as the corresponding low-level functions that operate on file
  3424. descriptors. For example, all of the functions that perform output to a
  3425. stream---such as @code{fputc}, @code{printf}, and @code{fflush}---are
  3426. implemented in terms of @code{write}, and all of the @code{errno} error
  3427. conditions defined for @code{write} are meaningful for these functions.
  3428. For more information about the descriptor-level I/O functions, see
  3429. @ref{Low-Level I/O}.
  3430. @node Error Recovery
  3431. @section Recovering from errors
  3432. You may explicitly clear the error and EOF flags with the @code{clearerr}
  3433. function.
  3434. @deftypefun void clearerr (FILE *@var{stream})
  3435. @standards{ISO, stdio.h}
  3436. @safety{@prelim{}@mtsafe{}@assafe{}@acunsafe{@aculock{}}}
  3437. This function clears the end-of-file and error indicators for the
  3438. stream @var{stream}.
  3439. The file positioning functions (@pxref{File Positioning}) also clear the
  3440. end-of-file indicator for the stream.
  3441. @end deftypefun
  3442. @deftypefun void clearerr_unlocked (FILE *@var{stream})
  3443. @standards{GNU, stdio.h}
  3444. @safety{@prelim{}@mtsafe{@mtsrace{:stream}}@assafe{}@acsafe{}}
  3445. The @code{clearerr_unlocked} function is equivalent to the @code{clearerr}
  3446. function except that it does not implicitly lock the stream.
  3447. This function is a GNU extension.
  3448. @end deftypefun
  3449. Note that it is @emph{not} correct to just clear the error flag and retry
  3450. a failed stream operation. After a failed write, any number of
  3451. characters since the last buffer flush may have been committed to the
  3452. file, while some buffered data may have been discarded. Merely retrying
  3453. can thus cause lost or repeated data.
  3454. A failed read may leave the file pointer in an inappropriate position for
  3455. a second try. In both cases, you should seek to a known position before
  3456. retrying.
  3457. Most errors that can happen are not recoverable --- a second try will
  3458. always fail again in the same way. So usually it is best to give up and
  3459. report the error to the user, rather than install complicated recovery
  3460. logic.
  3461. One important exception is @code{EINTR} (@pxref{Interrupted Primitives}).
  3462. Many stream I/O implementations will treat it as an ordinary error, which
  3463. can be quite inconvenient. You can avoid this hassle by installing all
  3464. signals with the @code{SA_RESTART} flag.
  3465. For similar reasons, setting nonblocking I/O on a stream's file
  3466. descriptor is not usually advisable.
  3467. @node Binary Streams
  3468. @section Text and Binary Streams
  3469. @gnusystems{} and other POSIX-compatible operating systems organize all
  3470. files as uniform sequences of characters. However, some other systems
  3471. make a distinction between files containing text and files containing
  3472. binary data, and the input and output facilities of @w{ISO C} provide for
  3473. this distinction. This section tells you how to write programs portable
  3474. to such systems.
  3475. @cindex text stream
  3476. @cindex binary stream
  3477. When you open a stream, you can specify either a @dfn{text stream} or a
  3478. @dfn{binary stream}. You indicate that you want a binary stream by
  3479. specifying the @samp{b} modifier in the @var{opentype} argument to
  3480. @code{fopen}; see @ref{Opening Streams}. Without this
  3481. option, @code{fopen} opens the file as a text stream.
  3482. Text and binary streams differ in several ways:
  3483. @itemize @bullet
  3484. @item
  3485. The data read from a text stream is divided into @dfn{lines} which are
  3486. terminated by newline (@code{'\n'}) characters, while a binary stream is
  3487. simply a long series of characters. A text stream might on some systems
  3488. fail to handle lines more than 254 characters long (including the
  3489. terminating newline character).
  3490. @cindex lines (in a text file)
  3491. @item
  3492. On some systems, text files can contain only printing characters,
  3493. horizontal tab characters, and newlines, and so text streams may not
  3494. support other characters. However, binary streams can handle any
  3495. character value.
  3496. @item
  3497. Space characters that are written immediately preceding a newline
  3498. character in a text stream may disappear when the file is read in again.
  3499. @item
  3500. More generally, there need not be a one-to-one mapping between
  3501. characters that are read from or written to a text stream, and the
  3502. characters in the actual file.
  3503. @end itemize
  3504. Since a binary stream is always more capable and more predictable than a
  3505. text stream, you might wonder what purpose text streams serve. Why not
  3506. simply always use binary streams? The answer is that on these operating
  3507. systems, text and binary streams use different file formats, and the
  3508. only way to read or write ``an ordinary file of text'' that can work
  3509. with other text-oriented programs is through a text stream.
  3510. In @theglibc{}, and on all POSIX systems, there is no difference
  3511. between text streams and binary streams. When you open a stream, you
  3512. get the same kind of stream regardless of whether you ask for binary.
  3513. This stream can handle any file content, and has none of the
  3514. restrictions that text streams sometimes have.
  3515. @node File Positioning
  3516. @section File Positioning
  3517. @cindex file positioning on a stream
  3518. @cindex positioning a stream
  3519. @cindex seeking on a stream
  3520. The @dfn{file position} of a stream describes where in the file the
  3521. stream is currently reading or writing. I/O on the stream advances the
  3522. file position through the file. On @gnusystems{}, the file position is
  3523. represented as an integer, which counts the number of bytes from the
  3524. beginning of the file. @xref{File Position}.
  3525. During I/O to an ordinary disk file, you can change the file position
  3526. whenever you wish, so as to read or write any portion of the file. Some
  3527. other kinds of files may also permit this. Files which support changing
  3528. the file position are sometimes referred to as @dfn{random-access}
  3529. files.
  3530. You can use the functions in this section to examine or modify the file
  3531. position indicator associated with a stream. The symbols listed below
  3532. are declared in the header file @file{stdio.h}.
  3533. @pindex stdio.h
  3534. @deftypefun {long int} ftell (FILE *@var{stream})
  3535. @standards{ISO, stdio.h}
  3536. @safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{} @acucorrupt{}}}
  3537. This function returns the current file position of the stream
  3538. @var{stream}.
  3539. This function can fail if the stream doesn't support file positioning,
  3540. or if the file position can't be represented in a @code{long int}, and
  3541. possibly for other reasons as well. If a failure occurs, a value of
  3542. @code{-1} is returned.
  3543. @end deftypefun
  3544. @deftypefun off_t ftello (FILE *@var{stream})
  3545. @standards{Unix98, stdio.h}
  3546. @safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{} @acucorrupt{}}}
  3547. The @code{ftello} function is similar to @code{ftell}, except that it
  3548. returns a value of type @code{off_t}. Systems which support this type
  3549. use it to describe all file positions, unlike the POSIX specification
  3550. which uses a long int. The two are not necessarily the same size.
  3551. Therefore, using ftell can lead to problems if the implementation is
  3552. written on top of a POSIX compliant low-level I/O implementation, and using
  3553. @code{ftello} is preferable whenever it is available.
  3554. If this function fails it returns @code{(off_t) -1}. This can happen due
  3555. to missing support for file positioning or internal errors. Otherwise
  3556. the return value is the current file position.
  3557. The function is an extension defined in the Unix Single Specification
  3558. version 2.
  3559. When the sources are compiled with @code{_FILE_OFFSET_BITS == 64} on a
  3560. 32 bit system this function is in fact @code{ftello64}. I.e., the
  3561. LFS interface transparently replaces the old interface.
  3562. @end deftypefun
  3563. @deftypefun off64_t ftello64 (FILE *@var{stream})
  3564. @standards{Unix98, stdio.h}
  3565. @safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{} @acucorrupt{}}}
  3566. This function is similar to @code{ftello} with the only difference that
  3567. the return value is of type @code{off64_t}. This also requires that the
  3568. stream @var{stream} was opened using either @code{fopen64},
  3569. @code{freopen64}, or @code{tmpfile64} since otherwise the underlying
  3570. file operations to position the file pointer beyond the @twoexp{31}
  3571. bytes limit might fail.
  3572. If the sources are compiled with @code{_FILE_OFFSET_BITS == 64} on a 32
  3573. bits machine this function is available under the name @code{ftello}
  3574. and so transparently replaces the old interface.
  3575. @end deftypefun
  3576. @deftypefun int fseek (FILE *@var{stream}, long int @var{offset}, int @var{whence})
  3577. @standards{ISO, stdio.h}
  3578. @safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{} @acucorrupt{}}}
  3579. The @code{fseek} function is used to change the file position of the
  3580. stream @var{stream}. The value of @var{whence} must be one of the
  3581. constants @code{SEEK_SET}, @code{SEEK_CUR}, or @code{SEEK_END}, to
  3582. indicate whether the @var{offset} is relative to the beginning of the
  3583. file, the current file position, or the end of the file, respectively.
  3584. This function returns a value of zero if the operation was successful,
  3585. and a nonzero value to indicate failure. A successful call also clears
  3586. the end-of-file indicator of @var{stream} and discards any characters
  3587. that were ``pushed back'' by the use of @code{ungetc}.
  3588. @code{fseek} either flushes any buffered output before setting the file
  3589. position or else remembers it so it will be written later in its proper
  3590. place in the file.
  3591. @end deftypefun
  3592. @deftypefun int fseeko (FILE *@var{stream}, off_t @var{offset}, int @var{whence})
  3593. @standards{Unix98, stdio.h}
  3594. @safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{} @acucorrupt{}}}
  3595. This function is similar to @code{fseek} but it corrects a problem with
  3596. @code{fseek} in a system with POSIX types. Using a value of type
  3597. @code{long int} for the offset is not compatible with POSIX.
  3598. @code{fseeko} uses the correct type @code{off_t} for the @var{offset}
  3599. parameter.
  3600. For this reason it is a good idea to prefer @code{ftello} whenever it is
  3601. available since its functionality is (if different at all) closer the
  3602. underlying definition.
  3603. The functionality and return value are the same as for @code{fseek}.
  3604. The function is an extension defined in the Unix Single Specification
  3605. version 2.
  3606. When the sources are compiled with @code{_FILE_OFFSET_BITS == 64} on a
  3607. 32 bit system this function is in fact @code{fseeko64}. I.e., the
  3608. LFS interface transparently replaces the old interface.
  3609. @end deftypefun
  3610. @deftypefun int fseeko64 (FILE *@var{stream}, off64_t @var{offset}, int @var{whence})
  3611. @standards{Unix98, stdio.h}
  3612. @safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{} @acucorrupt{}}}
  3613. This function is similar to @code{fseeko} with the only difference that
  3614. the @var{offset} parameter is of type @code{off64_t}. This also
  3615. requires that the stream @var{stream} was opened using either
  3616. @code{fopen64}, @code{freopen64}, or @code{tmpfile64} since otherwise
  3617. the underlying file operations to position the file pointer beyond the
  3618. @twoexp{31} bytes limit might fail.
  3619. If the sources are compiled with @code{_FILE_OFFSET_BITS == 64} on a 32
  3620. bits machine this function is available under the name @code{fseeko}
  3621. and so transparently replaces the old interface.
  3622. @end deftypefun
  3623. @strong{Portability Note:} In non-POSIX systems, @code{ftell},
  3624. @code{ftello}, @code{fseek} and @code{fseeko} might work reliably only
  3625. on binary streams. @xref{Binary Streams}.
  3626. The following symbolic constants are defined for use as the @var{whence}
  3627. argument to @code{fseek}. They are also used with the @code{lseek}
  3628. function (@pxref{I/O Primitives}) and to specify offsets for file locks
  3629. (@pxref{Control Operations}).
  3630. @deftypevr Macro int SEEK_SET
  3631. @standards{ISO, stdio.h}
  3632. This is an integer constant which, when used as the @var{whence}
  3633. argument to the @code{fseek} or @code{fseeko} functions, specifies that
  3634. the offset provided is relative to the beginning of the file.
  3635. @end deftypevr
  3636. @deftypevr Macro int SEEK_CUR
  3637. @standards{ISO, stdio.h}
  3638. This is an integer constant which, when used as the @var{whence}
  3639. argument to the @code{fseek} or @code{fseeko} functions, specifies that
  3640. the offset provided is relative to the current file position.
  3641. @end deftypevr
  3642. @deftypevr Macro int SEEK_END
  3643. @standards{ISO, stdio.h}
  3644. This is an integer constant which, when used as the @var{whence}
  3645. argument to the @code{fseek} or @code{fseeko} functions, specifies that
  3646. the offset provided is relative to the end of the file.
  3647. @end deftypevr
  3648. @deftypefun void rewind (FILE *@var{stream})
  3649. @standards{ISO, stdio.h}
  3650. @safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{} @acucorrupt{}}}
  3651. The @code{rewind} function positions the stream @var{stream} at the
  3652. beginning of the file. It is equivalent to calling @code{fseek} or
  3653. @code{fseeko} on the @var{stream} with an @var{offset} argument of
  3654. @code{0L} and a @var{whence} argument of @code{SEEK_SET}, except that
  3655. the return value is discarded and the error indicator for the stream is
  3656. reset.
  3657. @end deftypefun
  3658. These three aliases for the @samp{SEEK_@dots{}} constants exist for the
  3659. sake of compatibility with older BSD systems. They are defined in two
  3660. different header files: @file{fcntl.h} and @file{sys/file.h}.
  3661. @vtable @code
  3662. @item L_SET
  3663. @standards{BSD, sys/file.h}
  3664. An alias for @code{SEEK_SET}.
  3665. @item L_INCR
  3666. @standards{BSD, sys/file.h}
  3667. An alias for @code{SEEK_CUR}.
  3668. @item L_XTND
  3669. @standards{BSD, sys/file.h}
  3670. An alias for @code{SEEK_END}.
  3671. @end vtable
  3672. @node Portable Positioning
  3673. @section Portable File-Position Functions
  3674. On @gnusystems{}, the file position is truly a character count. You
  3675. can specify any character count value as an argument to @code{fseek} or
  3676. @code{fseeko} and get reliable results for any random access file.
  3677. However, some @w{ISO C} systems do not represent file positions in this
  3678. way.
  3679. On some systems where text streams truly differ from binary streams, it
  3680. is impossible to represent the file position of a text stream as a count
  3681. of characters from the beginning of the file. For example, the file
  3682. position on some systems must encode both a record offset within the
  3683. file, and a character offset within the record.
  3684. As a consequence, if you want your programs to be portable to these
  3685. systems, you must observe certain rules:
  3686. @itemize @bullet
  3687. @item
  3688. The value returned from @code{ftell} on a text stream has no predictable
  3689. relationship to the number of characters you have read so far. The only
  3690. thing you can rely on is that you can use it subsequently as the
  3691. @var{offset} argument to @code{fseek} or @code{fseeko} to move back to
  3692. the same file position.
  3693. @item
  3694. In a call to @code{fseek} or @code{fseeko} on a text stream, either the
  3695. @var{offset} must be zero, or @var{whence} must be @code{SEEK_SET} and
  3696. the @var{offset} must be the result of an earlier call to @code{ftell}
  3697. on the same stream.
  3698. @item
  3699. The value of the file position indicator of a text stream is undefined
  3700. while there are characters that have been pushed back with @code{ungetc}
  3701. that haven't been read or discarded. @xref{Unreading}.
  3702. @end itemize
  3703. But even if you observe these rules, you may still have trouble for long
  3704. files, because @code{ftell} and @code{fseek} use a @code{long int} value
  3705. to represent the file position. This type may not have room to encode
  3706. all the file positions in a large file. Using the @code{ftello} and
  3707. @code{fseeko} functions might help here since the @code{off_t} type is
  3708. expected to be able to hold all file position values but this still does
  3709. not help to handle additional information which must be associated with
  3710. a file position.
  3711. So if you do want to support systems with peculiar encodings for the
  3712. file positions, it is better to use the functions @code{fgetpos} and
  3713. @code{fsetpos} instead. These functions represent the file position
  3714. using the data type @code{fpos_t}, whose internal representation varies
  3715. from system to system.
  3716. These symbols are declared in the header file @file{stdio.h}.
  3717. @pindex stdio.h
  3718. @deftp {Data Type} fpos_t
  3719. @standards{ISO, stdio.h}
  3720. This is the type of an object that can encode information about the
  3721. file position of a stream, for use by the functions @code{fgetpos} and
  3722. @code{fsetpos}.
  3723. In @theglibc{}, @code{fpos_t} is an opaque data structure that
  3724. contains internal data to represent file offset and conversion state
  3725. information. In other systems, it might have a different internal
  3726. representation.
  3727. When compiling with @code{_FILE_OFFSET_BITS == 64} on a 32 bit machine
  3728. this type is in fact equivalent to @code{fpos64_t} since the LFS
  3729. interface transparently replaces the old interface.
  3730. @end deftp
  3731. @deftp {Data Type} fpos64_t
  3732. @standards{Unix98, stdio.h}
  3733. This is the type of an object that can encode information about the
  3734. file position of a stream, for use by the functions @code{fgetpos64} and
  3735. @code{fsetpos64}.
  3736. In @theglibc{}, @code{fpos64_t} is an opaque data structure that
  3737. contains internal data to represent file offset and conversion state
  3738. information. In other systems, it might have a different internal
  3739. representation.
  3740. @end deftp
  3741. @deftypefun int fgetpos (FILE *@var{stream}, fpos_t *@var{position})
  3742. @standards{ISO, stdio.h}
  3743. @safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{} @acucorrupt{}}}
  3744. This function stores the value of the file position indicator for the
  3745. stream @var{stream} in the @code{fpos_t} object pointed to by
  3746. @var{position}. If successful, @code{fgetpos} returns zero; otherwise
  3747. it returns a nonzero value and stores an implementation-defined positive
  3748. value in @code{errno}.
  3749. When the sources are compiled with @code{_FILE_OFFSET_BITS == 64} on a
  3750. 32 bit system the function is in fact @code{fgetpos64}. I.e., the LFS
  3751. interface transparently replaces the old interface.
  3752. @end deftypefun
  3753. @deftypefun int fgetpos64 (FILE *@var{stream}, fpos64_t *@var{position})
  3754. @standards{Unix98, stdio.h}
  3755. @safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{} @acucorrupt{}}}
  3756. This function is similar to @code{fgetpos} but the file position is
  3757. returned in a variable of type @code{fpos64_t} to which @var{position}
  3758. points.
  3759. If the sources are compiled with @code{_FILE_OFFSET_BITS == 64} on a 32
  3760. bits machine this function is available under the name @code{fgetpos}
  3761. and so transparently replaces the old interface.
  3762. @end deftypefun
  3763. @deftypefun int fsetpos (FILE *@var{stream}, const fpos_t *@var{position})
  3764. @standards{ISO, stdio.h}
  3765. @safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{} @acucorrupt{}}}
  3766. This function sets the file position indicator for the stream @var{stream}
  3767. to the position @var{position}, which must have been set by a previous
  3768. call to @code{fgetpos} on the same stream. If successful, @code{fsetpos}
  3769. clears the end-of-file indicator on the stream, discards any characters
  3770. that were ``pushed back'' by the use of @code{ungetc}, and returns a value
  3771. of zero. Otherwise, @code{fsetpos} returns a nonzero value and stores
  3772. an implementation-defined positive value in @code{errno}.
  3773. When the sources are compiled with @code{_FILE_OFFSET_BITS == 64} on a
  3774. 32 bit system the function is in fact @code{fsetpos64}. I.e., the LFS
  3775. interface transparently replaces the old interface.
  3776. @end deftypefun
  3777. @deftypefun int fsetpos64 (FILE *@var{stream}, const fpos64_t *@var{position})
  3778. @standards{Unix98, stdio.h}
  3779. @safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{} @acucorrupt{}}}
  3780. This function is similar to @code{fsetpos} but the file position used
  3781. for positioning is provided in a variable of type @code{fpos64_t} to
  3782. which @var{position} points.
  3783. If the sources are compiled with @code{_FILE_OFFSET_BITS == 64} on a 32
  3784. bits machine this function is available under the name @code{fsetpos}
  3785. and so transparently replaces the old interface.
  3786. @end deftypefun
  3787. @node Stream Buffering
  3788. @section Stream Buffering
  3789. @cindex buffering of streams
  3790. Characters that are written to a stream are normally accumulated and
  3791. transmitted asynchronously to the file in a block, instead of appearing
  3792. as soon as they are output by the application program. Similarly,
  3793. streams often retrieve input from the host environment in blocks rather
  3794. than on a character-by-character basis. This is called @dfn{buffering}.
  3795. If you are writing programs that do interactive input and output using
  3796. streams, you need to understand how buffering works when you design the
  3797. user interface to your program. Otherwise, you might find that output
  3798. (such as progress or prompt messages) doesn't appear when you intended
  3799. it to, or displays some other unexpected behavior.
  3800. This section deals only with controlling when characters are transmitted
  3801. between the stream and the file or device, and @emph{not} with how
  3802. things like echoing, flow control, and the like are handled on specific
  3803. classes of devices. For information on common control operations on
  3804. terminal devices, see @ref{Low-Level Terminal Interface}.
  3805. You can bypass the stream buffering facilities altogether by using the
  3806. low-level input and output functions that operate on file descriptors
  3807. instead. @xref{Low-Level I/O}.
  3808. @menu
  3809. * Buffering Concepts:: Terminology is defined here.
  3810. * Flushing Buffers:: How to ensure that output buffers are flushed.
  3811. * Controlling Buffering:: How to specify what kind of buffering to use.
  3812. @end menu
  3813. @node Buffering Concepts
  3814. @subsection Buffering Concepts
  3815. There are three different kinds of buffering strategies:
  3816. @itemize @bullet
  3817. @item
  3818. Characters written to or read from an @dfn{unbuffered} stream are
  3819. transmitted individually to or from the file as soon as possible.
  3820. @cindex unbuffered stream
  3821. @item
  3822. Characters written to a @dfn{line buffered} stream are transmitted to
  3823. the file in blocks when a newline character is encountered.
  3824. @cindex line buffered stream
  3825. @item
  3826. Characters written to or read from a @dfn{fully buffered} stream are
  3827. transmitted to or from the file in blocks of arbitrary size.
  3828. @cindex fully buffered stream
  3829. @end itemize
  3830. Newly opened streams are normally fully buffered, with one exception: a
  3831. stream connected to an interactive device such as a terminal is
  3832. initially line buffered. @xref{Controlling Buffering}, for information
  3833. on how to select a different kind of buffering. Usually the automatic
  3834. selection gives you the most convenient kind of buffering for the file
  3835. or device you open.
  3836. The use of line buffering for interactive devices implies that output
  3837. messages ending in a newline will appear immediately---which is usually
  3838. what you want. Output that doesn't end in a newline might or might not
  3839. show up immediately, so if you want them to appear immediately, you
  3840. should flush buffered output explicitly with @code{fflush}, as described
  3841. in @ref{Flushing Buffers}.
  3842. @node Flushing Buffers
  3843. @subsection Flushing Buffers
  3844. @cindex flushing a stream
  3845. @dfn{Flushing} output on a buffered stream means transmitting all
  3846. accumulated characters to the file. There are many circumstances when
  3847. buffered output on a stream is flushed automatically:
  3848. @itemize @bullet
  3849. @item
  3850. When you try to do output and the output buffer is full.
  3851. @item
  3852. When the stream is closed. @xref{Closing Streams}.
  3853. @item
  3854. When the program terminates by calling @code{exit}.
  3855. @xref{Normal Termination}.
  3856. @item
  3857. When a newline is written, if the stream is line buffered.
  3858. @item
  3859. Whenever an input operation on @emph{any} stream actually reads data
  3860. from its file.
  3861. @end itemize
  3862. If you want to flush the buffered output at another time, call
  3863. @code{fflush}, which is declared in the header file @file{stdio.h}.
  3864. @pindex stdio.h
  3865. @deftypefun int fflush (FILE *@var{stream})
  3866. @standards{ISO, stdio.h}
  3867. @safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{} @acucorrupt{}}}
  3868. This function causes any buffered output on @var{stream} to be delivered
  3869. to the file. If @var{stream} is a null pointer, then
  3870. @code{fflush} causes buffered output on @emph{all} open output streams
  3871. to be flushed.
  3872. This function returns @code{EOF} if a write error occurs, or zero
  3873. otherwise.
  3874. @end deftypefun
  3875. @deftypefun int fflush_unlocked (FILE *@var{stream})
  3876. @standards{POSIX, stdio.h}
  3877. @safety{@prelim{}@mtsafe{@mtsrace{:stream}}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{}}}
  3878. The @code{fflush_unlocked} function is equivalent to the @code{fflush}
  3879. function except that it does not implicitly lock the stream.
  3880. @end deftypefun
  3881. The @code{fflush} function can be used to flush all streams currently
  3882. opened. While this is useful in some situations it does often more than
  3883. necessary since it might be done in situations when terminal input is
  3884. required and the program wants to be sure that all output is visible on
  3885. the terminal. But this means that only line buffered streams have to be
  3886. flushed. Solaris introduced a function especially for this. It was
  3887. always available in @theglibc{} in some form but never officially
  3888. exported.
  3889. @deftypefun void _flushlbf (void)
  3890. @standards{GNU, stdio_ext.h}
  3891. @safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{} @acucorrupt{}}}
  3892. The @code{_flushlbf} function flushes all line buffered streams
  3893. currently opened.
  3894. This function is declared in the @file{stdio_ext.h} header.
  3895. @end deftypefun
  3896. @strong{Compatibility Note:} Some brain-damaged operating systems have
  3897. been known to be so thoroughly fixated on line-oriented input and output
  3898. that flushing a line buffered stream causes a newline to be written!
  3899. Fortunately, this ``feature'' seems to be becoming less common. You do
  3900. not need to worry about this with @theglibc{}.
  3901. In some situations it might be useful to not flush the output pending
  3902. for a stream but instead simply forget it. If transmission is costly
  3903. and the output is not needed anymore this is valid reasoning. In this
  3904. situation a non-standard function introduced in Solaris and available in
  3905. @theglibc{} can be used.
  3906. @deftypefun void __fpurge (FILE *@var{stream})
  3907. @standards{GNU, stdio_ext.h}
  3908. @safety{@prelim{}@mtsafe{@mtsrace{:stream}}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{}}}
  3909. The @code{__fpurge} function causes the buffer of the stream
  3910. @var{stream} to be emptied. If the stream is currently in read mode all
  3911. input in the buffer is lost. If the stream is in output mode the
  3912. buffered output is not written to the device (or whatever other
  3913. underlying storage) and the buffer is cleared.
  3914. This function is declared in @file{stdio_ext.h}.
  3915. @end deftypefun
  3916. @node Controlling Buffering
  3917. @subsection Controlling Which Kind of Buffering
  3918. After opening a stream (but before any other operations have been
  3919. performed on it), you can explicitly specify what kind of buffering you
  3920. want it to have using the @code{setvbuf} function.
  3921. @cindex buffering, controlling
  3922. The facilities listed in this section are declared in the header
  3923. file @file{stdio.h}.
  3924. @pindex stdio.h
  3925. @deftypefun int setvbuf (FILE *@var{stream}, char *@var{buf}, int @var{mode}, size_t @var{size})
  3926. @standards{ISO, stdio.h}
  3927. @safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{} @acucorrupt{}}}
  3928. This function is used to specify that the stream @var{stream} should
  3929. have the buffering mode @var{mode}, which can be either @code{_IOFBF}
  3930. (for full buffering), @code{_IOLBF} (for line buffering), or
  3931. @code{_IONBF} (for unbuffered input/output).
  3932. If you specify a null pointer as the @var{buf} argument, then @code{setvbuf}
  3933. allocates a buffer itself using @code{malloc}. This buffer will be freed
  3934. when you close the stream.
  3935. Otherwise, @var{buf} should be a character array that can hold at least
  3936. @var{size} characters. You should not free the space for this array as
  3937. long as the stream remains open and this array remains its buffer. You
  3938. should usually either allocate it statically, or @code{malloc}
  3939. (@pxref{Unconstrained Allocation}) the buffer. Using an automatic array
  3940. is not a good idea unless you close the file before exiting the block
  3941. that declares the array.
  3942. While the array remains a stream buffer, the stream I/O functions will
  3943. use the buffer for their internal purposes. You shouldn't try to access
  3944. the values in the array directly while the stream is using it for
  3945. buffering.
  3946. The @code{setvbuf} function returns zero on success, or a nonzero value
  3947. if the value of @var{mode} is not valid or if the request could not
  3948. be honored.
  3949. @end deftypefun
  3950. @deftypevr Macro int _IOFBF
  3951. @standards{ISO, stdio.h}
  3952. The value of this macro is an integer constant expression that can be
  3953. used as the @var{mode} argument to the @code{setvbuf} function to
  3954. specify that the stream should be fully buffered.
  3955. @end deftypevr
  3956. @deftypevr Macro int _IOLBF
  3957. @standards{ISO, stdio.h}
  3958. The value of this macro is an integer constant expression that can be
  3959. used as the @var{mode} argument to the @code{setvbuf} function to
  3960. specify that the stream should be line buffered.
  3961. @end deftypevr
  3962. @deftypevr Macro int _IONBF
  3963. @standards{ISO, stdio.h}
  3964. The value of this macro is an integer constant expression that can be
  3965. used as the @var{mode} argument to the @code{setvbuf} function to
  3966. specify that the stream should be unbuffered.
  3967. @end deftypevr
  3968. @deftypevr Macro int BUFSIZ
  3969. @standards{ISO, stdio.h}
  3970. The value of this macro is an integer constant expression that is good
  3971. to use for the @var{size} argument to @code{setvbuf}. This value is
  3972. guaranteed to be at least @code{256}.
  3973. The value of @code{BUFSIZ} is chosen on each system so as to make stream
  3974. I/O efficient. So it is a good idea to use @code{BUFSIZ} as the size
  3975. for the buffer when you call @code{setvbuf}.
  3976. Actually, you can get an even better value to use for the buffer size
  3977. by means of the @code{fstat} system call: it is found in the
  3978. @code{st_blksize} field of the file attributes. @xref{Attribute Meanings}.
  3979. Sometimes people also use @code{BUFSIZ} as the allocation size of
  3980. buffers used for related purposes, such as strings used to receive a
  3981. line of input with @code{fgets} (@pxref{Character Input}). There is no
  3982. particular reason to use @code{BUFSIZ} for this instead of any other
  3983. integer, except that it might lead to doing I/O in chunks of an
  3984. efficient size.
  3985. @end deftypevr
  3986. @deftypefun void setbuf (FILE *@var{stream}, char *@var{buf})
  3987. @standards{ISO, stdio.h}
  3988. @safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{} @acucorrupt{}}}
  3989. If @var{buf} is a null pointer, the effect of this function is
  3990. equivalent to calling @code{setvbuf} with a @var{mode} argument of
  3991. @code{_IONBF}. Otherwise, it is equivalent to calling @code{setvbuf}
  3992. with @var{buf}, and a @var{mode} of @code{_IOFBF} and a @var{size}
  3993. argument of @code{BUFSIZ}.
  3994. The @code{setbuf} function is provided for compatibility with old code;
  3995. use @code{setvbuf} in all new programs.
  3996. @end deftypefun
  3997. @deftypefun void setbuffer (FILE *@var{stream}, char *@var{buf}, size_t @var{size})
  3998. @standards{BSD, stdio.h}
  3999. @safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{} @acucorrupt{}}}
  4000. If @var{buf} is a null pointer, this function makes @var{stream} unbuffered.
  4001. Otherwise, it makes @var{stream} fully buffered using @var{buf} as the
  4002. buffer. The @var{size} argument specifies the length of @var{buf}.
  4003. This function is provided for compatibility with old BSD code. Use
  4004. @code{setvbuf} instead.
  4005. @end deftypefun
  4006. @deftypefun void setlinebuf (FILE *@var{stream})
  4007. @standards{BSD, stdio.h}
  4008. @safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{} @acucorrupt{}}}
  4009. This function makes @var{stream} be line buffered, and allocates the
  4010. buffer for you.
  4011. This function is provided for compatibility with old BSD code. Use
  4012. @code{setvbuf} instead.
  4013. @end deftypefun
  4014. It is possible to query whether a given stream is line buffered or not
  4015. using a non-standard function introduced in Solaris and available in
  4016. @theglibc{}.
  4017. @deftypefun int __flbf (FILE *@var{stream})
  4018. @standards{GNU, stdio_ext.h}
  4019. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  4020. The @code{__flbf} function will return a nonzero value in case the
  4021. stream @var{stream} is line buffered. Otherwise the return value is
  4022. zero.
  4023. This function is declared in the @file{stdio_ext.h} header.
  4024. @end deftypefun
  4025. Two more extensions allow to determine the size of the buffer and how
  4026. much of it is used. These functions were also introduced in Solaris.
  4027. @deftypefun size_t __fbufsize (FILE *@var{stream})
  4028. @standards{GNU, stdio_ext.h}
  4029. @safety{@prelim{}@mtsafe{@mtsrace{:stream}}@asunsafe{@asucorrupt{}}@acsafe{}}
  4030. The @code{__fbufsize} function return the size of the buffer in the
  4031. stream @var{stream}. This value can be used to optimize the use of the
  4032. stream.
  4033. This function is declared in the @file{stdio_ext.h} header.
  4034. @end deftypefun
  4035. @deftypefun size_t __fpending (FILE *@var{stream})
  4036. @standards{GNU, stdio_ext.h}
  4037. @safety{@prelim{}@mtsafe{@mtsrace{:stream}}@asunsafe{@asucorrupt{}}@acsafe{}}
  4038. The @code{__fpending}
  4039. function returns the number of bytes currently in the output buffer.
  4040. For wide-oriented streams the measuring unit is wide characters. This
  4041. function should not be used on buffers in read mode or opened read-only.
  4042. This function is declared in the @file{stdio_ext.h} header.
  4043. @end deftypefun
  4044. @node Other Kinds of Streams
  4045. @section Other Kinds of Streams
  4046. @Theglibc{} provides ways for you to define additional kinds of
  4047. streams that do not necessarily correspond to an open file.
  4048. One such type of stream takes input from or writes output to a string.
  4049. These kinds of streams are used internally to implement the
  4050. @code{sprintf} and @code{sscanf} functions. You can also create such a
  4051. stream explicitly, using the functions described in @ref{String Streams}.
  4052. More generally, you can define streams that do input/output to arbitrary
  4053. objects using functions supplied by your program. This protocol is
  4054. discussed in @ref{Custom Streams}.
  4055. @strong{Portability Note:} The facilities described in this section are
  4056. specific to GNU. Other systems or C implementations might or might not
  4057. provide equivalent functionality.
  4058. @menu
  4059. * String Streams:: Streams that get data from or put data in
  4060. a string or memory buffer.
  4061. * Custom Streams:: Defining your own streams with an arbitrary
  4062. input data source and/or output data sink.
  4063. @end menu
  4064. @node String Streams
  4065. @subsection String Streams
  4066. @cindex stream, for I/O to a string
  4067. @cindex string stream
  4068. The @code{fmemopen} and @code{open_memstream} functions allow you to do
  4069. I/O to a string or memory buffer. These facilities are declared in
  4070. @file{stdio.h}.
  4071. @pindex stdio.h
  4072. @deftypefun {FILE *} fmemopen (void *@var{buf}, size_t @var{size}, const char *@var{opentype})
  4073. @standards{GNU, stdio.h}
  4074. @safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{} @asulock{}}@acunsafe{@acsmem{} @aculock{}}}
  4075. @c Unlike open_memstream, fmemopen does (indirectly) call _IO_link_in,
  4076. @c bringing with it additional potential for async trouble with
  4077. @c list_all_lock.
  4078. This function opens a stream that allows the access specified by the
  4079. @var{opentype} argument, that reads from or writes to the buffer specified
  4080. by the argument @var{buf}. This array must be at least @var{size} bytes long.
  4081. If you specify a null pointer as the @var{buf} argument, @code{fmemopen}
  4082. dynamically allocates an array @var{size} bytes long (as with @code{malloc};
  4083. @pxref{Unconstrained Allocation}). This is really only useful
  4084. if you are going to write things to the buffer and then read them back
  4085. in again, because you have no way of actually getting a pointer to the
  4086. buffer (for this, try @code{open_memstream}, below). The buffer is
  4087. freed when the stream is closed.
  4088. The argument @var{opentype} is the same as in @code{fopen}
  4089. (@pxref{Opening Streams}). If the @var{opentype} specifies
  4090. append mode, then the initial file position is set to the first null
  4091. character in the buffer. Otherwise the initial file position is at the
  4092. beginning of the buffer.
  4093. When a stream open for writing is flushed or closed, a null character
  4094. (zero byte) is written at the end of the buffer if it fits. You
  4095. should add an extra byte to the @var{size} argument to account for this.
  4096. Attempts to write more than @var{size} bytes to the buffer result
  4097. in an error.
  4098. For a stream open for reading, null characters (zero bytes) in the
  4099. buffer do not count as ``end of file''. Read operations indicate end of
  4100. file only when the file position advances past @var{size} bytes. So, if
  4101. you want to read characters from a null-terminated string, you should
  4102. supply the length of the string as the @var{size} argument.
  4103. @end deftypefun
  4104. Here is an example of using @code{fmemopen} to create a stream for
  4105. reading from a string:
  4106. @smallexample
  4107. @include memopen.c.texi
  4108. @end smallexample
  4109. This program produces the following output:
  4110. @smallexample
  4111. Got f
  4112. Got o
  4113. Got o
  4114. Got b
  4115. Got a
  4116. Got r
  4117. @end smallexample
  4118. @deftypefun {FILE *} open_memstream (char **@var{ptr}, size_t *@var{sizeloc})
  4119. @standards{GNU, stdio.h}
  4120. @safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
  4121. This function opens a stream for writing to a buffer. The buffer is
  4122. allocated dynamically and grown as necessary, using @code{malloc}.
  4123. After you've closed the stream, this buffer is your responsibility to
  4124. clean up using @code{free} or @code{realloc}. @xref{Unconstrained Allocation}.
  4125. When the stream is closed with @code{fclose} or flushed with
  4126. @code{fflush}, the locations @var{ptr} and @var{sizeloc} are updated to
  4127. contain the pointer to the buffer and its size. The values thus stored
  4128. remain valid only as long as no further output on the stream takes
  4129. place. If you do more output, you must flush the stream again to store
  4130. new values before you use them again.
  4131. A null character is written at the end of the buffer. This null character
  4132. is @emph{not} included in the size value stored at @var{sizeloc}.
  4133. You can move the stream's file position with @code{fseek} or
  4134. @code{fseeko} (@pxref{File Positioning}). Moving the file position past
  4135. the end of the data already written fills the intervening space with
  4136. zeroes.
  4137. @end deftypefun
  4138. Here is an example of using @code{open_memstream}:
  4139. @smallexample
  4140. @include memstrm.c.texi
  4141. @end smallexample
  4142. This program produces the following output:
  4143. @smallexample
  4144. buf = `hello', size = 5
  4145. buf = `hello, world', size = 12
  4146. @end smallexample
  4147. @node Custom Streams
  4148. @subsection Programming Your Own Custom Streams
  4149. @cindex custom streams
  4150. @cindex programming your own streams
  4151. This section describes how you can make a stream that gets input from an
  4152. arbitrary data source or writes output to an arbitrary data sink
  4153. programmed by you. We call these @dfn{custom streams}. The functions
  4154. and types described here are all GNU extensions.
  4155. @c !!! this does not talk at all about the higher-level hooks
  4156. @menu
  4157. * Streams and Cookies:: The @dfn{cookie} records where to fetch or
  4158. store data that is read or written.
  4159. * Hook Functions:: How you should define the four @dfn{hook
  4160. functions} that a custom stream needs.
  4161. @end menu
  4162. @node Streams and Cookies
  4163. @subsubsection Custom Streams and Cookies
  4164. @cindex cookie, for custom stream
  4165. Inside every custom stream is a special object called the @dfn{cookie}.
  4166. This is an object supplied by you which records where to fetch or store
  4167. the data read or written. It is up to you to define a data type to use
  4168. for the cookie. The stream functions in the library never refer
  4169. directly to its contents, and they don't even know what the type is;
  4170. they record its address with type @code{void *}.
  4171. To implement a custom stream, you must specify @emph{how} to fetch or
  4172. store the data in the specified place. You do this by defining
  4173. @dfn{hook functions} to read, write, change ``file position'', and close
  4174. the stream. All four of these functions will be passed the stream's
  4175. cookie so they can tell where to fetch or store the data. The library
  4176. functions don't know what's inside the cookie, but your functions will
  4177. know.
  4178. When you create a custom stream, you must specify the cookie pointer,
  4179. and also the four hook functions stored in a structure of type
  4180. @code{cookie_io_functions_t}.
  4181. These facilities are declared in @file{stdio.h}.
  4182. @pindex stdio.h
  4183. @deftp {Data Type} {cookie_io_functions_t}
  4184. @standards{GNU, stdio.h}
  4185. This is a structure type that holds the functions that define the
  4186. communications protocol between the stream and its cookie. It has
  4187. the following members:
  4188. @table @code
  4189. @item cookie_read_function_t *read
  4190. This is the function that reads data from the cookie. If the value is a
  4191. null pointer instead of a function, then read operations on this stream
  4192. always return @code{EOF}.
  4193. @item cookie_write_function_t *write
  4194. This is the function that writes data to the cookie. If the value is a
  4195. null pointer instead of a function, then data written to the stream is
  4196. discarded.
  4197. @item cookie_seek_function_t *seek
  4198. This is the function that performs the equivalent of file positioning on
  4199. the cookie. If the value is a null pointer instead of a function, calls
  4200. to @code{fseek} or @code{fseeko} on this stream can only seek to
  4201. locations within the buffer; any attempt to seek outside the buffer will
  4202. return an @code{ESPIPE} error.
  4203. @item cookie_close_function_t *close
  4204. This function performs any appropriate cleanup on the cookie when
  4205. closing the stream. If the value is a null pointer instead of a
  4206. function, nothing special is done to close the cookie when the stream is
  4207. closed.
  4208. @end table
  4209. @end deftp
  4210. @deftypefun {FILE *} fopencookie (void *@var{cookie}, const char *@var{opentype}, cookie_io_functions_t @var{io-functions})
  4211. @standards{GNU, stdio.h}
  4212. @safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{} @asulock{}}@acunsafe{@acsmem{} @aculock{}}}
  4213. This function actually creates the stream for communicating with the
  4214. @var{cookie} using the functions in the @var{io-functions} argument.
  4215. The @var{opentype} argument is interpreted as for @code{fopen};
  4216. see @ref{Opening Streams}. (But note that the ``truncate on
  4217. open'' option is ignored.) The new stream is fully buffered.
  4218. The @code{fopencookie} function returns the newly created stream, or a null
  4219. pointer in case of an error.
  4220. @end deftypefun
  4221. @node Hook Functions
  4222. @subsubsection Custom Stream Hook Functions
  4223. @cindex hook functions (of custom streams)
  4224. Here are more details on how you should define the four hook functions
  4225. that a custom stream needs.
  4226. You should define the function to read data from the cookie as:
  4227. @smallexample
  4228. ssize_t @var{reader} (void *@var{cookie}, char *@var{buffer}, size_t @var{size})
  4229. @end smallexample
  4230. This is very similar to the @code{read} function; see @ref{I/O
  4231. Primitives}. Your function should transfer up to @var{size} bytes into
  4232. the @var{buffer}, and return the number of bytes read, or zero to
  4233. indicate end-of-file. You can return a value of @code{-1} to indicate
  4234. an error.
  4235. You should define the function to write data to the cookie as:
  4236. @smallexample
  4237. ssize_t @var{writer} (void *@var{cookie}, const char *@var{buffer}, size_t @var{size})
  4238. @end smallexample
  4239. This is very similar to the @code{write} function; see @ref{I/O
  4240. Primitives}. Your function should transfer up to @var{size} bytes from
  4241. the buffer, and return the number of bytes written. You can return a
  4242. value of @code{0} to indicate an error. You must not return any
  4243. negative value.
  4244. You should define the function to perform seek operations on the cookie
  4245. as:
  4246. @smallexample
  4247. int @var{seeker} (void *@var{cookie}, off64_t *@var{position}, int @var{whence})
  4248. @end smallexample
  4249. For this function, the @var{position} and @var{whence} arguments are
  4250. interpreted as for @code{fgetpos}; see @ref{Portable Positioning}.
  4251. After doing the seek operation, your function should store the resulting
  4252. file position relative to the beginning of the file in @var{position}.
  4253. Your function should return a value of @code{0} on success and @code{-1}
  4254. to indicate an error.
  4255. You should define the function to do cleanup operations on the cookie
  4256. appropriate for closing the stream as:
  4257. @smallexample
  4258. int @var{cleaner} (void *@var{cookie})
  4259. @end smallexample
  4260. Your function should return @code{-1} to indicate an error, and @code{0}
  4261. otherwise.
  4262. @deftp {Data Type} cookie_read_function_t
  4263. @standards{GNU, stdio.h}
  4264. This is the data type that the read function for a custom stream should have.
  4265. If you declare the function as shown above, this is the type it will have.
  4266. @end deftp
  4267. @deftp {Data Type} cookie_write_function_t
  4268. @standards{GNU, stdio.h}
  4269. The data type of the write function for a custom stream.
  4270. @end deftp
  4271. @deftp {Data Type} cookie_seek_function_t
  4272. @standards{GNU, stdio.h}
  4273. The data type of the seek function for a custom stream.
  4274. @end deftp
  4275. @deftp {Data Type} cookie_close_function_t
  4276. @standards{GNU, stdio.h}
  4277. The data type of the close function for a custom stream.
  4278. @end deftp
  4279. @ignore
  4280. Roland says:
  4281. @quotation
  4282. There is another set of functions one can give a stream, the
  4283. input-room and output-room functions. These functions must
  4284. understand stdio internals. To describe how to use these
  4285. functions, you also need to document lots of how stdio works
  4286. internally (which isn't relevant for other uses of stdio).
  4287. Perhaps I can write an interface spec from which you can write
  4288. good documentation. But it's pretty complex and deals with lots
  4289. of nitty-gritty details. I think it might be better to let this
  4290. wait until the rest of the manual is more done and polished.
  4291. @end quotation
  4292. @end ignore
  4293. @c ??? This section could use an example.
  4294. @node Formatted Messages
  4295. @section Formatted Messages
  4296. @cindex formatted messages
  4297. On systems which are based on System V messages of programs (especially
  4298. the system tools) are printed in a strict form using the @code{fmtmsg}
  4299. function. The uniformity sometimes helps the user to interpret messages
  4300. and the strictness tests of the @code{fmtmsg} function ensure that the
  4301. programmer follows some minimal requirements.
  4302. @menu
  4303. * Printing Formatted Messages:: The @code{fmtmsg} function.
  4304. * Adding Severity Classes:: Add more severity classes.
  4305. * Example:: How to use @code{fmtmsg} and @code{addseverity}.
  4306. @end menu
  4307. @node Printing Formatted Messages
  4308. @subsection Printing Formatted Messages
  4309. Messages can be printed to standard error and/or to the console. To
  4310. select the destination the programmer can use the following two values,
  4311. bitwise OR combined if wanted, for the @var{classification} parameter of
  4312. @code{fmtmsg}:
  4313. @vtable @code
  4314. @item MM_PRINT
  4315. Display the message in standard error.
  4316. @item MM_CONSOLE
  4317. Display the message on the system console.
  4318. @end vtable
  4319. The erroneous piece of the system can be signalled by exactly one of the
  4320. following values which also is bitwise ORed with the
  4321. @var{classification} parameter to @code{fmtmsg}:
  4322. @vtable @code
  4323. @item MM_HARD
  4324. The source of the condition is some hardware.
  4325. @item MM_SOFT
  4326. The source of the condition is some software.
  4327. @item MM_FIRM
  4328. The source of the condition is some firmware.
  4329. @end vtable
  4330. A third component of the @var{classification} parameter to @code{fmtmsg}
  4331. can describe the part of the system which detects the problem. This is
  4332. done by using exactly one of the following values:
  4333. @vtable @code
  4334. @item MM_APPL
  4335. The erroneous condition is detected by the application.
  4336. @item MM_UTIL
  4337. The erroneous condition is detected by a utility.
  4338. @item MM_OPSYS
  4339. The erroneous condition is detected by the operating system.
  4340. @end vtable
  4341. A last component of @var{classification} can signal the results of this
  4342. message. Exactly one of the following values can be used:
  4343. @vtable @code
  4344. @item MM_RECOVER
  4345. It is a recoverable error.
  4346. @item MM_NRECOV
  4347. It is a non-recoverable error.
  4348. @end vtable
  4349. @deftypefun int fmtmsg (long int @var{classification}, const char *@var{label}, int @var{severity}, const char *@var{text}, const char *@var{action}, const char *@var{tag})
  4350. @standards{XPG, fmtmsg.h}
  4351. @safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acsafe{}}
  4352. Display a message described by its parameters on the device(s) specified
  4353. in the @var{classification} parameter. The @var{label} parameter
  4354. identifies the source of the message. The string should consist of two
  4355. colon separated parts where the first part has not more than 10 and the
  4356. second part not more than 14 characters. The @var{text} parameter
  4357. describes the condition of the error, the @var{action} parameter possible
  4358. steps to recover from the error and the @var{tag} parameter is a
  4359. reference to the online documentation where more information can be
  4360. found. It should contain the @var{label} value and a unique
  4361. identification number.
  4362. Each of the parameters can be a special value which means this value
  4363. is to be omitted. The symbolic names for these values are:
  4364. @vtable @code
  4365. @item MM_NULLLBL
  4366. Ignore @var{label} parameter.
  4367. @item MM_NULLSEV
  4368. Ignore @var{severity} parameter.
  4369. @item MM_NULLMC
  4370. Ignore @var{classification} parameter. This implies that nothing is
  4371. actually printed.
  4372. @item MM_NULLTXT
  4373. Ignore @var{text} parameter.
  4374. @item MM_NULLACT
  4375. Ignore @var{action} parameter.
  4376. @item MM_NULLTAG
  4377. Ignore @var{tag} parameter.
  4378. @end vtable
  4379. There is another way certain fields can be omitted from the output to
  4380. standard error. This is described below in the description of
  4381. environment variables influencing the behavior.
  4382. The @var{severity} parameter can have one of the values in the following
  4383. table:
  4384. @cindex severity class
  4385. @vtable @code
  4386. @item MM_NOSEV
  4387. Nothing is printed, this value is the same as @code{MM_NULLSEV}.
  4388. @item MM_HALT
  4389. This value is printed as @code{HALT}.
  4390. @item MM_ERROR
  4391. This value is printed as @code{ERROR}.
  4392. @item MM_WARNING
  4393. This value is printed as @code{WARNING}.
  4394. @item MM_INFO
  4395. This value is printed as @code{INFO}.
  4396. @end vtable
  4397. The numeric value of these five macros are between @code{0} and
  4398. @code{4}. Using the environment variable @code{SEV_LEVEL} or using the
  4399. @code{addseverity} function one can add more severity levels with their
  4400. corresponding string to print. This is described below
  4401. (@pxref{Adding Severity Classes}).
  4402. @noindent
  4403. If no parameter is ignored the output looks like this:
  4404. @smallexample
  4405. @var{label}: @var{severity-string}: @var{text}
  4406. TO FIX: @var{action} @var{tag}
  4407. @end smallexample
  4408. The colons, new line characters and the @code{TO FIX} string are
  4409. inserted if necessary, i.e., if the corresponding parameter is not
  4410. ignored.
  4411. This function is specified in the X/Open Portability Guide. It is also
  4412. available on all systems derived from System V.
  4413. The function returns the value @code{MM_OK} if no error occurred. If
  4414. only the printing to standard error failed, it returns @code{MM_NOMSG}.
  4415. If printing to the console fails, it returns @code{MM_NOCON}. If
  4416. nothing is printed @code{MM_NOTOK} is returned. Among situations where
  4417. all outputs fail this last value is also returned if a parameter value
  4418. is incorrect.
  4419. @end deftypefun
  4420. There are two environment variables which influence the behavior of
  4421. @code{fmtmsg}. The first is @code{MSGVERB}. It is used to control the
  4422. output actually happening on standard error (@emph{not} the console
  4423. output). Each of the five fields can explicitly be enabled. To do
  4424. this the user has to put the @code{MSGVERB} variable with a format like
  4425. the following in the environment before calling the @code{fmtmsg} function
  4426. the first time:
  4427. @smallexample
  4428. MSGVERB=@var{keyword}[:@var{keyword}[:@dots{}]]
  4429. @end smallexample
  4430. Valid @var{keyword}s are @code{label}, @code{severity}, @code{text},
  4431. @code{action}, and @code{tag}. If the environment variable is not given
  4432. or is the empty string, a not supported keyword is given or the value is
  4433. somehow else invalid, no part of the message is masked out.
  4434. The second environment variable which influences the behavior of
  4435. @code{fmtmsg} is @code{SEV_LEVEL}. This variable and the change in the
  4436. behavior of @code{fmtmsg} is not specified in the X/Open Portability
  4437. Guide. It is available in System V systems, though. It can be used to
  4438. introduce new severity levels. By default, only the five severity levels
  4439. described above are available. Any other numeric value would make
  4440. @code{fmtmsg} print nothing.
  4441. If the user puts @code{SEV_LEVEL} with a format like
  4442. @smallexample
  4443. SEV_LEVEL=[@var{description}[:@var{description}[:@dots{}]]]
  4444. @end smallexample
  4445. @noindent
  4446. in the environment of the process before the first call to
  4447. @code{fmtmsg}, where @var{description} has a value of the form
  4448. @smallexample
  4449. @var{severity-keyword},@var{level},@var{printstring}
  4450. @end smallexample
  4451. The @var{severity-keyword} part is not used by @code{fmtmsg} but it has
  4452. to be present. The @var{level} part is a string representation of a
  4453. number. The numeric value must be a number greater than 4. This value
  4454. must be used in the @var{severity} parameter of @code{fmtmsg} to select
  4455. this class. It is not possible to overwrite any of the predefined
  4456. classes. The @var{printstring} is the string printed when a message of
  4457. this class is processed by @code{fmtmsg} (see above, @code{fmtsmg} does
  4458. not print the numeric value but instead the string representation).
  4459. @node Adding Severity Classes
  4460. @subsection Adding Severity Classes
  4461. @cindex severity class
  4462. There is another possibility to introduce severity classes besides using
  4463. the environment variable @code{SEV_LEVEL}. This simplifies the task of
  4464. introducing new classes in a running program. One could use the
  4465. @code{setenv} or @code{putenv} function to set the environment variable,
  4466. but this is toilsome.
  4467. @deftypefun int addseverity (int @var{severity}, const char *@var{string})
  4468. @safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{} @asulock{}}@acunsafe{@aculock{} @acsmem{}}}
  4469. This function allows the introduction of new severity classes which can be
  4470. addressed by the @var{severity} parameter of the @code{fmtmsg} function.
  4471. The @var{severity} parameter of @code{addseverity} must match the value
  4472. for the parameter with the same name of @code{fmtmsg}, and @var{string}
  4473. is the string printed in the actual messages instead of the numeric
  4474. value.
  4475. If @var{string} is @code{NULL} the severity class with the numeric value
  4476. according to @var{severity} is removed.
  4477. It is not possible to overwrite or remove one of the default severity
  4478. classes. All calls to @code{addseverity} with @var{severity} set to one
  4479. of the values for the default classes will fail.
  4480. The return value is @code{MM_OK} if the task was successfully performed.
  4481. If the return value is @code{MM_NOTOK} something went wrong. This could
  4482. mean that no more memory is available or a class is not available when
  4483. it has to be removed.
  4484. This function is not specified in the X/Open Portability Guide although
  4485. the @code{fmtsmg} function is. It is available on System V systems.
  4486. @end deftypefun
  4487. @node Example
  4488. @subsection How to use @code{fmtmsg} and @code{addseverity}
  4489. Here is a simple example program to illustrate the use of both
  4490. functions described in this section.
  4491. @smallexample
  4492. @include fmtmsgexpl.c.texi
  4493. @end smallexample
  4494. The second call to @code{fmtmsg} illustrates a use of this function as
  4495. it usually occurs on System V systems, which heavily use this function.
  4496. It seems worthwhile to give a short explanation here of how this system
  4497. works on System V. The value of the
  4498. @var{label} field (@code{UX:cat}) says that the error occurred in the
  4499. Unix program @code{cat}. The explanation of the error follows and the
  4500. value for the @var{action} parameter is @code{"refer to manual"}. One
  4501. could be more specific here, if necessary. The @var{tag} field contains,
  4502. as proposed above, the value of the string given for the @var{label}
  4503. parameter, and additionally a unique ID (@code{001} in this case). For
  4504. a GNU environment this string could contain a reference to the
  4505. corresponding node in the Info page for the program.
  4506. @noindent
  4507. Running this program without specifying the @code{MSGVERB} and
  4508. @code{SEV_LEVEL} function produces the following output:
  4509. @smallexample
  4510. UX:cat: NOTE2: invalid syntax
  4511. TO FIX: refer to manual UX:cat:001
  4512. @end smallexample
  4513. We see the different fields of the message and how the extra glue (the
  4514. colons and the @code{TO FIX} string) is printed. But only one of the
  4515. three calls to @code{fmtmsg} produced output. The first call does not
  4516. print anything because the @var{label} parameter is not in the correct
  4517. form. The string must contain two fields, separated by a colon
  4518. (@pxref{Printing Formatted Messages}). The third @code{fmtmsg} call
  4519. produced no output since the class with the numeric value @code{6} is
  4520. not defined. Although a class with numeric value @code{5} is also not
  4521. defined by default, the call to @code{addseverity} introduces it and
  4522. the second call to @code{fmtmsg} produces the above output.
  4523. When we change the environment of the program to contain
  4524. @code{SEV_LEVEL=XXX,6,NOTE} when running it we get a different result:
  4525. @smallexample
  4526. UX:cat: NOTE2: invalid syntax
  4527. TO FIX: refer to manual UX:cat:001
  4528. label:foo: NOTE: text
  4529. TO FIX: action tag
  4530. @end smallexample
  4531. Now the third call to @code{fmtmsg} produced some output and we see how
  4532. the string @code{NOTE} from the environment variable appears in the
  4533. message.
  4534. Now we can reduce the output by specifying which fields we are
  4535. interested in. If we additionally set the environment variable
  4536. @code{MSGVERB} to the value @code{severity:label:action} we get the
  4537. following output:
  4538. @smallexample
  4539. UX:cat: NOTE2
  4540. TO FIX: refer to manual
  4541. label:foo: NOTE
  4542. TO FIX: action
  4543. @end smallexample
  4544. @noindent
  4545. I.e., the output produced by the @var{text} and the @var{tag} parameters
  4546. to @code{fmtmsg} vanished. Please also note that now there is no colon
  4547. after the @code{NOTE} and @code{NOTE2} strings in the output. This is
  4548. not necessary since there is no more output on this line because the text
  4549. is missing.