Return-Path: Delivered-To: apmail-apr-commits-archive@www.apache.org Received: (qmail 13890 invoked from network); 13 Nov 2005 18:36:11 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 13 Nov 2005 18:36:11 -0000 Received: (qmail 98584 invoked by uid 500); 13 Nov 2005 18:36:10 -0000 Delivered-To: apmail-apr-commits-archive@apr.apache.org Received: (qmail 98472 invoked by uid 500); 13 Nov 2005 18:36:10 -0000 Mailing-List: contact commits-help@apr.apache.org; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: Reply-To: dev@apr.apache.org List-Id: Delivered-To: mailing list commits@apr.apache.org Received: (qmail 98461 invoked by uid 99); 13 Nov 2005 18:36:10 -0000 X-ASF-Spam-Status: No, hits=-9.4 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [209.237.227.194] (HELO minotaur.apache.org) (209.237.227.194) by apache.org (qpsmtpd/0.29) with SMTP; Sun, 13 Nov 2005 10:36:09 -0800 Received: (qmail 13845 invoked by uid 65534); 13 Nov 2005 18:35:49 -0000 Message-ID: <20051113183549.13844.qmail@minotaur.apache.org> Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r333088 - in /apr/examples/trunk: README ls/ ls/ls.c Date: Sun, 13 Nov 2005 18:35:48 -0000 To: commits@apr.apache.org From: rooneg@apache.org X-Mailer: svnmailer-1.0.5 X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N Author: rooneg Date: Sun Nov 13 10:35:45 2005 New Revision: 333088 URL: http://svn.apache.org/viewcvs?rev=333088&view=rev Log: Add a basic ls implementation, to show how to read directories. * ls/ls.c: A very bare bones APR ls program. * README: Note the presence of ls. Added: apr/examples/trunk/ls/ apr/examples/trunk/ls/ls.c Modified: apr/examples/trunk/README Modified: apr/examples/trunk/README URL: http://svn.apache.org/viewcvs/apr/examples/trunk/README?rev=333088&r1=333087&r2=333088&view=diff ============================================================================== --- apr/examples/trunk/README (original) +++ apr/examples/trunk/README Sun Nov 13 10:35:45 2005 @@ -8,3 +8,6 @@ helloworld The ubiquitous first-example. + +ls + A basic implementation of the unix ls command. Added: apr/examples/trunk/ls/ls.c URL: http://svn.apache.org/viewcvs/apr/examples/trunk/ls/ls.c?rev=333088&view=auto ============================================================================== --- apr/examples/trunk/ls/ls.c (added) +++ apr/examples/trunk/ls/ls.c Sun Nov 13 10:35:45 2005 @@ -0,0 +1,65 @@ +/* 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 "apr_file_info.h" +#include + +static apr_status_t ls(const char *path, apr_file_t *out, apr_pool_t *pool) +{ + apr_status_t rv; + apr_dir_t *d; + + rv = apr_dir_open(&d, path, pool); + if (rv) + return rv; + + for (;;) { + apr_finfo_t fi; + + rv = apr_dir_read(&fi, APR_FINFO_NAME | APR_FINFO_TYPE, d); + if (rv == APR_EOF) + break; + else if (rv) + return rv; + + if ((fi.name[0] == '.' && fi.name[1] == '\0') + || (fi.name[0] == '.' && fi.name[1] == '.' && fi.name[2] == '\0')) + continue; + + apr_file_printf(out, "%s%s" APR_EOL_STR, fi.name, + fi.filetype == APR_DIR ? "/" : ""); + } + + return APR_SUCCESS; +} + +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 = ls(argv[i], out, subpool); + if (rv) + return EXIT_FAILURE; + } + + apr_pool_destroy(subpool); + + return EXIT_SUCCESS; +}