0

「others exec」権限を持つファイルのパス内のすべてのサブフォルダーを見つけようとしています。

strtok(path_str,"/")パス文字列を分割するために使用しようとしましたstat()が、実行するプロセスのルートのサブディレクトリに使用すると、「ファイルまたはフォルダーではありません」というエラーが発生します。

このエラーを克服する方法について何か提案はありますか?

4

2 に答える 2

1

パスが、の場合、、、、およびを呼び出す"long/path/to/the/file.txt"必要がありstat()ます。これらをチェックする順序を気にしない場合、最も簡単な方法はおそらく繰り返し使用することです:"long""long/path""long/path/to""long/path/to/the"strrchr()

char *s;

while (s = strrchr(path, '/'))
{
    *s = 0;
    if (strlen(path) > 0)
        stat(path, &statbuf);
    else
        stat("/", &statbuf);

    /* Do something with statbuf */
}

/(特殊なケーシングは、ルートディレクトリ自体をチェックするために、で始まるパス用です)。

于 2010-12-30T12:04:25.980 に答える
0

私はそれを修正しました、

最初に、パスから最初の「/」を削除しました (理由が完全にはわかりません)。コードを do-while に変更して、最後にファイルにアクセスしました。したがって、コード全体は次のとおりです。

do{
    int retval;
    if (temp_ptr != NULL) //before the first strrchr its null
        *temp_ptr = 0;
    if (*temp_path)
       retval = stat(temp_path, statbuf);
    else
        retval = stat("/", statbuf);
    if (retval < 0){
        perror("stat");
    }
     printf("%s\n",temp_path);

    if(S_ISDIR(statbuf->st_mode)){
        printf("\tis a directory\n");
    }else if(S_ISREG(statbuf->st_mode)){
        printf("\tis a regular file\n");
    }


}   while ((temp_ptr = strrchr(temp_path, '/')));

カフェの皆様、ご協力ありがとうございました。

于 2011-01-02T09:34:34.793 に答える