You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@apr.apache.org by ro...@apache.org on 2005/11/13 19:35:48 UTC

svn commit: r333088 - in /apr/examples/trunk: README ls/ ls/ls.c

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 <stdlib.h>
+
+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;
+}