4

この fts_children() の質問に頭を悩ませています。man ページhttp://www.kernel.org/doc/man-pages/online/pages/man3/fts.3.htmlでは、As a special case, if fts_read() has not yet been called for a hierarchy, fts_children() will return a pointer to the files in the logical directory specified to fts_open(), that is, the arguments specified to fts_open(). どのファイルがリンクされたリストであるかを明確に示しています。現在のディレクトリが返されます。そうではないことがわかりました。この問題について何か助けていただければ幸いです。リンクされたリストが返されることを期待していたので、それを繰り返し処理して、一致するファイル名 (最終目標) を持つファイルを見つけました。ただし、現在、リンクされたリストを繰り返し処理しようとしています (ベイビーステップ)。現在、1 つのファイルを返し、ループを終了します。これは私には意味がありません。どんな助けでも大歓迎です!!!

ファイルシステムのオープン:

char* const path[PATH_MAX] = {directory_name(argv[argc-index]), NULL}; 
            char* name = file_name(argv[argc-index]);

            if ((file_system = fts_open(path, FTS_COMFOLLOW, NULL)) == NULL){
                fprintf(stderr,"%s:%s\n", strerror(errno), getprogname());
                        exit(EXIT_FAILURE);
                 }/*Ends the files system check if statement*/

            /*Displays the information about the specified file.*/
            file_ls(file_system,name, flags);

明確にするために、directory_name はユーザーから入力されたパスを解析し、/home/tpar44 のようなものを返します。そのディレクトリが開かれます。

ファイルシステム内を検索:

void
file_ls(FTS* file_system, char* file_name,  int* flags){
    FTSENT* parent = NULL;
    //dint stop = 0;

    parent = fts_children(file_system, 0);

    while( parent != NULL ){
        printf("parent = %s\n", parent->fts_name);
        parent = parent->fts_link;
    }
}

ありがとう!

4

1 に答える 1

4

これは完全に設計によるものだと思います。

…つまり、fts_open()に指定する引数は…

それが言っているのは、便宜上、path_argvパラメーターのルート要素をリストするということです。path_argv配列を論理ディレクトリ自体として扱います。

言い換えれば、これは:

int main(int argc, char* const argv[])
{
    char* const path[] = { ".", "/home", "more/root/paths", NULL }; 

    FTS* file_system = fts_open(path, FTS_COMFOLLOW | FTS_NOCHDIR, &compare);

    if (file_system)
    {
        file_ls(file_system, "", 0);
        fts_close(file_system);
    }
    return 0;
}

出力します

parent = .
parent = /home
parent = more/root/paths

実際、そうです ( http://liveworkspace.org/code/c2d794117eae2d8af1166ccd620d29ebを参照)。

以下は、完全なディレクトリ トラバーサルを示すより完全なサンプルです。

#include<stdlib.h>
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fts.h>
#include<string.h>
#include<errno.h>

int compare (const FTSENT**, const FTSENT**);

void file_ls(FTS* file_system, const char* file_name, int* flags)
{
    FTSENT* node = fts_children(file_system, 0);

    if (errno != 0)
        perror("fts_children");

    while (node != NULL)
    {
        // TODO use file_name and flags
        printf("found: %s%s\n", node->fts_path, node->fts_name);
        node = node->fts_link;
    }
}

int main(int argc, char* const argv[])
{
    FTS* file_system = NULL;
    FTSENT* node = NULL;

    if (argc<2)
    {
        printf("Usage: %s <path-spec>\n", argv[0]);
        exit(255);
    }

    char* const path[] = { argv[1], NULL }; 
    const char* name = "some_name";

    file_system = fts_open(path, FTS_COMFOLLOW | FTS_NOCHDIR, &compare);

    if (file_system)
    {
        file_ls(file_system, name, 0); // shows roots

        while( (node = fts_read(file_system)) != NULL)
            file_ls(file_system, name, 0); // shows child elements

        fts_close(file_system);
    }
    return 0;
}

int compare(const FTSENT** one, const FTSENT** two)
{
    return (strcmp((*one)->fts_name, (*two)->fts_name));
}
于 2012-10-06T21:48:13.650 に答える