Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/tests/sfio/tpipe.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
22
static int line;
23
24
#define SYNC line = __LINE__;
25
26
#if __STD_C
27
void alrmf(int sig)
28
#else
29
void alrmf(sig)
30
int sig;
31
#endif
32
{
33
terror("blocked at line %d", line);
34
}
35
36
tmain()
37
{
38
int fd[2];
39
Sfio_t *fr, *fw;
40
char *s;
41
int i, j, n, to;
42
char buf[1024];
43
44
to = argc > 1 ? 0 : 4;
45
46
if(sfnew(sfstdout,buf,sizeof(buf),SF_UNBOUND,SF_STRING|SF_WRITE) != sfstdout)
47
terror("Reopen sfstdout");
48
49
if(pipe(fd) < 0)
50
terror("Can't open pipe");
51
if(!(fr = sfnew(NIL(Sfio_t*),NIL(Void_t*),(size_t)SF_UNBOUND,fd[0],SF_READ)) ||
52
!(fw = sfnew(NIL(Sfio_t*),NIL(Void_t*),(size_t)SF_UNBOUND,fd[1],SF_WRITE)) )
53
terror("Can't open stream");
54
if(to)
55
{ signal(SIGALRM,alrmf);
56
alarm(4);
57
}
58
SYNC sfwrite(fw,"0123456789",10);
59
if(sfread(fr,buf,10) != 10)
60
terror("Can't read data from pipe");
61
SYNC sfwrite(fw,"0123456789",10);
62
SYNC if(sfmove(fr,fw,(Sfoff_t)10,-1) != 10)
63
terror("sfmove failed");
64
if(to)
65
alarm(0);
66
sfpurge(fw);
67
sfclose(fw);
68
sfpurge(fr);
69
sfclose(fr);
70
71
if(pipe(fd) < 0)
72
terror("Can't open pipe2");
73
if(!(fr = sfnew(NIL(Sfio_t*),NIL(Void_t*),(size_t)SF_UNBOUND,fd[0],SF_READ)) ||
74
!(fw = sfnew(NIL(Sfio_t*),NIL(Void_t*),(size_t)SF_UNBOUND,fd[1],SF_WRITE)) )
75
terror("Can't open stream");
76
sfset(fr,SF_SHARE|SF_LINE,1);
77
sfset(fw,SF_SHARE,1);
78
79
if(to)
80
alarm(4);
81
SYNC if(sfwrite(fw,"1\n2\n3\n",6) != 6)
82
terror("sfwrite failed");
83
i = j = -1;
84
SYNC if(sfscanf(fr,"%d%d\n%n",&i,&j,&n) != 2 || i != 1 || j != 2 || n != 4)
85
terror("sfscanf failed");
86
SYNC if(sfscanf(fr,"%d\n%n",&i,&n) != 1 || i != 3 || n != 2)
87
terror("sfscanf failed");
88
if(to)
89
alarm(0);
90
91
if(to)
92
alarm(4);
93
SYNC if(sfwrite(fw,"123\n",4) != 4)
94
terror("sfwrite failed");
95
SYNC if(!(s = sfreserve(fr,4,0)) )
96
terror("sfreserve failed");
97
if(to)
98
alarm(0);
99
100
if(to)
101
alarm(4);
102
SYNC sfputr(fw,"abc",'\n');
103
SYNC if(sfmove(fr,fw,(Sfoff_t)1,'\n') != 1)
104
terror("sfmove failed");
105
SYNC if(!(s = sfgetr(fr,'\n',1)) || strcmp(s,"abc"))
106
terror("sfgetr failed");
107
if(to)
108
alarm(0);
109
110
if(to)
111
alarm(4);
112
SYNC if(sfwrite(fw,"111\n222\n333\n444\n",16) != 16)
113
terror("Bad write to pipe");
114
SYNC if(!(s = sfgetr(fr,'\n',1)) )
115
terror("sfgetr failed");
116
if(to)
117
alarm(0);
118
if(strcmp(s,"111"))
119
terror("sfgetr got wrong string");
120
121
if(to)
122
alarm(4);
123
SYNC if(sfmove(fr,sfstdout,(Sfoff_t)2,'\n') != 2)
124
terror("sfmove failed");
125
SYNC sfputc(sfstdout,0);
126
SYNC if(strcmp("222\n333\n",buf))
127
terror("sfmove got wrong data");
128
SYNC if(sfmove(fr,NIL(Sfio_t*),(Sfoff_t)1,'\n') != 1)
129
terror("sfmove failed");
130
if(to)
131
alarm(0);
132
133
if(to)
134
alarm(4);
135
SYNC if(sfwrite(fw,"0123456789",11) != 11)
136
terror("Bad write to pipe2");
137
SYNC if(!(s = sfreserve(fr,11,0)) )
138
terror("Bad peek size %d, expect 11",sfvalue(fr));
139
if(to)
140
alarm(0);
141
if(strncmp(s,"0123456789",10))
142
terror("Bad peek str %s",s);
143
144
/* test for handling pipe error */
145
if(pipe(fd) < 0)
146
terror("Can't create pipe");
147
close(fd[0]);
148
if(!(fw = sfnew(NIL(Sfio_t*),NIL(Void_t*),sizeof(buf),fd[1],SF_WRITE)) )
149
terror("Can't open stream");
150
signal(SIGPIPE,SIG_IGN);
151
152
for(i = 0; i < sizeof(buf); ++i)
153
buf[i] = 'a';
154
buf[sizeof(buf)-1] = 0;
155
for(i = 0; i < 3; ++i)
156
{ if(to)
157
{ signal(SIGALRM,alrmf); /* do this to avoid infinite loop */
158
alarm(4);
159
}
160
sfprintf(fw, "%s\n", buf); /* this should not block */
161
if(to)
162
alarm(0);
163
}
164
165
texit(0);
166
}
167
168