summaryrefslogtreecommitdiffabout
path: root/libetpan/src/data-types/mailstream_low.c
Unidiff
Diffstat (limited to 'libetpan/src/data-types/mailstream_low.c') (more/less context) (ignore whitespace changes)
-rw-r--r--libetpan/src/data-types/mailstream_low.c164
1 files changed, 164 insertions, 0 deletions
diff --git a/libetpan/src/data-types/mailstream_low.c b/libetpan/src/data-types/mailstream_low.c
new file mode 100644
index 0000000..ab268bb
--- a/dev/null
+++ b/libetpan/src/data-types/mailstream_low.c
@@ -0,0 +1,164 @@
1/*
2 * libEtPan! -- a mail stuff library
3 *
4 * Copyright (C) 2001, 2005 - DINH Viet Hoa
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the libEtPan! project nor the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32/*
33 * $Id$
34 */
35
36#include "mailstream_low.h"
37#include <stdlib.h>
38
39#ifdef LIBETPAN_MAILSTREAM_DEBUG
40
41#define STREAM_DEBUG
42
43#include <stdio.h>
44#include <sys/types.h>
45#include <sys/stat.h>
46#include "maillock.h"
47
48#define LOG_FILE "libetpan-stream-debug.log"
49
50int mailstream_debug = 0;
51
52#define STREAM_LOG_BUF(buf, size) \
53 if (mailstream_debug) { \
54 FILE * f; \
55 mode_t old_mask; \
56 \
57 old_mask = umask(0077); \
58 f = fopen(LOG_FILE, "a"); \
59 umask(old_mask); \
60 if (f != NULL) { \
61 maillock_write_lock(LOG_FILE, fileno(f)); \
62 fwrite((buf), 1, (size), f); \
63 maillock_write_unlock(LOG_FILE, fileno(f)); \
64 fclose(f); \
65 } \
66 }
67
68#define STREAM_LOG(str) \
69 if (mailstream_debug) { \
70 FILE * f; \
71 mode_t old_mask; \
72 \
73 old_mask = umask(0077); \
74 f = fopen(LOG_FILE, "a"); \
75 umask(old_mask); \
76 if (f != NULL) { \
77 maillock_write_lock(LOG_FILE, fileno(f)); \
78 fputs((str), f); \
79 maillock_write_unlock(LOG_FILE, fileno(f)); \
80 fclose(f); \
81 } \
82 }
83
84#else
85
86#define STREAM_LOG_BUF(buf, size) do { } while (0)
87#define STREAM_LOG(buf) do { } while (0)
88
89#endif
90
91
92/* general functions */
93
94mailstream_low * mailstream_low_new(void * data,
95 mailstream_low_driver * driver)
96{
97 mailstream_low * s;
98
99 s = malloc(sizeof(* s));
100 if (s == NULL)
101 return NULL;
102
103 s->data = data;
104 s->driver = driver;
105
106 return s;
107}
108
109int mailstream_low_close(mailstream_low * s)
110{
111 if (s == NULL)
112 return -1;
113 s->driver->mailstream_close(s);
114
115 return 0;
116}
117
118int mailstream_low_get_fd(mailstream_low * s)
119{
120 if (s == NULL)
121 return -1;
122 return s->driver->mailstream_get_fd(s);
123}
124
125void mailstream_low_free(mailstream_low * s)
126{
127 s->driver->mailstream_free(s);
128}
129
130ssize_t mailstream_low_read(mailstream_low * s, void * buf, size_t count)
131{
132 ssize_t r;
133
134 if (s == NULL)
135 return -1;
136 r = s->driver->mailstream_read(s, buf, count);
137
138#ifdef STREAM_DEBUG
139 if (r > 0) {
140 STREAM_LOG("<<<<<<< read <<<<<<\n");
141 STREAM_LOG_BUF(buf, r);
142 STREAM_LOG("\n");
143 STREAM_LOG("<<<<<<< end read <<<<<<\n");
144 }
145#endif
146
147 return r;
148}
149
150ssize_t mailstream_low_write(mailstream_low * s,
151 const void * buf, size_t count)
152{
153 if (s == NULL)
154 return -1;
155
156#ifdef STREAM_DEBUG
157 STREAM_LOG(">>>>>>> send >>>>>>\n");
158 STREAM_LOG_BUF(buf, count);
159 STREAM_LOG("\n");
160 STREAM_LOG(">>>>>>> end send >>>>>>\n");
161#endif
162
163 return s->driver->mailstream_write(s, buf, count);
164}