




已閱讀5頁,還剩2頁未讀, 繼續免費閱讀
版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
函數1:path_alloc函數為路徑名動態地分配空間/*這個跟版本關系很大*/#include apue.h #include #include #ifdef PATH_MAX /*如果定義了路徑名的最大長度*/static int pathmax = PATH_MAX; /*將它賦予我們自己定義的變量pathmax*/#else static int pathmax = 0; /*否則該位置0*/#endif /*上面那個if結束*/#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相對路徑名的最大字節數,包括null_PC_PATH_MAX*/ if (errno = 0) pathmax = PATH_MAX_GUESS; /* 確認是未決的就使用我們猜測的值 */ else /*errno不為0說明pathconf執行錯誤*/ err_sys(pathconf error for _PC_PATH_MAX); else pathmax+; /* 沒有/說明是根目錄,它也有一個路徑 */ if (posix_version SUSV3) /*size和版本有關*/ size = pathmax + 1; else size = pathmax; if (ptr = malloc(size) = NULL) /*分配空間失敗則返回空指針*/ err_sys(malloc error for pathname); if (sizep != NULL) *sizep = size; /*如果傳入的參數不是null,說明它需要分配空間,把size分給它*/return(ptr); /*返回這段空間的首地址指針*/ 遞歸降序遍歷目錄層次結構,并按文件類型計數#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) /*輸入的參數個數不為2,提醒輸入的格式*/err_quit(usage: ftw );ret = myftw(argv1, myfunc);/* 解析整個完整的路徑 */if ( (ntot = nreg + ndir + nblk + nchr + nfifo + nslink + nsock) = 0) /*一個文件也沒有,這個類每個都是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; /* 包含每個文件的完整路徑名 */static int /* 我們返回任何func()函數返回的*/myftw(char *pathname, Myfunc *func)fullpath = path_alloc(NULL);/這里采用默認的path_alloc大小*/*這里調用path_alloc函數,所以要gcc 2.c error.c path_alloc.c -o 2*/strcpy(fullpath, pathname); /不用像課本那樣,strcpy自動在結束的位置補上/n /* 為fullpath變量初始化 */ return(dopath(func);/* * 通過層次進行下降操作,我們從全路徑開始 * 如果是一個絕對的路徑而非一個目錄,我們調用lstat函數 * 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結構體的實例*/struct dirent*dirp; /*為獲取文件夾目錄所用的結構體*/DIR*dp; /*與目錄有關的指針*/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函數int lstat(const char *pathname, struct stat *buf); 文件類型用中說明的文件類型宏測試S_ISREG(mode) = 1正規文件(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符號鏈接(S_IFLNK)S_ISSOCK(mode) = 1套接口(S_IFSOCK)針對每個命令參數打印其文件類型#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等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 高血黏度的預防和控制
- 寵物解剖考試題及答案
- 漢族音樂節奏課件
- 2025年 廣州醫科大學附屬醫院招聘筆試考試試卷附答案
- 農機保養培訓課件
- 電力設備安裝培訓
- 住家養老護理培訓課件
- 法治教育與宣傳體系構建
- 物業防汛演練培訓
- 動畫大師制作教程
- FZ/T 10025-2022本色布技術要求規范
- GB/T 5097-2005無損檢測滲透檢測和磁粉檢測觀察條件
- GB/T 27770-2011病媒生物密度控制水平鼠類
- GB/T 1041-2008塑料壓縮性能的測定
- GA/T 527.1-2015道路交通信號控制方式第1部分:通用技術條件
- 社區社群團長招募書經典案例干貨課件
- 物理必修一第一章章末檢測卷(一)
- 蘇教版六年級科學下冊單元測試卷及答案(全冊)
- 如何審議預算及其報告新演示文稿
- 融資并購項目財務顧問協議賣方大股東為個人模版
- 北京市朝陽區2020-2021學年五年級下學期期末考試語文試卷(含答案解析)
評論
0/150
提交評論