操作系統(tǒng)第三次實(shí)驗(yàn)報(bào)告_第1頁
操作系統(tǒng)第三次實(shí)驗(yàn)報(bào)告_第2頁
操作系統(tǒng)第三次實(shí)驗(yàn)報(bào)告_第3頁
操作系統(tǒng)第三次實(shí)驗(yàn)報(bào)告_第4頁
操作系統(tǒng)第三次實(shí)驗(yàn)報(bào)告_第5頁
已閱讀5頁,還剩3頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

-7-操作系統(tǒng)實(shí)驗(yàn)報(bào)告實(shí)驗(yàn)名稱:實(shí)現(xiàn)ls命令實(shí)驗(yàn)計(jì)算機(jī)科學(xué)與技術(shù)學(xué)院目錄一、實(shí)驗(yàn)?zāi)康暮鸵?2二、實(shí)驗(yàn)內(nèi)容 2三、實(shí)驗(yàn)步驟 2四、實(shí)驗(yàn)結(jié)果與分析 31.輸出當(dāng)前目錄下文件 32.輸出指定目錄下文件 3五、程序源代碼 5六、實(shí)驗(yàn)體會 7一、實(shí)驗(yàn)?zāi)康暮鸵?在linux系統(tǒng)下用C語言編寫一個(gè)程序?qū)崿F(xiàn)linux系統(tǒng)下ls命令的功能。二、實(shí)驗(yàn)內(nèi)容 問題:編寫一個(gè)程序?qū)崿F(xiàn)linux系統(tǒng)下ls命令的功能。首先通過Linux系統(tǒng)的文件目錄流打開函數(shù)DIR*opendir(constchar*name);來先打開文件目錄,然后通過系統(tǒng)調(diào)用函數(shù)structdirent*readdir(DIR*dirp);讀取文件目錄下的文件信息。其中文件結(jié)構(gòu)體dirent的結(jié)構(gòu)如下:structdirent{ino_td_ino;/*inodenumber*/off_td_off;/*offsettothenextdirent*/unsignedshortd_reclen;/*lengthofthisrecord*/unsignedchard_type;/*typeoffile;notsupportedbyallfilesystemtypes*/chard_name[256];/*filename*/};三、實(shí)驗(yàn)步驟 根據(jù)實(shí)驗(yàn)要求,實(shí)現(xiàn)指令ls的基本功能,即實(shí)現(xiàn)列出當(dāng)前目錄下的文件名,還有就是實(shí)現(xiàn)列出指定目錄下的所有文件的文件名信息。 在編寫好代碼之后編譯代碼然后輸入指令測試實(shí)驗(yàn)結(jié)果是否符合要求。四、實(shí)驗(yàn)結(jié)果與分析1、輸入指令:./myls輸入默認(rèn)的指令,輸出當(dāng)前目錄下的文件名稱,并用不同顏色區(qū)分文件夾和文件的區(qū)別。實(shí)驗(yàn)結(jié)果如下圖圖1所示:圖1輸出當(dāng)前目錄下所有的文件名2、輸入指令:./myls./Documents給定一個(gè)制定的路徑,輸出所給定路徑下所有文件的文件名。結(jié)果如下圖圖2所示:圖2輸出指定目錄下的所有文件名五、程序源代碼/**************************FileName:myls.c*Author:*Date:2014/1/5***************************/#include<stdio.h>#include<stdlib.h>#include<dirent.h>#include<errno.h>#include<string.h>#include<sys/ioctl.h>#include<unistd.h>#include<termios.h>#include<sys/stat.h>#defineSPACE4/*自定義的字符串結(jié)構(gòu)體*/typedefstruct{ char*str;/*實(shí)際內(nèi)容*/ unsignedlength;/*字符串長度*/}String;/*文件信息結(jié)構(gòu)體*/typedefstruct{ unsignedlength;/*集合中文件名的數(shù)量*/ unsignedi,j;/*i是行數(shù),j是列數(shù)*/ String*filenames; unsignedmaxlen;/*文件名集合中最大的文件名長度*/ char**formats;/*格式化輸出序列*/}FormatInfo;/*獲取無符號整型數(shù)十進(jìn)制數(shù)的長度*/unsignedunsigned_length(unsignedn){ unsignedlength=0; do{ ++length; n/=10; }while(n) returnlength;}/*預(yù)讀根目錄,將格式化輸出的信息保存在info中*/voidPreread(char*root,FormatInfo*info){ DIR*dir; unsignedi,j,count=0; structdirent*ptr; Stringfilename; unsigned*g=NULL;/*g是根據(jù)目錄下的目錄項(xiàng)名的長度進(jìn)行分組的輔助變量*/ unsignedwidth=0; unsignedgroup=0; unsignedmax=0; unsignedrow=1; unsignedpos=0; structwinsizet_size; ioctl(STDIN_FILENO,TIOCGWINSZ,&t_size);/*獲取終端的行數(shù)和列數(shù)*/ if(info==NULL){ printf("Error!\"info\"cannotbeNULL!\n"); return; } dir=opendir(root);

