s/qmail  3.3.23
Next generation secure email transport
substdi.c
Go to the documentation of this file.
1 #include "substdio.h"
2 #include "byte.h"
3 #include "error.h"
4 
5 static int oneread(register int (*op)(),register int fd,register char *buf,register int len)
6 {
7  register int r;
8 
9  for (;;) {
10  r = op(fd,buf,len);
11  if (r == -1) if (errno == error_intr) continue;
12  return r;
13  }
14 }
15 
16 static int getthis(register substdio *s,register char *buf,register int len)
17 {
18  register int r;
19  register int q;
20 
21  r = s->p;
22  q = r - len;
23  if (q > 0) { r = len; s->p = q; } else s->p = 0;
24  byte_copy(buf,r,s->x + s->n);
25  s->n += r;
26  return r;
27 }
28 
29 int substdio_feed(register substdio *s)
30 {
31  register int r;
32  register int q;
33 
34  if (s->p) return s->p;
35  q = s->n;
36  r = oneread(s->op,s->fd,s->x,q);
37  if (r <= 0) return r;
38  s->p = r;
39  q -= r;
40  s->n = q;
41  if (q > 0) /* damn, gotta shift */ byte_copyr(s->x + q,r,s->x);
42  return r;
43 }
44 
45 int substdio_bget(register substdio *s,register char *buf,register int len)
46 {
47  register int r;
48 
49  if (s->p > 0) return getthis(s,buf,len);
50  r = s->n; if (r <= len) return oneread(s->op,s->fd,buf,r);
51  r = substdio_feed(s); if (r <= 0) return r;
52  return getthis(s,buf,len);
53 }
54 
55 int substdio_get(register substdio *s,register char *buf,register int len)
56 {
57  register int r;
58 
59  if (s->p > 0) return getthis(s,buf,len);
60  if (s->n <= len) return oneread(s->op,s->fd,buf,len);
61  r = substdio_feed(s); if (r <= 0) return r;
62  return getthis(s,buf,len);
63 }
64 
65 char *substdio_peek(register substdio *s)
66 {
67  return s->x + s->n;
68 }
69 
70 void substdio_seek(register substdio *s,register int len)
71 {
72  s->n += len;
73  s->p -= len;
74 }
int fd
Definition: idedit.c:16
void byte_copy(char *, unsigned int, char *)
int fd
Definition: substdio.h:8
int substdio_bget(register substdio *s, register char *buf, register int len)
Definition: substdi.c:45
void byte_copyr(char *, unsigned int, char *)
unsigned len
Definition: matchup.c:36
int errno
int error_intr
Definition: error.c:6
void substdio_seek(register substdio *s, register int len)
Definition: substdi.c:70
int n
Definition: substdio.h:7
unsigned char * buf
Definition: dns.c:41
int substdio_feed(register substdio *s)
Definition: substdi.c:29
int substdio_get(register substdio *s, register char *buf, register int len)
Definition: substdi.c:55
int(* op)()
Definition: substdio.h:9
char * substdio_peek(register substdio *s)
Definition: substdi.c:65
char * x
Definition: substdio.h:5
int p
Definition: substdio.h:6