/* This code is PUBLIC DOMAIN, and is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND. See the accompanying 
 * LICENSE file.
 */
#include "apr.h"
#include "apr_file_io.h"
#include <stdlib.h>

apr_status_t cat(const char *fname, apr_file_t *out, apr_pool_t *pool)
{
    apr_size_t nbytes = 1;
    apr_status_t rv;
    apr_file_t *f;
    char c;

    rv = apr_file_open(&f, fname, APR_FOPEN_READ, APR_OS_DEFAULT, pool);
    if (rv)
        return rv;

    while ((rv = apr_file_read(f, &c, &nbytes)) == APR_SUCCESS) {
        rv = apr_file_write(out, &c, &nbytes);
        if (rv)
            return rv;
    }

    if (APR_STATUS_IS_EOF(rv))
        return APR_SUCCESS;
    else
        return rv;
}

int main(int argc, const char * const argv[])
{
    apr_pool_t *pool, *subpool;
    apr_file_t *out;
    apr_status_t rv;
    int i;

    apr_initialize();
    atexit(apr_terminate);
    apr_pool_create(&pool, NULL);
    
    apr_file_open_stdout(&out, pool);

    apr_pool_create(&subpool, pool);

    for (i = 1; i < argc; ++i) {
        apr_pool_clear(subpool);

        rv = cat(argv[i], out, subpool);
        if (rv)
            return EXIT_FAILURE;
    }

    apr_pool_destroy(pool);

    return EXIT_SUCCESS;
}