if(dir==NULL){ perror("Failtoopendir.\n"); exit(1); } while(readdir(dir))++count;/*統(tǒng)計(jì)文件和目錄的個(gè)數(shù)*/ rewinddir(dir);/*回卷讀取目錄的指針*/ info->filenames=(String*)malloc(sizeof(String)*count); g=(unsigned*)malloc(sizeof(unsigned)*count); i=0;/*獲取文件或目錄名和計(jì)算其長度*/ while((ptr=readdir(dir))!=NULL){ filename.str=ptr->d_name;/*文件或目錄名*/ filename.length=strlen(ptr->d_name); info->filenames[i++]=filename; }/*對目錄項(xiàng)根據(jù)目錄名或文件名用選擇排序法從小到大進(jìn)行排序*/ for(i=0;i<count;++i){ for(j=i;j<count;++j){ if(strcmp(info->filenames[i].str, info->filenames[j].str)>0){ filename=info->filenames[i]; info->filenames[i]=info->filenames[j]; info->filenames[j]=filename; } } } /*用試探的方法測試最后列表的組數(shù)*/ group=count;/*先假定分為count個(gè)列表*/ row=1;/*每列的行數(shù)為1*/ do{ for(j=0,width=0;j<group;++j){ for(i=0,max=0;i<row;++i){ /*找出每一列中文件或目錄名最長的那一個(gè)*/ pos=j*row+i; if(pos>=count)break; if(info->filenames[pos].length>max){ max=info->filenames[pos].length; g[j]=max;/*把每個(gè)列表的最大寬度存放到g數(shù)組里*/ } } width+=(max+SPACE);/*將各組最長的文件或目錄名的長度相加,把列表之間的間隔也算上*/ } /*假如其總寬度大于終端的寬度(列數(shù)),則把分組列表的數(shù)量減少,繼續(xù)試探。*/ if(width>=t_size.ws_col){ --group; row=(count%group)?(count/group+1):(count/group); }else{

break;/*否則,估算列表的數(shù)量結(jié)束*/ } }while(1); info->formats=(char**)malloc(sizeof(char*)*group); info->maxlen=0; for(i=0;i<group;++i){ if(g[i]>info->maxlen) info->maxlen=g[i];/*這里找出最大文件或目錄名的長度*/ g[i]+=SPACE;/*把列表之間的間隔算上*/ j=unsigned_length(g[i])+4; info->formats[i]=(char*)malloc(sizeof(char)*j); sprintf(info->formats[i],"%%-%us",g[i]);/*這里就是組織格式化輸出字符串的關(guān)鍵部分*/ info->formats[i][j]='\0'; } putchar('\n'); info->length=count; info->i=row;/*列表行數(shù)*/ info->j=group;/*列表數(shù)量*/ free(g); closedir(dir);}voidMyls(char*root){ FormatInfofi; inti,j,pos,len; char*filepath=NULL; structstatbuf; Preread(root,&fi);/*預(yù)讀目錄*/ len=strlen(root); filepath=(char*)malloc(sizeof(char)*(strlen(root)+fi.maxlen+1)); filepath[0]='\0'; strcat(filepath,root); if(filepath[len-1]!='/'){ filepath[len++]='/'; } for(i=0,pos=0;i<fi.i;++i){ for(j=0;j<fi.j;++j){ pos=j*fi.i+i; if(pos<fi.length){ filepath[len]='\0'; strcat(filepath,fi.filenames[pos].str); stat(filepath,&buf); if(S_ISDIR(buf.st_mode)){

printf("\033[01;36m");//Setcolor_dark-green }else{ printf("\033[0m");//Setdefault_color } printf(fi.formats[j],fi.filenames[pos]); }elseif(i<fi.i){ break; }else{ putchar('\n'); return; } } putchar('\n'); } printf("\033[0m");//Restoredefault_color free(filepath);}intmain(intargc,char*argv[]){ if(argc<1){ printf("參數(shù)錯(cuò)誤!/n"); exit(1); }elseif(argc==1){ Myls("."); }else{ Myls(argv[1]); } return0;}六、實(shí)驗(yàn)體會 這次實(shí)驗(yàn)明顯感覺到比前兩次實(shí)驗(yàn)要簡單一些,因?yàn)檫@些內(nèi)

溫馨提示

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

評論

0/150

提交評論