ucspi-tcp6 1.13.02
ucspi-tcp6
Loading...
Searching...
No Matches
fixcrio.c
Go to the documentation of this file.
1#include "sig.h"
2#include "buffer.h"
3#include "logmsg.h"
4#include "byte.h"
5#include "exit.h"
6#include "iopause.h"
7#include "pathexec.h"
8#include "fd.h"
9
10#define WHO "fixcrio"
11
12extern int read(int, char *,int);
13extern int write(int, char *,int);
14extern int pipe(int[2]);
15extern int close(int);
16extern int fork();
17
18char prebuf[256];
19
20int leftstatus = 0;
21char leftbuf[512];
24int leftflagcr = 0;
25
27char rightbuf[512];
31
32void doit(int fdleft,int fdright)
33{
34 struct taia stamp;
35 struct taia deadline;
36 iopause_fd x[4];
37 int xlen;
38 iopause_fd *io0;
39 iopause_fd *ioleft;
40 iopause_fd *io1;
41 iopause_fd *ioright;
42 int r, riop;
43 int i;
44 char ch;
45
46 for (;;) {
47 xlen = 0;
48
49 io0 = 0;
50 if (leftstatus == 0) {
51 io0 = &x[xlen++];
52 io0->fd = 0;
53 io0->events = IOPAUSE_READ;
54 }
55 ioleft = 0;
56 if (leftstatus == 1) {
57 ioleft = &x[xlen++];
58 ioleft->fd = fdleft;
59 ioleft->events = IOPAUSE_WRITE;
60 }
61
62 ioright = 0;
63 if (rightstatus == 0) {
64 ioright = &x[xlen++];
65 ioright->fd = fdright;
66 ioright->events = IOPAUSE_READ;
67 }
68 io1 = 0;
69 if (rightstatus == 1) {
70 io1 = &x[xlen++];
71 io1->fd = 1;
72 io1->events = IOPAUSE_WRITE;
73 }
74
75 taia_now(&stamp);
76 taia_uint(&deadline,3600);
77 taia_add(&deadline,&stamp,&deadline);
78 riop = iopause(x,xlen,&deadline,&stamp);
79
80 if (riop > 0 && io0 && io0->revents) {
81 r = read(0,prebuf,sizeof(prebuf));
82 if (r <= 0) {
83 leftstatus = -1;
84 close(fdleft);
85 }
86 else {
87 leftstatus = 1;
88 leftpos = 0;
89 leftlen = 0;
90 for (i = 0; i < r; ++i) {
91 ch = prebuf[i];
92 if (ch == '\n')
93 if (!leftflagcr)
94 leftbuf[leftlen++] = '\r';
95 leftbuf[leftlen++] = ch;
96 leftflagcr = (ch == '\r');
97 }
98 }
99 }
100
101 if (ioleft && ioleft->revents) {
102 r = write(fdleft,leftbuf + leftpos,leftlen - leftpos);
103 if (r == -1) break;
104 leftpos += r;
105 if (leftpos == leftlen) leftstatus = 0;
106 }
107
108 if (ioright && ioright->revents) {
109 r = read(fdright,prebuf,sizeof(prebuf));
110 if (r <= 0) break;
111 rightstatus = 1;
112 rightpos = 0;
113 rightlen = 0;
114 for (i = 0;i < r;++i) {
115 ch = prebuf[i];
116 if (ch == '\n')
117 if (!rightflagcr) rightbuf[rightlen++] = '\r';
118 rightbuf[rightlen++] = ch;
119 rightflagcr = (ch == '\r');
120 }
121 }
122
123 if (io1 && io1->revents) {
125 if (r == -1) break;
126 rightpos += r;
127 if (rightpos == rightlen) rightstatus = 0;
128 }
129 }
130
131 _exit(0);
132}
133
134int main(int argc,char **argv,char **envp)
135{
136 int piin[2];
137 int piout[2];
138
139 if (argc < 2) {
140 logmsg(WHO,100,USAGE,"program [ arg ... ]");
141 }
142
143 if (pipe(piin) == -1)
144 logmsg(WHO,111,FATAL,"unable to create pipe");
145 if (pipe(piout) == -1)
146 logmsg(WHO,111,FATAL,"unable to create pipe");
147
148 switch(fork()) {
149 case -1:
150 logmsg(WHO,111,FATAL,"unable to fork");
151 case 0:
152 sig_ignore(sig_pipe);
153 close(piin[0]);
154 close(piout[1]);
155 doit(piin[1],piout[0]);
156 }
157
158 close(piin[1]);
159 close(piout[0]);
160 if (fd_move(0,piin[0]) == -1)
161 logmsg(WHO,111,FATAL,"unable to move descriptors");
162 if (fd_move(1,piout[1]) == -1)
163 logmsg(WHO,111,FATAL,"unable to move descriptors");
164
165 pathexec_run(argv[1],argv + 1,envp);
166 logmsg(WHO,111,FATAL,B("unable to run: ",argv[1]));
167
168 return 111;
169}
int main()
Definition: addcr.c:4
int rightflagcr
Definition: fixcrio.c:30
int rightpos
Definition: fixcrio.c:29
int leftpos
Definition: fixcrio.c:23
int rightlen
Definition: fixcrio.c:28
char rightbuf[512]
Definition: fixcrio.c:27
int pipe(int[2])
char prebuf[256]
Definition: fixcrio.c:18
int leftflagcr
Definition: fixcrio.c:24
char leftbuf[512]
Definition: fixcrio.c:21
int leftlen
Definition: fixcrio.c:22
int fork()
int read(int, char *, int)
void doit(int fdleft, int fdright)
Definition: fixcrio.c:32
#define WHO
Definition: fixcrio.c:10
int close(int)
int rightstatus
Definition: fixcrio.c:26
int leftstatus
Definition: fixcrio.c:20
int write(int, char *, int)