Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/tests/sfio/tmultiple.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
23
/* Test multiple processes reading/writing from same file
24
** descriptor.
25
*/
26
tmain()
27
{
28
char* s;
29
30
if(argc > 1)
31
{ if(strcmp(argv[1],"-r") == 0) /* doing sfgetr */
32
{ if(!(s = sfgetr(sfstdin,'\n',1)) || strcmp(s,"Line2") != 0)
33
terror("Coprocess getr did not get Line2");
34
if(!(s = sfgetr(sfstdin,'\n',1)) || strcmp(s,"Line3") != 0)
35
terror("Coprocess getr did not get Line3");
36
}
37
else /* doing sfmove */
38
{ Sfio_t* f = sfopen(NIL(Sfio_t*),NIL(char*),"swr");
39
if(!f)
40
terror("Can't open string stream");
41
if(sfmove(sfstdin,f,(Sfoff_t)2,'\n') != 2)
42
terror("Coprocess sfmove failed");
43
sfseek(f,(Sfoff_t)0,0);
44
if(!(s = sfgetr(f,'\n',1)) || strcmp(s,"Line2") != 0)
45
terror("Coprocess move did not get Line2");
46
if(!(s = sfgetr(f,'\n',1)) || strcmp(s,"Line3") != 0)
47
terror("Coprocess move did not get Line3");
48
}
49
texit(0);
50
}
51
52
if(sfopen(sfstdout, tstfile("sf", 0), "w") != sfstdout )
53
terror("Opening file");
54
if(sfputr(sfstdout,"Line1",'\n') < 0 ||
55
sfputr(sfstdout,"Line2",'\n') < 0 ||
56
sfputr(sfstdout,"Line3",'\n') < 0 ||
57
sfputr(sfstdout,"Line4",'\n') < 0)
58
terror("Writing data");
59
sfopen(sfstdout,"/dev/null","w");
60
61
/* testing coprocess calling sfgetr */
62
if(sfopen(sfstdin, tstfile("sf", 0),"r") != sfstdin)
63
terror("Opening to read");
64
if(!(s = sfgetr(sfstdin,'\n',1)) || strcmp(s,"Line1") != 0)
65
terror("Did not get Line1 for sfgetr");
66
sfsync(sfstdin);
67
system(sfprints("%s -r",argv[0]));
68
sfseek(sfstdin, (Sfoff_t)lseek(sffileno(sfstdin), (off_t)0, 1), 0);
69
if(!(s = sfgetr(sfstdin,'\n',1)) || strcmp(s,"Line4") != 0)
70
terror("Did not get Line4 for sfgetr");
71
72
/* testing coprocess calling sfmove */
73
if(sfopen(sfstdin, tstfile("sf", 0), "r") != sfstdin)
74
terror("Opening to read");
75
if(!(s = sfgetr(sfstdin,'\n',1)) || strcmp(s,"Line1") != 0)
76
terror("Did not get Line1 for sfmove");
77
sfsync(sfstdin);
78
system(sfprints("%s -m",argv[0]));
79
sfseek(sfstdin, (Sfoff_t)lseek(sffileno(sfstdin), (off_t)0, 1), 0);
80
if(!(s = sfgetr(sfstdin,'\n',1)) || strcmp(s,"Line4") != 0)
81
terror("Did not get Line4 for sfmove");
82
83
/* testing the head program */
84
#ifdef HEAD
85
if(sfopen(sfstdin, tstfile("sf", 0), "r") != sfstdin)
86
terror("Opening to read");
87
if(!(s = sfgetr(sfstdin,'\n',1)) || strcmp(s,"Line1") != 0)
88
terror("Did not get Line1 for head");
89
sfsync(sfstdin);
90
system("head -2 > /dev/null"); /* for testing the head program */
91
sfseek(sfstdin, (Sfoff_t)lseek(sffileno(sfstdin), (off_t)0, 1), 0);
92
if(!(s = sfgetr(sfstdin,'\n',1)) || strcmp(s,"Line4") != 0)
93
terror("Did not get Line4 for head");
94
#endif
95
96
texit(0);
97
}
98
99