本程式使用了一些對目錄進行操作的函式編寫了乙個遍歷檔案層次結構的程式,最後對各種型別的檔案計數。這個程式只有乙個引數,它說明起點路徑名,從該點開始遞迴降序遍歷檔案層次結構。其中還用到了乙個為路徑名動態分配儲存區的函式path_alloc。
// ftw.c
// 2015-08-18 lucifer zhang
// recursively descend a directory hierarchy, counting file types
#include "apue.h"
#include "pathalloc.h"
#include #include // function type that is called for each filename
typedef int myfunc(const char *, const struct stat *, int);
static myfunc myfunc;
static int myftw(char *, myfunc *);
static int dopath(myfunc *);
static long nreg, ndir, nblk, nchr, nfifo, nslink, nsock, ntot;
int main(int argc, char *argv)
ret = myftw(argv[1], myfunc); // does it all
ntot = nreg + ndir + nblk + nchr + nfifo + nslink + nsock;
if (ntot == 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("fifls = %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 caller's func() is called for every file.
* */
#define ftw_f 1 // file other than directory
#define ftw_d 2 // directory
#define ftw_dnr 3 // directory thar can't be read
#define ftw_ns 4 // file that we can't stat
static char *fullpath; // contains full pathname for every file
static size_t pathlen;
static int myftw(char *pathname, myfunc *func) // we return whatever func() returns
}strcpy(fullpath, pathname);
return (dopath(func));}/*
* descend through the hierarchy, starting at "fullpath".
* if "fullpath" is anything other than a directory, we lstat() it
* call func(), and return. for a directory, we call ourself
* recursively for each name in the directory.
* */
static int dopath(myfunc *func) // we return whatever func() returns
if (s_isdir(statbuf.st_mode) == 0)
/** it's a directory. first call func() for the directory,
* then process each filename in the directory.
* */
if ((ret = func(fullpath, &statbuf, ftw_d)) != 0)
n = strlen(fullpath);
if (n + name_max +2 > pathlen)
}fullpath[n++] = '/';
fullpath[n] = 0;
if ((dp = opendir(fullpath)) == null)
while ((dirp = readdir(dp)) != null)
if ((ret = dopath(func)) != 0)
}fullpath[n - 1] = 0; // erase everything from slash onward
if (closedir(dp) < 0)
return ret;
}static int myfunc(const char *pathname, const struct stat *statptr, int type)
break;
case ftw_d:
++ndir;
break;
case ftw_dnr:
err_ret("can't read directory %s", pathname);
break;
case ftw_ns:
err_ret("stat error for %s", pathname);
break;
default:
err_dump("unknow type %d for pathname %s", type, pathname);
}return 0;
}
// pathalloc.h
// 2015-08-18 lucifer zhang
// dynamically allocate space for a pathname
#include "apue.h"
#include #include #ifdef path_max
static long pathmax = path_max;
#else
static long pathmax = 0;
#endif
static long posix_version = 0;
static long xsi_version = 0;
// if path_max is indeterminate, no guarantee this is adequate
#define path_max_guess 1024
char* path_alloc(size_t *sizep)
if (xsi_version == 0)
if (pathmax == 0) else
} else }/*
* before posix.1-2001, we aren't guaranteed that phat_max includes
* the terminating null byte. same goes for xpg3.
*/if ((posix_version < 200112l) && (xsi_version < 4)) else
if ((ptr = malloc(size)) == null)
if (sizep != null)
return ptr;
}
centos6.7上的測試結果:
遞迴降序遍歷層次目錄
關於path alloc函式 include include include include include include function type that is called for each filename typedef int myfunc const char const stru...
遞迴遍歷目錄
遞迴 乙個函式內部在其內部不呼叫其他函式,而是呼叫自身,類似迴圈 注,自己玩自己,防止死遞迴 使用遞迴來遍歷出乙個目錄中的所有檔案 import os def getalldirre path,sp 引數 路徑 返回值 none 完成功能,獲取呼叫者傳遞的路徑下的所有檔案 檔案 直接顯示,資料夾 二...
層次遍歷的非遞迴演算法
include include btree.h include using namespace std intmain else if q rchild null else printf n 4 二叉樹深度 d n btnodedepth p printf n 5 層次遍歷 dispbtnodebe...