-
Notifications
You must be signed in to change notification settings - Fork 14
/
buffer.c
145 lines (130 loc) · 3.45 KB
/
buffer.c
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
/*
* ser2net - A program for allowing telnet connection to serial ports
* Copyright (C) 2001 Corey Minyard <[email protected]>
*
* This program 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 2 of the License, 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include "buffer.h"
#include "devio.h"
static int
lbuffer_write(struct devio *io, int fd, struct sbuf *buf, int *buferr)
{
ssize_t write_count;
int towrite1;
int towrite2 = 0;
if (buf->pos + buf->cursize > buf->maxsize) {
towrite1 = buf->maxsize - buf->pos;
towrite2 = buf->cursize - towrite1;
} else
towrite1 = buf->cursize;
if (towrite1 > 0) {
if (io)
write_count = io->f->write(io, buf->buf + buf->pos, towrite1);
else
write_count = write(fd, buf->buf + buf->pos, towrite1);
if (write_count == -1) {
if (errno == EINTR) {
/* EINTR means we were interrupted, just retry by returning. */
return 0;
} else if (errno == EAGAIN) {
/* This again was due to O_NONBLOCK, just ignore it. */
return 0;
} else {
*buferr = errno;
return -1;
}
}
buf->pos += write_count;
buf->cursize -= write_count;
}
if (towrite2 > 0) {
/* We wrapped */
buf->pos = 0;
if (io)
write_count = io->f->write(io, buf->buf + buf->pos, towrite2);
else
write_count = write(fd, buf->buf + buf->pos, towrite2);
if (write_count == -1) {
if (errno == EINTR) {
/* EINTR means we were interrupted, just retry by returning. */
return 0;
} else if (errno == EAGAIN) {
/* This again was due to O_NONBLOCK, just ignore it. */
return 0;
} else {
*buferr = errno;
return -1;
}
}
buf->pos += write_count;
buf->cursize -= write_count;
}
return 0;
}
int
buffer_write(int fd, struct sbuf *buf, int *buferr)
{
return lbuffer_write(NULL, fd, buf, buferr);
}
int
buffer_io_write(struct devio *io, struct sbuf *buf, int *buferr)
{
return lbuffer_write(io, 0, buf, buferr);
}
int
buffer_output(struct sbuf *buf, unsigned char *data, unsigned int len)
{
int end;
if (buffer_left(buf) < len)
return -1;
end = buf->pos + buf->cursize;
if (end > buf->maxsize)
end -= buf->maxsize;
if (end + len > buf->maxsize) {
int availend = buf->maxsize - end;
memcpy(buf->buf + end, data, availend);
buf->cursize += availend;
end = 0;
len -= availend;
data += availend;
}
memcpy(buf->buf + end, data, len);
buf->cursize += len;
return 0;
}
int
buffer_outchar(struct sbuf *buf, unsigned char data)
{
int end;
if (buffer_left(buf) < 1)
return -1;
end = buf->pos + buf->cursize;
if (end >= buf->maxsize)
end -= buf->maxsize;
buf->buf[end] = data;
buf->cursize += 1;
return 0;
}
void
buffer_init(struct sbuf *buf, unsigned char *data, unsigned int datasize)
{
buf->buf = data;
buf->maxsize = datasize;
buf->cursize = 0;
buf->pos = 0;
}