Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/tests/sfio/tpopenrw.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
char buf[1024], *s;
26
int n;
27
#ifdef DEBUG
28
Sfio_t* logf = sfopen(0,"LOG","a"); sfsetbuf(logf,NIL(Void_t*),0);
29
#endif
30
31
alarm(10);
32
if(argc > 1)
33
{ /* coprocess only */
34
while((s = sfreserve(sfstdin,SF_UNBOUND,0)) )
35
{
36
#ifdef DEBUG
37
sfwrite(logf, s, sfvalue(sfstdin));
38
#endif
39
sfwrite(sfstdout, s, sfvalue(sfstdin));
40
}
41
return 0;
42
}
43
44
/* make coprocess */
45
if(!(f = sfpopen(NIL(Sfio_t*), sfprints("%s -p",argv[0]), "r+")))
46
terror("Opening for read/write");
47
for(n = 0; n < 10; ++n)
48
{ sfsprintf(buf,sizeof(buf),"Line %d",n);
49
sfputr(f,buf,'\n');
50
if(!(s = sfgetr(f,'\n',1)))
51
terror("Did not read back line");
52
if(strcmp(s,buf) != 0)
53
terror("Input=%s, Expect=%s",s,buf);
54
}
55
56
if(sfputr(f,"123456789",'\n') != 10)
57
terror("Bad write");
58
59
if(sfread(f,buf,3) != 3)
60
terror("Did not get data back");
61
if(strncmp(s,"123",3) != 0)
62
terror("Wrong data");
63
64
if(sfwrite(f,"aaa",3) != 3 || sfputc(f,'\n') != '\n')
65
terror("Fail on write");
66
67
if(!(s = sfgetr(f,'\n',1)) )
68
terror("Should have gotten 456789");
69
if(strcmp(s,"456789") != 0)
70
terror("Wrong data2");
71
72
if(!(s = sfgetr(f,'\n',1)) )
73
terror("Should have gotten aaa");
74
if(strcmp(s,"aaa") != 0)
75
terror("Wrong data3");
76
77
sfclose(f);
78
79
texit(0);
80
}
81
82