标题: 谁有遍历目录的程序
realhac
新生入学
Rank: 1
注册用户



UID 168144
精华 0
积分 0(积分有什么用?)
帖子 19
阅读权限 10
注册 2006-9-25
状态 离线
发表于 2007-3-2 10:00  资料  个人空间  短消息  加为好友 
谁有遍历目录的程序

我想编写一个程序,能够遍历指定的目录,该目录下有子目录,有文件,我的目的是能够定位到每一个文件,就像病毒扫描那样。谢了

顶部
dearvoid
版主
Rank: 7Rank: 7Rank: 7
Clark J. Wang



UID 26605
精华 26
积分 61(积分有什么用?)
帖子 4814
阅读权限 100
注册 2002-10-12
来自 Beijing, China
状态 离线
发表于 2007-3-2 12:15  资料  个人空间  短消息  加为好友 
俺以前写的一个简单实现:

/*
*  Author: dearvoid at gmail.com
*
*  $Date: 2006-06-14 09:01:52 +0800 (Wed, 14 Jun 2006) $
*  $HeadURL: svn://svnserver/clark/void/trunk/c/ls-R.c $
*  $Revision: 554 $
*/

#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif

#include <stdio.h>
#include <stdbool.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/stat.h>

bool
isDir(const char * path, const struct dirent * ent)
{
#ifdef HAVE_STRUCT_DIRENT_D_TYPE
    /*
     * On Mac OS X 10.3.x (PPC) the struct dirent does not
     * has the d_type field.
     */
    return ent->d_type == DT_DIR;
#else
    char buf[1024]; // FIXME
    struct stat fstat;

    sprintf(buf, "%s/%s", path, ent->d_name);
    if (stat(buf, &fstat) < 0) {
        perror("stat()");
        exit(1);
    } else {
        return S_ISDIR(fstat.st_mode);
    }
#endif
}

int
ls_R(char * path)
{
    int i;
    int nEntries;
    char buf[1024];     // FIXME
    struct dirent ** list;

    nEntries = scandir(path, &list, NULL, NULL);
    if (nEntries < 0) {
        printf("scandir('%s'): %s\n", path, strerror(errno));
        return 1;
    }

    printf("%s:\n", path);
    for (i = 0; i < nEntries; ++i) {
        printf("\t%s\n", list[i]->d_name);
        if (!isDir(path, list[i]) || !strcmp(list[i]->d_name, ".")
            || !strcmp(list[i]->d_name, "..")) {
            free(list[i]);
            list[i] = NULL;
        }
    }
    printf("\n");

    for (i = 0; i < nEntries; ++i) {
        if (list[i]) {
            sprintf(buf, "%s/%s", path, list[i]->d_name);
            ls_R(buf);
        }
    }

    for (i = 0; i < nEntries; ++i) {
        free(list[i]);
    }
    free(list);

    return 0;
}

int
main(int argc, char *argv[])
{
    if (argc != 2) {
        printf("Usage:\n");
        printf("    %s /your/dir\n", argv[0]);
        exit(1);
    }

    ls_R(argv[1]);

    return 0;
}






'
◆ 发帖时请【突出主题】, 以便您的问题能够及时得到回复
◆ 发帖时请将您的【代码】或者【脚本】写在 [code] 和 [/code] 中间
顶部
vcacm
新生入学
Rank: 1
注册用户



UID 193762
精华 0
积分 0(积分有什么用?)
帖子 1
阅读权限 10
注册 2007-5-2
状态 离线
发表于 2007-5-2 11:20  资料  个人空间  短消息  加为好友 
不错。。。

顶部
MMMIX
小试牛刀
Rank: 2
注册用户



UID 147141
精华 0
积分 0(积分有什么用?)
帖子 79
阅读权限 10
注册 2006-1-11
状态 离线
发表于 2007-5-2 16:53  资料  个人空间  短消息  加为好友 
[quote=realhac;570980]我想编写一个程序,能够遍历指定的目录,该目录下有子目录,有文件,我的目的是能够定位到每一个文件,就像病毒扫描那样。谢了[/quote]
6m[4q
km O
看一下 ftw(3).

顶部
wfwt
新生入学
Rank: 1
注册用户



UID 62300
精华 0
积分 0(积分有什么用?)
帖子 16
阅读权限 10
注册 2003-11-7
状态 离线
发表于 2007-5-4 10:16  资料  个人空间  短消息  加为好友 
ftw(3)会跟踪符号链接,用nftw(3),BSD下是fts(3)

顶部
不容错过的IBM 集群技术资源汇总
 



当前时区 GMT+8, 现在时间是 2008-5-17 03:00

    Powered by Discuz! 5.5.0  © 2001-2007 Comsenz Inc.
Processed in 0.107174 second(s), 5 queries

清除 Cookies - 联系我们 - Linux伊甸园 - Archiver - WAP