Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/tests/sfio/tmmap2read.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
#define mmap ___mmap
21
#define mmap64 ___mmap64
22
23
#include "sftest.h"
24
25
#undef off_t
26
#undef mmap
27
#undef mmap64
28
29
/* This test causes mmap() to fail so that read() must be used.
30
On a system such as BSDI, malloc uses mmap() so if mmap()
31
fails, not much else will work. In such a case, we make this
32
test automatically success.
33
*/
34
35
static int Success = 1;
36
37
#if __STD_C
38
void* mmap(void* addr, size_t size, int x, int y, int z, off_t offset)
39
#else
40
void* mmap()
41
#endif
42
{
43
if(Success)
44
texit(0);
45
46
return (void*)(-1);
47
}
48
49
#if __STD_C
50
void* mmap64(void* addr, size_t size, int x, int y, int z, Sfoff_t offset)
51
#else
52
void* mmap64()
53
#endif
54
{
55
if(Success)
56
texit(0);
57
58
return (void*)(-1);
59
}
60
61
tmain()
62
{
63
Sfio_t* f;
64
char buf[1024], buf2[1024], *data;
65
int n, r;
66
67
/* test to see if malloc() winds up calling mmap() */
68
if(!(data = (char*)malloc(8*1024)) )
69
terror("Malloc failed");
70
free(data);
71
Success = 0;
72
73
/* our real work */
74
if(!(f = sfopen(NIL(Sfio_t*), tstfile("sf", 0),"w")) )
75
terror("Can't open to write");
76
77
for(n = 0; n < sizeof(buf); ++n)
78
buf[n] = '0' + (n%10);
79
80
for(n = 0; n < 10; ++n)
81
sfwrite(f,buf,sizeof(buf));
82
83
if(!(f = sfopen(f, tstfile("sf", 0),"r")) )
84
terror("Can't open to read");
85
86
for(n = 0; n < 10; ++n)
87
{ if((r = sfread(f,buf2,sizeof(buf))) != sizeof(buf))
88
terror("Bad read size=%d",r);
89
if(strncmp(buf,buf2,sizeof(buf)) != 0)
90
terror("Get wrong data");
91
}
92
93
texit(0);
94
}
95
96