简体   繁体   English

我如何获得此readdir代码示例以搜索其他目录

[英]how can I get this readdir code sample to search other directories

I am currently working with a code example that initially is designed to take an argument, then search for that argument in the current directory, I've tried to make it search another directory (/dev/shm to exact) by replacing the "." 我目前正在使用一个代码示例,该示例最初旨在接受一个参数,然后在当前目录中搜索该参数,我试图通过替换“使其搜索另一个目录(精确到/ dev / shm)。 “ with "/dev/shm" but the code turns up nothing when i search for something* (notice the wildcard). 与“ / dev / shm”一起使用,但是当我搜索某些内容*时,代码却什么都没出现(注意通配符)。 The wild card search works fine in the current directory so I do not think it is the wild card that is the problem, If someone could help me out though I would really appreciate it, thanks! 通配符搜索在当前目录中可以正常工作,因此我认为不是通配符成为问题,如果有人可以帮助我,尽管我真的很感激,谢谢!

#include <dirent.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>


static void lookup(const char *arg)
{
    DIR *dirp;
    struct dirent *dp;


    if ((dirp = opendir(".")) == NULL) {
        perror("couldn't open '.'");
        return;
    }


    do {
        errno = 0;
        if ((dp = readdir(dirp)) != NULL) {
            if (strcmp(dp->d_name, arg) != 0)
                continue;


            (void) printf("found %s\n", arg);
            (void) closedir(dirp);
                return;


        }
    } while (dp != NULL);


    if (errno != 0)
        perror("error reading directory");
    else
        (void) printf("failed to find %s\n", arg);
    (void) closedir(dirp);
    return;
}


int main(int argc, char *argv[])
{
    int i;
    for (i = 1; i < argc; i++)
        lookup(argv[i]);
    return (0);
}

opendir doesn't handle wildcards. opendir不处理通配符。 It expects a real directory path. 它需要一个真实的目录路径。 I'm not sure what you mean when you say 你说的时候我不确定你的意思

wildcard search works in the current directory 通配符搜索在当前目录中有效

If you mean it works in your shell, that's to be expected. 如果您认为它可以在您的Shell中运行,那是可以预期的。 The shell will first expand the wildcard and then perform the command you typed. Shell将首先展开通配符,然后执行您键入的命令。

So how to solve this? 那么如何解决这个问题? Expand the wildcard yourself using glob before calling opendir . 在调用opendir之前,使用glob自己扩展通配符。


Edit: sorry, I thought you were trying to match the wildcard in the directory name. 编辑:对不起,我以为您正在尝试匹配目录名称中的通配符。 It looks like you want to match directory contents using the wildcard. 看起来您想使用通配符来匹配目录内容。 In that case simply replace 在这种情况下,只需更换

if (strcmp(dp->d_name, arg) != 0)

with

if (fnmatch(arg, dp->d_name, 0) != 0)

You could also use glob for this. 您也可以为此使用glob It will actually replace the call to opendir and the loop. 它实际上将替换对opendir和循环的调用。 Here is an example for using glob : 这是使用glob的示例:

#include <glob.h>
#include <stdio.h>


static void lookup(const char *root, const char *arg)
{
    size_t n;
    glob_t res;
    char **p;

    chdir(root);
    glob(arg, 0, 0, &res);

    n = res.gl_pathc;
    if (n < 1) {
        printf("failed to find %s\n", arg);
    } else {
        for (p = res.gl_pathv; n; p++, n--) {
            printf("found %s\n", *p);
        }
    }
    globfree(&res);
}


int main(int argc, char *argv[])
{
    int i;
    for (i = 2; i < argc; i++)
        lookup(argv[1], argv[i]);
    return (0);
}

I'm not sure what you are expecting. 我不确定您的期望。 If your program is called lookup then if you call it in the 'current directory', where that directory holds files something.1, something.2 and something.3 like this: 如果您的程序被称为lookup那么如果您在“当前目录”中调用该程序,则该目录中将包含文件something.1,something.2和something.3,如下所示:

lookup something*

the shell will expand it to 外壳会将其扩展为

lookup  something.1 something.2 something.3

and your program will see three command line args and will be able to find a match in the readdir loop. 并且您的程序将看到三个命令行参数,并将能够在readdir循环中找到匹配项。

If you change the opendir call to "/dev/shm" and call it from the original directory (the one that has something.[1-3]) then the shell will again expand the wildcard in the current directory . 如果将opendir调用更改为“ / dev / shm”并从原始目录(包含东西的文件。[1-3])中调用它,则Shell将再次in the current directory扩展通配符。 But unless the files something.1, something.2 and something.3 are also present in /dev/shm, the readdir loop will not see them. 但是,除非在/ dev / shm中也存在something.1,something.2和something.3文件,否则readdir循环将看不到它们。

Note that your lookup function is a bit odd. 请注意,您的查询功能有点奇怪。 I would expect it to be more like this: 我希望它更像这样:

    static int lookup(const char * dir, const char *arg)
    {
        DIR *dirp;
        struct dirent *dp;

        if ((dirp = opendir(dir)) == NULL) {
            perror(dir);
            return -1;
        }

        while ((dp = readdir(dirp)) != NULL) {
            if (!strcmp(dp->d_name, arg)) {
                break;
            }
        }
        (void) closedir(dirp);

        printf("%s %s\n", dp ? "found" : "failed to find", arg);
        return 0;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM