资源描述:
《alp-ch02-writing-good-gnu-linux-software.pdf》由会员上传分享,免费在线阅读,更多相关内容在学术论文-天天文库。
1、030430CH025/22/0110:20AMPage172WritingGoodGNU/LinuxSoftwareTHISCHAPTERCOVERSSOMEBASICTECHNIQUESTHATMOSTGNU/Linuxprogram-mersuse.Byfollowingtheguidelinespresented,you’llbeabletowriteprogramsthatworkwellwithintheGNU/LinuxenvironmentandmeetGNU/Linuxusers’expec-tationsofhowprograms
2、shouldoperate.2.1InteractionWiththeExecutionEnvironmentWhenyoufirststudiedCorC++,youlearnedthatthespecialmainfunctionistheprimaryentrypointforaprogram.Whentheoperatingsystemexecutesyourpro-gram,itautomaticallyprovidescertainfacilitiesthathelptheprogramcommunicatewiththeoperatin
3、gsystemandtheuser.Youprobablylearnedaboutthetwoparame-terstomain,usuallycalledargcandargv,whichreceiveinputstoyourprogram.Youlearnedaboutthestdoutandstdin(orthecoutandcinstreamsinC++)thatprovideconsoleinputandoutput.ThesefeaturesareprovidedbytheCandC++languages,andtheyinteractw
4、iththeGNU/Linuxsystemincertainways.GNU/Linuxprovidesotherwaysforinteractingwiththeoperatingenvironment,too.030430CH025/22/0110:20AMPage1818Chapter2WritingGoodGNU/LinuxSoftware2.1.1TheArgumentListYourunaprogramfromashellpromptbytypingthenameoftheprogram.Optionally,youcansupplyad
5、ditionalinformationtotheprogrambytypingoneormorewordsaftertheprogramname,separatedbyspaces.Thesearecalledcommand-linearguments.(Youcanalsoincludeanargumentthatcontainsaspace,byenclosingtheargumentinquotes.)Moregenerally,thisisreferredtoastheprogram’sargumentlistbecauseitneednot
6、originatefromashellcommandline.InChapter3,“Processes,”you’llseeanotherwayofinvokingaprogram,inwhichaprogramcanspecifytheargumentlistofanotherprogramdirectly.Whenaprogramisinvokedfromtheshell,theargumentlistcontainstheentirecommandline,includingthenameoftheprogramandanycommand-l
7、ineargumentsthatmayhavebeenprovided.Suppose,forexample,thatyouinvokethelscommandinyourshelltodisplaythecontentsoftherootdirectoryandcorrespondingfilesizeswiththiscommandline:%ls-s/Theargumentlistthatthelsprogramreceiveshasthreeelements.Thefirstoneisthenameoftheprogramitself,ass
8、pecifiedonthecommandline,namelyls.Thesecondandthirdele