apr-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From roo...@apache.org
Subject svn commit: r414711 - in /apr/examples/trunk: README cat/ cat/cat.c
Date Fri, 16 Jun 2006 00:21:32 GMT
Author: rooneg
Date: Thu Jun 15 17:21:32 2006
New Revision: 414711

URL: http://svn.apache.org/viewvc?rev=414711&view=rev
Log:
Add an implementation of 'cat', which serves to introduce file i/o.

* cat/cat.c: New example.

* README: Note the addition of cat.

Added:
    apr/examples/trunk/cat/
    apr/examples/trunk/cat/cat.c
Modified:
    apr/examples/trunk/README

Modified: apr/examples/trunk/README
URL: http://svn.apache.org/viewvc/apr/examples/trunk/README?rev=414711&r1=414710&r2=414711&view=diff
==============================================================================
--- apr/examples/trunk/README (original)
+++ apr/examples/trunk/README Thu Jun 15 17:21:32 2006
@@ -12,5 +12,8 @@
 ls
     A basic implementation of the unix ls command.
 
+cat
+    A basic implementation of the unix cat command.
+
 echoserver
     A basic TCP server that writes back whatever you send to it.

Added: apr/examples/trunk/cat/cat.c
URL: http://svn.apache.org/viewvc/apr/examples/trunk/cat/cat.c?rev=414711&view=auto
==============================================================================
--- apr/examples/trunk/cat/cat.c (added)
+++ apr/examples/trunk/cat/cat.c Thu Jun 15 17:21:32 2006
@@ -0,0 +1,58 @@
+/* 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;
+}



Mime
View raw message