




已閱讀5頁(yè),還剩2頁(yè)未讀, 繼續(xù)免費(fèi)閱讀
版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(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 /*如果定義了路徑名的最大長(zhǎng)度*/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說(shuō)明pathconf執(zhí)行錯(cuò)誤*/ err_sys(pathconf error for _PC_PATH_MAX); else pathmax+; /* 沒(méi)有/說(shuō)明是根目錄,它也有一個(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,說(shuō)明它需要分配空間,把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è)文件也沒(méi)有,這個(gè)類每個(gè)都是0*/ntot = 1; /*特殊技巧,分母為0,置1也無(wú)妨,只要非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);/* * 通過(guò)層次進(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); 文件類型用中說(shuō)明的文件類型宏測(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. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 湖北省荊州市成豐學(xué)校2025學(xué)年高二下學(xué)期期中考試數(shù)學(xué)試題
- 介入輻射考試練習(xí)卷含答案(一)
- 2025年病案信息技術(shù)試題
- 2025年高考第一次模擬考試語(yǔ)文(新高考Ⅰ卷02)(考試版A3)
- 職業(yè)資格-交通工程真題庫(kù)-19
- 職業(yè)資格-公路水運(yùn)公共基礎(chǔ)真題庫(kù)-14
- 財(cái)務(wù)管理考試真題解析與復(fù)習(xí)體會(huì)試題及答案
- 陜西在職碩士考試試題及答案
- 財(cái)務(wù)管理考試復(fù)習(xí)中的心理平衡技巧試題及答案
- 托幼保健醫(yī)考試試題及答案
- 安全管理人員安全生產(chǎn)責(zé)任制考核表
- 無(wú)勞動(dòng)關(guān)系證明
- 設(shè)備日常維護(hù)保養(yǎng)檢查評(píng)分表
- 國(guó)有股權(quán)轉(zhuǎn)讓法律意見書
- 六年級(jí)說(shuō)明文閱讀復(fù)習(xí)公開課課件
- 監(jiān)理規(guī)劃(精裝修)(DOC)
- 《守株待兔》“課本劇”背景PPT
- 互聯(lián)網(wǎng)+大學(xué)生創(chuàng)新創(chuàng)業(yè)項(xiàng)目計(jì)劃書范本-智能外賣柜項(xiàng)目創(chuàng)業(yè)計(jì)劃書
- 閩教版英語(yǔ)五年級(jí)下知識(shí)點(diǎn)歸納及練習(xí)
- 酒店開業(yè)籌備計(jì)劃表
- (礦業(yè)有限公司)安全生產(chǎn)責(zé)任制度+安全生產(chǎn)責(zé)任制
評(píng)論
0/150
提交評(píng)論