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 <glenn.s.fowler@gmail.com>                *
18 *                                                                      *
19 ***********************************************************************/
20 #include	"sftest.h"
21 
22 #if __STD_C
discread(Sfio_t * f,void * buf,size_t n,Sfdisc_t * disc)23 static ssize_t discread(Sfio_t* f, void* buf, size_t n, Sfdisc_t* disc)
24 #else
25 static ssize_t discread(f, buf, n, disc)
26 Sfio_t*		f;
27 Void_t*		buf;
28 size_t		n;
29 Sfdisc_t*	disc;
30 #endif
31 {
32 	return sfrd(f, buf, n, disc);
33 }
34 
35 #if __STD_C
discwrite(Sfio_t * f,const void * buf,size_t n,Sfdisc_t * disc)36 static ssize_t discwrite(Sfio_t* f, const void* buf, size_t n, Sfdisc_t* disc)
37 #else
38 static ssize_t discwrite(f, buf, n, disc)
39 Sfio_t*		f;
40 Void_t*		buf;
41 size_t		n;
42 Sfdisc_t*	disc;
43 #endif
44 {
45 	return sfwr(f, buf, n, disc);
46 }
47 
48 #if __STD_C
discseek(Sfio_t * f,Sfoff_t offset,int type,Sfdisc_t * disc)49 static Sfoff_t discseek(Sfio_t* f, Sfoff_t offset, int type, Sfdisc_t* disc)
50 #else
51 static Sfoff_t discseek(f, offset, type, disc)
52 Sfio_t*		f;
53 Sfoff_t		offset;
54 int		type;
55 Sfdisc_t*	disc;
56 #endif
57 {
58 	return (Sfoff_t)(-1);	/* pretend that stream is unseekable */
59 }
60 
61 Sfdisc_t	Disc1 = { discread, discwrite };
62 Sfdisc_t	Disc2 = { discread, discwrite };
63 Sfdisc_t	Disc3 = { discread, discwrite };
64 
tmain()65 tmain()
66 {
67 	Sfio_t*	f;
68 	char*	s;
69 	int	i;
70 
71 	alarm(10);
72 	if(argc > 1)
73         {       /* coprocess only */
74                 while((s = sfgetr(sfstdin,'\n',0)) )
75                         sfwrite(sfstdout,s,sfvalue(sfstdin));
76 		texit(0);
77         }
78 
79 	if(!(f = sfpopen(NIL(Sfio_t*),sfprints("%s -p",argv[0]),"r+")) )
80 		terror("Open coprocess");
81 
82 	if(sfwrite(f,"123\n",4) != 4)
83 		terror("Write coprocess");
84 	if(sftell(f) != 4)
85 		terror("sftell1");
86 
87 	if(!(s = sfreserve(f,4,0)) || strncmp(s,"123\n",4) != 0 )
88 		terror("Read coprocess");
89 	if(sftell(f) != 8)
90 		terror("sftell2");
91 
92 	sfset(f,SF_SHARE,1);
93 
94 	if(sfwrite(f,"456\n",4) != 4)
95 		terror("Write coprocess2");
96 	if(sftell(f) != 12)
97 		terror("sftell 3");
98 
99 	if(!(s = sfreserve(f,4,0)) || strncmp(s,"456\n",4) != 0 )
100 		terror("Read coprocess2");
101 	if(sftell(f) != 16)
102 		terror("sftell 4");
103 
104 	sfclose(f);
105 
106 	/* the below tests to see if stream position is correct when
107 	   multiple disciplines are put on a stream.
108 	*/
109 	if(!(f = sfopen(NIL(Sfio_t*), tstfile("sf", 0), "w")))
110 		terror("Opening file to write");
111 	sfdisc(f,&Disc1);
112 	sfdisc(f,&Disc2);
113 	sfdisc(f,&Disc3);
114 
115 	for(i = 0; i < 100; ++i)
116 	{	if(sfputr(f, "123456789", '\n') != 10)
117 			terror("Can't write out strings");
118 		sfsync(f);
119 		if(sftell(f) != (Sfoff_t)((i+1)*10) )
120 			terror("Wrong position");
121 	}
122 	sfclose(f);
123 
124 	if(!(f = sfopen(NIL(Sfio_t*), tstfile("sf", 0), "r")))
125 		terror("Opening file to read");
126 	sfdisc(f,&Disc1);
127 	sfdisc(f,&Disc2);
128 	sfdisc(f,&Disc3);
129 
130 	for(i = 0; i < 100; ++i)
131 	{	if(!(s = sfgetr(f, '\n', 1)) )
132 			terror("Can't read string");
133 		if(strcmp(s,"123456789") != 0)
134 			terror("Wrong string");
135 		if(sftell(f) != (Sfoff_t)((i+1)*10) )
136 			terror("Wrong position");
137 	}
138 	sfclose(f);
139 
140 	if(!(f = sfopen(NIL(Sfio_t*), tstfile("sf", 0), "r")))
141 		terror("Opening file to read");
142 	Disc1.seekf = discseek; sfdisc(f,&Disc1);
143 	Disc2.seekf = discseek; sfdisc(f,&Disc2);
144 	Disc3.seekf = discseek; sfdisc(f,&Disc3);
145 
146 	for(i = 0; i < 100; ++i)
147 	{	if(!(s = sfgetr(f, '\n', 1)) )
148 			terror("Can't read string");
149 		if(strcmp(s,"123456789") != 0)
150 			terror("Wrong string");
151 		if(sftell(f) != (Sfoff_t)((i+1)*10) )
152 			terror("Wrong position");
153 	}
154 	sfclose(f);
155 
156 	texit(0);
157 }
158