apache - 是否可以使用 apache 可移植运行时获取目录中的文件列表?

标签 apache apr

我需要使用 APR 获取目录中的文件列表。我该怎么做? 我在文档中寻找答案,但一无所获。 谢谢!

最佳答案

你想要的函数是apr_dir_open。我发现头文件是 APR 的最佳文档

http://apr.apache.org/docs/apr/1.4/group_apr_dir.html

这是读取“.”的示例。并报告错误(如果遇到)

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
#include <apr.h>
#include <apr_errno.h>
#include <apr_pools.h>
#include <apr_file_info.h>

static void apr_fatal(apr_status_t rv);

int main(void)
{
    apr_pool_t *pool;
    apr_status_t rv;

    // Initialize APR and pool
    apr_initialize();
    if ((rv = apr_pool_create(&pool, NULL)) != APR_SUCCESS) {
        apr_fatal(rv);
    }

    // Open the directory
    apr_dir_t *dir;
    if ((rv = apr_dir_open(&dir, ".", pool)) != APR_SUCCESS) {
        apr_fatal(rv);
    }

    // Read the directory
    apr_finfo_t finfo;
    apr_int32_t wanted = APR_FINFO_NAME | APR_FINFO_SIZE;
    while ((rv = apr_dir_read(&finfo, wanted, dir)) == APR_SUCCESS) {
        printf("%s\t%10"PRIu64"\n", finfo.name, (uint64_t)finfo.size);
    }
    if (!APR_STATUS_IS_ENOENT(rv)) {
        apr_fatal(rv);
    }

    // Clean up
    apr_dir_close(dir);
    apr_pool_destroy(pool);
    apr_terminate();
    return 0;
}

static void apr_fatal(apr_status_t rv)
{
    const int bufsize = 1000;
    char buf[bufsize+1];
    printf("APR Error %d: %s\n", rv, apr_strerror(rv, buf, bufsize));
    exit(1);
}

关于apache - 是否可以使用 apache 可移植运行时获取目录中的文件列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19907411/

相关文章:

php - 安装 nginx 后找不到 Laravel 路由

apache - 将域 + 所有内容 + SSL EV(扩展验证)证书转移到另一个托管服务提供商。如何转移SSL EV?

ruby-on-rails - 乘客使用错误的 ruby​​ 版本

将 apr_time_t 转换为不同的 int 类型

apache - 为什么对 Apache 提供的文本文件使用 deflate 而不是 gzip?

apache - Plesk-Server 上的 Laravel - 只能通过调用/公共(public)访问

c++ - APR 和大文件

c - 在函数 ‘APR_DECLARE’ : error: expected declaration specifiers before ‘apr_strerror’ 中

Tomcat 在使用 sendfile 和 APR Connector 传输期间断开连接

JBoss 7.1.1 和 JBoss Web Native