-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubstdo.c
168 lines (153 loc) · 2.61 KB
/
substdo.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
* $Log: substdo.c,v $
* Revision 1.1 2014-08-23 09:07:12+05:30 Cprogrammer
* Initial revision
*
*
*/
#include "substdio.h"
#include "str.h"
#include "byte.h"
#include "error.h"
#ifndef lint
static char sccsid[] = "$Id: substdo.c,v 1.1 2014-08-23 09:07:12+05:30 Cprogrammer Exp mbhangui $";
#endif
static int
allwrite(op, fd, buf, len)
register ssize_t (*op) ();
register int fd;
register char *buf;
register int len;
{
register int w;
while (len)
{
w = op(fd, buf, len);
if (w == -1)
{
if (errno == error_intr)
continue;
return -1; /*- note that some data may have been written */
}
if (w == 0); /*- luser's fault */
buf += w;
len -= w;
}
return 0;
}
int
substdio_flush(s)
register substdio *s;
{
register int p;
p = s->p;
if (!p)
return 0;
s->p = 0;
return allwrite(s->op, s->fd, s->x, p);
}
int
substdio_bput(s, buf, len)
register substdio *s;
register char *buf;
register int len;
{
register int n;
while (len > (n = s->n - s->p))
{
byte_copy(s->x + s->p, n, buf);
s->p += n;
buf += n;
len -= n;
if (substdio_flush(s) == -1)
return -1;
}
/*
* now len <= s->n - s->p
*/
byte_copy(s->x + s->p, len, buf);
s->p += len;
return 0;
}
int
substdio_putalign(substdio *s, char *buf, unsigned int len)
{
unsigned int n;
while (len > (n = s->n - s->p)) {
byte_copy(s->x + s->p,n,buf);
s->p += n;
buf += n;
len -= n;
if (substdio_flush(s) == -1)
return -1;
}
/* now len <= s->n - s->p */
byte_copy(s->x + s->p,len,buf);
s->p += len;
return 0;
}
int
substdio_put(s, buf, len)
register substdio *s;
register char *buf;
register int len;
{
register int n;
n = s->n;
if (len > n - s->p)
{
if (substdio_flush(s) == -1)
return -1;
/*
* now s->p == 0
*/
if (n < SUBSTDIO_OUTSIZE)
n = SUBSTDIO_OUTSIZE;
while (len > s->n)
{
if (n > len)
n = len;
if (allwrite(s->op, s->fd, buf, n) == -1)
return -1;
buf += n;
len -= n;
}
}
/*
* now len <= s->n - s->p
*/
byte_copy(s->x + s->p, len, buf);
s->p += len;
return 0;
}
int
substdio_putflush(s, buf, len)
register substdio *s;
register char *buf;
register int len;
{
if (substdio_flush(s) == -1)
return -1;
return allwrite(s->op, s->fd, buf, len);
}
int
substdio_bputs(s, buf)
register substdio *s;
register char *buf;
{
return substdio_bput(s, buf, str_len(buf));
}
int
substdio_puts(s, buf)
register substdio *s;
register char *buf;
{
return substdio_put(s, buf, str_len(buf));
}
int
substdio_putsflush(s, buf)
register substdio *s;
register char *buf;
{
return substdio_putflush(s, buf, str_len(buf));
}