Added to the repository
Showing
1 changed file
with
100 additions
and
0 deletions
examples/murun.c
0 → 100644
1 | /* GNU Mailutils -- a suite of utilities for electronic mail | ||
2 | Copyright (C) 2003 Free Software Foundation, Inc. | ||
3 | |||
4 | GNU Mailutils is free software; you can redistribute it and/or modify | ||
5 | it under the terms of the GNU General Public License as published by | ||
6 | the Free Software Foundation; either version 2, or (at your option) | ||
7 | any later version. | ||
8 | |||
9 | GNU Mailutils is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | GNU General Public License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU General Public License | ||
15 | along with GNU Mailutils; if not, write to the Free Software | ||
16 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ | ||
17 | |||
18 | #ifdef HAVE_CONFIG_H | ||
19 | # include <config.h> | ||
20 | #endif | ||
21 | #include <sys/types.h> | ||
22 | #include <sys/wait.h> | ||
23 | #include <unistd.h> | ||
24 | #include <stdio.h> | ||
25 | #include <assert.h> | ||
26 | #include <ctype.h> | ||
27 | #include <string.h> | ||
28 | #include <mailutils/mailutils.h> | ||
29 | #include <mailutils/argcv.h> | ||
30 | |||
31 | static void | ||
32 | read_and_print (stream_t in, stream_t out) | ||
33 | { | ||
34 | size_t size; | ||
35 | char buffer[128]; | ||
36 | |||
37 | while (stream_sequential_readline (in, buffer, sizeof (buffer), &size) == 0 | ||
38 | && size > 0) | ||
39 | { | ||
40 | stream_sequential_write (out, buffer, size); | ||
41 | } | ||
42 | } | ||
43 | |||
44 | int | ||
45 | main (int argc, char *argv[]) | ||
46 | { | ||
47 | int rc; | ||
48 | stream_t stream, out; | ||
49 | int read_stdin = 0; | ||
50 | int i = 1; | ||
51 | char *cmdline; | ||
52 | int flags = MU_STREAM_READ; | ||
53 | |||
54 | if (argc > 1 && strcmp (argv[i], "--stdin") == 0) | ||
55 | { | ||
56 | read_stdin = 1; | ||
57 | flags |= MU_STREAM_WRITE; | ||
58 | i++; | ||
59 | } | ||
60 | |||
61 | if (i == argc) | ||
62 | { | ||
63 | fprintf (stderr, "Usage: %s [--stdin] progname [args]\n", argv[0]); | ||
64 | exit (1); | ||
65 | } | ||
66 | |||
67 | assert (argcv_string (argc - i, &argv[i], &cmdline) == 0); | ||
68 | if (read_stdin) | ||
69 | { | ||
70 | stream_t in; | ||
71 | assert (stdio_stream_create (&in, stdin, 0) == 0); | ||
72 | assert (stream_open (in) == 0); | ||
73 | rc = filter_prog_stream_create (&stream, cmdline, in); | ||
74 | } | ||
75 | else | ||
76 | rc = prog_stream_create (&stream, cmdline, flags); | ||
77 | if (rc) | ||
78 | { | ||
79 | fprintf (stderr, "%s: cannot create program filter stream: %s\n", | ||
80 | argv[0], mu_strerror (rc)); | ||
81 | exit (1); | ||
82 | } | ||
83 | |||
84 | rc = stream_open (stream); | ||
85 | if (rc) | ||
86 | { | ||
87 | fprintf (stderr, "%s: cannot open program filter stream: %s\n", | ||
88 | argv[0], mu_strerror (rc)); | ||
89 | exit (1); | ||
90 | } | ||
91 | |||
92 | assert (stdio_stream_create (&out, stdout, 0) == 0); | ||
93 | assert (rc == 0); | ||
94 | assert (stream_open (out) == 0); | ||
95 | |||
96 | read_and_print (stream, out); | ||
97 | stream_close (stream); | ||
98 | stream_destroy (&stream, stream_get_owner (stream)); | ||
99 | return 0; | ||
100 | } |
-
Please register or sign in to post a comment