




已閱讀5頁,還剩2頁未讀, 繼續(xù)免費(fèi)閱讀
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
函數(shù)1:path_alloc函數(shù)為路徑名動(dòng)態(tài)地分配空間/*這個(gè)跟版本關(guān)系很大*/#include apue.h #include #include #ifdef PATH_MAX /*如果定義了路徑名的最大長度*/static int pathmax = PATH_MAX; /*將它賦予我們自己定義的變量pathmax*/#else static int pathmax = 0; /*否則該位置0*/#endif /*上面那個(gè)if結(jié)束*/#define SUSV3 200112L static long posix_version = 0; /* If PATH_MAX is indeterminate, no guarantee this is adequate */ #define PATH_MAX_GUESS 1024 char * path_alloc(int *sizep) /* also return allocated size, if nonnull */ char *ptr; int size; if (posix_version = 0) posix_version = sysconf(_SC_VERSION); if (pathmax = 0) /* first time through */ errno = 0; if (pathmax = pathconf(/, _PC_PATH_MAX) 0)/*PATH_MAX相對(duì)路徑名的最大字節(jié)數(shù),包括null_PC_PATH_MAX*/ if (errno = 0) pathmax = PATH_MAX_GUESS; /* 確認(rèn)是未決的就使用我們猜測(cè)的值 */ else /*errno不為0說明pathconf執(zhí)行錯(cuò)誤*/ err_sys(pathconf error for _PC_PATH_MAX); else pathmax+; /* 沒有/說明是根目錄,它也有一個(gè)路徑 */ if (posix_version SUSV3) /*size和版本有關(guān)*/ size = pathmax + 1; else size = pathmax; if (ptr = malloc(size) = NULL) /*分配空間失敗則返回空指針*/ err_sys(malloc error for pathname); if (sizep != NULL) *sizep = size; /*如果傳入的參數(shù)不是null,說明它需要分配空間,把size分給它*/return(ptr); /*返回這段空間的首地址指針*/ 遞歸降序遍歷目錄層次結(jié)構(gòu),并按文件類型計(jì)數(shù)#include#include#include#include#includeourhdr.htypedefintMyfunc(const char *, const struct stat *, int);/* function type thats called for each filename */static Myfuncmyfunc;static intmyftw(char *, Myfunc *);static intdopath(Myfunc *);static longnreg, ndir, nblk, nchr, nfifo, nslink, nsock, ntot;intmain(int argc, char *argv)intret;if (argc != 2) /*輸入的參數(shù)個(gè)數(shù)不為2,提醒輸入的格式*/err_quit(usage: ftw );ret = myftw(argv1, myfunc);/* 解析整個(gè)完整的路徑 */if ( (ntot = nreg + ndir + nblk + nchr + nfifo + nslink + nsock) = 0) /*一個(gè)文件也沒有,這個(gè)類每個(gè)都是0*/ntot = 1; /*特殊技巧,分母為0,置1也無妨,只要非0即可 0/1=0*/printf(regular files = %7ld, %5.2f %n, nreg, nreg*100.0/ntot);printf(directories = %7ld, %5.2f %n, ndir, ndir*100.0/ntot);printf(block special = %7ld, %5.2f %n, nblk, nblk*100.0/ntot);printf(char special = %7ld, %5.2f %n, nchr, nchr*100.0/ntot);printf(FIFOs = %7ld, %5.2f %n, nfifo, nfifo*100.0/ntot);printf(symbolic links = %7ld, %5.2f %n, nslink,nslink*100.0/ntot);printf(sockets = %7ld, %5.2f %n, nsock, nsock*100.0/ntot);exit(ret);/* * Descend through the hierarchy, starting at pathname. * The callers func() is called for every file. */#defineFTW_F1/* 不是目錄的文件 */#defineFTW_D2/* 目錄*/#defineFTW_DNR3/* 不可以讀的文件 */#defineFTW_NS4/* 不能被stat的文件 */static char*fullpath; /* 包含每個(gè)文件的完整路徑名 */static int /* 我們返回任何func()函數(shù)返回的*/myftw(char *pathname, Myfunc *func)fullpath = path_alloc(NULL);/這里采用默認(rèn)的path_alloc大小*/*這里調(diào)用path_alloc函數(shù),所以要gcc 2.c error.c path_alloc.c -o 2*/strcpy(fullpath, pathname); /不用像課本那樣,strcpy自動(dòng)在結(jié)束的位置補(bǔ)上/n /* 為fullpath變量初始化 */ return(dopath(func);/* * 通過層次進(jìn)行下降操作,我們從全路徑開始 * 如果是一個(gè)絕對(duì)的路徑而非一個(gè)目錄,我們調(diào)用lstat函數(shù) * call func(), and return. For a directory, we call ourself * recursively for each name in the directory. */static int dopath(Myfunc* func)struct statstatbuf; /*statbuf是stat結(jié)構(gòu)體的實(shí)例*/struct dirent*dirp; /*為獲取文件夾目錄所用的結(jié)構(gòu)體*/DIR*dp; /*與目錄有關(guān)的指針*/intret;char*ptr; /*保存要顯示的字符串*/if (lstat(fullpath, &statbuf) d_name, .) = 0 | strcmp(dirp-d_name, .) = 0)continue; /*忽略.和.*/strcpy(ptr, dirp-d_name);/* append name after slash */if ( (ret = dopath(func) != 0)/* recursive */break;/* time to leave */ptr-1 = 0;/* erase everything from slash onwards */if (closedir(dp) st_mode & S_IFMT) case S_IFREG:nreg+;break;case S_IFBLK:nblk+;break;case S_IFCHR:nchr+;break;case S_IFIFO:nfifo+;break;case S_IFLNK:nslink+;break;case S_IFSOCK:nsock+;break;case S_IFDIR:err_dump(for S_IFDIR for %s, pathname);/* directories should have type = FTW_D */break;case FTW_D:ndir+;break;case FTW_DNR:err_ret(cant read directory %s, pathname);break; case FTW_NS:err_ret(stat error for %s, pathname);break;default:err_dump(unknown type %d for pathname %s, type, pathname);return(0);3. lstat函數(shù)int lstat(const char *pathname, struct stat *buf); 文件類型用中說明的文件類型宏測(cè)試S_ISREG(mode) = 1正規(guī)文件(S_IFREG)S_ISDIR(mode) = 1目錄文件(S_IFDIR)S_ISCHR(mode) = 1字符特殊文件(S_IFCHR)S_ISBLK(mode) = 1塊特殊文件(S_IFBLK)S_ISFIFO(mode) = 1管道或FIFO文件(S_IFFIFO)S_ISLNK(mode) = 1符號(hào)鏈接(S_IFLNK)S_ISSOCK(mode) = 1套接口(S_IFSOCK)針對(duì)每個(gè)命令參數(shù)打印其文件類型#include#include#includeourhdr.hint main(int argc, char *argv)inti;struct statbuf;char*ptr;for (i = 1; i argc; i+) printf(%s: , argvi);if (lstat(argvi, &buf) 0) err_ret(lstat error);continue;if (S_ISREG(buf.st_mode)ptr = regular;else if (S_ISDIR(buf.st_mode)ptr = directory;else if (S_ISCHR(buf.st_mode)ptr = character special);else if (S_ISBLK(buf.st_mode)ptr
溫馨提示
- 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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 小小農(nóng)場(chǎng)體驗(yàn)活動(dòng)的組織計(jì)劃
- 領(lǐng)導(dǎo)崗位任職資格設(shè)置計(jì)劃
- 數(shù)據(jù)科學(xué)在商業(yè)中的應(yīng)用試題及答案
- 學(xué)校秋季特色課程設(shè)計(jì)計(jì)劃
- 業(yè)務(wù)計(jì)劃編制與風(fēng)險(xiǎn)考核試題及答案
- 計(jì)算機(jī)網(wǎng)絡(luò)安全管理題及答案
- 高中階段學(xué)業(yè)規(guī)劃輔導(dǎo)計(jì)劃
- 秋季全員培訓(xùn)與學(xué)習(xí)計(jì)劃
- 備考2025年VB考試試題資源
- 2025屆四川省眉山市名校數(shù)學(xué)八下期末檢測(cè)模擬試題含解析
- 《計(jì)算機(jī)網(wǎng)絡(luò)基礎(chǔ)》課件-OSI參考模型
- 工程量清單及招標(biāo)控制價(jià)編制服務(wù)采購服務(wù)方案
- 心源性猝死的預(yù)防和急救
- 輸血科感控知識(shí)培訓(xùn)課件
- 《常見職業(yè)病危害與防護(hù)宣傳手冊(cè)》
- 兒童主任培訓(xùn)課件
- 土地平整工程施工方案與技術(shù)措施
- 變壓器損耗對(duì)照表
- 2025版電動(dòng)車充電站投資合作合同范本2篇
- 醫(yī)院水電維護(hù)與管理方案
- 重點(diǎn)和難點(diǎn)工程的施工方案、方法與技術(shù)措施
評(píng)論
0/150
提交評(píng)論