Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/tests/sfio/texcept.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 Type;
23
static int Sfn;
24
25
#if __STD_C
26
static int except(Sfio_t* f, int type, Void_t* data, Sfdisc_t* disc)
27
#else
28
static int except(f, type, data, disc)
29
Sfio_t* f;
30
int type;
31
Void_t* data;
32
Sfdisc_t* disc;
33
#endif
34
{
35
switch(type)
36
{
37
case SF_WRITE :
38
Sfn += 1;
39
return 0;
40
case SF_CLOSING:
41
if(Type == SF_CLOSING)
42
return 0;
43
case SF_SYNC:
44
if(Type == SF_CLOSING)
45
return 0;
46
}
47
48
Type = type;
49
return -1;
50
}
51
52
#if __STD_C
53
static int except2(Sfio_t* f, int type, Void_t* data, Sfdisc_t* disc)
54
#else
55
static int except2(f, type, data, disc)
56
Sfio_t* f;
57
int type;
58
Void_t* data;
59
Sfdisc_t* disc;
60
#endif
61
{ return 0;
62
}
63
64
#if __STD_C
65
static int except3(Sfio_t* f, int type, Void_t* data, Sfdisc_t* disc)
66
#else
67
static int except3(f, type, data, disc)
68
Sfio_t* f;
69
int type;
70
Void_t* data;
71
Sfdisc_t* disc;
72
#endif
73
{ if(type == SF_LOCKED)
74
{ Type = type;
75
return -1;
76
}
77
return 0;
78
}
79
#if __STD_C
80
static ssize_t readfunc(Sfio_t* f, Void_t* buf, size_t n, Sfdisc_t* disc)
81
#else
82
static ssize_t readfunc(f, buf, n, disc)
83
Sfio_t* f;
84
Void_t* buf;
85
size_t n;
86
Sfdisc_t* disc;
87
#endif
88
{
89
if(sfgetc(f) >= 0)
90
terror("Can't access stream here!");
91
return 0;
92
}
93
94
static Sfdisc_t Disc, Disc2;
95
96
tmain()
97
{
98
Sfio_t *f, *f2;
99
char buf[1024];
100
char rbuf[4*1024];
101
off_t o;
102
int i;
103
104
if(!(f = sfopen(NIL(Sfio_t*), tstfile("sf", 0), "w")) )
105
terror("Can't open file");
106
sfset(f,SF_IOCHECK,1);
107
108
Disc.exceptf = except;
109
if(!sfdisc(f,&Disc) )
110
terror("Pushing discipline failed");
111
112
sfdisc(f,&Disc);
113
if(Type != SF_DPUSH)
114
terror("Did not get push event");
115
116
/* this is to test sfraise(NULL,...) */
117
if(!(f2 = sfopen(NIL(Sfio_t*), tstfile("sf", 0), "w")) )
118
terror("Can't open file");
119
sfdisc(f2,&Disc);
120
121
Sfn = 0;
122
if(sfraise(0, SF_WRITE, 0) < 0)
123
terror("sfraise failed");
124
if(Sfn != 2)
125
terror("Didn't get right event count");
126
127
sfdisc(f,NIL(Sfdisc_t*));
128
if(Type != SF_DPOP)
129
terror("Did not get pop event");
130
131
sfwrite(f,"123",3);
132
sfsync(f);
133
if(Type != SF_SYNC)
134
terror("Did not get sync event");
135
136
sfwrite(f,"123",3);
137
sfpurge(f);
138
if(Type != SF_PURGE)
139
terror("Did not get purge event");
140
141
sfclose(f);
142
if(Type != SF_CLOSING)
143
terror("Did not get close event");
144
145
sfclose(f);
146
if(Type != SF_FINAL)
147
terror("Did not get final event");
148
149
if(!(f = sfopen(NIL(Sfio_t*), tstfile("sf", 0), "r")) )
150
terror("Can't open file");
151
Disc2.readf = readfunc;
152
Disc2.exceptf = except3;
153
sfdisc(f,&Disc2);
154
if(sfgetc(f) >= 0)
155
terror("There should be no data here");
156
if(Type != SF_LOCKED)
157
terror("Did not get lock event");
158
159
/* test to see if sfclose() preserves seek location */
160
if(!(f = sftmp(0)) )
161
terror("Can't create temp file");
162
sfsetbuf(f,buf,sizeof(buf));
163
for(i = 0; i < sizeof(rbuf); ++i)
164
rbuf[i] = i;
165
sfwrite(f,rbuf,sizeof(rbuf));
166
sfset(f,SF_WRITE,0);
167
168
Disc.exceptf = except2;
169
sfdisc(f,&Disc);
170
sfseek(f,(Sfoff_t)0,0);
171
if(sfread(f,rbuf,4) != 4)
172
terror("reading 4 bytes");
173
for(i = 0; i < 4; ++i)
174
if(rbuf[i] != i)
175
terror("wrong 4 bytes");
176
177
sfsync(f);
178
if((o = lseek(sffileno(f), (off_t)0, SEEK_CUR)) != 4)
179
terror("Wrong seek location %lld", (Sfoff_t)o);
180
181
if((i = dup(sffileno(f))) < 0)
182
terror("Can't dup file descriptor");
183
if((o = lseek(i, (off_t)0, SEEK_CUR)) != 4)
184
terror("Wrong seek location %lld", (Sfoff_t)o);
185
186
sfclose(f);
187
if((o = lseek(i, (off_t)0, SEEK_CUR)) != 4)
188
terror("Wrong seek location %lld", (Sfoff_t)o);
189
190
texit(0);
191
}
192
193