diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2010-09-06 19:18:32 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2010-09-08 16:11:37 +0300 |
commit | 62c5c62a7e8fd3524227cd241986d6ce4711b239 (patch) | |
tree | 6d32831019056a5fa98515563d37fc6665bc3921 /examples | |
parent | 69f7dcae5a906acc9126ca25e33881cba944f85d (diff) | |
download | mailutils-62c5c62a7e8fd3524227cd241986d6ce4711b239.tar.gz mailutils-62c5c62a7e8fd3524227cd241986d6ce4711b239.tar.bz2 |
Implement "read cache" streams. Rewrite stdio and socket streams.
* include/mailutils/sys/stdio_stream.h: Remove.
* include/mailutils/sys/socket_stream.h: Remove.
* include/mailutils/sys/rdcache_stream.h: New file.
* include/mailutils/sys/Makefile.am: Update.
* mailbox/rdcache_stream.c: New file.
* mailbox/Makefile.am: Update.
* examples/mucat.c: New example.
* examples/musocio.c: New example.
* examples/Makefile.am (noinst_PROGRAMS): Build new examples.
* include/mailutils/stream.h (mu_fd_stream_create): New proto.
(mu_rdcache_stream_create): New proto.
* include/mailutils/sys/file_stream.h (_mu_file_stream_create): Change
prototype.
* mailbox/file_stream.c (fd_open): Raise the MU_STREAM_AUTOCLOSE bit.
(fd_ioctl): Support MU_IOCTL_SET_TRANSPORT.
(_mu_file_stream_create): Change signature. All uses updated.
Allocate a copy of the filename argument, unless it is NULL.
(mu_fd_stream_create): New function.
* mailbox/socket_stream.c: Rewrite using file_stream directly.
* mailbox/stdio_stream.c: Rewrite. Use rdcache_stream if
the seek capability is required on an input stream.
* mailbox/streamcpy.c (mu_stream_copy): Handle eventual
EACCES return from mu_stream_seek as equivalent to ENOSYS.
Diffstat (limited to 'examples')
-rw-r--r-- | examples/Makefile.am | 2 | ||||
-rw-r--r-- | examples/mucat.c | 87 | ||||
-rw-r--r-- | examples/musocio.c | 124 |
3 files changed, 213 insertions, 0 deletions
diff --git a/examples/Makefile.am b/examples/Makefile.am index b0911b57d..7e858d78a 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -49,9 +49,11 @@ noinst_PROGRAMS = \ mimetest\ msg-send\ mta\ + mucat\ muauth\ muemail\ murun\ + musocio\ $(NNTPCLIENT)\ $(POP3CLIENT)\ sfrom\ diff --git a/examples/mucat.c b/examples/mucat.c new file mode 100644 index 000000000..b8cb314f7 --- /dev/null +++ b/examples/mucat.c @@ -0,0 +1,87 @@ +/* GNU Mailutils -- a suite of utilities for electronic mail + Copyright (C) 1999, 2000, 2001, 2002, 2005, 2007, 2010 Free Software + Foundation, Inc. + + GNU Mailutils is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3, or (at your option) + any later version. + + GNU Mailutils is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with GNU Mailutils; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + MA 02110-1301 USA */ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif +#include <unistd.h> +#include <stdio.h> +#include <assert.h> +#include <ctype.h> +#include <string.h> +#include <mailutils/mailutils.h> + +int +main (int argc, char * argv []) +{ + int c; + mu_stream_t in, out; + int reread_option = 0; + mu_off_t reread_off; + int skip_option = 0; + mu_off_t skip_off; + + while ((c = getopt (argc, argv, "hs:r:")) != EOF) + switch (c) + { + case 'r': + reread_option = 1; + reread_off = strtoul (optarg, NULL, 10); + break; + + case 's': + skip_option = 1; + skip_off = strtoul (optarg, NULL, 10); + break; + + case 'h': + printf ("usage: cat [-s off]\n"); + exit (0); + + default: + exit (1); + } + + + MU_ASSERT (mu_stdio_stream_create (&in, MU_STDIN_FD, MU_STREAM_SEEK)); + MU_ASSERT (mu_stdio_stream_create (&out, MU_STDOUT_FD, 0)); + + if (skip_option) + { + mu_stream_printf (out, "skipping to %lu:\n", + (unsigned long) skip_off); + MU_ASSERT (mu_stream_seek (in, skip_off, MU_SEEK_SET, NULL)); + } + + MU_ASSERT (mu_stream_copy (out, in, 0)); + + if (reread_option) + { + mu_stream_printf (out, "rereading from %lu:\n", + (unsigned long) reread_off); + MU_ASSERT (mu_stream_seek (in, reread_off, MU_SEEK_SET, NULL)); + MU_ASSERT (mu_stream_copy (out, in, 0)); + } + + mu_stream_close (in); + mu_stream_destroy (&in); + mu_stream_close (out); + mu_stream_destroy (&out); + return 0; +} diff --git a/examples/musocio.c b/examples/musocio.c new file mode 100644 index 000000000..66bf346f3 --- /dev/null +++ b/examples/musocio.c @@ -0,0 +1,124 @@ +/* GNU Mailutils -- a suite of utilities for electronic mail + Copyright (C) 1999, 2000, 2001, 2002, 2005, 2007, 2010 Free Software + Foundation, Inc. + + GNU Mailutils is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3, or (at your option) + any later version. + + GNU Mailutils is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with GNU Mailutils; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + MA 02110-1301 USA */ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include <sys/types.h> +#include <sys/wait.h> +#include <unistd.h> +#include <stdio.h> +#include <assert.h> +#include <ctype.h> +#include <string.h> +#include <mailutils/mailutils.h> + +int verbose; + +void +ioloop (char *id, mu_stream_t in, mu_stream_t out) +{ + char *buf = NULL; + size_t size = 0, n; + int rc; + + while ((rc = mu_stream_getline (in, &buf, &size, &n)) == 0 && n > 0) + { + if (rc) + { + mu_error("%s: read error: %s", id, mu_stream_strerror (in, rc)); + exit (1); + } + MU_ASSERT (mu_stream_write (out, buf, n, NULL)); + } + mu_stream_flush (out); + if (verbose) + fprintf (stderr, "%s exited\n", id); +} + +int +main (int argc, char * argv []) +{ + mu_stream_t in, out, sock; + pid_t pid; + int status, c; + + while ((c = getopt (argc, argv, "v")) != EOF) + switch (c) + { + case 'v': + verbose++; + break; + + case 'h': + printf ("usage: musocio file\n"); + return 0; + + default: + return 1; + } + + argc -= optind; + argv += optind; + + if (argc != 2) + { + mu_error ("usage: musocio file"); + return 1; + } + + MU_ASSERT (mu_stdio_stream_create (&in, MU_STDIN_FD, 0)); + mu_stream_set_buffer (in, mu_buffer_line, 1024); + MU_ASSERT (mu_stdio_stream_create (&out, MU_STDOUT_FD, 0)); + mu_stream_set_buffer (out, mu_buffer_line, 1024); + MU_ASSERT (mu_socket_stream_create (&sock, argv[1], MU_STREAM_RDWR)); + mu_stream_set_buffer (sock, mu_buffer_line, 1024); + MU_ASSERT (mu_stream_open (sock)); + + pid = fork (); + if (pid == -1) + { + mu_error ("fork failed: %s", mu_strerror (errno)); + return 1; + } + + if (pid == 0) + { + mu_stream_close (in); + mu_stream_destroy (&in); + ioloop ("reader", sock, out); + exit (0); + } + + ioloop ("writer", in, sock); + + mu_stream_close (in); + mu_stream_destroy (&in); + + mu_stream_shutdown (sock, MU_STREAM_WRITE); + waitpid (pid, &status, 0); + + mu_stream_close (sock); + mu_stream_destroy (&sock); + + mu_stream_close (out); + mu_stream_destroy (&out); + return 0; +} |