Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/tests/sfio/tclose.c
1810 views
1
/***********************************************************************
2
* *
3
* This software is part of the ast package *
4
* Copyright (c) 1999-2012 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 Write_error = 0;
23
24
#if __STD_C
25
static int except(Sfio_t* f, int type, Void_t* obj, Sfdisc_t* disc)
26
#else
27
static int except(f, type, obj, disc)
28
Sfio_t* f;
29
int type;
30
Void_t* obj;
31
Sfdisc_t* disc;
32
#endif
33
{
34
if(type == SF_WRITE)
35
Write_error = 1;
36
return 0;
37
}
38
39
static Sfdisc_t Wdisc = {NIL(Sfread_f), NIL(Sfwrite_f), NIL(Sfseek_f), except};
40
41
tmain()
42
{
43
int fd[2];
44
Sfio_t *r, *w;
45
char* s;
46
void(* handler)_ARG_((int));
47
48
#define N_STR 10
49
alarm(10);
50
if(argc > 1) /* to act as a coprocess that read/write ten lines */
51
{ int i, n, rv;
52
53
n = atoi(argv[1]);
54
for(i = 0; i < n; ++i)
55
{ if(!(s = sfgetr(sfstdin,'\n',1)) )
56
terror("Failed to read from stdin");
57
if((rv = sfputr(sfstdout, s, '\n')) != sfvalue(sfstdin))
58
terror("Failed to write rv=%d stdin=%d",
59
rv, sfvalue(sfstdin));
60
}
61
sfsync(sfstdout);
62
texit(0);
63
}
64
65
signal(SIGPIPE,SIG_IGN);
66
67
if(pipe(fd) < 0)
68
terror("Opening pipes");
69
70
if(!(w = sfnew(NIL(Sfio_t*),NIL(Void_t*),(size_t)SF_UNBOUND, fd[1],SF_WRITE)) )
71
terror("Opening write stream");
72
if(!(r = sfnew(NIL(Sfio_t*),NIL(Void_t*),(size_t)SF_UNBOUND, fd[0],SF_READ)) )
73
terror("Opening read stream");
74
75
sfdisc(w,&Wdisc);
76
77
if(sfputr(w,"abc",'\n') != 4)
78
terror("sfputr failed");
79
if(!(s = sfgetr(r,'\n',1)) || strcmp(s,"abc") != 0)
80
terror("sfgetr failed");
81
82
if(sfclose(r) < 0)
83
terror("sfclose failed closing read stream");
84
85
if(sfputr(w,"def",'\n') != 4)
86
terror("sfputr failed2");
87
88
if(Write_error)
89
terror("Write exception should not have been raised");
90
91
if(sfclose(w) >= 0)
92
terror("sfclose should have failed closing write stream");
93
94
if(!Write_error)
95
terror("Write exception did not get raised");
96
97
signal(SIGPIPE,SIG_DFL);
98
if((w = sfpopen(NIL(Sfio_t*), sfprints("%s %d", argv[0], N_STR), "w+")) )
99
{ int i;
100
101
if((handler = signal(SIGPIPE,SIG_IGN)) == SIG_DFL ||
102
handler == SIG_IGN)
103
terror("Bad signal handler for SIGPIPE");
104
signal(SIGPIPE,handler);
105
106
Write_error = 0;
107
sfdisc(w,&Wdisc);
108
109
for(i = 0; i < N_STR*10; ++i)
110
if(sfputr(w, "abc",'\n') != 4)
111
terror("Writing to coprocess1");
112
113
sfsync(w);
114
sfset(w,SF_READ,1);
115
i = sffileno(w);
116
sfset(w,SF_WRITE,1);
117
if (i != sffileno(w))
118
close(sffileno(w));
119
120
for(i = 0; i < N_STR; ++i)
121
if(!(s = sfgetr(w,'\n',1)) || strcmp(s,"abc") != 0)
122
terror("Reading coprocess [%s]", s);
123
if((s = sfgetr(w,'\n',1)) )
124
terror("sfgetr should have failed");
125
126
if(sfputr(w, "abc",'\n') != 4)
127
terror("Writing to coprocess2");
128
129
if(Write_error)
130
terror("Write exception should not have been raised yet");
131
132
if(sfclose(w))
133
terror("sfclose should not have returned error status");
134
135
if(!Write_error)
136
terror("Write exception should have been raised");
137
}
138
139
if(signal(SIGPIPE,SIG_DFL) != SIG_DFL)
140
terror("SIGPIPE handler should have been SIG_DFL");
141
142
/* test for stdio signal handling behavior */
143
w = sfpopen((Sfio_t*)(-1), sfprints("%s %d 2>/dev/null",argv[0],N_STR), "w+");
144
if(w && (handler = signal(SIGPIPE,SIG_DFL)) != SIG_DFL)
145
terror("SIGPIPE handler should have been SIG_DFL");
146
147
texit(0);
148
}
149
150