Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/tests/sfio/thole.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
/* Test for /dev/null and hole-preserving code */
23
24
tmain()
25
{
26
Sfio_t* null;
27
Sfio_t* f;
28
int k, n;
29
static char buf[256*1024], b[256*1024];
30
31
if(!(null = sfopen(NIL(Sfio_t*),"/dev/null","w")) )
32
terror("Opening /dev/null");
33
34
sfsetbuf(null,NIL(char*),(size_t)SF_UNBOUND);
35
36
if(!(f = sfopen(NIL(Sfio_t*), tstfile("sf", 0), "w+")) )
37
terror("Creating %s", tstfile("sf", 0));
38
sfwrite(f,"1234",4);
39
sfseek(f,(Sfoff_t)1,0);
40
sfsync(f);
41
42
sfsetfd(null,-1);
43
sfsetfd(null,sffileno(f));
44
sfsync(null);
45
46
sfseek(f,(Sfoff_t)0,0);
47
if(sfread(f,buf,4) != 4 || strncmp(buf,"1234",4) != 0)
48
terror("Bad data");
49
50
for(k = 0; k < sizeof(buf); ++k)
51
buf[k] = 1;
52
for(k = sizeof(buf)/4; k < sizeof(buf)/2; ++k) /* make a big hole */
53
buf[k] = 0;
54
55
if(!(f = sfopen(f, tstfile("sf", 0), "w+")) )
56
terror("Creating %s", tstfile("sf", 0));
57
n = sizeof(buf)-127;
58
if(sfwrite(f,buf,n) != n)
59
terror("Writing large buffer");
60
sfseek(f,(Sfoff_t)0,0);
61
if(sfread(f,b,n) != n)
62
terror("Reading large buffer");
63
for(k = 0; k < n; ++k)
64
if(b[k] != buf[k])
65
terror("Bad data");
66
67
texit(0);
68
}
69
70