ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

unix - 文件I/O - 目录遍历

2021-10-05 22:35:24  阅读:274  来源: 互联网

标签:遍历 return FTW int unix func directory fullpath 目录


示例代码

#include "apue.h"
#include <dirent.h>
#include <limits.h>
#include <time.h>

/* 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[])
{
    int        ret;
    clock_t c_start,c_end;
    
    c_start = clock();

    if (argc != 2)
        err_quit("usage:  ftw  <starting-pathname>");

    ret = myftw(argv[1], myfunc);        /* does it all */

    ntot = nreg + ndir + nblk + nchr + nfifo + nslink + nsock;
    if (ntot == 0)
        ntot = 1;        /* avoid divide by 0; print 0 for all counts */
    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);

    c_end = clock();
    printf("elapsed: %f seconds/n", (c_end-c_start)/(double)CLOCKS_PER_SEC); 
    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 that can't be read */
#define    FTW_NS    4        /* file that we can't stat */

static char    *fullpath;        /* contains full pathname for every file */

static char *filename; 

static int                    /* we return whatever func() returns */
myftw(char *pathname, Myfunc *func)
{
    int len;
    filename = path_alloc(&len); 
    fullpath = path_alloc(&len);    /* malloc's for PATH_MAX+1 bytes */
                                    /* ({Prog pathalloc}) */
    strncpy(fullpath, pathname, len);    /* protect against */
    fullpath[len-1] = 0;                /* buffer overrun */
    strcpy(filename,fullpath); 

    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                    /* we return whatever func() returns */
dopath(Myfunc* func)
{
    struct stat        statbuf;
    struct dirent    *dirp;
    DIR                *dp;
    int                ret;
    char            *ptr;

    if (lstat(filename , &statbuf) < 0)    /* stat error */
        return(func(fullpath, &statbuf, FTW_NS));
    if (S_ISDIR(statbuf.st_mode) == 0)    /* not a directory */
        return(func(fullpath, &statbuf, FTW_F));

    /*
     * 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)
        return(ret);

    ptr = fullpath + strlen(fullpath);    /* point to end of fullpath */
    *ptr++ = '/';
    *ptr = 0;
    
    chdir(fullpath) ; /* set local work directory  */

    if ((dp = opendir(fullpath)) == NULL)    /* can't read directory */
        return(func(fullpath, &statbuf, FTW_DNR));

    while ((dirp = readdir(dp)) != NULL) {
        if (strcmp(dirp->d_name, ".") == 0  ||
            strcmp(dirp->d_name, "..") == 0)
                continue;        /* ignore dot and dot-dot */

        strcpy(ptr, dirp->d_name);    /* append name after slash */
        strcpy(filename,dirp->d_name); 
        if ((ret = dopath(func)) != 0)        /* recursive */
            break;    /* time to leave */
    }
    ptr[-1] = 0;    /* erase everything from slash onwards */
    chdir("..");  /* return to parent directory */

    if (closedir(dp) < 0)
        err_ret("can't close directory %s", fullpath);

    return(ret);
}

static int
myfunc(const char *pathname, const struct stat *statptr, int type)
{
    switch (type) {
    case FTW_F:
        switch (statptr->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("can't 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);
}

标签:遍历,return,FTW,int,unix,func,directory,fullpath,目录
来源: https://www.cnblogs.com/feiwatson/p/15369783.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有