-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubstdi.c
129 lines (116 loc) · 1.91 KB
/
substdi.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
/*
* $Log: substdi.c,v $
* Revision 1.1 2014-08-23 09:07:05+05:30 Cprogrammer
* Initial revision
*
*
*/
#include "substdio.h"
#include "byte.h"
#include "error.h"
#ifndef lint
static char sccsid[] = "$Id: substdi.c,v 1.1 2014-08-23 09:07:05+05:30 Cprogrammer Exp mbhangui $";
#endif
static int
oneread(op, fd, buf, len)
register ssize_t (*op) ();
register int fd;
register char *buf;
register int len;
{
register int r;
for (;;)
{
r = op(fd, buf, len);
if (r == -1)
if (errno == error_intr)
continue;
return r;
}
}
static int
getthis(s, buf, len)
register substdio *s;
register char *buf;
register int len;
{
register int r;
register int q;
r = s->p;
q = r - len;
if (q > 0)
{
r = len;
s->p = q;
} else
s->p = 0;
byte_copy(buf, r, s->x + s->n);
s->n += r;
return r;
}
int
substdio_feed(s)
register substdio *s;
{
register int r;
register int q;
if (s->p)
return s->p;
q = s->n;
r = oneread(s->op, s->fd, s->x, q);
if (r <= 0)
return r;
s->p = r;
q -= r;
s->n = q;
if (q > 0) /*- damn, gotta shift */
byte_copyr(s->x + q, r, s->x);
return r;
}
int
substdio_bget(s, buf, len)
register substdio *s;
register char *buf;
register int len;
{
register int r;
if (s->p > 0)
return getthis(s, buf, len);
r = s->n;
if (r <= len)
return oneread(s->op, s->fd, buf, r);
r = substdio_feed(s);
if (r <= 0)
return r;
return getthis(s, buf, len);
}
int
substdio_get(s, buf, len)
register substdio *s;
register char *buf;
register int len;
{
register int r;
if (s->p > 0)
return getthis(s, buf, len);
if (s->n <= len)
return oneread(s->op, s->fd, buf, len);
r = substdio_feed(s);
if (r <= 0)
return r;
return getthis(s, buf, len);
}
char *
substdio_peek(s)
register substdio *s;
{
return s->x + s->n;
}
void
substdio_seek(s, len)
register substdio *s;
register int len;
{
s->n += len;
s->p -= len;
}