bio.c
5.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999, 2000 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Library Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program 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 Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include "bio.h"
/* This is so annoying, we can not use the standard I/O(stdio) because a
socket may write/read less then requested and with the stdio functions
there is no way to know how much data was written/read, meaning also things
can get tricky in NONBLOCKING. */
struct _bio
{
char *read_buffer; /* Read Buffer. */
char *read_ptr;
size_t read_buflen;
char *write_buffer; /* Write Buffer. */
char *write_ptr;
size_t write_buflen;
stream_t stream;
};
int
bio_create (bio_t *pbio, stream_t stream)
{
bio_t bio;
/* 512 is the minimum for POP answer buffer. */
#define MIOBUF 512
if (pbio == NULL)
return EINVAL;
bio = calloc (1, sizeof (*bio));
if (bio == NULL)
return ENOMEM;
/* + 1 for the sentinel. */
bio->read_buffer = calloc (MIOBUF + 1, sizeof (char));
if (bio->read_buffer == NULL)
{
bio_destroy (&bio);
return ENOMEM;
}
bio->read_ptr = bio->read_buffer;
bio->read_buflen = MIOBUF;
/* + 1 for the sentinel. */
bio->write_buffer = calloc (MIOBUF/2 + 1, sizeof (char));
if (bio->write_buffer == NULL)
{
bio_destroy (&bio);
return ENOMEM;
}
bio->write_ptr = bio->write_buffer;
bio->write_buflen = MIOBUF;
bio->stream = stream;
*pbio = bio;
return 0;
}
void
bio_destroy (bio_t *pbio)
{
if (pbio && *pbio)
{
free ((*pbio)->read_buffer);
free ((*pbio)->write_buffer);
/* We don't close the file descriptor ? */
/* stream_close (stream); */
free ((*pbio));
*pbio = NULL;
}
}
/* buffered read */
int
bio_read (bio_t bio, char *ptr, size_t n, size_t *pnread)
{
int nleft;
size_t nread = 0;
size_t len = 0;
/* sanity check */
if (bio == NULL || pnread == NULL)
return EINVAL;
/* noop */
if (n == 0 || ptr == NULL)
{
*pnread = 0;
return 0;
}
/* fill up the global buffer */
if (bio->read_ptr == bio->read_buffer)
{
int err = stream_read (bio->stream, bio->read_buffer,
bio->read_buflen, 0, &len);
if (err != 0)
return err;
/* If len == read_buflen, read_ptr points to the sentinel. */
bio->read_ptr = bio->read_buffer + len;
}
else
len = bio->read_ptr - bio->read_buffer;
/* How much can wd copy out ? */
nleft = n - len;
/* We got more then requested. */
if (nleft < 0)
{
size_t sentinel;
nread = n;
/* where to move the sentinel to mark the end. */
sentinel = bio->read_ptr - (bio->read_buffer + nread);
memcpy (ptr, bio->read_buffer, nread);
memmove (bio->read_buffer, bio->read_buffer + nread, sentinel);
bio->read_ptr = bio->read_buffer + sentinel ;
}
else
{
/* Drain our buffer. */;
nread = len;
memcpy (ptr, bio->read_buffer, nread);
bio->read_ptr = bio->read_buffer;
}
if (pnread)
*pnread = nread;
return 0;
}
int
bio_write (bio_t bio, const char *ptr, size_t n, size_t *pnwriten)
{
int nleft;
int err;
size_t nwriten = 0;
size_t total = 0;
nleft = n;
/* First try to send it all. */
while (nleft > 0)
{
err = stream_write (bio->stream, ptr, nleft, 0, &nwriten);
if (err != 0)
break;
nleft -= nwriten;
total += nwriten;
ptr += nwriten;
}
/* Not recoverable. */
if (err != EAGAIN && err != EINTR)
bio->write_ptr = bio->write_buffer;
if (pnwriten)
*pnwriten = total;
return err;
}
/* Return the number of char read excluding the null char */
int
bio_readline (bio_t bio, char *ptr, size_t maxlen, size_t *pwriten)
{
int rc = 0;
size_t n = 0;
int err;
char c;
/* Noop. */
if (ptr == NULL || maxlen == 0)
{
if (pwriten)
*pwriten = 0;
return 0;
}
maxlen--;
while ((err = bio_read (bio, &c, 1, &rc)) == 0 && rc == 1)
{
*ptr++ = c;
n++;
if (c == '\n')
break; /* newline is stored, like fgets () */
if (n == maxlen)
break;
}
/* This is tricky, We may have data in the buffer, so we flush it
first and return success. On the next read if the error persist
it will be returned. This can happen for NONBLOCKING. */
if (n > 0)
err = 0;
*ptr = '\0';
if (pwriten)
*pwriten = n;
//fprintf (stderr, "\n%d %d BIO %s\n", n, *(ptr -n), (ptr -n ));
return err;
}