ch09linux程序設(shè)計(jì)入門、文件操作和環(huán)境_第1頁
ch09linux程序設(shè)計(jì)入門、文件操作和環(huán)境_第2頁
ch09linux程序設(shè)計(jì)入門、文件操作和環(huán)境_第3頁
ch09linux程序設(shè)計(jì)入門、文件操作和環(huán)境_第4頁
ch09linux程序設(shè)計(jì)入門、文件操作和環(huán)境_第5頁
已閱讀5頁,還剩45頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、Central Soutiversity%* .KPWZ廢 庂+棉 Linux/Linuxc X : XL?nLLinuxc 80t j!|j!:iLinuxWZ!8?!Linux!LinuxWZLinuxh& .! 5executables:scriptsLinux o&8:!vi/vimemacsCgcc!LinuxWZemacs8:!7 Linux Coworld.c*/*#ide #include main()pr exf(”0);oWorldn”); $gcc./-oorldoworld doworld.cgcc-o a.out./ o&8-1h& , /usr/bin/usr/lo

2、cal/bin F8ETBo /usr/includeVTBo /usr/ include/sys/usr/include/linux ,gcc -I #j m TBo!6$gcc-I/usr/openwin/includeo.co&8-28Bo xF/lib/usr/lib Bo #! .a&.soBo O#lib(T.H! .O gcc -l #P Bo?-LPm!6gcc gccgcc-o-o-oo ooo.co.co.c/usr/lib/libm.a-lm-L/usr/lib-l/usr/temp/lib/x.aLinux:&8?Linux+ru #Q0Bo& l& = ,Linux

3、vK S Linux ,open?write?read?close?iotclLinux ! zz 3) o l& , 3 N Xoc _ o v ,iotcl ) /&Bo19 Linux %? 8?&1. writef buf Lnbytes jBofildesc Bo 8Qj 6:fileds R 8 Fnbytes80aj8-1aj R #include #include main() if (write(1, “Here is some datan”, 18) != 18)write(2, “A write error has occurred on file descriptor

4、1n”,46); exit(0);8?&2. read&read, t #includesize_t read(fildes, void *buf, size_t nbytes);f bBofildesc Bo jnbytes Ujbuf 8Qj Fnbytes 80ajM Bo8-1ajR#include #include main() char buffer128; nread;nread = read(0, buffer, 128); if (nread = -1)write(2, “A read error has occurredn”, 26); if (write(1,buffer

5、,nread) != nread)write(2, “A write error has occurredn”,27); exit(0);8?open, Bo6:,)8 #=read?write&f-, BoG( BoK path/oflags,FP G(BoNh 3Kopen,) 8 Bos WL, Bo 8-1oflags h a &f- open(const char *path,oflags);8?&4. close&!ioctl&close, t #includeclose(fildes);ioctl, t #includeioctl(fildes,cmd,.);G( LED6: i

6、octl(tty_fd,KDSETLED,LED_NUM|LED_CAP|LED_SCR); Bo q #include #include #include#include!main() charc;in,out;inout=open(“file.in”,O_RDONLY);open(“file.out”,O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR);while(read(in,&c,1) = write(out,&c,1);exit(0);1)$TIMEFORMAT=”“time./copy_system4.67user.146.90system2:32.57elaps

7、ed99%CPU$ls-lsfile.infile.out10291029-rw-r-r-rw-11neilneilusersusers10485761048576SepSep171710:4610:51file.infile.oute-2#include!#include#include#includemain()charblock1024;in,out;nread;inout=open(“file.in”,O_RDONLY);open(“file.out”,O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR);while(nread=read(in,block,sizeof(

8、block)0)write(out,block,nread);exit(0);$rmfile.outTIMEFORMAT=”“time./copy_block0.00user0.02system0:00.04elapsed78%CPUI/OI/OstdiofTtdio.h I/O, M) ANSIC !,stdio&,Bo 4 G( Bo# lm K 8K f-I/O stdio XBo estream=Q0 yFILE , I/O fopen,close,fread,write,fflush,fseek,fgetc,getc,getchar,fp an,prp,p shar,fgets,ge

9、ts,prf,fprf,f,fscanf,sscanfe-3 8t!#include#includemain()c;FILE*in,*out;inout=fopen(“file.in”,”r”);fopen(“file.out”,”w”);while(c fpexit(0);=fgetc(in)!=EOF)(c,out);$TIMEFORMAT=”“time./copy_stdio0.06user0.02system0:00.11elapsed81%CPUkvmain!Linux mainC !main(argc,char*argv)f argca argv, !6 $myprogleftri

10、ghtandcenter!argc:argv:4“myprog”,“l(fā)eft”,“right”,“andcenter”Linux #-(.H |!LinuxLinux 5 4 ykvT_ 9 8 JU&!40 2a $!%2a w t!kvmain&!main(argc, char *argv)#include #include main(arg;argc, char *argv) for(arg = 0; arg argc; arg+)if(argvarg0 = -)prf(“option: %sn”, argvarg+1);elseprf(“argument%d: %sn”,arg,arg

11、varg);exit(0);$ ./args -i argument 0: option: i option: lr argument 3: option: fargument 5:-lrhi there -ffred.c./argshi therefred.ckvgetopt &! 1VLinuxgetopt() ) getopt() t !#includegetopt(argc,*optarg;char*constargv,constchar*optstring);externexterncharoptind,opterr,optopt;getopt()/main() argc&argvK

12、 I n Poptstringoptstring a!main(opt;argc, char *argv) while(opt = getopt(argc, argv, “:if:lr”) != -1) switch(opt) case i: case l: case r:prf(“option: %cn”, opt);break;case f:prf(“filename: %sn”, optarg);break;case :prf(“option needs a valuen”);break;case ?:prf(“unknown option: %cn”, optopt);break;fo

13、r(; optind argc; optind+)prexit(0);f(“argument: %sn”, argvoptind);T_Linux +h&D T_y 2a6C putenv()!getenv(). YT_!S #include!char*getenv(constchar*name);putenv(constchar*string);T_putenv()!getenv()&!G$N Cgu 6:/A gu (1)+p 7 !#include #include #includemain(argc,char *argv)char*var,*value;if(argcfpr=1|arg

14、c3) f(stderr,”usage:environvarvaluen”);exit(1);T_putenv()!getenv()&(2)2.,getenv()h$gu !var=argv1;value=getenv(var);if(value)pr elseprf(“Variable%shasvalue%sn”,var,value);f(“Variable%shasnovaluen”,var);T_putenv()!getenv()&(3) A 6: ,putenv()!gu if(argc = 3) char *string; value = argv2;string = malloc(

15、strlen(var)+strlen(value)+2);if(!string) fprf(stderr,”out of memoryn”);exit(1);strcpy(string,var); strcat(string,”=”); strcat(string,value);prf(“Calling putenv with: %sn”,string);if(putenv(string) != 0) fprf(stderr,”putenv failedn”);free(string);exit(1);T_putenv()!getenv()&(4)J5,getenv() gu !value=g

16、etenv(var);if(value)pr elseprf(“Newvalueof%sis%sn”,var,value);f(“Newvalueof%sisnull?n”,var);exit(0);T_putenv()!getenv()&(5) ?y: !$./environHOMEVariableHOMEhasvalue/home/neil$./environFREDVariableFREDhasnovalue o value FRED=o$./environFREDVariableFREDhasnoCallingputenvwith:oNewvalueofFREDis$./environ

17、FREDVariableFREDhasnovalue%f! 9JU%f! 9Linux h& R Linux 9 D time_t sLtime_t AIsL 9 D F8time.h time&;JU 9S !#includetime_ttime(time_t*tloc);8x 6:tloc time() j *tloc!%f! 9time()&!#include #include #include#include$ ./envtimeThe The TheThetime time time time time time time time timetimeis is is is is is

18、 is is isis1179643852117964385411796438561179643858117964386011796438621179643864117964386611796438681179643870main()i; time_tthe_time;The The The The TheThefor(i=1;i =10;i+)the_time=time(time_t*)0);prf(“Thetimeis%ldn”,the_time);sleep(2);exit(0);%f! 9difftime()&;. 9,9 !ft #include!double difftime(ti

19、me_ttime1,time_ttime2);gmtime()&;D 9 uD!#includeft struct tm *gmtime(constyitm )!time_ttimeval);!%f! 9difftime()!gmtime()&H 9!%f!#include#include #includemain() struct time_t (void)tm_ptrtm *tm_ptr; the_time; time(&the_time);= gmtime(&the_time);pr prprf(“Raw time is %ldn”, the_time); f(“gmtime gives

20、:n”);f(“date: %02d/%02d/%02dn”,tm_ptr-tm_year, tm_ptr-tm_mon+1, tm_ptr-tm_mday); f(“time: %02d:%02d:%02dn”,tm_ptr-tm_hour, tm_ptr-tm_min, tm_ptr-tm_sec);prexit(0);%f! 9difftime()!gmtime()&5b1!$./gmtime;date1179644196Rawtimeisgmtimegives:date:time:107/05/2006:56:36SunMay2007:56:37BST2007 8 8 Linux +R

21、 x3o&;x 9b1Q? 86! Linux & r x 8 8C Otmpnam()&; $ 8C!ft #include!char *tmpnam(char*s); r & 8 &tmpfile() (S C MS#!ft #include!FILE*tmpfile(void); 8tmpnam()!tmpfile()*filename;*tmpfp;filename = tmpnam(tmpname);prf(“Temporary file name is: %sn”, filename);tmpfp = tmpfile();if(tmpfp)prf(“Opened a tempora

22、ry file OKn”);elseperror(“tmpfile”);exit(0);$ ./tmpnamTemporary file name is: /tmp/file2S64zc Opened a temporary file OK&2aLinux getuid()!getlogin() J U& NQ2a!ft #include!#includeuid_tgetuid(void);char*getlogin(void);Linux +*&! JU&2a jlzgetpwuid()!getpwnam()!ft #include!#includestruct struct*name);p

23、asswdpasswd*getpwuid(uid_t*getpwnam(constuid);char&2af passwdyi )6 aN!&2aJU&2a!#include #include #include #include#includemain() uid_t uid; gid_t gid;struct passwd *pw;uid gid prpr= getuid();= getgid(); f(“User is %sn”,getlogin();f(“User IDs: uid=%d, gid=%dn”, getpwuid(uid);f(“UID passwd entry:n nam

24、e=%s, pw-pw_name, pw-pw_uid,getpwnam(“root”);f(“root passwd entry:n”);uid, gid);pwpr=uid=%d, gid=%d, home=%s,s=%sn”,pw-pw_gid, pw-pw_dir, pw-pw_s);pw prpr=f(“name=%s, uid=%d, gid=%d, home=%s, s=%sn”,pw-pw_name, pw-pw_uid, pw-pw_gid, pw-pw_dir, pw-pw_sexit(0););402aIJU&2aLinux JU402a jlgethostname()!

25、uname()!S #include!gethostname(char*name,size_tnamelen);#includeuname(structutsname*name);S bVutsname $p- PB!402aJU402a!#include #include #include#includemain() char computer256; struct utsname uts; if(gethostname(computer,255) != 0 | uname(&uts) 0) fprf(stderr, “Could not get host informationn”);ex

26、it(1);pr pr pr prf(“Computer host name f(“System is %s on %sf(“Nodename is %sn”,is %sn”, computer);hardwaren”, uts.sysname, uts.machine); uts.nodename);f(“Veris %s, %sn”, uts.release, uts.ver);exit(0);%2aLinux 4 u m % h&-rk%8G%2a syslog() E)!#includeft void!syslog(priority,constchar*message,args.);w

27、 priorityP* dZP Z6 aN!%2a%2a&!#include #include#includemain() FILE *f;f = fopen(“not_here”,”r”); if(!f)syslog(LOG_ERR|LOG_USER,”oopsexit(0);- %mn”);q 5b1 /var/log/messages%8 G 5!Jun909:24:50suse103syslog:oops-Nosuchfileordirectory!tLinux 5 d t +t 3= gLOt-c t -CPU5 9VE) t-A!8C A6 t D F8limits.h -!t?3

28、= t D F8resource.h resource.h D ? Kt jl!#includegetpriority( setpriority( getrlimit( setrlimit(getrusage(which, which,resource,resource,id_tid_twho);who,priority);structrlimit*r_limit);conststructrlimit*r_limit);who,structrusage*r_usage);!tt&(1) , TBo!#include #include #include #include #include #include#include!tt&(2)work() j Bo1000052.v )CPUk!voidwork() FILE *f;i;double x = 4.5; f = tmpfile();for(i = 0; i 10000; i+) fprf(f,”Do some outputn”);if(ferror(f) fprf(stder

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論