Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/tests/sfio/tpoll.c
1810 views
1
/***********************************************************************
2
* *
3
* This software is part of the ast package *
4
* Copyright (c) 1999-2011 AT&T Intellectual Property *
5
* and is licensed under the *
6
* Eclipse Public License, Version 1.0 *
7
* by AT&T Intellectual Property *
8
* *
9
* A copy of the License is available at *
10
* http://www.eclipse.org/org/documents/epl-v10.html *
11
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
12
* *
13
* Information and Software Systems Research *
14
* AT&T Research *
15
* Florham Park NJ *
16
* *
17
* Glenn Fowler <[email protected]> *
18
* *
19
***********************************************************************/
20
#include "sftest.h"
21
#if _sys_socket
22
#include <sys/socket.h>
23
#endif
24
25
tmain()
26
{
27
Sfio_t *f, *g, *str, *fr, *fw, *sf[2];
28
int c;
29
char *s;
30
int fd[2];
31
32
if(argc > 1)
33
{ while((s = sfgetr(sfstdin, '\n', 1)) )
34
{ sfputr(sfstdout, s, '\n');
35
sfsync(sfstdout);
36
}
37
texit(0);
38
}
39
40
if(!(str = sfopen(NIL(Sfio_t*),"abc","s")) )
41
terror("Opening string stream");
42
43
if(pipe(fd) < 0)
44
terror("pipe failed");
45
46
if(!(fr = sfnew(NIL(Sfio_t*),NIL(Void_t*),(size_t)SF_UNBOUND,
47
fd[0],SF_READ)) )
48
terror("Opening read pipe stream");
49
if(!(fw = sfnew(NIL(Sfio_t*),NIL(Void_t*),(size_t)SF_UNBOUND,
50
fd[1],SF_WRITE)) )
51
terror("Opening write pipe stream");
52
53
sf[0] = fr;
54
sf[1] = str;
55
if((c = sfpoll(sf,2,0)) != 1 || sf[0] != str)
56
terror("Only str should be available c=%d",c);
57
58
sf[0] = fr;
59
if(sfpoll(sf,1,0) != 0 )
60
terror("Pipe stream should not be ready");
61
62
sfputc(fw,'a'); sfsync(fw);
63
sf[0] = fr;
64
if(sfpoll(sf,1,0) != 1 )
65
terror("Pipe read should be ready");
66
if((c = sfgetc(fr)) != 'a')
67
terror("Didn't get back right data");
68
69
sf[0] = fr;
70
sf[1] = str;
71
if(sfpoll(sf,2,0) != 1 || sf[0] != str)
72
terror("Only str should be available2");
73
74
sf[0] = fw;
75
sf[1] = str;
76
if(sfpoll(sf,2,0) != 2)
77
terror("Both str&pipe write should be available");
78
79
if(pipe(fd) < 0)
80
terror("Can't create pipe");
81
82
if(!(fr = sfnew(fr,NIL(Void_t*),(size_t)SF_UNBOUND,fd[0],SF_READ)) )
83
terror("Can't create stream");
84
85
if(write(fd[1],"0123456789",10) != 10)
86
terror("Can't write to pipe");
87
88
if(sfpoll(&fr,1,1000) != 1)
89
terror("Data should be available");
90
91
s = sfprints("%s 1", argv[0]);
92
if(!(f = sfpopen(0, s, "w+")) )
93
terror("Can't create read/write process");
94
95
/* this write does not flush yet */
96
if(sfwrite(f, "abc\n",4) != 4)
97
terror("Writing to pipe");
98
99
if(sfpoll(&f, 1, 0) != 1)
100
terror("Poll should succeed");
101
if(sfvalue(f)&SF_READ) /* data has not been flushed to the child yet */
102
terror("Read should not be ready");
103
if(!(sfvalue(f)&SF_WRITE) )
104
terror("Write should be ready");
105
106
if(sfsync(f) < 0) /* now flush data to the child process */
107
terror("Bad sync");
108
if(sfpoll(&f, 1, 1000) != 1)
109
terror("Poll should succeed2");
110
if(!(sfvalue(f)&SF_READ) ) /* the child should have read and rewritten */
111
terror("Read should be ready");
112
if(!(sfvalue(f)&SF_WRITE) )
113
terror("Write should be ready");
114
if(!(s = sfgetr(f,'\n',1)) || strcmp(s, "abc") != 0)
115
terror("Bad read");
116
117
#if _lib_socketpair
118
if(socketpair(AF_UNIX, SOCK_STREAM, 0, fd) != 0)
119
terror("socketpair failed");
120
if(!(f = sfnew(0,NIL(Void_t*),(size_t)SF_UNBOUND,fd[0],SF_READ|SF_WRITE)) )
121
terror("Can't create stream with socket file descriptor");
122
if(!(g = sfnew(0,NIL(Void_t*),(size_t)SF_UNBOUND,fd[1],SF_READ|SF_WRITE)) )
123
terror("Can't create stream with socket file descriptor");
124
125
/* turn off write-capability for f */
126
sfset(f,SF_WRITE,0);
127
128
sf[0] = f;
129
sf[1] = g;
130
if(sfpoll(sf,2,0) != 1)
131
terror("Exactly one stream should be ready!");
132
if(sf[0] != g)
133
terror("Stream g should be ready");
134
if(sfvalue(g)&SF_READ)
135
terror("Read should not be ready for g");
136
if(!(sfvalue(g)&SF_WRITE) )
137
terror("Write should be ready for g");
138
139
if(sfwrite(g, "abc\n", 4) != 4 || sfsync(g) < 0)
140
terror("Writing to g socket");
141
if(sfpoll(sf, 2, 0) != 2)
142
terror("Poll should succeed with both streams");
143
if(!(sfvalue(f)&SF_READ) )
144
terror("Read should be ready for f");
145
146
if(sfgetc(f) != 'a' )
147
terror("sfgetc failed");
148
149
/* turn back on write-capability for f */
150
sfset(f,SF_WRITE,1);
151
152
if(sfwrite(f,"def\n",4) != 4 || sfsync(f) < 0)
153
terror("Writing to f socket");
154
155
if(sfpoll(sf, 2, 0) != 2)
156
terror("Poll should succeed for both streams");
157
if(!sfvalue(f)&SF_READ)
158
terror("Read should be ready for f");
159
if(!sfvalue(g)&SF_READ)
160
terror("Read should be ready for g");
161
162
if(!(s = sfgetr(f,'\n',1)) || strcmp(s,"bc") != 0)
163
terror("f gets wrong data");
164
165
if(!(s = sfgetr(g,'\n',1)) || strcmp(s,"def") != 0)
166
terror("g gets wrong data");
167
168
if(sfpoll(sf, 2, 0) != 2)
169
terror("Poll should succeed for both streams");
170
if(sfvalue(f)&SF_READ)
171
terror("Read should not be ready for f");
172
if(sfvalue(g)&SF_READ)
173
terror("Read should not be ready for g");
174
#endif
175
176
texit(0);
177
}
178
179