博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

APUE Exercises 4.11

Posted on 2011-05-03 21:07  天地玄黄  阅读(619)  评论(0)    收藏  举报

4.11

In Section 4.21, our version of ftw never changes its directory. Modify this routine so that each time it encounters a directory, it does a chdir to that directory, allowing it to use the filename and not the pathname for each call to lstat. When all the entries in a directory have been processed, execute chdir(".."). Compare the time used by this version and the version in the text.

 

     主要是修改dopath()函数,每当找到一个新的目录后,就使用chdir()把current working directory设置成那个目录,这样路径搜索就会首先从current working directory开始。

     还把fullpath这个全局变量改成了filename,因为不再需要完整路径。

     修改的地方(与fig4.22比较):

          · myftw函数:把fullpath改为filename

          · dopath函数改动很多,标记“//改”的地方

 

main 函数

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

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;

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

    ret = myftw(argv[1], myfunc);

    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);

    exit(ret);
}

 

myftw函数:

/*
 * Descent 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 than can't be read */
#define FTW_NS  4   /* file that we can't stat */

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

static int myftw(char *pathname, Myfunc *func)
{
    int len;
    filename = path_alloc(&len);

    strncpy(filename, pathname, len);
    filename[len-1] = 0;

    return dopath(func);
}

dopath函数:

static int dopath(Myfunc *func)
{
    struct stat statbuf;
    struct dirent *dirp;
    DIR *dp;
    int ret;
    //char *ptr;

    if (lstat(filename, &statbuf) < 0)
        return func(filename, &statbuf, FTW_NS);
    if (S_ISDIR(statbuf.st_mode) == 0)
        return func(filename, &statbuf, FTW_F);

    if ( (ret = func(filename, &statbuf, FTW_D)) != 0)
        return ret;

    /*
    ptr = filename + strlen(filename);
    *ptr++ = '/';
    *ptr = 0;
    */

    if (chdir(filename) < 0)  //改
        err_quit("chdir error");

    if ( (dp = opendir(".")) == NULL)  //改
        return func(".", &statbuf, FTW_DNR);  //改

    while ( (dirp = readdir(dp)) != NULL) { /* read dir entry one by one  */
        if (strcmp(dirp->d_name, ".") == 0 || strcmp(dirp->d_name, "..") == 0)
            continue;
        strcpy(filename, dirp->d_name);  //改

        if ( (ret = dopath(func)) != 0)
            break;
    }
    //ptr[-1] = 0;

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

    if (chdir("..") < 0)  //改
        err_quit("chdir to .. error");

    return ret;
}

myfunc函数:

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);
            }
            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;
}

 

注意:全局变量和函数之前使用static修饰,只表示这个变量或者函数不能在其他文件中引用。没有其他别的意思。

         chdir(“.."); 这个函数中的参数不必是"./temp/”这样的类型,即最后的斜杠不需要。它会改变current working directory,这个应该由什么东西维护,表示现在正在工作的目录,搜索的时候就会首先从这里搜索。

         opendir(“."); 这个函数的参数也和上面函数一样,最后的斜杠不需要的。写成opendir(“./")没有必要。

         readdir()返回的dirp中的dirp->d_name在kdbg中显示得比较奇怪,首先会显示它的正确信息,比如".",之后会跟一大溜乱七八糟的数字。没关系的。