Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/tests/sfio/tmpread.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
/* This test checks to see if sfread() will always fill the buffer
23
** from a piped-stream correctly even if the other end of the pipe
24
** is writing odd amounts of bytes.
25
*/
26
#define RBUF 16
27
#define ITER 1024
28
29
tmain()
30
{
31
Sfio_t *fr;
32
int p[2];
33
char wbuf[1023], rbuf[RBUF*1023], *s;
34
int i, r, n;
35
36
if(pipe(p) < 0 )
37
terror("Making pipe for communication");
38
39
if(!(fr = sfnew(0, 0, (size_t)SF_UNBOUND, p[0], SF_READ)) )
40
terror("Making read stream");
41
42
for(i = 0; i < sizeof(wbuf); ++i)
43
wbuf[i] = (i%10) + '0';
44
45
switch(fork())
46
{
47
case -1 :
48
terror("fork() failed");
49
case 0 :
50
for(i = 0; i < RBUF*ITER; ++i)
51
if(write(p[1], wbuf, sizeof(wbuf)) != sizeof(wbuf))
52
terror("Write to pipe failed i=%d", i);
53
break;
54
default:
55
for(i = 0; i < ITER; ++i)
56
{ if(sfread(fr, rbuf, sizeof(rbuf)) != sizeof(rbuf))
57
terror("Read from pipe failed i=%d", i);
58
for(r = 0, s = rbuf; r < RBUF; r += 1, s += n)
59
for(n = 0; n < sizeof(wbuf); ++n)
60
if(s[n] != (n%10)+'0')
61
terror("Bad data i=%d n=%d", i, n);
62
}
63
break;
64
}
65
66
texit(0);
67
}
68
69