Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/tests/sfio/tpublic.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
tmain()
23
{
24
Sfio_t* f;
25
Sfio_t* g;
26
char* s;
27
char buf[1024];
28
int n, i;
29
30
if(!(f = sfopen(NIL(Sfio_t*), tstfile("sf", 0), "w")) )
31
terror("Can't open file to write");
32
sfputr(f,"1111",'\n');
33
sfputr(f,"2222",'\n');
34
sfputr(f,"3333",'\n');
35
sfputr(f,"4444",'\n');
36
37
if(!(f = sfopen(f, tstfile("sf", 0), "r")) )
38
terror("Can't open file to read1");
39
if(!(g = sfnew(NIL(Sfio_t*),
40
NIL(Void_t*),(size_t)SF_UNBOUND,dup(sffileno(f)),SF_READ)) )
41
terror("Can't open file to read2");
42
43
sfset(f,SF_SHARE|SF_PUBLIC,1);
44
sfset(g,SF_SHARE|SF_PUBLIC,1);
45
46
if(!(s = sfgetr(f,'\n',1)) || strcmp(s,"1111") != 0)
47
terror("Wrong data1");
48
sfsync(f);
49
if(!(s = sfgetr(g,'\n',1)) || strcmp(s,"2222") != 0)
50
terror("Wrong data2");
51
sfsync(g);
52
if(!(s = sfgetr(f,'\n',1)) || strcmp(s,"3333") != 0)
53
terror("Wrong data3");
54
sfsync(f);
55
if(!(s = sfgetr(g,'\n',1)) || strcmp(s,"4444") != 0)
56
terror("Wrong data4");
57
sfsync(g);
58
59
sfclose(f);
60
sfclose(g);
61
if(!(f = sfopen(NIL(Sfio_t*), tstfile("sf", 0), "r+")) )
62
terror("Can't open file to write2");
63
if(!(g = sfnew(NIL(Sfio_t*),
64
NIL(Void_t*),(size_t)SF_UNBOUND,dup(sffileno(f)),SF_READ)) )
65
terror("Can't open file to read3");
66
67
sfset(f,SF_SHARE|SF_PUBLIC,1);
68
sfset(g,SF_SHARE|SF_PUBLIC,1);
69
70
if(sfputr(f,"1111",'\n') <= 0)
71
terror("bad write1");
72
sfsync(f);
73
if(!(s = sfgetr(g,'\n',1)) || strcmp(s,"2222") != 0)
74
terror("Wrong data5");
75
sfsync(g);
76
if(sfputr(f,"3333",'\n') <= 0)
77
terror("bad write2");
78
sfsync(f);
79
if(!(s = sfgetr(g,'\n',1)) || strcmp(s,"4444") != 0)
80
terror("Wrong data6");
81
sfsync(g);
82
83
if(!(f = sfopen(f, tstfile("sf", 0), "w")) )
84
terror("Can't open file to write3");
85
86
for(i = 0; i < sizeof(buf); ++i)
87
buf[i] = 0;
88
for(i = 0; i < 256; ++i)
89
if(sfwrite(f,buf,sizeof(buf)) != sizeof(buf))
90
terror("Writing buffer0");
91
92
for(i = 0; i < sizeof(buf); ++i)
93
buf[i] = 1;
94
for(i = 0; i < 256; ++i)
95
if(sfwrite(f,buf,sizeof(buf)) != sizeof(buf))
96
terror("Writing buffer1");
97
98
if(!(f = sfopen(f, tstfile("sf", 0), "r")) )
99
terror("Can't open file to read3");
100
sfset(f,SF_SHARE|SF_PUBLIC,1);
101
102
for(n = 0; n < 256; ++n)
103
{ if(!(s = sfreserve(f,sizeof(buf),0)) )
104
terror("Can't reserve buffer1");
105
for(i = 0; i < sizeof(buf); ++i)
106
if(s[i] != 0)
107
terror("Bad data1");
108
}
109
110
for(n = 0; n < 256; ++n)
111
{ if(!(s = sfreserve(f,sizeof(buf),0)) )
112
terror("Can't reserve buffer2");
113
for(i = 0; i < sizeof(buf); ++i)
114
if(s[i] != 1)
115
terror("Bad data2");
116
}
117
118
if((s = sfreserve(f,1,0)) )
119
terror("Reading beyond eof");
120
121
if(!(f = sfopen(f, tstfile("sf", 0), "w")) )
122
terror("Can't open to write");
123
if(sfwrite(f,"aaa\nbbb\nccc\n",12) != 12)
124
terror("Can't write");
125
sfclose(f);
126
if(sfopen(sfstdin,tstfile("sf", 0),"r") != sfstdin)
127
terror("Can't open file as sfstdin");
128
if((n = (int)sfmove(sfstdin,NIL(Sfio_t*),(Sfoff_t)SF_UNBOUND,'\n')) != 3)
129
terror("sfmove wrong number of lines %d",n);
130
if(sfseek(sfstdin,(Sfoff_t)0,0) != 0)
131
terror("Can't seek back to 0");
132
if((n = (int)sfmove(sfstdin,NIL(Sfio_t*),(Sfoff_t)2,'\n')) != 2)
133
terror("sfmove2 wrong number of lines %d",n);
134
if((n = (int)sfmove(sfstdin,NIL(Sfio_t*),(Sfoff_t)SF_UNBOUND,'\n')) != 1)
135
terror("sfmove3 wrong number of lines %d",n);
136
137
texit(0);
138
}
139
140