File: xcio.c

package info (click to toggle)
ppxp 0.99120923-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 2,812 kB
  • ctags: 3,704
  • sloc: ansic: 24,532; tcl: 3,992; makefile: 517; sh: 80
file content (110 lines) | stat: -rw-r--r-- 1,969 bytes parent folder | download | duplicates (3)
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/file.h>
#include <sys/types.h>

#include <config.h>
#include <support.h>
#include <xcio.h>

struct pppinfo_s pppInfo;

int
XcioWrite(int fd, struct xcio_s *xcp)
{
    return(write(fd, xcp, sizeof(xcp->xid) + sizeof(xcp->len)
		 + sizeof(xcp->type) + xcp->len));
}

/*
 * New scheme
 */

static struct xciobuf_s {
    struct xciobuf_s *next;
    struct xcio_s xc;
    int fd, got;
    enum {XCIN_TYPE=0, XCIN_XID, XCIN_LEN, XCIN_DATA} st;
} *xbHead;

void
XcioOpen(int fd)
{
    struct xciobuf_s *xbp;

#if 0
    int fdf;
    if ((fdf = fcntl(fd, F_GETFL)) >= 0)
	fcntl(fd, F_SETFL, fdf | O_NONBLOCK);
#endif
    xbp = TALLOC(struct xciobuf_s);
    memset(xbp, 0, sizeof(*xbp));
    xbp->fd = fd;
    xbp->next = xbHead;
    xbHead = xbp;
}

void
XcioClose(int fd)
{
    struct xciobuf_s *xbp=xbHead, *xbp0;

    xbp0 = NULL;
    while (xbp) {
	if (xbp->fd == fd) {
	    if (xbp0) xbp0->next = xbp->next;
	    else xbHead = xbp->next;
	    Free(xbp);
	    break;
	}
	xbp0 = xbp;
	xbp = xbp->next;
    }
}

int
XcioRead(int fd, struct xcio_s *xc)
{
    struct xciobuf_s *xbp=xbHead;
    int n, ret=-1;
    u_char ch;

    while (xbp) {
	if (xbp->fd == fd) break;
	xbp = xbp->next;
    }
    if (!xbp) return(-1);
    while ((n = read(fd, &ch, 1)) > 0) {
	ret = 0;
	switch (xbp->st) {
	case XCIN_LEN:
	    xbp->xc.len = ch;
	    xbp->st = XCIN_DATA;
	    if (ch) break;
	case XCIN_DATA:
	    xbp->xc.buf[xbp->got] = ch;
	    xbp->got ++;
	    if (xbp->got >= xbp->xc.len) {
		memcpy(xc, &xbp->xc, xbp->xc.len + sizeof(xbp->xc.xid)
		       + sizeof(xbp->xc.len) + sizeof(xbp->xc.type));
		xbp->st = XCIN_TYPE;
		return(xc->type);
	    }
	    break;
	case XCIN_XID:
	    xbp->xc.xid = ch;
	    xbp->st = XCIN_LEN;
	    break;
	case XCIN_TYPE:
	default:
	    xbp->xc.type = ch;
	    xbp->got = 0;
	    xbp->st = XCIN_XID;
	    break;
	}
    }
    return(ret);
}