Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/tests/sfio/tresize.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[8*1024];
26
int i;
27
Sfoff_t s;
28
29
/* test file resizing */
30
#if _lib_ftruncate
31
if(!(f = sfopen(NIL(Sfio_t*), tstfile("sf", 0), "w")) )
32
terror("Can't open file %s", tstfile("sf", 0));
33
34
for(i = 0; i < sizeof(buf); ++i)
35
buf[i] = '1';
36
37
for(i = 0; i < 1024; ++i)
38
if(sfwrite(f, buf, sizeof(buf)) != sizeof(buf) )
39
terror("Can't write data");
40
41
if(sfclose(f) < 0)
42
terror("Can't sync/close file");
43
if(!(f = sfopen(NIL(Sfio_t*), tstfile("sf", 0), "r+")) )
44
terror("Can't open file %s again", tstfile("sf", 0));
45
if(sfsize(f) != (s = 1024*sizeof(buf)) )
46
terror("Bad file size");
47
48
if(sfresize(f, s + sizeof(buf)) < 0)
49
terror("Can't resize file");
50
if(sfsize(f) != s+sizeof(buf))
51
terror("Bad file size");
52
53
sfseek(f, s, 0);
54
if(sfread(f,buf,sizeof(buf)) != sizeof(buf))
55
terror("Can't read data");
56
57
for(i = 0; i < sizeof(buf); ++i)
58
if(buf[i] != 0)
59
terror("Bad data");
60
sfclose(f);
61
62
if(!(f = sfopen(NIL(Sfio_t*), tstfile("sf", 0), "r+")) )
63
terror("Can't open file %s again", tstfile("sf", 0));
64
if(sfsize(f) != s+sizeof(buf))
65
terror("Bad file size");
66
67
if(sfresize(f, s) < 0)
68
terror("Can't resize file");
69
sfclose(f);
70
71
if(!(f = sfopen(NIL(Sfio_t*), tstfile("sf", 0), "r+")) )
72
terror("Can't open file %s again", tstfile("sf", 0));
73
if(sfsize(f) != s)
74
terror("Bad file size");
75
#endif
76
77
/* test resizing string stream */
78
if(!(f = sfopen(NIL(Sfio_t*), NIL(char*), "rws")) )
79
terror("Can't open string stream");
80
81
for(i = 0; i < sizeof(buf); ++i)
82
buf[i] = '1';
83
84
for(i = 0; i < 1024; ++i)
85
if(sfwrite(f, buf, sizeof(buf)) != sizeof(buf) )
86
terror("Can't write data");
87
88
if(sfsize(f) != (s = 1024*sizeof(buf)) )
89
terror("Bad stream size");
90
91
if(sfresize(f, s + sizeof(buf)) < 0)
92
terror("Can't resize stream");
93
if(sfsize(f) != s+sizeof(buf))
94
terror("Bad stream size");
95
96
if(sfseek(f, s, 0) != s)
97
terror("seek failed");
98
if(sfread(f,buf,sizeof(buf)) != sizeof(buf))
99
terror("Can't read data");
100
101
for(i = 0; i < sizeof(buf); ++i)
102
if(buf[i] != 0)
103
terror("Bad data");
104
105
if(sfresize(f, s) < 0)
106
terror("Can't resize stream");
107
if(sfsize(f) != s)
108
terror("Bad stream size");
109
110
texit(0);
111
}
112
113